Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/chess.py
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2012-07-23 17:58:48 (GMT)
committer Walter Bender <walter.bender@gmail.com>2012-07-23 17:58:48 (GMT)
commit16fa3093a5b7a59bfa9e57acceecdce01f9ed516 (patch)
treeeb0fbd35656bb2299575614c09d4fb5ead570a27 /chess.py
parent3d0be449b7c119899c4e3c524742deb6f07d8760 (diff)
better hinting
Diffstat (limited to 'chess.py')
-rw-r--r--chess.py266
1 files changed, 130 insertions, 136 deletions
diff --git a/chess.py b/chess.py
index 6483bbd..86c88fc 100644
--- a/chess.py
+++ b/chess.py
@@ -126,12 +126,15 @@ class Gnuchess():
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
- if self._activity.playing_mode == 'easy':
+ if my_move == HINT:
+ level = 'hard\nbook on\n' # may as well get a good hint
+ elif self._activity.playing_mode == 'easy':
level = 'easy\nbook off\ndepth 1\n'
else:
level = 'hard\nbook on\n'
if my_move in [REMOVE, UNDO, RESTORE, HINT, GAME, NEW]:
+ hint = False
if my_move == REMOVE:
self.move_list = self.move_list[:-2]
elif my_move == UNDO:
@@ -140,14 +143,16 @@ class Gnuchess():
for move in self.move_list:
cmd += '%s\n' % (move)
if my_move == HINT:
- cmd += 'show moves\nquit\n'
+ # cmd += '%sshow moves\nquit\n' % (level)
+ cmd += '%sgo\nquit\n' % (level)
+ hint = True
elif my_move == GAME:
cmd += 'show game\nquit\n'
else:
cmd += 'show board\nquit\n'
_logger.debug(cmd)
output = p.communicate(input=cmd)
- self._process_output(output[0], my_move=None)
+ self._process_output(output[0], my_move=None, hint=hint)
elif my_move == ROBOT: # Ask the computer to play
cmd = 'force manual\n'
for move in self.move_list:
@@ -156,7 +161,7 @@ class Gnuchess():
_logger.debug(cmd)
output = p.communicate(input=cmd)
self._process_output(output[0], my_move='robot')
- elif my_move is not None: # My move
+ elif my_move is not None: # human's move
cmd = 'force manual\n'
for move in self.move_list:
cmd += '%s\n' % (move)
@@ -166,9 +171,9 @@ class Gnuchess():
output = p.communicate(input=cmd)
self._process_output(output[0], my_move=my_move)
else:
- _logger.debug('my move == None')
+ _logger.debug('my_move == None')
- def _process_output(self, output, my_move=None):
+ def _process_output(self, output, my_move=None, hint=False):
''' process output '''
checkmate = False
_logger.debug(output)
@@ -177,22 +182,12 @@ class Gnuchess():
output = output[find(output, target):]
self.game = output[:find(output, '\n\n')]
return
- elif 'moves generated' in output: # processing hint
- if self._activity.playing_white:
- target = 'White (%d) : ' % (1 + int(len(self.move_list) / 2))
- else:
- target = 'Black (%d) : ' % (1 + int(len(self.move_list) / 2))
- output = output[find(output, target):]
- output = output[find(output, ':') + 1: find(output, 'No. of')]
- hint_list = output.split()
- hint = ''
- maxv = -10000
- for i in range(len(hint_list)):
- if i % 2 == 1 and int(hint_list[i]) > maxv:
- hint = hint_list[i - 1]
- maxv = int(hint_list[i])
- _logger.debug(hint)
+ elif hint: # What would the robot do?
+ output = output[find(output, ROBOT_MOVE):]
+ hint = output[len(ROBOT_MOVE):find(output, '\n')]
self._activity.status.set_label(hint)
+ _logger.debug(hint)
+ # self._animate_hint(hint)
return
elif 'wins' in output:
self._activity.status.set_label(_('Checkmate'))
@@ -203,15 +198,6 @@ class Gnuchess():
self._last_piece_played[0].move(self._last_piece_played[1])
self._last_piece_played[0] = None
elif my_move == ROBOT:
- '''
- if find(output, ROBOT_MOVE) < 0:
- if self._activity.playing_white:
- _logger.debug('Black cannot move')
- self._activity.status.set_label('Black cannot move')
- else:
- _logger.debug('White cannot move')
- self._activity.status.set_label('White cannot move')
- '''
output = output[find(output, ROBOT_MOVE):]
robot_move = output[len(ROBOT_MOVE):find(output, '\n')]
_logger.debug(robot_move)
@@ -268,105 +254,6 @@ class Gnuchess():
elif not self._activity.playing_white and len(self.move_list) % 2 == 0:
_logger.debug('asking computer to play white')
- def _load_board(self, board):
- ''' Load the board based on gnuchess board output '''
- black_pawns = 0
- black_rooks = 0
- black_knights = 0
- black_bishops = 0
- black_queens = 0
- white_pawns = 0
- white_rooks = 0
- white_knights = 0
- white_bishops = 0
- white_queens = 0
- w, h = self.white[0].get_dimensions()
- xo = self._width - 8 * self._scale
- xo = int(xo / 2)
- yo = int(self._scale / 2)
- for i in range(16):
- self.black[i].move((-self._scale, -self._scale))
- self.white[i].move((-self._scale, -self._scale))
- k = 1
- for i in range(8):
- x = xo
- y = yo + i * self._scale
- for j in range(8):
- piece = board[k]
- k += 2
- if piece in 'PRNBQK': # white
- if piece == 'P':
- self.white[8 + white_pawns].move((x, y))
- white_pawns += 1
- elif piece == 'R':
- if white_rooks == 0:
- self.white[0].move((x, y))
- white_rooks += 1
- else:
- self.white[7].move((x, y))
- white_rooks += 1
- elif piece == 'N':
- if white_knights == 0:
- self.white[1].move((x, y))
- white_knights += 1
- else:
- self.white[6].move((x, y))
- white_knights += 1
- elif piece == 'B':
- if white_bishops == 0:
- self.white[2].move((x, y))
- white_bishops += 1
- else:
- self.white[5].move((x, y))
- white_bishops += 1
- elif piece == 'Q':
- if white_queens == 0:
- self.white[3].move((x, y))
- white_queens += 1
- else:
- self.white[16].move((x, y))
- self.white[16].set_layer(MID)
- elif piece == 'K':
- self.white[4].move((x, y))
- elif piece in 'prnbqk': # black
- if piece == 'p':
- self.black[8 + black_pawns].move((x, y))
- black_pawns += 1
- elif piece == 'r':
- if black_rooks == 0:
- self.black[0].move((x, y))
- black_rooks += 1
- else:
- self.black[7].move((x, y))
- black_rooks += 1
- elif piece == 'n':
- if black_knights == 0:
- self.black[1].move((x, y))
- black_knights += 1
- else:
- self.black[6].move((x, y))
- black_knights += 1
- elif piece == 'b':
- if black_bishops == 0:
- self.black[2].move((x, y))
- black_bishops += 1
- else:
- self.black[5].move((x, y))
- black_bishops += 1
- elif piece == 'q':
- if black_queens == 0:
- self.black[3].move((x, y))
- black_queens += 1
- else:
- self.black[16].move((x, y))
- self.black[16].set_layer(MID)
- elif piece == 'k':
- self.black[4].move((x, y))
- x += self._scale
- x = xo
- y += self._scale
- k += 1
-
def _all_clear(self):
''' Things to reinitialize when starting up a new game. '''
self.move_list = []
@@ -413,10 +300,16 @@ class Gnuchess():
if spr == None or spr.type == None:
return
- if self._activity.playing_white and spr.type[0] in 'prnbqk':
- return
- elif not self._activity.playing_white and spr.type[0] in 'PRNBQK':
- return
+ if self._activity.playing_robot:
+ if self._activity.playing_white and spr.type[0] in 'prnbqk':
+ return
+ elif not self._activity.playing_white and spr.type[0] in 'PRNBQK':
+ return
+ else:
+ if len(self.move_list) % 2 == 0 and spr.type[0] in 'prnbqk':
+ return
+ elif len(self.move_list) % 2 == 1 and spr.type[0] in 'PRNBQK':
+ return
self._release = None
self._press = spr
@@ -472,14 +365,16 @@ class Gnuchess():
elif spr.type == 'P' and g2[1] == '8':
move += 'Q'
- if self._activity.playing_white:
+ if len(self.move_list) % 2 == 0:
self._activity.white_entry.set_text(move)
else:
self._activity.black_entry.set_text(move)
self.move(move)
- self._activity.status.set_label('Thinking')
- gobject.timeout_add(500, self.move, ROBOT)
+ if self._activity.playing_robot:
+ self._activity.status.set_label('Thinking')
+ gobject.timeout_add(500, self.move, ROBOT)
+
return True
def undo(self):
@@ -529,6 +424,105 @@ class Gnuchess():
def _destroy_cb(self, win, event):
gtk.main_quit()
+ def _load_board(self, board):
+ ''' Load the board based on gnuchess board output '''
+ white_pawns = 0
+ white_rooks = 0
+ white_knights = 0
+ white_bishops = 0
+ white_queens = 0
+ black_pawns = 0
+ black_rooks = 0
+ black_knights = 0
+ black_bishops = 0
+ black_queens = 0
+ w, h = self.white[0].get_dimensions()
+ xo = self._width - 8 * self._scale
+ xo = int(xo / 2)
+ yo = int(self._scale / 2)
+ for i in range(17): # extra queen
+ self.black[i].move((-self._scale, -self._scale))
+ self.white[i].move((-self._scale, -self._scale))
+ k = 1
+ for i in range(8):
+ x = xo
+ y = yo + i * self._scale
+ for j in range(8):
+ piece = board[k]
+ k += 2
+ if piece in 'PRNBQK': # white
+ if piece == 'P':
+ self.white[8 + white_pawns].move((x, y))
+ white_pawns += 1
+ elif piece == 'R':
+ if white_rooks == 0:
+ self.white[0].move((x, y))
+ white_rooks += 1
+ else:
+ self.white[7].move((x, y))
+ white_rooks += 1
+ elif piece == 'N':
+ if white_knights == 0:
+ self.white[1].move((x, y))
+ white_knights += 1
+ else:
+ self.white[6].move((x, y))
+ white_knights += 1
+ elif piece == 'B':
+ if white_bishops == 0:
+ self.white[2].move((x, y))
+ white_bishops += 1
+ else:
+ self.white[5].move((x, y))
+ white_bishops += 1
+ elif piece == 'Q':
+ if white_queens == 0:
+ self.white[3].move((x, y))
+ white_queens += 1
+ else:
+ self.white[16].move((x, y))
+ self.white[16].set_layer(MID)
+ elif piece == 'K':
+ self.white[4].move((x, y))
+ elif piece in 'prnbqk': # black
+ if piece == 'p':
+ self.black[8 + black_pawns].move((x, y))
+ black_pawns += 1
+ elif piece == 'r':
+ if black_rooks == 0:
+ self.black[0].move((x, y))
+ black_rooks += 1
+ else:
+ self.black[7].move((x, y))
+ black_rooks += 1
+ elif piece == 'n':
+ if black_knights == 0:
+ self.black[1].move((x, y))
+ black_knights += 1
+ else:
+ self.black[6].move((x, y))
+ black_knights += 1
+ elif piece == 'b':
+ if black_bishops == 0:
+ self.black[2].move((x, y))
+ black_bishops += 1
+ else:
+ self.black[5].move((x, y))
+ black_bishops += 1
+ elif piece == 'q':
+ if black_queens == 0:
+ self.black[3].move((x, y))
+ black_queens += 1
+ else:
+ self.black[16].move((x, y))
+ self.black[16].set_layer(MID)
+ elif piece == 'k':
+ self.black[4].move((x, y))
+ x += self._scale
+ x = xo
+ y += self._scale
+ k += 1
+
def reskin(self, piece, file_path):
DICT = {'white_pawn': WP, 'black_pawn': BP,
'white_rook': WR, 'black_rook': BR,