forked from braughtg/github-issues-activity-F17
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTTBoardSoln.java
203 lines (185 loc) · 4.96 KB
/
TTTBoardSoln.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
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
/**
* Simple representation of a Tic-Tac-Toe board. This class has a not
* particularly clever implementation of winner checking. However, it is good
* for illustrating the use of Git/GitHub.
*/
public class TTTBoard {
private char[][] board;
/**
* Construct an empty TTT board.
*/
public TTTBoard() {
board = new char[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = '.';
}
}
}
/**
* Place the specified mark in the specified row and column.
*
* @param row the row
* @param col the column
* @param mark the mark to place
*/
public void placeMark(int row, int col, char mark) {
if (row < 0 || row > 2) {
throw new IllegalArgumentException("Row must be in range 0-2.");
}
if (col < 0 || col > 2) {
throw new IllegalArgumentException("Col must be in range 0-2.");
}
if (mark != 'x' && mark != 'o') {
throw new IllegalArgumentException("Mark must be 'x' or 'o'.");
}
if (board[row][col] != '.') {
throw new IllegalArgumentException("Location is not empty.");
}
board[row][col] = mark;
}
/**
* Check if there is a winner in row 0
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char row0winner() {
if (board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x') {
return 'x';
} else if (board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner in row 1
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char row1winner() {
if (board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x') {
return 'x';
} else if (board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner in row 2
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char row2winner() {
if (board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x') {
return 'x';
} else if (board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner in col 0
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char col0winner() {
if (board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x') {
return 'x';
} else if (board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner in col 1
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char col1winner() {
if (board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x') {
return 'x';
} else if (board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner in col 2
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char col2winner() {
if (board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x') {
return 'x';
} else if (board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner diagonally from top left to bottom right.
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char diagLeftToRightWinner() {
if (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x') {
return 'x';
} else if (board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner diagonally from top right to bottom left.
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char diagRightToLeftWinner() {
if (board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x') {
return 'x';
} else if (board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o') {
return 'o';
} else {
return '\0';
}
}
/**
* Check if there is a winner in any row.
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char rowWinner() {
return (char) (row0winner() | row1winner() | row2winner());
}
/**
* Check if there is a winner in any column.
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char colWinner() {
return (char) (col0winner() | col1winner() | col2winner());
}
/**
* Check if there is a winner diagonally.
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char diagWinner() {
return (char) (diagLeftToRightWinner() | diagRightToLeftWinner());
}
/**
* Check if there is a winner on the board.
*
* @return 'x' if x wins, 'o' if o wins, and null (i.e. '\0') otherwise.
*/
public char winner() {
return (char) (rowWinner() | colWinner() | diagWinner());
}
}