-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
84 lines (74 loc) · 2.36 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
import java.util.Arrays;
public class Board {
// values
char [][] board;
int dimension;
BoardChecker board_check;
// constructor
Board (int n) {
board = new char[n][n];
for (char[] row : board)
Arrays.fill(row, ' ');
dimension = n;
board_check = new BoardChecker();
}
// methods
public void print_board() {
// print row and col values
System.out.print(" ");
for (int i = 0; i < dimension; i++) {
System.out.print(" " + i);
}
System.out.println();
for (int i = 0; i < dimension; i++) {
System.out.print(i);
for (int j = 0; j < dimension; j++) {
System.out.print(" " + board[i][j]);
}
System.out.println();
}
}
public boolean place_piece(int row, int col, char y) {
if (board[row][col] == ' ') {
board[row][col] = y;
return true;
}
return false;
}
public class BoardChecker {
BoardChecker() {
array = new int[dimension];
Arrays.fill(array, ' ');
}
// index = column, value at index = row
int [] array;
public boolean win_cond_helper(char character, int column) {
if (column == dimension) {
return true; // have not failed in any column
}
// look at each value: is the row full? If not, add exactly one value from the column
// to the array with index = row of origin
for (int i = 0; i < dimension; i++) {
if (array[i] == ' ' && board[i][column] == character) {
array[i] = column;
if (win_cond_helper(character, (column + 1))) {
return true;
}
else {
array[i] = ' ';
}
}
}
return false;
}
// only returns true when we are on the last column
// char x is the value we are looking for (true = X or false = O)
public boolean win_condition_check(char x) {
return win_cond_helper(x, 0);
}
}
public static void main(String[] args) {
Board current_board = new Board(5);
current_board.print_board();
}
}