-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoGame.pde
406 lines (381 loc) · 11.2 KB
/
GoGame.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// These are the three image resources, goban (the name of the board), black stone, and white stone
PImage gobanImg, stoneBlackImg, stoneWhiteImg;
int boardSize = 19;
int windowSize = 600;
int windowWidth = 450;
int bSize = int(windowSize * .75);
// This is the 2d array representation of the board. There are three possible strings for each index:
// "n" for (n)ot placed, "w" for white stone, "b" for black stone
String[][] goban = new String[boardSize][boardSize];
String[][] captureBoard;
// Image resources
String[] fname = {"go_board.jpg", "stone_black.png", "stone_white.png"};
int blackScore=0, whiteScore=5;
boolean bTerritory, wTerritory;//for scoring territory
boolean blackTurn = true;
String curError = ""; // String of current error message to be drawn to screen
float border, lineWidth; // Used to calculate drawing of stones on board
PFont font;
boolean gameComplete = false;
int passXL = 225;
int passXR = 370;
int passYT = 470;
int passYB = 520;
int completeXL = 80;
int completeXR = 225;
int completeYT = 470;
int completeYB = 520;
int scoreXL = 150;
int scoreXR = 300;
int scoreYT = 480;
int scoreYB = 590;
PImage topRightUI;
PImage topLeftUI;
PImage botRightUI;
PImage botLeftUI;
int winnerYT = 200;
int winnerYB = 275;
int winnerXL = 125;
int winnerXR = 325;
int restartYT = 300;
int restartYB = 350;
int restartXL = 150;
int restartXR = 300;
void setup() {
fill(255,0,0); //Set text color to red
textSize(24);
// Fill the board with "n", as a game starts with no stones played. (note: java.util.Arrays.fill does NOT work for 2d arrays)
for (int x = 0; x<goban.length; x++) {
for (int y = 0; y<goban.length; y++) {
goban[x][y] = "n";
}
}
// Window size can be changed, as long as it remains square (no, this can't be a variable because Processing doesn't support variables in size())
size(100, 100);
surface.setResizable(true);
surface.setSize(windowWidth, windowSize);
border = windowSize*.75/boardSize/2; // Size of the border around the board
lineWidth = windowSize*.75/boardSize; // Distance between adjacent stones
gobanImg = loadImage(fname[0]);
stoneBlackImg = loadImage(fname[1]);
stoneWhiteImg = loadImage(fname[2]);
font = createFont("Verdana-12.vlw",16);
textFont(font);
topRightUI = loadImage("cornerRT.jpg");
topLeftUI = loadImage("cornerLT.jpg");
botRightUI = loadImage("cornerRB.jpg");
botLeftUI = loadImage("cornerLB.jpg");
}
void placedStone(int x, int y) {
curError = ""; //Reset the error message each time a new placement attempt is made
if (suicideCheck(x, y)) {
curError = "This was a suicide move, please try again";
}
else if (goban[x][y] == "n") {
goban[x][y] = blackTurn ? "b" : "w"; // If it's black turn, place black stone, else place a white stone
captureBoard = new String[boardSize][boardSize];
if (captureCheck(x,y)) {
for(int cx=0; cx<boardSize; cx++) {
for(int cy=0; cy<boardSize; cy++) {
if (captureBoard[cx][cy] == "c") {
goban[cx][cy] = "n";
if (blackTurn) blackScore += 1;
else whiteScore += 1;
}
}
}
}
blackTurn = !blackTurn; // Switch whose turn it is.
}
}
boolean suicideCheck(int x, int y) {
// Check if it's a suicide move, if so return true
return false;
}
boolean captureCheck(int x, int y) {
String opp = (blackTurn) ? "w" : "b"; //Identify opponent
println(opp);
if (noCardinalOpps(x,y,opp)) {
println("No Cardinal Opps");
return false;
} else {
if (y-1 >= 0 && goban[x][y-1] == opp && !cardinalOpen(x,y-1)) {
println("Hit Up");
goban[x][y-1] = "c";
captureBoard[x][y-1] = "c";
if (!noCardinalOpps(x,y-1,opp)) {
if (!captureCheck(x,y-1)) {
goban[x][y-1] = opp;
captureBoard[x][y-1] = null;
}
}
}
if (y+1 < boardSize && goban[x][y+1] == opp && !cardinalOpen(x,y+1)) {
println("Hit Down");
goban[x][y+1] = "c";
captureBoard[x][y+1] = "c";
if (!noCardinalOpps(x,y+1,opp)) {
if (!captureCheck(x,y+1)) {
goban[x][y+1] = opp;
captureBoard[x][y+1] = null;
}
}
}
if (x-1 >=0 && goban[x-1][y] == opp && !cardinalOpen(x-1,y)) {
println("Hit Left");
goban[x-1][y] = "c";
captureBoard[x-1][y] = "c";
if (!noCardinalOpps(x-1,y,opp)) {
if (!captureCheck(x-1,y)) {
goban[x-1][y] = opp;
captureBoard[x-1][y] = opp;
}
}
}
if (x+1 < boardSize && goban[x+1][y] == opp && !cardinalOpen(x+1,y)) {
println("Hit Right");
goban[x+1][y] = "c";
captureBoard[x+1][y] = "c";
if (!noCardinalOpps(x+1,y,opp)) {
if (!captureCheck(x+1,y)) {
goban[x+1][y] = opp;
captureBoard[x+1][y] = opp;
}
}
}
return true;
}
}
boolean cardinalOpen(int x, int y) {
if (x-1 >= 0 && goban[x-1][y] == "n") return true;
if (x+1 <= boardSize && goban[x+1][y] == "n") return true;
if (y+1 <= boardSize && goban[x][y+1] == "n") return true;
if (y-1 >= 0 && goban[x][y-1] == "n") return true;
return false;
}
boolean noCardinalOpps(int x, int y, String opp) {
if (y-1 >= 0 && goban[x][y-1] == opp) return false;
if (y+1 < boardSize && goban[x][y+1] == opp) return false;
if (x-1 >=0 && goban[x-1][y] == opp) return false;
if (x+1 < boardSize && goban[x+1][y] == opp) return false;
return true;
}
void mousePressed() {
if (!gameComplete && mousePressed) {
if((mouseX < bSize) && (mouseY < bSize)){
println("BSIZE: ", bSize);
println(mouseX, mouseY);
int xpos = int(mouseX / lineWidth);
int ypos = int(mouseY / lineWidth);
placedStone(xpos,ypos); // Call the function to attempt a stone placement
}
}
if(!gameComplete && mouseX<=completeXR && mouseX>=completeXL && mouseY<=completeYB && mouseY>=completeYT && !insideCircle())
{
gameComplete = true;
score();
}
if(!gameComplete && mouseX<=passXR && mouseX>=passXL && mouseY<=passYB && mouseY>=passYT && !insideCircle())
{
blackTurn = !blackTurn;
}
if(gameComplete && mouseX<=restartXR && mouseX>=restartXL && mouseY<=restartYB && mouseY>=restartYT) // when reset button is pressed
{
for (int x = 0; x<goban.length; x++) { // set all positions on board as "not placed"
for (int y = 0; y<goban.length; y++) {
goban[x][y] = "n";
}
}
blackTurn = true; //black starts the game
gameComplete = false; // game is no longer complete, so reset scores
blackScore=0; // reset scores back to default of 0 for black and komi for white
whiteScore=5;
}
}
// Function called at the finish of the game which calculates the owned territory of each player
void score()
{
//assume captured stones added as captured
//count valid stones and unmarked territory
for(int x=0; x<boardSize; x++)
{
for(int y=0; y<boardSize; y++)
{
bTerritory=false;
wTerritory=false;
label(x,y,"c");
if(bTerritory||wTerritory)//if unmarked territory detected
{
for(int cx=0; cx<boardSize; cx++)
{
for(int cy=0; cy<boardSize; cy++)
{
if(goban[cx][cy]=="c" && bTerritory && wTerritory)//mutual territory
{
goban[cx][cy] = "m";
}
else if(goban[cx][cy]=="c" && bTerritory ==true)
{
goban[cx][cy] = "b";
}
else if(goban[cx][cy]=="c" && wTerritory ==true)
{
goban[cx][cy] = "w";
}
}
}
}
if(goban[x][y] == "w")
{
whiteScore++;
}
if(goban[x][y] == "b")
{
blackScore++;
}
}
}
}
void label(int x, int y, String p)
{
//check for border colors
if(goban[x][y]=="b")
{
bTerritory=true;
}
if(goban[x][y]=="w")
{
wTerritory=true;
}
//check for non-checked blanks
if(goban[x][y]=="n")
{
goban[x][y] = p;
if(x>0)
{
label( x-1, y, p);
}
if(x<boardSize-1)
{
label(x+1, y, p);
}
if(y>0)
{
label(x, y-1, p);
}
if(y<boardSize-1)
{
label(x, y+1, p);
}
}
}
boolean insideCircle()
{
if(dist(mouseX,mouseY, 225,495)<=35)
{
return true;
}
return false;
}
void drawUI()
{
textAlign(CENTER);
noStroke();
fill(220,207,186);
quad(scoreXR, scoreYT, scoreXR, scoreYB, scoreXL,scoreYB, scoreXL, scoreYT);
fill(0);
text("Scores",(scoreXR-scoreXL)/2+scoreXL,550);
stroke(161,148,129);
strokeWeight(7);
fill(220,207,186);
quad(completeXR, completeYT, completeXR, completeYB, completeXL, completeYB, completeXL, completeYT);
fill(0);
text("Complete",(completeXR-25-completeXL)/2+completeXL,(completeYB-completeYT)/2+completeYT+6);
fill(220,207,186);
quad(passXR, passYT, passXR, passYB, passXL,passYB, passXL, passYT);
fill(0);
text("Pass",(passXR-passXL+15)/2+passXL,(passYB-passYT)/2+passYT+6);
noStroke();
ellipseMode(CENTER);
if(blackTurn)
{
fill(0);
}
else
{
fill(255);
}
ellipse(450/2,495,70,70);
fill(255);
arc(450/2,495,60,60,PI+(PI/2),2*PI+(PI/2));
fill(0);
arc(450/2,495,60,60,(PI/2),PI+(PI/2));
if(gameComplete)
{
fill(0);
text(""+blackScore,scoreXL+40,570);
text(""+whiteScore,scoreXR-40, 570 );
noStroke();
fill(220,207,186);
quad(winnerXR, winnerYT, winnerXR, winnerYB, winnerXL,winnerYB, winnerXL, winnerYT);
fill(0);
String winner;
if(blackScore>whiteScore)
{
winner = "Black Wins";
}
else if(blackScore<whiteScore)
{
winner = "White Wins";
}
else
{
winner = "It's a Tie";
}
text(winner,(winnerXR-winnerXL)/2+winnerXL,(winnerYB-winnerYT)/2+winnerYT+6);
stroke(161,148,129);
strokeWeight(7);
fill(220,207,186);
quad(restartXR, restartYT, restartXR, restartYB, restartXL,restartYB, restartXL, restartYT);
fill(0);
text("Restart",(restartXR-restartXL)/2+restartXL,(restartYB-restartYT)/2+restartYT+6);
}
else
{
fill(0);
text("--.-",scoreXL+40,570);
text("--.-",scoreXR-40, 570 );
}
textAlign(LEFT);
}
// Draw function to loop continuously drawing the board and all pieces
void draw() {
background(109,116,65);
imageMode(CENTER);
image(topRightUI,440,460);
image(topLeftUI,10,460);
image(botRightUI,440,590);
image(botLeftUI,10,590);
imageMode(CORNER);
image(gobanImg, 0, 0, bSize, bSize); // Draw the board
imageMode(CENTER); // Center the stone images, it makes the math easier to read
if (curError != "") {
text(curError, 0, height-30);
}
if(!gameComplete)
{
for (int x = 0; x<goban.length; x++) { //fill our array with "n", which denoted (n)ot-placed
for (int y = 0; y<goban.length; y++) {
if (goban[x][y] != "n") { //Skip blank stones
if (goban[x][y] == "b") { // Draw black stones
image(stoneBlackImg, int(border+(x*lineWidth)), int(border+(y*lineWidth)), int(lineWidth), int(lineWidth));
}
else if (goban[x][y] == "w") { // Draw white stones
image(stoneWhiteImg, int(border+(x*lineWidth)), int(border+(y*lineWidth)), int(lineWidth), int(lineWidth));
}
}
}
}
}
drawUI();
}