def play(self, player, old_pos, new_pos): self.checkStatePlaying() player_index = self.getPlayerIndex(player) color = WHITE if player_index == 0 else BLACK if self.next_turn != color: raise ValueError(tr("It's not your turn.")) old_index = parsePieceIndex(old_pos) new_index = parsePieceIndex(new_pos) piece = self.board[old_index] if not piece: raise ValueError(tr("There is no piece at %s.") % old_pos) moves = self.possibleMoves(old_index) if new_index not in moves: raise ValueError(tr("Invalid move: %s") % new_pos) moved_piece = self.board[old_index] if moved_piece.color != color: raise ValueError(tr("You cannot move your opponent's pieces.")) taken = self.board[new_index] self.board[old_index] = None self.board[new_index] = moved_piece self.notifyAll(u'chess_move', player.identifier, old_pos, new_pos, exclude=player) if taken is not None: self.notifyAll(u'chess_take', player.identifier, new_pos, taken.export(), exclude=player) if self.next_turn == WHITE: self.next_turn = BLACK else: self.next_turn = WHITE
def getPossibleMoves(self, piece): piece_index = parsePieceIndex(piece) moves = self.possibleMoves(piece_index) moves = [formatPieceIndex(move) for move in moves] return Response(u'chess_possible_moves', moves)