Miscellaneous cleanup

This commit is contained in:
Anna Rose 2012-04-13 15:27:07 -04:00
parent 01e64ec05f
commit 542e5aed37

View File

@ -17,28 +17,6 @@ except ImportError:
BOARDSIZE=361
MAX_MOVE_HISTORY=361
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;
@ -46,6 +24,28 @@ class SGFTree(Structure):
]
# 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 Gameinfo(Structure):
# _fields_ = [
# ('handicap', c_int),
@ -60,16 +60,19 @@ class SGFTree(Structure):
class Goban:
# This is our dynamic interface to the gnugo libraries
libboard = None
BLACK = 2
WHITE = 1
lib = None
EMPTY = 0
WHITE = 1
BLACK = 2
def __init__(self):
if not Goban.libboard:
Goban.libboard = CDLL('lib/libboard.so')
if not Goban.lib:
Goban.lib = CDLL('lib/libboard.so')
Goban.libboard.clear_board()
# self.ginfo = Gameinfo()
Goban.lib.clear_board()
self.to_move = Goban.BLACK
self.hover = None
@ -77,13 +80,20 @@ class Goban:
def play_move(self, pos, color=None):
"""Make a move."""
# i,j = pos
# if color is None:
# color = self.ginfo.to_move
# return Goban.lib.gameinfo_play_move(self.ginfo, i, j, color)
realpos = _real_pos(pos)
if color is None:
color = self.to_move
if Goban.libboard.is_legal(realpos, color):
Goban.libboard.play_move(realpos, color)
if Goban.lib.is_legal(realpos, color):
Goban.lib.play_move(realpos, color)
self.to_move = self.OTHER_COLOR(self.to_move)
return True
else:
@ -93,7 +103,7 @@ class Goban:
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)
return Goban.lib.undo_move(n)
def set_hover(self, pos):
@ -105,7 +115,7 @@ class Goban:
if self.hover == realpos:
return
if Goban.libboard.is_legal(realpos):
if Goban.lib.is_legal(realpos):
self.hover = realpos
else:
self.clear_hover()
@ -136,11 +146,11 @@ class Goban:
inc = size / 19
p = c_int * 421
board = p.in_dll(Goban.libboard, 'board')
board = p.in_dll(Goban.lib, 'board')
for pos in range(421):
if _i(pos) < 0 or _i(pos) > 18 or _j(pos) < 0 or _j(pos) > 18:
continue
for i in range(19):
for j in range(19):
pos = _real_pos((i,j))
code = self._get_draw_code(pos, board[pos])
if code == 'e':
@ -152,6 +162,7 @@ class Goban:
if self.hover == pos:
c = img_res['bH']
# if self.ginfo.to_move == Goban.WHITE:
if self.to_move == Goban.WHITE:
c = img_res['wH']
@ -215,8 +226,8 @@ class Goban:
# This is equivalent to gnugo's POS macro
def _real_pos(pos):
x,y = pos
return 21 + x * 20 + y
i,j = pos
return 21 + i * 20 + j
def _i(pos):