-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFourBoard.java
More file actions
215 lines (181 loc) · 6.7 KB
/
ConnectFourBoard.java
File metadata and controls
215 lines (181 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import java.util.ArrayList;
import java.util.List;
public class ConnectFourBoard extends Board {
// ATTRIBUTES
private final int rows = 6;
private final int columns = 7;
// CONSTRUCTOR
public ConnectFourBoard() {
super();
this.addMatchTypes();
this.addDisappearTypes();
this.createBoardGame(this.createBoardTiles());
}
// GETTERS
public int getRows() {
return this.rows;
}
public int getColumns() {
return this.columns;
}
// ABSTRACT METHOD IMPLEMENTATIONS
@Override
public void displayBoard() {
System.out.println("Rows are displayed on the LEFT side of the board (displayed vertically).");
System.out.println("Columns are displayed on the TOP of the board (displayed horizontally).\n");
System.out.print(" ");
for (int colNum=1; colNum<=columns; colNum++) {
if (colNum == columns) {
System.out.println(" " + colNum + " ");
} else {
System.out.print(" " + colNum + " ");
}
}
for (int rowNum=1; rowNum<=rows; rowNum++) {
System.out.print(rowNum + " ");
for (int colNum=1; colNum<=columns; colNum++) {
String display = super.getGameBoard().get(rowNum-1).get(colNum-1).printDisplay();
if (colNum == columns) {
System.out.println("[" + display + "]");
} else {
System.out.print("[" + display + "]");
}
}
}
System.out.println();
}
@Override
public void addMatchTypes() {
super.getMatchTypes().add(new VerticalMatch());
super.getMatchTypes().add(new HorizontalMatch());
super.getMatchTypes().add(new DiagonalMatch());
}
@Override
public void addDisappearTypes() {
super.getDisappearTypes().add(new SimpleDisappear());
}
@Override
public boolean isValidMove(String move) {
// check if column is free (there is at least one available space at the top in that column)
if (super.getGameBoard().get(0).get(Integer.parseInt(move)-1).getDisplay() == null) {
return true;
} else {
System.out.println("ERROR: Column is full. Choose a different column.");
return false;
}
}
@Override
public void execute(List<Tile> tiles, Player player) {
// in connect four, only one Tile can be placed
int chosenColumn = tiles.get(0).getColumn();
// start checking from the lowest Tile in that column
for (int row = this.getRows()-1; row >= 0; row--) {
Tile currentTile = super.getGameBoard().get(row).get(chosenColumn);
if (currentTile.getDisplay() == null) {
// set Tile to Player's display symbol
currentTile.setDisplay(player.getDisplay());
return;
}
}
}
@Override
public List<Tile> createBoardTiles() {
List<Tile> tiles = new ArrayList<>();
// Create and add tiles to the list based on the number of rows and columns
for (int row = 0; row < this.getRows(); row++) {
for (int col = 0; col < this.getColumns(); col++) {
tiles.add(new BasicTile(row, col));
}
}
return tiles; // return the ArrayList of type Tile
}
@Override
public void createBoardGame(List<Tile> tiles) {
// Create list of tiles per row
List<Tile> rowTiles = new ArrayList<>();
// Populate the game board with the provided tiles
for (Tile tile: tiles) {
// Add a tile
rowTiles.add(tile);
// If the row is maxed out to column size
if (rowTiles.size() == columns) {
// Add row of tiles to gameBoard
super.getGameBoard().add(rowTiles);
// Empty list for new row
rowTiles = new ArrayList<>();
}
}
}
@Override
public List<Tile> checkMatches() {
// find any and all matches (horizontal, vertical, diagonal, or a mix)
List<Tile> matchedTiles = new ArrayList<>();
for (Matchable IMatch: super.getMatchTypes()) {
List<Tile> foundMatches = IMatch.match(super.getGameBoard());
if (foundMatches != null) {
matchedTiles.addAll(foundMatches);
}
}
return (!matchedTiles.isEmpty()) ? matchedTiles : null;
}
@Override
public void removeMatchedTiles(List<Tile> matchedTiles) {
List<DisappearingTile> disappearingTiles = new ArrayList<>();
for (Tile tile : matchedTiles) {
if (tile instanceof DisappearingTile dTile) {
disappearingTiles.add(dTile);
}
}
// only one type of disappearing
for (Disappearable IDisappear : super.getDisappearTypes()) {
IDisappear.disappear(disappearingTiles, super.getGameBoard());
}
// drop the tiles
this.dropTiles(disappearingTiles);
}
@Override
public boolean isFull() {
List<List<Tile>> gameBoard = super.getGameBoard();
for (List<Tile> row : gameBoard) {
for (Tile tile : row) {
if (tile.getDisplay() == null) {
return false;
}
}
}
return true;
}
// HELPER FUNCTIONS
private void dropTiles(List<DisappearingTile> disappearingTiles) {
// order Tiles by highest row (lowest row number) first
List<DisappearingTile> orderedTiles = new ArrayList<>();
for (int i=0; i<this.getRows(); i++) {
for (DisappearingTile tile: disappearingTiles) {
if (tile.getRow() == i) {
orderedTiles.add(tile);
}
}
}
// algorithm to swap display to top
for (DisappearingTile tile: orderedTiles) {
this.drop((Tile) tile);
}
}
private void drop(Tile tile) {
int row = tile.getRow();
int column = tile.getColumn();
// start from the current row and move upwards
for (int currentRow = row; currentRow > 0; currentRow--) {
Tile currentTile = super.getGameBoard().get(currentRow).get(column);
Tile aboveTile = super.getGameBoard().get(currentRow - 1).get(column);
// swap null tile to one above
if (aboveTile.getDisplay() != null) {
String tempDisplay = currentTile.getDisplay();
currentTile.setDisplay(aboveTile.getDisplay());
aboveTile.setDisplay(tempDisplay);
} else {
return;
}
}
}
}