-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardGame.java
159 lines (146 loc) · 3.9 KB
/
BoardGame.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
import java.util.Arrays;
public class BoardGame {
private int n;
private char[][] gameBoard;
public BoardGame(int board_size, int empty_positions, int max_levels) {
this.n = board_size;
this.gameBoard = new char[n][n];
for(char[] row: gameBoard) {
Arrays.fill(row, 'g');
}
}
public HashDictionary makeDictionary() {
return new HashDictionary(9973);
}
public int isRepeatedConfig(HashDictionary dict) {
String code = Arrays.deepToString(gameBoard).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
return dict.getScore(code);
}
public void putConfig(HashDictionary dict, int score) {
String code = Arrays.deepToString(gameBoard).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
Configuration data = new Configuration(code, score);
try {
dict.put(data);
}
catch (DictionaryException e) {
}
}
public void savePlay(int row, int col, char symbol) {
gameBoard[row][col] = symbol;
}
public boolean positionIsEmpty(int row, int col) {
return gameBoard[row][col] == 'g';
}
public boolean tileOfComputer(int row, int col) {
return gameBoard[row][col] == 'o';
}
public boolean tileOfHuman(int row, int col) {
return gameBoard[row][col] == 'b';
}
public boolean wins(char symbol) {
// Check rows for win
for(int row = 0; row < n; row++) {
int count = 0;
for(int column = 0; column < n; column++) {
if(gameBoard[row][column] == symbol) count++;
}
if(count == n) return true;
}
// Check columns for win
for(int column = 0; column < n; column++) {
int count = 0;
for(int row = 0; row < n; row++) {
if(gameBoard[row][column] == symbol) count++;
}
if(count == n) return true;
}
// Check top left to bottom right diagonal
int topLeftBottomRight = 0;
for(int row = 0, column = 0; row < n; row++, column++) {
if(gameBoard[row][column] == symbol) topLeftBottomRight++;
if(topLeftBottomRight == n) return true;
}
// Check bottom left to top right diagonal
int bottomLeftTopRight = 0;
for(int row = n - 1, column = 0; column < n; row--, column++) {
if(gameBoard[row][column] == symbol) bottomLeftTopRight++;
if(bottomLeftTopRight == n) return true;
}
return false;
}
public boolean isDraw(char symbol, int empty_positions) {
int spacesFull = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(gameBoard[i][j] != 'g') spacesFull++;
}
}
if(empty_positions == 0 && spacesFull == (n*n)) return true;
else if(empty_positions > 0 && spacesFull == (n*n) - empty_positions) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(gameBoard[i][j] == 'g') {
try {
if(gameBoard[i+1][j] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i-1][j] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i][j+1] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i][j-1] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i+1][j+1] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i+1][j-1] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i-1][j-1] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
try {
if(gameBoard[i-1][j+1] == symbol) return false;
}
catch (IndexOutOfBoundsException e) {
}
}
else {
return true;
}
}
}
}
return false;
}
public int evalBoard(char symbol, int empty_positions) {
if(wins('o')) {
return 3;
}
else if(isDraw(symbol, empty_positions)) {
return 2;
}
else if(wins('b')) {
return 0;
}
else {
return 1;
}
}
}