-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_game.py
440 lines (318 loc) · 12.1 KB
/
go_game.py
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# -*- coding: utf-8 -*-
import os
import sys
import settings_form
from settings_form import board_size, handicap, komi, board_style
board_9x9 = [
[7, 7, 7, 7, 7, 7,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
]
# boards lookup
BOARDS = {
'9': board_9x9,
'13': board_13x13,
'19': board_19x19
}
TRANSLATE = {
"a" : 0,
"b" : 1,
"c" : 2,
"d" : 3,
"e" : 4,
"f" : 5,
"g": 6,
"h": 7,
"j": 8,
"k": 9,
"l": 10,
"m": 11,
"n": 12,
"o": 13,
"p": 14,
"q": 15,
"r": 16,
"s": 17,
"t": 18
}
# stuff
liberties = []
group = []
groups = []
black_turn = True
black_points = 0
white_points = 0
#properties from settings_form
_handicap = int(handicap)
_komi = float(komi)
_board_size = int(board_size)
_board_style = board_style
#spaces
SPACES9 = " "
SPACES13 = " "
SPACES19 = " "
# stones
EMPTY = 0
BLACK = 1
WHITE = 2
MARKER = 4
OFFBOARD = 7
LIBERTY = 8
BLACK_MARKED = 5
WHITE_MARKED = 6
# current board used
board = None
HANDICAP = [[4, 4] , [_board_size - 3, _board_size - 3], [_board_size - 3, 4] , [4, _board_size - 3], [int(_board_size / 2 ) + 1, int(_board_size /2) + 1]]
# GO ban size
BOARD_WIDTH = 0
BOARD_RANGE = 0
MARGIN = 2
# notation
notation = ' a b c d e f g h j k l m n o p q r s t'
# representation of stones
if _board_style == '*': pieces = '*\u2B24\u25EF bw +'
else: pieces = '·\u2B24\u25EF bw +'
# ●○ ⚪⚫ ⚪🟩 🔵〇⬤○◯
def print_board():
for row in range(BOARD_RANGE):
for col in range(BOARD_RANGE):
stone = board[row][col]
# print rank
if col == 0 and row > 0 and row < BOARD_RANGE - 1:
rank = BOARD_RANGE - 1 - row
print((' ' if rank < 10 else ' ') + str(rank), end='')
print(pieces[stone] + ' ', end='')
print()
# print notation
print(notation[0:BOARD_RANGE*2] + '\n')
# setting all spaces to empty
def clear_board():
for row in range(BOARD_RANGE):
for col in range(BOARD_RANGE):
stone = board[row][col]
if stone == BLACK:
board[row][col] = EMPTY
if stone == WHITE:
board[row][col] = EMPTY
# set Go ban size
def set_board_size(size):
global BOARD_WIDTH, BOARD_RANGE, board
BOARD_WIDTH = size
BOARD_RANGE = BOARD_WIDTH + MARGIN
board = BOARDS[str(size)]
# recursively counting liberties
def count_liberties(x,y, color):
curr_stone = board[x][y]
if curr_stone == OFFBOARD:
return
if curr_stone != BLACK_MARKED and curr_stone != WHITE_MARKED and curr_stone != LIBERTY and color == curr_stone:
board[x][y] += MARKER
group.append([x , y])
count_liberties(x , y + 1, color)
count_liberties(x , y - 1, color)
count_liberties(x + 1 , y, color)
count_liberties(x - 1, y, color)
elif curr_stone == EMPTY:
liberties.append([x, y])
board[x][y] = LIBERTY
else: return
def set_stone(row, col, color):
global black_turn
if board[row][col] != EMPTY:
print(" There is already a stone!")
black_turn = not black_turn
else:
if color == BLACK:
board[row][col] = BLACK
else:
board[row][col] = WHITE
def demarked_board ():
for row in range(BOARD_RANGE):
for col in range(BOARD_RANGE):
stone = board[row][col]
if stone == BLACK_MARKED:
board[row][col] = BLACK
if stone == WHITE_MARKED:
board[row][col] = WHITE
if stone == LIBERTY:
board[row][col] = EMPTY
def set_handicap():
for i in range(_handicap):
board[HANDICAP[i][0]][HANDICAP[i][1]] = BLACK
def check_board(x_cerrent, y_cerrent):
global black_turn
global white_points
global black_points
# checking if all groups have liberties
for row in range(BOARD_RANGE):
for col in range(BOARD_RANGE):
color = board[row][col]
if color == BLACK or color == WHITE:
liberties.clear()
group.clear()
if [row, col] in groups:
continue
else:
count_liberties(row, col, color)
for i in group:
groups.append(i)
# delete all marked stones
demarked_board()
# to see all current groups separately on the different Gobans
# print(x_cerrent,y_cerrent)
# for i in range(9):
# for j in range(9):
# if [i + 1, j + 1] in group:
# print(pieces[color], end = " ")
# else:
# print(pieces[EMPTY], end = " ")
# print()
#
# print(group)
if len(liberties) == 0:
if [x_cerrent, y_cerrent] in group:
print(f" Suicide move for {'BLACK' if color == BLACK else 'WHITE'}!")
if color == 1:
black_turn = True
else:
black_turn = False
board[x_cerrent][y_cerrent] = EMPTY
else:
for i in group:
board[i[0]][i[1]] = EMPTY
if color == BLACK:
white_points+=1
else:
black_points+=1
groups.clear()
# main function
def main():
global black_turn
global white_points
global black_points
# if handicap is bigger than 2 white should go first (official rules)
if _handicap >= 2:
black_turn = False
else:
black_turn = True
board_size = _board_size
white_points = 0
black_points = 0
set_board_size(board_size)
set_handicap()
# spaces for score
if board_size == 9: score_space = SPACES9
elif board_size == 13: score_space = SPACES13
else: score_space = SPACES19
os.system('cls')
print("Welcome to my version 1.0 of my Game of Go!")
print("Now it is at most part console-based but i will add some GUI with matplotlib in the next version.")
print("User commands:")
print(" stop - to stop the app")
# print(" count - to count the points on the board")
print()
print(f" ⬤ - BLACK{score_space[0:len(score_space)-10]}◯ - WHITE")
print("\n Make your first move!")
# subloop
while True:
print_board()
try:
turn = input(f" {'BLACK' if black_turn else 'WHITE'} MOVE: ")
os.system('cls')
# stop the program
if turn == "stop":
os.system('cls')
sys.exit()
# restart the current game
if turn == "restart":
os.system('cls')
clear_board()
white_points = 0
black_points = 0
break
# in thi version count() is in development)
# if turn == "count":
# pass
# exception handling
if (len(turn) != 2 and board_size == 9) or (len(turn) not in (2,3) and (board_size == 13 or board_size == 19)) and turn != "stop":
print(" Wrong string size! Example: 'f3'")
continue
elif (turn[0] not in TRANSLATE.keys() or (int( turn[1]) not in range(1,10) and board_size == 9) or ( int(turn[1:3]) not in range(1,14) and board_size == 13) or (int(turn[1:3]) not in range(1,20) and board_size == 19)) and turn != "stop" :
print(" Wrong format or out of the boundaries of this board size! Example: 'a13'")
continue
elif turn != "stop":
# making moves on different board sizes
if board_size == 9 or len(turn) == 2:
move_col = TRANSLATE[turn[0]] + 1
move_row = board_size - int(turn[1]) + 1
else:
move_col = TRANSLATE[turn[0]] + 1
move_row = board_size - int(turn[1:3]) + 1
if black_turn:
black_turn = False
set_stone(move_row , move_col , BLACK)
else:
black_turn = True
set_stone(move_row , move_col , WHITE)
except SystemExit:
sys.exit()
except:
os.system('cls')
print(" Invalid input!")
continue
# checking for "dead" groups of stones and deleting them
check_board(move_row, move_col)
# score
print(f" B: {black_points}{score_space}W: {white_points} + {_komi}")
print(f" Last move: {turn[0].upper()}{turn[1:3]}")
# main loop
while True:
main()