class ChessGui: def __init__(self, player1, player2): self.player1 = player1 self.player2 = player2 self.game = ChessGame(player1, player2) self.app = QApplication(sys.argv) self.svgWidget = QtSvg.QSvgWidget() self.svgWidget.setGeometry(50, 50, 400, 400) self.svgWidget.show() def start(self): self.timer = QTimer() self.timer.timeout.connect(self.make_move) self.timer.start(10) self.display_board() def display_board(self): svgboard = chess.svg.board(self.game.board) svgbytes = QByteArray() svgbytes.append(svgboard) self.svgWidget.load(svgbytes) def make_move(self): print("making move, white turn " + str(self.game.board.turn)) self.game.make_move() self.display_board()
def run_game(player2, move_num=20): player1 = RandomAI() game = ChessGame(player1, player2) for _ in range(move_num): print(game) game.make_move() return game
class ChessGui: def __init__(self, player1, player2): self.player1 = player1 self.player2 = player2 self.game = ChessGame(player1, player2) self.app = QApplication(sys.argv) self.svgWidget = QtSvg.QSvgWidget() self.svgWidget.setGeometry(50, 50, 400, 400) self.svgWidget.show() def start(self): self.timer = QTimer() self.timer.timeout.connect(self.make_move) self.timer.start(10) self.display_board() def display_board(self): svgboard = chess.svg.board(self.game.board) svgbytes = QByteArray() svgbytes.append(svgboard) self.svgWidget.load(svgbytes) def make_move(self): if not self.game.is_game_over(): self.game.make_move() self.display_board() else: print("Game over!") if self.game.board.is_checkmate(): print("The winner is {}".format( "Black" if self.game.board.turn else "White")) else: print("The game resulted in a tie!") game.players[0].end_report() game.players[1].end_report() sleep(100)
# black's turn, black can absolutely win in 3 turns def black_checkmate(game): board = game.board board.clear() board.turn = False white_pawn1 = chess.Piece(1, True) white_pawn2 = chess.Piece(1, True) white_knight = chess.Piece(2, True) white_king = chess.Piece(6, True) black_king = chess.Piece(6, False) black_rook = chess.Piece(4, False) board.set_piece_at(chess.parse_square("a2"), white_pawn1) board.set_piece_at(chess.parse_square("b2"), white_pawn2) board.set_piece_at(chess.parse_square("a1"), white_king) board.set_piece_at(chess.parse_square("b3"), white_knight) board.set_piece_at(chess.parse_square("h8"), black_king) board.set_piece_at(chess.parse_square("h7"), black_rook) # open_board(game) # black_checkmate(game) # e2e4 while not game.is_game_over(): print(game) game.make_move() #print(hash(str(game.board)))
def test(): player1 = MinimaxAI(3) player2 = RandomAI() game = ChessGame(player1, player2) game.make_move()