Added pass, resign, and quit buttons using SGC
This commit is contained in:
39
lib/goban.py
39
lib/goban.py
@ -24,9 +24,11 @@ class Goban:
|
||||
self.black_captures = 0
|
||||
self.white_captures = 0
|
||||
self.last_move = None
|
||||
self.passed_last = False
|
||||
self.ko = None
|
||||
self.hover = None
|
||||
self.elapsed_time = 0
|
||||
self.winner = Goban.EMPTY
|
||||
|
||||
|
||||
def set_hover(self, pos):
|
||||
@ -34,7 +36,7 @@ class Goban:
|
||||
if rpos == self.hover:
|
||||
return
|
||||
|
||||
if not self._valid_move(rpos):
|
||||
if not self._valid_move(rpos) or self.to_move == Goban.EMPTY:
|
||||
self.clear_hover()
|
||||
return
|
||||
|
||||
@ -46,6 +48,9 @@ class Goban:
|
||||
|
||||
|
||||
def play_move(self, pos):
|
||||
if self.to_move == Goban.EMPTY:
|
||||
return
|
||||
|
||||
rpos = self._real_pos(pos)
|
||||
|
||||
if not self._valid_move(rpos):
|
||||
@ -54,12 +59,33 @@ class Goban:
|
||||
self.board[rpos] = self.to_move
|
||||
self._capture(rpos)
|
||||
self.last_move = rpos
|
||||
self.passed_last = False
|
||||
|
||||
self.to_move = self._other_color(self.to_move)
|
||||
self.clear_hover()
|
||||
|
||||
|
||||
|
||||
# fixme: need to handle post-game stuff here... scoring code
|
||||
def pass_move(self):
|
||||
if self.passed_last:
|
||||
self.to_move = Goban.EMPTY
|
||||
else:
|
||||
self.to_move = self._other_color(self.to_move)
|
||||
self.passed_last = True
|
||||
|
||||
self.last_move = None
|
||||
self.ko = None
|
||||
|
||||
|
||||
def resign(self):
|
||||
self.passed_last = False
|
||||
self.last_move = None
|
||||
self.ko = None
|
||||
self.winner = self._other_color(self.to_move)
|
||||
self.to_move = Goban.EMPTY
|
||||
|
||||
|
||||
def _capture(self, pos):
|
||||
"""Look for stones captured on the 4 sides of pos, remove them and increment
|
||||
capture counter. This pos must be a *real* position value, not an x,y tuple."""
|
||||
@ -240,7 +266,7 @@ class Goban:
|
||||
textbox.fill((250, 250, 250))
|
||||
|
||||
font = pygame.font.Font(None, 24)
|
||||
time = font.render('Time: {:02d}:{:02d}'.format(self.elapsed_time / 60, self.elapsed_time % 60), 1, (10, 10, 10))
|
||||
# time = font.render('Time: {:02d}:{:02d}'.format(self.elapsed_time / 60, self.elapsed_time % 60), 1, (10, 10, 10))
|
||||
heading = font.render('Captures', 1, (10, 10, 10))
|
||||
black_cap = font.render('Black: {}'.format(self.black_captures), 1, (10, 10, 10))
|
||||
white_cap = font.render('White: {}'.format(self.white_captures), 1, (10, 10, 10))
|
||||
@ -248,12 +274,19 @@ class Goban:
|
||||
turn = font.render('To move: Black', 1, (10, 10, 10))
|
||||
elif self.to_move == Goban.WHITE:
|
||||
turn = font.render('To move: White', 1, (10, 10, 10))
|
||||
else:
|
||||
if self.winner == Goban.WHITE:
|
||||
turn = font.render('Winner: White', 1, (10, 10, 10))
|
||||
elif self.winner == Goban.BLACK:
|
||||
turn = font.render('Winner: Black', 1, (10, 10, 10))
|
||||
else:
|
||||
turn = font.render('Scoring', 1, (10, 10, 10))
|
||||
|
||||
textbox.blit(heading, (0, 0))
|
||||
textbox.blit(black_cap, (0, 28))
|
||||
textbox.blit(white_cap, (0, 56))
|
||||
textbox.blit(turn, (0, 100))
|
||||
textbox.blit(time, (0, 150))
|
||||
# textbox.blit(time, (0, 150))
|
||||
|
||||
return textbox
|
||||
|
||||
|
Reference in New Issue
Block a user