-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicTTC.java
80 lines (76 loc) · 1.68 KB
/
BasicTTC.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
public class BasicTTC {
String[][] board = {{"-", "-", "-"},
{"-", "-", "-"},
{"-", "-", "-"}};
public BasicTTC() {
}
/*
* @param player
* whether the player is x or o
*
* @param row
* the row intending to have a mark placed
*
* @param col
* the column intending to have a mark placed
*
* checks that the space at [row][col] is "-", or empty
* fills it and returns true if empty
*
* returns false if full. does no mutation
*/
public boolean mark(String player, int row, int col) {
if (board[row][col].equals("-")) {
board[row][col] = player;
return true;
}
return false;
}
/*
* @param player
* whether check for an o or x victory
*
* checks horiz, diag, and vert
*/
public boolean isWin(String player) {
for (int i = 0; i < board.length; i++) {
boolean vertWin = true;
boolean horizWin = true;
for (int j = 0; j < board.length; j++) {
if (!board[i][j].equals(player)) {
horizWin = false;
}
if (!board[j][i].equals(player)) {
vertWin = false;
}
}
if (vertWin || horizWin) {
return true;
}
}
boolean leftDiag = true;
boolean rightDiag = true;
for (int k = 0; k < board.length; k++) {
if (!board[k][k].equals(player)) {
leftDiag = false;
}
if (!board[2-k][2-k].equals(player)) {
rightDiag = false;
}
}
return (leftDiag||rightDiag);
}
/*
* Prints out the board
*/
public String toString() {
String x = "";
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
x += (" " + board[i][j] + " |");
}
x += "\n";
}
return x;
}
}