-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
177 lines (170 loc) · 4.4 KB
/
Board.java
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
/**
* Class that creates and populates the tic tac toe board on which the game
* runs. Contains methods to check for win conditions for each player,
* add marks to the board, and to display the board after each player turn
*/
public class Board implements Constants {
private char theBoard[][];
private int markCount;
/**
* Method to populate the board with empty spaces
*/
public Board() {
markCount = 0;
theBoard = new char[3][];
for (int i = 0; i < 3; i++) {
theBoard[i] = new char[3];
for (int j = 0; j < 3; j++)
theBoard[i][j] = SPACE_CHAR;
}
}
public void clearBoard() {
markCount = 0;
for (int i = 0; i < 3; i++) {
theBoard[i] = new char[3];
for (int j = 0; j < 3; j++)
theBoard[i][j] = SPACE_CHAR;
}
}
/**
* Method to return the mark at the specified row/column
* @param row: represents the user selected row number
* @param col: represents the user selected column number
* @return: returns the position of the board where mark is found
*/
public char getMark(int row, int col) {
return theBoard[row][col];
}
/**
* Method to check if board is fully populated with X/O values
* @return: returns true if counter is 9 (9 moves), false otherwise
*/
public boolean isFull() {
return markCount == 9;
}
/**
* Method to check player X win condition
* @return: returns true if x player wins and false if they lose
*/
public boolean xWins() {
if (checkWinner(LETTER_X) == 1)
return true;
else
return false;
}
/**
* Method to check player O win condition
* @return: returns true of o player wins and false if they lose
*/
public boolean oWins() {
if (checkWinner(LETTER_O) == 1)
return true;
else
return false;
}
/**
* Method to print display of board to console after each player turn
*/
public void display() {
displayColumnHeaders();
addHyphens();
for (int row = 0; row < 3; row++) {
addSpaces();
System.out.print(" row " + row + ' ');
for (int col = 0; col < 3; col++)
System.out.print("| " + getMark(row, col) + " ");
System.out.println("|");
addSpaces();
addHyphens();
}
}
/**
* Method to add X/O mark to board based on player input
* @param row: represents row number to add mark
* @param col: represents column number to add mark
* @param mark: represents mark to add (O/X)
*/
public void addMark(int row, int col, char mark) {
theBoard[row][col] = mark;
markCount++;
}
/**
* Method to fully clear the tic tac toe board
*/
public void clear() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
theBoard[i][j] = SPACE_CHAR;
markCount = 0;
}
/**
* * Method to check for winners on the current instance of the board
* (ie 3 in a row of the same symbol)
* @param mark: indicates mark of winner to check conditions
* @return: returns 1 if winner or 0 if loser
*/
int checkWinner(char mark) {
int row, col;
int result = 0;
for (row = 0; result == 0 && row < 3; row++) {
int row_result = 1;
for (col = 0; row_result == 1 && col < 3; col++)
if (theBoard[row][col] != mark)
row_result = 0;
if (row_result != 0)
result = 1;
}
for (col = 0; result == 0 && col < 3; col++) {
int col_result = 1;
for (row = 0; col_result != 0 && row < 3; row++)
if (theBoard[row][col] != mark)
col_result = 0;
if (col_result != 0)
result = 1;
}
if (result == 0) {
int diag1Result = 1;
for (row = 0; diag1Result != 0 && row < 3; row++)
if (theBoard[row][row] != mark)
diag1Result = 0;
if (diag1Result != 0)
result = 1;
}
if (result == 0) {
int diag2Result = 1;
for (row = 0; diag2Result != 0 && row < 3; row++)
if (theBoard[row][3 - 1 - row] != mark)
diag2Result = 0;
if (diag2Result != 0)
result = 1;
}
return result;
}
/**
* Method to print the game board column headers
*/
void displayColumnHeaders() {
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print("|col " + j);
System.out.println();
}
/**
* Method to help construct the structure of the game board (hyphens)
*/
void addHyphens() {
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print("+-----");
System.out.println("+");
}
/**
* Method to help construct the structure of the game board (spaces)
*/
void addSpaces() {
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print("| ");
System.out.println("|");
}
}