pygo/lib/goban.py

205 lines
5.3 KiB
Python
Raw Normal View History

# 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 *
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
BOARDSIZE=361
MAX_MOVE_HISTORY=361
2012-04-12 20:43:57 +00:00
class board_state(Structure):
_fields_ = [
('board_size', c_int),
('board', c_char * BOARDSIZE),
('board_ko_pos', c_int),
('black_captured', c_int),
('white_captured', c_int),
('initial_board', c_char * BOARDSIZE),
('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;
]
# 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)
# ]
class Goban:
# This is our dynamic interface to the gnugo libraries
libboard = None
BLACK = 2
WHITE = 1
EMPTY = 0
def __init__(self):
2012-04-12 20:43:57 +00:00
if not Goban.libboard:
Goban.libboard = CDLL('lib/libboard.so')
Goban.libboard.clear_board()
self.to_move = Goban.BLACK
self.hover = None
def play_move(self, pos, color=None):
"""Make a move."""
x,y = pos
realpos = x*19 + y
if color is None:
color = self.to_move
if Goban.libboard.is_legal(realpos, color):
Goban.libboard.play_move(realpos, color)
return True
else:
return False
def undo_move(self, n):
"""Undo n moves. Return True on success, False on failure. On failure, no moves are removed."""
return Goban.libboard.undo_move(n)
def set_hover(self, pos):
x,y = pos
realpos = x*19 + y
if self.hover == realpos:
return
if Goban.libboard.is_legal(realpos):
self.hover = realpos
else:
self.clear_hover()
def clear_hover(self):
self.hover = None
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):
""" Return a pygame.Surface() with an image of the board.
If pygame isn't available, this function prints an error and returns harmlessly."""
if not have_pygame:
return
ret = pygame.Surface((size,size))
inc = size / 19
p = c_int * 361
board = p.in_dll(Goban.libboard, 'board')
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()
def draw_info(self):
""" 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."""
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'
2012-04-13 05:01:07 +00:00
if pos == 342:
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'