2012-04-12 17:46:27 +00:00
|
|
|
# A Python interface to libboard from gnugo
|
|
|
|
# Also has functions for printing the board via pygame,
|
|
|
|
# and for controlling that printing to some degree.
|
|
|
|
#
|
|
|
|
# I'm only implementing what I need for my own purposes.
|
|
|
|
# Further API is welcome!
|
|
|
|
|
|
|
|
from ctypes import *
|
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
try:
|
|
|
|
import pygame
|
|
|
|
have_pygame = True
|
|
|
|
except ImportError:
|
|
|
|
print 'Warning: pygame is not installed. Goban.draw_board() and Goban.draw_info() will fail.'
|
|
|
|
have_pygame = False
|
2012-04-12 17:46:27 +00:00
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
BOARDSIZE=361
|
|
|
|
MAX_MOVE_HISTORY=361
|
2012-04-12 20:43:57 +00:00
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
class board_state(Structure):
|
2012-04-12 17:46:27 +00:00
|
|
|
_fields_ = [
|
|
|
|
('board_size', c_int),
|
|
|
|
('board', c_char * BOARDSIZE),
|
|
|
|
('board_ko_pos', c_int),
|
|
|
|
('black_captured', c_int),
|
|
|
|
('white_captured', c_int),
|
2012-04-13 04:38:11 +00:00
|
|
|
('initial_board', c_char * BOARDSIZE),
|
2012-04-12 17:46:27 +00:00
|
|
|
|
|
|
|
('initial_board_ko_pos', c_int),
|
|
|
|
('initial_white_captured', c_int),
|
|
|
|
('initial_black_captured', c_int),
|
|
|
|
|
|
|
|
('move_history_color', c_int * MAX_MOVE_HISTORY),
|
|
|
|
('move_history_pos', c_int * MAX_MOVE_HISTORY),
|
|
|
|
('move_history_pointer', c_int),
|
|
|
|
|
|
|
|
('komi', c_float),
|
|
|
|
('move_number', c_int)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SGFTree(Structure):
|
|
|
|
_fields_ = [
|
|
|
|
('root', c_void_p), # SGFNode *root;
|
|
|
|
('lastnode', c_void_p) # SGFNode *lastnode;
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
# class Gameinfo(Structure):
|
|
|
|
# _fields_ = [
|
|
|
|
# ('handicap', c_int),
|
|
|
|
# ('to_move', c_int),
|
|
|
|
# ('game_record', SGFTree),
|
|
|
|
# ('computer_player', c_int),
|
|
|
|
# ('outfilename', c_char * 128),
|
|
|
|
# ('outfile', c_void_p)
|
|
|
|
# ]
|
|
|
|
|
|
|
|
|
2012-04-12 17:46:27 +00:00
|
|
|
|
|
|
|
class Goban:
|
2012-04-13 04:38:11 +00:00
|
|
|
# This is our dynamic interface to the gnugo libraries
|
2012-04-12 17:46:27 +00:00
|
|
|
libboard = None
|
2012-04-13 04:38:11 +00:00
|
|
|
BLACK = 2
|
|
|
|
WHITE = 1
|
|
|
|
EMPTY = 0
|
2012-04-12 17:46:27 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2012-04-12 20:43:57 +00:00
|
|
|
if not Goban.libboard:
|
2012-04-13 01:19:19 +00:00
|
|
|
Goban.libboard = CDLL('lib/libboard.so')
|
2012-04-12 17:46:27 +00:00
|
|
|
|
|
|
|
Goban.libboard.clear_board()
|
2012-04-13 04:38:11 +00:00
|
|
|
self.to_move = Goban.BLACK
|
|
|
|
self.hover = None
|
|
|
|
|
2012-04-12 17:46:27 +00:00
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
def play_move(self, pos, color=None):
|
|
|
|
"""Make a move."""
|
2012-04-12 17:46:27 +00:00
|
|
|
|
|
|
|
x,y = pos
|
2012-04-13 04:38:11 +00:00
|
|
|
realpos = x*19 + y
|
|
|
|
|
|
|
|
if color is None:
|
|
|
|
color = self.to_move
|
|
|
|
|
2012-04-12 17:46:27 +00:00
|
|
|
if Goban.libboard.is_legal(realpos, color):
|
|
|
|
Goban.libboard.play_move(realpos, color)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-04-13 01:28:49 +00:00
|
|
|
def undo_move(self, n):
|
2012-04-12 17:46:27 +00:00
|
|
|
"""Undo n moves. Return True on success, False on failure. On failure, no moves are removed."""
|
2012-04-13 04:38:11 +00:00
|
|
|
|
2012-04-12 17:46:27 +00:00
|
|
|
return Goban.libboard.undo_move(n)
|
|
|
|
|
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
def set_hover(self, pos):
|
|
|
|
x,y = pos
|
|
|
|
realpos = x*19 + y
|
|
|
|
|
|
|
|
if self.hover == realpos:
|
|
|
|
return
|
2012-04-12 17:46:27 +00:00
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
if Goban.libboard.is_legal(realpos):
|
|
|
|
self.hover = realpos
|
|
|
|
else:
|
|
|
|
self.clear_hover()
|
2012-04-12 17:46:27 +00:00
|
|
|
|
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
def clear_hover(self):
|
|
|
|
self.hover = None
|
2012-04-12 17:46:27 +00:00
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
|
|
|
|
def OTHER_COLOR(self, color):
|
|
|
|
if color == Goban.WHITE:
|
|
|
|
return Goban.BLACK
|
|
|
|
elif color == Goban.BLACK:
|
|
|
|
return Goban.WHITE
|
|
|
|
else:
|
|
|
|
return Goban.EMPTY
|
|
|
|
|
|
|
|
|
|
|
|
def draw_board(self, size, img_res):
|
2012-04-12 17:46:27 +00:00
|
|
|
""" Return a pygame.Surface() with an image of the board.
|
|
|
|
If pygame isn't available, this function prints an error and returns harmlessly."""
|
2012-04-13 04:38:11 +00:00
|
|
|
|
|
|
|
if not have_pygame:
|
|
|
|
return
|
|
|
|
|
|
|
|
ret = pygame.Surface((size,size))
|
|
|
|
|
|
|
|
inc = size / 19
|
|
|
|
|
|
|
|
p = c_int * 361
|
|
|
|
board = p.in_dll(Goban.libboard, 'board')
|
2012-04-12 17:46:27 +00:00
|
|
|
|
2012-04-13 04:38:11 +00:00
|
|
|
for pos in range(361):
|
|
|
|
s = img_res[self._get_draw_code(pos, board[pos])]
|
|
|
|
s = pygame.transform.scale(s, (inc, inc))
|
|
|
|
ret.blit(s, ((pos%19)*inc, (pos/19)*inc))
|
|
|
|
|
|
|
|
if self.hover == pos:
|
|
|
|
c = img_res['bH']
|
|
|
|
if self.to_move == Goban.WHITE:
|
|
|
|
c = img_res['wH']
|
|
|
|
|
|
|
|
c = pygame.transform.scale(c, (inc, inc))
|
|
|
|
ret.blit(c, ((pos%19)*inc, (pos/19)*inc))
|
|
|
|
|
|
|
|
return ret.convert_alpha()
|
|
|
|
|
2012-04-12 17:46:27 +00:00
|
|
|
|
2012-04-13 01:28:49 +00:00
|
|
|
def draw_info(self):
|
2012-04-12 17:46:27 +00:00
|
|
|
""" Return a pygame.Surface() with an image of text describing the game state.
|
|
|
|
If pygame isn't available, this function prints an error and returns harmlessly."""
|
2012-04-13 04:38:11 +00:00
|
|
|
|
|
|
|
if not have_pygame:
|
|
|
|
return
|
|
|
|
|
|
|
|
textbox = pygame.Surface((150, 300))
|
|
|
|
textbox = textbox.convert()
|
|
|
|
textbox.fill((250, 250, 250))
|
|
|
|
|
|
|
|
font = pygame.font.Font(None, 24)
|
|
|
|
# heading = font.render('Captures', 1, (10, 10, 10))
|
|
|
|
# black_cap = font.render('Black: {}'.format(self.captures[0]), 1, (10, 10, 10))
|
|
|
|
# white_cap = font.render('White: {}'.format(self.captures[1]), 1, (10, 10, 10))
|
|
|
|
# turn = font.render('Turn: {}'.format(['Black', 'White'][self.turn]), 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))
|
|
|
|
|
|
|
|
return textbox
|
|
|
|
|
|
|
|
|
|
|
|
def _get_draw_code(self, pos, board_value):
|
|
|
|
if board_value == Goban.BLACK:
|
|
|
|
return 'b'
|
|
|
|
if board_value == Goban.WHITE:
|
|
|
|
return 'w'
|
|
|
|
if pos == 0:
|
|
|
|
return 'ul'
|
|
|
|
if pos == 18:
|
|
|
|
return 'ur'
|
|
|
|
if pos == 341:
|
|
|
|
return 'dl'
|
|
|
|
if pos == 360:
|
|
|
|
return 'dr'
|
|
|
|
if pos in [60, 66, 72, 174, 180, 186, 288, 294, 300]:
|
|
|
|
return 'h'
|
|
|
|
if pos < 18:
|
|
|
|
return 'u'
|
|
|
|
if pos % 19 == 0:
|
|
|
|
return 'l'
|
|
|
|
if pos > 341:
|
|
|
|
return 'd'
|
|
|
|
if pos % 19 == 18:
|
|
|
|
return 'r'
|
|
|
|
else:
|
|
|
|
return 'm'
|