-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.pde
185 lines (174 loc) · 5.73 KB
/
Board.pde
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
class Board {
Space[] spaces;
Player player1;
Player player2;
int currentGo = -1;
int totalGoes = 0;
int winner = 0;
Board(Player player1, Player player2) {
spaces = new Space[9];
for (int i = 0; i < spaces.length; i++) {
spaces[i] = new Space();
}
this.player1 = player1;
this.player2 = player2;
}
boolean checkIfWin(int currentGo, int positionNumber) {
int c = currentGo;
switch (positionNumber) {
case 0:
if ((spaces[1].team == c && spaces[2].team == c) || (spaces[3].team == c && spaces[6].team == c) || (spaces[4].team == c && spaces[8].team == c)) {
winner = c;
return true;
}
break;
case 1:
if ((spaces[4].team == c && spaces[7].team == c) || (spaces[0].team == c && spaces[2].team == c)) {
winner = c;
return true;
}
break;
case 2:
if ((spaces[1].team == c && spaces[0].team == c) || (spaces[4].team == c && spaces[6].team == c) || (spaces[5].team == c && spaces[8].team == c)) {
winner = c;
return true;
}
break;
case 3:
if ((spaces[0].team == c && spaces[6].team == c) || (spaces[4].team == c && spaces[5].team == c)) {
winner = c;
return true;
}
break;
case 4:
if ((spaces[0].team == c && spaces[8].team == c) || (spaces[1].team == c && spaces[7].team == c) || (spaces[2].team == c && spaces[6].team == c) || (spaces[5].team == c && spaces[3].team == c)) {
winner = c;
return true;
}
break;
case 5:
if ((spaces[2].team == c && spaces[8].team == c) || (spaces[3].team == c && spaces[4].team == c)) {
winner = c;
return true;
}
break;
case 6:
if ((spaces[0].team == c && spaces[3].team == c) || (spaces[4].team == c && spaces[2].team == c) || (spaces[7].team == c && spaces[8].team == c)) {
winner = c;
return true;
}
break;
case 7:
if ((spaces[4].team == c && spaces[1].team == c) || (spaces[6].team == c && spaces[8].team == c)) {
winner = c;
return true;
}
break;
case 8:
if ((spaces[6].team == c && spaces[7].team == c) || (spaces[0].team == c && spaces[4].team == c) || (spaces[2].team == c && spaces[5].team == c)) {
winner = c;
return true;
}
break;
}
return false;
}
void haveGo() {
float[] results;
int selectedSquare = 0;
float highestValue = 0;
if (currentGo == -1) results = player1.think(spaces, true);
else results = player2.think(spaces, false);
boolean[] squareTested = new boolean[results.length];
for (int i = 0; i < squareTested.length; i++) {
squareTested[i] = false;
}
for (int i = 0; i < results.length; i++) {
if (highestValue < results[i]) {
highestValue = results[i];
selectedSquare = i;
}
}
squareTested[selectedSquare] = true;
while (spaces[selectedSquare].team != 0) {
selectedSquare = 0;
highestValue = -1000;
for (int i = 0; i < results.length; i++) {
if (highestValue < results[i] && squareTested[i] == false) {
highestValue = results[i];
selectedSquare = i;
}
}
squareTested[selectedSquare] = true;
}
spaces[selectedSquare].team = currentGo;
totalGoes++;
if (checkIfWin(currentGo, selectedSquare)) {
winnerFound();
} else if (totalGoes == 9) {
player1.draws++;
player2.draws++;
player1.gamesPlayed++;
player2.gamesPlayed++;
winner = -2;
}
if (winner == 0) {
if (currentGo == -1) currentGo = 1;
else currentGo = -1;
}
}
void winnerFound() {
if (currentGo == -1)
{
player1.wins++;
player2.losses++;
player1.gamesPlayed++;
player2.gamesPlayed++;
} else {
player1.losses++;
player2.wins++;
player1.gamesPlayed++;
player2.gamesPlayed++;
}
}
void showGo() {
player1.brain.drawGenome(15, 400, 370, 260);
player2.brain.drawGenome(15, 640, 370, 260);
PImage zero = loadImage("0.png");
PImage cross = loadImage("1.png");
PImage nothing = loadImage("-1.png");
if (spaces[0].team == 0) image(nothing, 21, 56);
else if (spaces[0].team == -1) image(zero, 21, 56);
else if (spaces[0].team == 1) image(cross, 21, 56);
if (spaces[1].team == 0) image(nothing, 131, 56);
else if (spaces[1].team == -1) image(zero, 131, 56);
else if (spaces[1].team == 1) image(cross, 131, 56);
if (spaces[2].team == 0) image(nothing, 241, 56);
else if (spaces[2].team == -1) image(zero, 241, 56);
else if (spaces[2].team == 1) image(cross, 241, 56);
if (spaces[3].team == 0) image(nothing, 21, 165);
else if (spaces[3].team == -1) image(zero, 21, 165);
else if (spaces[3].team == 1) image(cross, 21, 165);
if (spaces[4].team == 0) image(nothing, 131, 165);
else if (spaces[4].team == -1) image(zero, 131, 165);
else if (spaces[4].team == 1) image(cross, 131, 165);
if (spaces[5].team == 0) image(nothing, 241, 165);
else if (spaces[5].team == -1) image(zero, 241, 165);
else if (spaces[5].team == 1) image(cross, 241, 165);
if (spaces[6].team == 0) image(nothing, 21, 275);
else if (spaces[6].team == -1) image(zero, 21, 275);
else if (spaces[6].team == 1) image(cross, 21, 275);
if (spaces[7].team == 0) image(nothing, 131, 275);
else if (spaces[7].team ==-1) image(zero, 131, 275);
else if (spaces[7].team == 1) image(cross, 131, 275);
if (spaces[8].team == 0) image(nothing, 241, 275);
else if (spaces[8].team == -1) image(zero, 241, 275);
else if (spaces[8].team == 1) image(cross, 241, 275);
}
public Player getPlayer1() {
return player1;
}
public Player getPlayer2() {
return player2;
}
}