From af6dc5b1e64ff51242d2b86169db5db26009fdfe Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 27 Jun 2014 00:35:56 +0000 Subject: clean up; check for off-board condition --- diff --git a/GNUChessActivity.py b/GNUChessActivity.py index 38ead14..9debaa4 100644 --- a/GNUChessActivity.py +++ b/GNUChessActivity.py @@ -33,7 +33,6 @@ from toolbar_utils import button_factory, label_factory, separator_factory, \ from utils import json_load, json_dump, get_hardware, \ pixbuf_to_base64, base64_to_pixbuf -import copy import telepathy import dbus from dbus.service import signal @@ -143,7 +142,7 @@ class GNUChessActivity(activity.Activity): def set_thinking_cursor(self): ''' Thinking, so set watch cursor. ''' self.old_cursor = self.get_window().get_cursor() - Watch = Gdk.Cursor(Gdk.CursorType.WATCH) + Watch = Gdk.Cursor(Gdk.CursorType.WATCH) self.get_window().set_cursor(Watch) def _setup_toolbars(self): @@ -242,16 +241,16 @@ class GNUChessActivity(activity.Activity): separator_factory(self.view_toolbar, False, True) skin_button1 = radio_factory('white-knight', - self.view_toolbar, - self.do_default_skin_cb, - tooltip=_('Default pieces'), - group=None) + self.view_toolbar, + self.do_default_skin_cb, + tooltip=_('Default pieces'), + group=None) skin_button2 = radio_factory('white-knight-sugar', - self.view_toolbar, - self.do_sugar_skin_cb, - tooltip=_('Sugar-style pieces'), - group=skin_button1) + self.view_toolbar, + self.do_sugar_skin_cb, + tooltip=_('Sugar-style pieces'), + group=skin_button1) xocolors = XoColor(self.colors) icon = Icon(icon_name='white-knight-sugar', xo_color=xocolors) icon.show() @@ -301,7 +300,7 @@ class GNUChessActivity(activity.Activity): self._robot_cb, group=None, tooltip=_( - 'Play against the computer')) + 'Play against the computer')) self.human_button = radio_factory('human', self.adjust_toolbar, @@ -430,14 +429,14 @@ class GNUChessActivity(activity.Activity): def _setup_timer_palette(self): self.timer_values = [None, 30, 180, 600] self.timer_tooltips = ['', _('30 seconds'), _('3 minutes'), - _('10 minutes')] + _('10 minutes')] self.timer_labels = [_('Disabled'), - #TRANS: Lightning chess 30 seconds between moves - _('Lightning: %d seconds') % (30), - #TRANS: Blitz chess 3 minutes between moves - _('Blitz: %d minutes') % (3), - #TRANS: Tournament chess 10 minutes between moves - _('Tournament: %d minutes') % (10)] + #TRANS: Lightning chess 30 seconds between moves + _('Lightning: %d seconds') % (30), + #TRANS: Blitz chess 3 minutes between moves + _('Blitz: %d minutes') % (3), + #TRANS: Tournament chess 10 minutes between moves + _('Tournament: %d minutes') % (10)] self.timer_palette = self.timer_button.get_palette() for i, label in enumerate(self.timer_labels): @@ -449,7 +448,7 @@ class GNUChessActivity(activity.Activity): def _timer_selected_cb(self, button, index): game_already_started = 0 - if self.time_interval != None: + if self.time_interval is not None: game_already_started = 1 self.time_interval = self.timer_values[index] @@ -678,7 +677,8 @@ class GNUChessActivity(activity.Activity): try: chooser = ObjectChooser(parent=self, what_filter=None) except TypeError: - chooser = ObjectChooser(None, activity, + chooser = ObjectChooser( + None, activity, Gtk.DialogType.MODAL | Gtk.DialogType.DESTROY_WITH_PARENT) if chooser is not None: try: @@ -687,7 +687,6 @@ class GNUChessActivity(activity.Activity): jobject = chooser.get_selected_object() if jobject and jobject.file_path: name = jobject.metadata['title'] - mime_type = jobject.metadata['mime_type'] finally: jobject.destroy() chooser.destroy() @@ -802,7 +801,7 @@ class GNUChessActivity(activity.Activity): if sharer: _logger.debug('This is my activity: making a tube...') - id = self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferDBusTube( + self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferDBusTube( SERVICE, {}) else: _logger.debug('I am joining an activity: waiting for a tube...') @@ -831,20 +830,21 @@ class GNUChessActivity(activity.Activity): def _new_tube_cb(self, id, initiator, type, service, params, state): ''' Create a new tube. ''' - _logger.debug('New tube: ID=%d initator=%d type=%d service=%s \ -params=%r state=%d' % (id, initiator, type, service, params, state)) + _logger.debug('New tube: ID=%d initator=%d type=%d service=%s ' + 'params=%r state=%d' % + (id, initiator, type, service, params, state)) if (type == telepathy.TUBE_TYPE_DBUS and service == SERVICE): if state == telepathy.TUBE_STATE_LOCAL_PENDING: - self.tubes_chan[ \ - telepathy.CHANNEL_TYPE_TUBES].AcceptDBusTube(id) + self.tubes_chan[ + telepathy.CHANNEL_TYPE_TUBES].AcceptDBusTube(id) - tube_conn = TubeConnection(self.conn, - self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES], id, \ + tube_conn = TubeConnection( + self.conn, self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES], id, group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP]) - self.chattube = ChatTube(tube_conn, self.initiating, \ - self.event_received_cb) + self.chattube = ChatTube(tube_conn, self.initiating, + self.event_received_cb) # Now that we have a tube, send the nick to our opponent if not self.initiating: diff --git a/chess.py b/chess.py index 384df31..66c1633 100644 --- a/chess.py +++ b/chess.py @@ -12,13 +12,10 @@ # Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA from gi.repository import Gtk, Gdk, GdkPixbuf, GObject -import cairo import os import subprocess from string import find -from random import uniform - from gettext import gettext as _ import logging @@ -128,9 +125,9 @@ class Gnuchess(): def move(self, my_move): ''' Send a command to gnuchess. ''' # Permisos para jugar - os.system('chmod -R 755 bin') + os.system('chmod -R 755 bin') p = subprocess.Popen(['%s/%s/gnuchess' % (self._bundle_path, - self._bin_path)], + self._bin_path)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) @@ -255,10 +252,10 @@ class Gnuchess(): self._activity.status.set_label(_('Checkmate')) if len(self.move_list) % 2 == 0: self._flash_tile([self._xy_to_file_and_rank( - self.white[4].get_xy())]) + self.white[4].get_xy())]) else: self._flash_tile([self._xy_to_file_and_rank( - self.black[4].get_xy())]) + self.black[4].get_xy())]) else: if self._activity.time_interval and \ (self._activity.time_interval >= 0): @@ -365,7 +362,7 @@ class Gnuchess(): self._total_drag = [0, 0] spr = self._sprites.find_sprite((x, y)) - if spr == None or spr.type == None: + if spr is None or spr.type is None: return if self._thinking or self._flashing: @@ -379,7 +376,7 @@ class Gnuchess(): return elif self._activity.playing_white and \ len(self.move_list) % 2 == 1: - self._wait_your_turn() + self._wait_your_turn() return # Only play your color @@ -452,7 +449,8 @@ class Gnuchess(): g1 = self._xy_to_file_and_rank(self._last_piece_played[1]) g2 = self._xy_to_file_and_rank((x, y)) - if g1 == g2: # We'll let beginners touch a piece and return it. + # We'll let beginners touch a piece and return it. + if g1 == g2 or g2 is None: spr.move(self._last_piece_played[1]) return True @@ -494,10 +492,10 @@ class Gnuchess(): self._activity.status.set_label(_('Checkmate')) if len(self.move_list) % 2 == 0: self._flash_tile([self._xy_to_file_and_rank( - self.white[4].get_xy())]) + self.white[4].get_xy())]) else: self._flash_tile([self._xy_to_file_and_rank( - self.black[4].get_xy())]) + self.black[4].get_xy())]) # Check to see if it is the robot's turn if self._activity.playing_robot and \ @@ -616,10 +614,8 @@ class Gnuchess(): def _parse_move(self, move): tiles = [] - label = move source_file = None source_rank = None - capture_piece = None capture_file = None capture_rank = None if 'x' in move: @@ -690,7 +686,7 @@ class Gnuchess(): move = move[find(move, 'x') + 1:] if white: if move[0] in 'KQBNR': - capture_piece = move[0] + # capture_piece = move[0] if len(move) > 1: if move[1] in FILES: capture_file = move[1] @@ -700,7 +696,7 @@ class Gnuchess(): elif move[1] in RANKS: capture_rank = move[1] else: - capture_piece = 'p' + # capture_piece = 'p' if move[0] in FILES: capture_file = move[0] if len(move) > 1: @@ -710,7 +706,7 @@ class Gnuchess(): capture_rank = move[0] else: if move[0] in 'KQBNR': - capture_piece = move[0] + # capture_piece = move[0] if len(move) > 1: if move[1] in FILES: capture_file = move[1] @@ -720,7 +716,7 @@ class Gnuchess(): elif move[1] in RANKS: capture_rank = move[1] else: - capture_piece = 'P' + # capture_piece = 'P' if move[0] in FILES: capture_file = move[0] if len(move) > 1: @@ -761,70 +757,75 @@ class Gnuchess(): tiles.append('%s%s' % (capture_file, capture_rank)) self._flash_tile(tiles) - def _search_for_pawn( - self, piece, source_file, source_rank, capture_file, capture_rank, - capture=False): + def _search_for_pawn(self, piece, source_file, source_rank, capture_file, + capture_rank, capture=False): # Check for capture if capture and len(self.move_list) % 2 == 0: if source_file == capture_file: f = FILES.index(capture_file) if f > 0: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[f - 1], RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[f - 1], RANKS[RANKS.index(capture_rank) - 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.white[8 + p].get_xy() if x == pos[0] and y == pos[1]: return FILES[f - 1], \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] if f < 7: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[f + 1], RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[f + 1], RANKS[RANKS.index(capture_rank) - 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.white[8 + p].get_xy() if x == pos[0] and y == pos[1]: return FILES[f + 1], \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] else: - i = self._file_and_rank_to_index('%s%s' % ( - source_file, RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (source_file, RANKS[RANKS.index(capture_rank) - 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.white[8 + p].get_xy() if x == pos[0] and y == pos[1]: return source_file, \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] elif capture: if source_file == capture_file: f = FILES.index(capture_file) if f > 0: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[f - 1], RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[f - 1], RANKS[RANKS.index(capture_rank) + 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.black[8 + p].get_xy() if x == pos[0] and y == pos[1]: return FILES[f - 1], \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] if f < 7: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[f + 1], RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[f + 1], RANKS[RANKS.index(capture_rank) + 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.black[8 + p].get_xy() if x == pos[0] and y == pos[1]: return FILES[f + 1], \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] else: - i = self._file_and_rank_to_index('%s%s' % ( - source_file, RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (source_file, RANKS[RANKS.index(capture_rank) + 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.black[8 + p].get_xy() if x == pos[0] and y == pos[1]: return source_file, \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] # Check for first move if piece == 'p' and capture_rank == '5': i = self._file_and_rank_to_index('%s7' % (capture_file)) @@ -842,16 +843,18 @@ class Gnuchess(): return capture_file, '2' # Check for previous space if piece == 'p': - i = self._file_and_rank_to_index('%s%s' % ( - capture_file, RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (capture_file, RANKS[RANKS.index(capture_rank) + 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.black[8 + p].get_xy() if x == pos[0] and y == pos[1]: return capture_file, RANKS[RANKS.index(capture_rank) + 1] elif piece == 'P': - i = self._file_and_rank_to_index('%s%s' % ( - capture_file, RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (capture_file, RANKS[RANKS.index(capture_rank) - 1])) x, y = self._index_to_xy(i) for p in range(8): pos = self.white[8 + p].get_xy() @@ -859,71 +862,75 @@ class Gnuchess(): return capture_file, RANKS[RANKS.index(capture_rank) - 1] return capture_file, capture_rank - def _search_for_rook( - self, piece, source_file, source_rank, capture_file, capture_rank): + def _search_for_rook(self, piece, source_file, source_rank, capture_file, + capture_rank): # Change rank if len(self.move_list) % 2 == 1: for r in range(7 - RANKS.index(capture_rank)): - i = self._file_and_rank_to_index('%s%s' % ( - capture_file, RANKS[RANKS.index(capture_rank) + r + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (capture_file, RANKS[RANKS.index(capture_rank) + r + 1])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if piece in 'rR' and (b == 0 or b == 7): return capture_file, \ - RANKS[RANKS.index(capture_rank) + r + 1] + RANKS[RANKS.index(capture_rank) + r + 1] elif piece in 'qQ' and b == 3: return capture_file, \ - RANKS[RANKS.index(capture_rank) + r + 1] + RANKS[RANKS.index(capture_rank) + r + 1] else: break elif p is not None: break for r in range(RANKS.index(capture_rank)): - i = self._file_and_rank_to_index('%s%s' % ( - capture_file, RANKS[RANKS.index(capture_rank) - r - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (capture_file, RANKS[RANKS.index(capture_rank) - r - 1])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if piece in 'rR' and (b == 0 or b == 7): return capture_file, \ - RANKS[RANKS.index(capture_rank) - r - 1] + RANKS[RANKS.index(capture_rank) - r - 1] elif piece in 'qQ' and b == 3: return capture_file, \ - RANKS[RANKS.index(capture_rank) - r - 1] + RANKS[RANKS.index(capture_rank) - r - 1] else: break elif p is not None: break else: for r in range(7 - RANKS.index(capture_rank)): - i = self._file_and_rank_to_index('%s%s' % ( - capture_file, RANKS[RANKS.index(capture_rank) + r + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (capture_file, RANKS[RANKS.index(capture_rank) + r + 1])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if piece == 'R' and (w == 0 or w == 7): return capture_file, \ - RANKS[RANKS.index(capture_rank) + r + 1] + RANKS[RANKS.index(capture_rank) + r + 1] elif piece == 'Q' and w == 3: return capture_file, \ - RANKS[RANKS.index(capture_rank) + r + 1] + RANKS[RANKS.index(capture_rank) + r + 1] else: break elif p is not None: break for r in range(RANKS.index(capture_rank)): - i = self._file_and_rank_to_index('%s%s' % ( - capture_file, RANKS[RANKS.index(capture_rank) - r - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (capture_file, RANKS[RANKS.index(capture_rank) - r - 1])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if piece == 'R' and (w == 0 or w == 7): return capture_file, \ - RANKS[RANKS.index(capture_rank) - r - 1] + RANKS[RANKS.index(capture_rank) - r - 1] elif piece == 'Q' and w == 3: return capture_file, \ - RANKS[RANKS.index(capture_rank) - r - 1] + RANKS[RANKS.index(capture_rank) - r - 1] else: break elif p is not None: @@ -931,66 +938,70 @@ class Gnuchess(): # Change file if len(self.move_list) % 2 == 1: for f in range(7 - FILES.index(capture_file)): - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + f + 1], capture_rank)) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + f + 1], capture_rank)) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if piece in 'rR' and (b == 0 or b == 7): return FILES[FILES.index(capture_file) + f + 1], \ - capture_rank + capture_rank elif piece in 'qQ' and b == 3: return FILES[FILES.index(capture_file) + f + 1], \ - capture_rank + capture_rank else: break elif p is not None: break for f in range(FILES.index(capture_file)): - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - f - 1], capture_rank)) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - f - 1], capture_rank)) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if piece in 'rR' and (b == 0 or b == 7): return FILES[FILES.index(capture_file) - f - 1], \ - capture_rank + capture_rank elif piece in 'qQ' and b == 3: return FILES[FILES.index(capture_file) - f - 1], \ - capture_rank + capture_rank else: break elif p is not None: break else: for f in range(7 - FILES.index(capture_file)): - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + f + 1], capture_rank)) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + f + 1], capture_rank)) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if piece == 'R' and (w == 0 or w == 7): return FILES[FILES.index(capture_file) + f + 1], \ - capture_rank + capture_rank elif piece == 'Q' and w == 3: return FILES[FILES.index(capture_file) + f + 1], \ - capture_rank + capture_rank else: break elif p is not None: break for f in range(FILES.index(capture_file)): - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - f - 1], capture_rank)) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - f - 1], capture_rank)) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if piece == 'R' and (w == 0 or w == 7): return FILES[FILES.index(capture_file) - f - 1], \ - capture_rank + capture_rank elif piece == 'Q' and w == 3: return FILES[FILES.index(capture_file) - f - 1], \ - capture_rank + capture_rank else: break elif p is not None: @@ -1000,174 +1011,190 @@ class Gnuchess(): else: return None, None - def _search_for_knight( - self, piece, source_file, source_rank, capture_file, capture_rank): + def _search_for_knight(self, piece, source_file, source_rank, capture_file, + capture_rank): if len(self.move_list) % 2 == 1: # if piece == 'n': if RANKS.index(capture_rank) < 6 and FILES.index(capture_file) > 0: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 1], - RANKS[RANKS.index(capture_rank) + 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 1], + RANKS[RANKS.index(capture_rank) + 2])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) - 1], \ - RANKS[RANKS.index(capture_rank) + 2] + RANKS[RANKS.index(capture_rank) + 2] if RANKS.index(capture_rank) < 6 and FILES.index(capture_file) < 7: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 1], - RANKS[RANKS.index(capture_rank) + 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 1], + RANKS[RANKS.index(capture_rank) + 2])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) + 1], \ - RANKS[RANKS.index(capture_rank) + 2] + RANKS[RANKS.index(capture_rank) + 2] if RANKS.index(capture_rank) > 1 and FILES.index(capture_file) < 7: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 1], - RANKS[RANKS.index(capture_rank) - 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 1], + RANKS[RANKS.index(capture_rank) - 2])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) + 1], \ - RANKS[RANKS.index(capture_rank) - 2] + RANKS[RANKS.index(capture_rank) - 2] if RANKS.index(capture_rank) > 1 and FILES.index(capture_file) > 0: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 1], - RANKS[RANKS.index(capture_rank) - 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 1], + RANKS[RANKS.index(capture_rank) - 2])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) - 1], \ - RANKS[RANKS.index(capture_rank) - 2] + RANKS[RANKS.index(capture_rank) - 2] if RANKS.index(capture_rank) < 7 and FILES.index(capture_file) > 1: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 2], - RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 2], + RANKS[RANKS.index(capture_rank) + 1])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) - 2], \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] if RANKS.index(capture_rank) < 7 and FILES.index(capture_file) < 6: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 2], - RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 2], + RANKS[RANKS.index(capture_rank) + 1])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) + 2], \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] if RANKS.index(capture_rank) > 0 and FILES.index(capture_file) < 6: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 2], - RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 2], + RANKS[RANKS.index(capture_rank) - 1])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) + 2], \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] if RANKS.index(capture_rank) > 0 and FILES.index(capture_file) > 1: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 2], - RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 2], + RANKS[RANKS.index(capture_rank) - 1])) p = self._find_piece_at_index(i) if p in self.black: b = self.black.index(p) if b in [1, 6]: return FILES[FILES.index(capture_file) - 2], \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] else: if RANKS.index(capture_rank) < 6 and FILES.index(capture_file) > 0: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 1], - RANKS[RANKS.index(capture_rank) + 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 1], + RANKS[RANKS.index(capture_rank) + 2])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) - 1], \ - RANKS[RANKS.index(capture_rank) + 2] + RANKS[RANKS.index(capture_rank) + 2] if RANKS.index(capture_rank) < 6 and FILES.index(capture_file) < 7: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 1], - RANKS[RANKS.index(capture_rank) + 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 1], + RANKS[RANKS.index(capture_rank) + 2])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) + 1], \ - RANKS[RANKS.index(capture_rank) + 2] + RANKS[RANKS.index(capture_rank) + 2] if RANKS.index(capture_rank) > 1 and FILES.index(capture_file) < 7: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 1], - RANKS[RANKS.index(capture_rank) - 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 1], + RANKS[RANKS.index(capture_rank) - 2])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) + 1], \ - RANKS[RANKS.index(capture_rank) - 2] + RANKS[RANKS.index(capture_rank) - 2] if RANKS.index(capture_rank) > 1 and FILES.index(capture_file) > 0: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 1], - RANKS[RANKS.index(capture_rank) - 2])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 1], + RANKS[RANKS.index(capture_rank) - 2])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) - 1], \ - RANKS[RANKS.index(capture_rank) - 2] + RANKS[RANKS.index(capture_rank) - 2] if RANKS.index(capture_rank) < 7 and FILES.index(capture_file) > 1: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 2], - RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 2], + RANKS[RANKS.index(capture_rank) + 1])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) - 2], \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] if RANKS.index(capture_rank) < 7 and FILES.index(capture_file) < 6: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 2], - RANKS[RANKS.index(capture_rank) + 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 2], + RANKS[RANKS.index(capture_rank) + 1])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) + 2], \ - RANKS[RANKS.index(capture_rank) + 1] + RANKS[RANKS.index(capture_rank) + 1] if RANKS.index(capture_rank) > 0 and FILES.index(capture_file) < 6: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) + 2], - RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) + 2], + RANKS[RANKS.index(capture_rank) - 1])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) + 2], \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] if RANKS.index(capture_rank) > 0 and FILES.index(capture_file) > 1: - i = self._file_and_rank_to_index('%s%s' % ( - FILES[FILES.index(capture_file) - 2], - RANKS[RANKS.index(capture_rank) - 1])) + i = self._file_and_rank_to_index( + '%s%s' % + (FILES[FILES.index(capture_file) - 2], + RANKS[RANKS.index(capture_rank) - 1])) p = self._find_piece_at_index(i) if p in self.white: w = self.white.index(p) if w in [1, 6]: return FILES[FILES.index(capture_file) - 2], \ - RANKS[RANKS.index(capture_rank) - 1] + RANKS[RANKS.index(capture_rank) - 1] return capture_file, capture_rank - def _search_for_bishop( - self, piece, source_file, source_rank, capture_file, capture_rank): + def _search_for_bishop(self, piece, source_file, source_rank, capture_file, + capture_rank): # rank++, file++ if len(self.move_list) % 2 == 1: # if piece in 'bq': r = RANKS.index(capture_rank) + 1 @@ -1318,8 +1345,8 @@ class Gnuchess(): f -= 1 return capture_file, capture_rank - def _search_for_queen( - self, piece, source_file, source_rank, capture_file, capture_rank): + def _search_for_queen(self, piece, source_file, source_rank, capture_file, + capture_rank): file_and_rank = self._search_for_rook( piece, source_file, source_rank, capture_file, capture_rank) if file_and_rank[0] is not None: @@ -1327,8 +1354,8 @@ class Gnuchess(): return self._search_for_bishop( piece, source_file, source_rank, capture_file, capture_rank) - def _search_for_king( - self, piece, source_file, source_rank, capture_file, capture_rank): + def _search_for_king(self, piece, source_file, source_rank, capture_file, + capture_rank): if len(self.move_list) % 2 == 1: # if piece == 'k': r = RANKS.index(capture_rank) + 1 f = FILES.index(capture_file) + 1 @@ -1522,20 +1549,23 @@ class Gnuchess(): ''' calculate the board column and row for an xy position ''' xo = self._width - 8 * self.scale xo = int(xo / 2) - x = pos[0] - xo yo = int(self.scale / 2) - y = yo - return ('%s%d' % (FILES[int((pos[0] - xo) / self.scale)], - 8 - int((pos[1] - yo) / self.scale))) + x = int((pos[0] - xo) / self.scale) + y = 8 - int((pos[1] - yo) / self.scale) + if x < 0 or x > 7: + return None # off the board + else: + return ('%s%d' % (FILES[x], y)) def __draw_cb(self, canvas, cr): - self._sprites.redraw_sprites(cr=cr) + self._sprites.redraw_sprites(cr=cr) + def do_expose_event(self, event): ''' Handle the expose-event by drawing ''' # Restrict Cairo to the exposed area cr = self._canvas.window.cairo_create() cr.rectangle(event.area.x, event.area.y, - event.area.width, event.area.height) + event.area.width, event.area.height) cr.clip() # Refresh sprite list self._sprites.redraw_sprites(cr=cr) @@ -1733,8 +1763,9 @@ class Gnuchess(): fontsize = 18 self.bg = [] for i in range(3): - self.bg.append(Sprite(self._sprites, 0, 0, self._box( - int(self._width), self._height, color=colors[1]))) + self.bg.append( + Sprite(self._sprites, 0, 0, self._box( + int(self._width), self._height, color=colors[1]))) self.bg[-1].set_layer(-1) self.bg[-1].set_margins(l=10, t=10, r=10, b=10) self.bg[-1].set_label_attributes(fontsize, horiz_align="left", @@ -1750,13 +1781,13 @@ class Gnuchess(): self.rank = Sprite(self._sprites, xo - self.scale, yo, GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/images/rank.svg' % (self._bundle_path), - self.scale, 8 * self.scale)) + '%s/images/rank.svg' % (self._bundle_path), + self.scale, 8 * self.scale)) self.rank.set_layer(0) - self.file = Sprite(self._sprites, xo, yo + int(self.scale * 8), - GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/images/file.svg' % (self._bundle_path), - 8 * self.scale, self.scale)) + self.file = Sprite(self._sprites, xo, yo + int(self.scale * 8), + GdkPixbuf.Pixbuf.new_from_file_at_size( + '%s/images/file.svg' % (self._bundle_path), + 8 * self.scale, self.scale)) self.file.set_layer(0) w = h = self.scale @@ -1780,29 +1811,29 @@ class Gnuchess(): y += self.scale self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/white-pawn.svg' % (self._bundle_path), w, h)) + '%s/icons/white-pawn.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/black-pawn.svg' % (self._bundle_path), w, h)) + '%s/icons/black-pawn.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/white-rook.svg' % (self._bundle_path), w, h)) + '%s/icons/white-rook.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/black-rook.svg' % (self._bundle_path), w, h)) + '%s/icons/black-rook.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/white-knight.svg' % (self._bundle_path), w, h)) + '%s/icons/white-knight.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/black-knight.svg' % (self._bundle_path), w, h)) + '%s/icons/black-knight.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/white-bishop.svg' % (self._bundle_path), w, h)) + '%s/icons/white-bishop.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/black-bishop.svg' % (self._bundle_path), w, h)) + '%s/icons/black-bishop.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/white-queen.svg' % (self._bundle_path), w, h)) + '%s/icons/white-queen.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/black-queen.svg' % (self._bundle_path), w, h)) + '%s/icons/black-queen.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/white-king.svg' % (self._bundle_path), w, h)) + '%s/icons/white-king.svg' % (self._bundle_path), w, h)) self.skins.append(GdkPixbuf.Pixbuf.new_from_file_at_size( - '%s/icons/black-king.svg' % (self._bundle_path), w, h)) + '%s/icons/black-king.svg' % (self._bundle_path), w, h)) self.white.append(Sprite(self._sprites, 0, 0, self.skins[WR])) self.white[-1].type = 'R' @@ -1873,10 +1904,10 @@ class Gnuchess(): self._svg_width = w self._svg_height = h return svg_str_to_pixbuf( - self._header() + \ - self._rect(self._svg_width, self._svg_height, 0, 0, - color=color) + \ - self._footer()) + self._header() + + self._rect(self._svg_width, self._svg_height, 0, 0, + color=color) + + self._footer()) def _header(self): return '