-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_c.java
More file actions
129 lines (98 loc) · 3.57 KB
/
Copy pathtest_c.java
File metadata and controls
129 lines (98 loc) · 3.57 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
package test;
public class a {
public a() {
board = new char[3][3];
currentPlayerMark = 'x';
initializeBoard();
}
private char[][] board;
private char currentPlayerMark;
// Set/Reset the board back to all empty values.
public void initializeBoard() {
// Loop through rows
for (int i = 0; i < 3; i++) {
// Loop through columns
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
// Print the current board (may be replaced by GUI implementation later)
public void printBoard() {
System.out.println("-------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
// Loop through all cells of the board and if one is found to be empty (contains char '-') then return false.
// Otherwise the board is full.
public boolean isBoardFull() {
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
return isFull;
}
// Returns true if there is a win, false otherwise.
// This calls our other win check functions to check the entire board.
public boolean checkForWin() {
return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());
}
// Loop through rows and see if any are winners.
private boolean checkRowsForWin() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
return true;
}
}
return false;
}
// Loop through columns and see if any are winners.
private boolean checkColumnsForWin() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
return true;
}
}
return false;
}
// Check the two diagonals to see if either is a win. Return true if either wins.
private boolean checkDiagonalsForWin() {
return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
}
// Check to see if all three values are the same (and not empty) indicating a win.
private boolean checkRowCol(char c1, char c2, char c3) {
return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}
// Change player marks back and forth.
public void changePlayer() {
if (currentPlayerMark == 'x') {
currentPlayerMark = 'o';
}
else {
currentPlayerMark = 'x';
}
}
// Places a mark at the cell specified by row and col with the mark of the current player.
public boolean placeMark(int row, int col) {
// Make sure that row and column are in bounds of the board.
if ((row >= 0) && (row < 3)) {
if ((col >= 0) && (col < 3)) {
if (board[row][col] == '-') {
board[row][col] = currentPlayerMark;
return true;
}
}
}
return false;
}
}