def choose(button): popup.dismiss() move = self.square_name(self._moving_piece_from) + self.square_name(square) + button.piece if move in sf.legal_moves(self.fen): self._game.moves.append(move) else: self._draw_board() self._draw_pieces()
def on_touch_up(self, touch): square = self._to_square(touch) if square == -1 or self._moving_piece == '.' or square == self._moving_piece_from or not self.collide_point(*touch.pos): return move = self.square_name(self._moving_piece_from) + self.square_name(square) if move in sf.legal_moves(self.fen): self._moving_piece_pos[0], self._moving_piece_pos[1] = self._to_coordinates( self._moving_piece_from) if self._animate_from_origin else (touch.x - self.square_size / 2, touch.y - self.square_size / 2) animation = Animation(_moving_piece_pos=self._to_coordinates(square), duration=0.1, t='in_out_sine') animation.move = move animation.bind(on_complete=self._update_after_animation) animation.start(self) print('MOVE : ' + move) else: if (self._moving_piece == 'P' and square < 8) or (self._moving_piece == 'p' and square > 55): #Show a popup for promotions layout = GridLayout(cols=2) def choose(button): popup.dismiss() move = self.square_name(self._moving_piece_from) + self.square_name(square) + button.piece if move in sf.legal_moves(self.fen): self._game.moves.append(move) else: self._draw_board() self._draw_pieces() for p in 'qrbn': btn = Button(text=self._front_textures[p], font_name='ressources/ChessCases.ttf', font_size=self.board_size / 8) btn.piece = p btn.bind(on_release=lambda b: choose(b)) layout.add_widget(btn) popup = Popup(title='Promote to', content=layout, size_hint=(.5, .5)) popup.open() else: # Illegal move self._moving_piece_pos[0] = touch.x - self.square_size / 2 self._moving_piece_pos[1] = touch.y - self.square_size / 2 animation = Animation(_moving_piece_pos=self._to_coordinates(self._moving_piece_from), duration=0.3, t='in_out_sine') animation.bind(on_complete=self._update_after_animation) animation.start(self) touch.ungrab(self) return True
def legal_moves(self): return sf.legal_moves(self.current_fen())