-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
209 lines (168 loc) · 7.95 KB
/
Copy pathBoard.java
File metadata and controls
209 lines (168 loc) · 7.95 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
class Board {
private int[][] board;
private int boardWidth;
private int boardHeight;
Board(int width, int height) {
boardWidth = width;
boardHeight = height;
initialiseBoard();
}
Board(int[][] boardCopy) {
board = new int[boardCopy.length][boardCopy[0].length];
for (int x = 0; x < boardCopy.length; x++)
for (int y = 0; y < boardCopy[0].length; y++)
board[x][y] = boardCopy[x][y];
}
// Initialise a new Board[width][height].
private void initialiseBoard() {
board = new int[boardWidth][boardHeight];
for (int x = 0; x < boardWidth; x++)
for (int y = 0; y < boardHeight; y++)
board[x][y] = 0;
}
public Board getBoard() {
return new Board(board);
}
public int getToken(int x, int y) {
return board[x][y];
}
// Display the current Board state, O = player 1, X = Player 2.
public void displayBoard() {
String output;
for (int y = 0; y < boardHeight; y++) {
output = "# ";
for (int x = 0; x < boardWidth; x++) {
switch (board[x][y]) {
case 0: output += " "; break;
case 1: output += "O "; break;
case 2: output += "X "; break;
}
}
output += "#";
System.out.println(output);
}
output = "# ";
for (int x = 0; x < boardWidth; x++)
output += x + " ";
System.out.println(output + "#\n");
}
// Place a Disk in the given column, if the column is not valid ignore the move.
public boolean placeDisk(int column, int playersTurn) {
boolean validMove = false;
// Starting from the bottom of the board, move upwards until an empty space is found
// and put the current playersTurn token in that spot.
for (int y = boardHeight - 1; y >= 0 && !validMove; y--) {
if (board[column][y] == 0) {
board[column][y] = playersTurn;
validMove = true;
}
}
if (!validMove)
System.out.println("Invalid move by Player " + playersTurn + ". Column " + column + " is already full.");
return validMove;
}
// Check if either player has won.
public boolean checkWin() {
System.out.println("Checking Win");
if (checkDiagonalLeft() || checkDiagonalRight() || checkHorizontal() || checkVertical())
return true;
return false;
}
// Checks if the top row of the game has an empty space.
public boolean checkFreeSpaces() {
for (int x = 0; x < boardWidth; x++)
if (board[x][0] == 0)
return true;
return false;
}
// Check for horizontal win by either player.
public boolean checkHorizontal() {
int currentStreak;
for (int player = 1; player <= 2; player++) { // Cycle through both players to see if either has won.
for (int y = 0; y < boardHeight; y++) { // From top to bottom
currentStreak = 0; // Each row reset the current streak (Aiming for 4 in a row)
for (int x = 0; x < boardWidth; x++) { // From left to right
if (board[x][y] == player) // If the current token == current player tested, increment streak.
currentStreak++;
else // Otherwise reset the streak and try again.
currentStreak = 0;
if (currentStreak == 4) { // If the streak hits 4, the current player has won.
System.out.println("Player " + player + " gets a horizontal win! (" + x + ", " + y + ")");
return true;
}
} // End for x
} // End for y
}// End for player
return false;
}
public boolean checkVertical() {
int currentStreak;
for (int player = 1; player <= 2; player++) { // Cycle through both players to see if either has won.
for (int x = 0; x < boardWidth; x++) { // From left to right
currentStreak = 0; // Each row reset the current streak (Aiming for 4 in a row)
for (int y = 0; y < boardHeight; y++) { // From top to bottom,
if (board[x][y] == player) // If the current token == current player tested, increment streak.
currentStreak++;
else // Otherwise reset the streak and try again.
currentStreak = 0;
if (currentStreak == 4) { // If the streak hits 4, the current player has won.
System.out.println("Player " + player + " gets a horizontal win! (" + x + ", " + y + ")");
return true;
}
} // End for y
} // End for x
}// End for player
return false;
}
public boolean checkDiagonalRight() {
int currentStreak;
for (int player = 1; player <= 2; player++) { // Cycle through both players to see if either has won.
for (int x = 0; x < boardWidth - 4; x++) { // Left to right, but stop 4 from the right because diagonal would exceeds board width
for (int y = 0; y < boardHeight - 4; y++) { // Top to bottom, but stop 4 from the bottom because diagonal would exceeds board height
currentStreak = 0;
for (int i = 0; i < 4; i++) { // Check down the diagonal 4 spaces and check if the streak == 4
if (board[x + i][y + i] == player)
currentStreak++;
else
currentStreak = 0;
}
if (currentStreak == 4) {
System.out.println("Player " + player + " gets a diagonal right win! (" + x + ", " + y + ")");
return true;
}
} // End for y
} // End for x
}// End for player
return false;
}
public boolean checkDiagonalLeft() {
int currentStreak = 0;
for (int player = 1; player <= 2; player++) { // Cycle through both players to see if either has won.
for (int x = 4; x < boardWidth; x++) { // Left to right, but start 4 from the left because diagonal would exceeds board width
for (int y = 0; y < boardHeight - 4; y++) { // Top to bottom, but stop 4 from the bottom because diagonal would exceeds board height
currentStreak = 0;
for (int i = 0; i < 4; i++) { // Check down the diagonal 4 spaces and check if the streak == 4
if (board[x - i][y + i] == player)
currentStreak++;
else
currentStreak = 0;
}
if (currentStreak == 4) {
System.out.println("Player " + player + " gets a diagonal left win! (" + x + ", " + y + ")");
return true;
}
} // End for y
} // End for x
}// End for player
return false;
}
public boolean isColumnFull(int column) {
return board[column][0] != 0;
}
public int getBoardWidth() {
return board.length;
}
public int getBoardHeight() {
return board[0].length;
}
}