def run_test_1(): print('------- Scenario 1: Equivance of Minimax and AlphaBeta --------') #-- Scenario 1: Test to make sure MiniMaxAI and AlphaBetaAI are giving the same results #-- Here are the various players. player_human = HumanPlayer() player_mini = MinimaxAI(3, color=True) player_alphaBeta = AlphaBetaAI(3, color=True) #-- Set up the board in a the following fashion. Remember our AIs are WHITE in these scenarios. game = ChessGame(player_human, player_mini) game.board.clear_board() game.board.set_piece_at(piece=chess.Piece(4, False), square=16) game.board.set_piece_at(piece=chess.Piece(2, False), square=8) game.board.set_piece_at(piece=chess.Piece(3, False), square=10) game.board.set_piece_at(piece=chess.Piece(3, True), square=1) #-- Display the board and possible moves. print(game) print("Possible Moves:") for move in game.board.pseudo_legal_moves: print(move) print('---------------------------') #-- Look at the choice for MinimaxAI: print('Chosen Move:', player_mini.choose_move(game.board)) print('---------------------------') #-- Look at the choice for AlphaBetaAI: print('Chosen Move:', player_alphaBeta.choose_move(game.board)) print('---------------------------')
def run_test_2(): print('------- Scenario 2: Take the Win! --------') #-- Scenario 2: Test to make dure MiniMax and AlphaBetaAI take the win for a more complicated scenario (4 more checkmate) in a predefined position. #-- Here are the various players. player_human = HumanPlayer() player_mini = MinimaxAI(3, color=True) player_alphaBeta = AlphaBetaAI(3, color=True) game = ChessGame(player_human, player_mini) game.board.reset() game.board.push(chess.Move(12,28)) game.board.push(chess.Move(52, 36)) game.board.push(chess.Move(5, 26)) game.board.push(chess.Move(57, 42)) game.board.push(chess.Move(3, 39)) game.board.push(chess.Move(62, 45)) #-- Display Board print(game) print('---------------------------') #-- Look at choice for MiniMaxAI print('Chosen Move:', player_mini.choose_move(game.board)) print('---------------------------') #-- Look at choice for AlphaBetaAI print('Chosen Move:', player_alphaBeta.choose_move(game.board)) print('---------------------------')
def __init__(self, timelimit): self.pygame = pygame self.pygame.init() self.fpsClock = pygame.time.Clock() self.board = Board() self.whitePlayer = Human(self, self.board, Board.white) self.blackPlayer = MinimaxAI(self, self.board, Board.black, timelimit) self.currentPlayer = self.blackPlayer self.screen = self.drawscreen()
# Iain Sheerin # 1/21/19 # CS76 HW3 import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI # from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame player1 = MinimaxAI(4, True, True) player2 = HumanPlayer() # Example 1, AI has mate in one as white game = ChessGame(player1, player2) game.board.clear_board() game.board.set_piece_at(0, chess.Piece(6, False)) game.board.set_piece_at(17, chess.Piece(6, True)) game.board.set_piece_at(15, chess.Piece(5, True)) while not game.is_game_over(): print(game) game.make_move() # Example 2, after human move, AI has mate in one as black game = ChessGame(player2, player1) game.board.clear_board() game.board.set_piece_at(0, chess.Piece(6, True)) game.board.set_piece_at(17, chess.Piece(6, False)) game.board.set_piece_at(15, chess.Piece(5, False))
# pip3 install python-chess import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame import sys player1 = RandomAI() player2 = MinimaxAI(2) game = ChessGame(player1, player2) while not game.is_game_over(): print(game) game.make_move() #print(hash(str(game.board)))
class GUIboard(object): # Load pygame def __init__(self, timelimit): self.pygame = pygame self.pygame.init() self.fpsClock = pygame.time.Clock() self.board = Board() self.whitePlayer = Human(self, self.board, Board.white) self.blackPlayer = MinimaxAI(self, self.board, Board.black, timelimit) self.currentPlayer = self.blackPlayer self.screen = self.drawscreen() def drawscreen(self): # Setup screen sqs = 70 bs = 8*sqs screen = self.pygame.display.set_mode((bs, bs)) tit = 'Othello ala Linus och Kasper!' pygame.display.set_caption(tit) pygame.mouse.set_visible(True) self.myfont = self.pygame.font.SysFont("monospace", 20) return screen def updatescreen(self): black = pygame.Color(0, 0, 0) white = pygame.Color(255, 255, 255) green = pygame.Color(0, 128, 0) sqs = 70 bs = 8*sqs self.screen.fill(green) for y in xrange(8): for x in xrange(8): # Draw black background squares self.pygame.draw.rect(self.screen, black, (y*sqs, x*sqs, sqs, sqs), 2) # Draw coordinate label = self.myfont.render("("+str(y)+","+str(x)+")", 1, (0,0,0)) self.screen.blit(label, (x*sqs+5, y*sqs+5)) # Draw disks if self.board.grid[y][x] == Board.black: self.pygame.draw.circle(self.screen, black, (x*sqs+sqs/2, y*sqs+sqs/2), sqs/2, 0) elif self.board.grid[y][x] == Board.white: self.pygame.draw.circle(self.screen, white, (x*sqs+sqs/2, y*sqs+sqs/2), sqs/2, 0) def rungame(self): # Main application loop while True: self.updatescreen() self.pygame.display.update() self.fpsClock.tick(30) # Handle actions canWhite = self.board.canMakeMove(Board.white) canBlack = self.board.canMakeMove(Board.black) if not canWhite and not canBlack: print "Score (black, white):", self.board.score() while True: for event in self.pygame.event.get(): if event.type == QUIT: self.pygame.quit() sys.exit() elif self.currentPlayer == self.whitePlayer and canWhite: y,x = self.whitePlayer.makeMove() self.currentPlayer = self.blackPlayer elif self.currentPlayer == self.whitePlayer and not canWhite: self.currentPlayer = self.blackPlayer elif self.currentPlayer == self.blackPlayer and canBlack: y,x = self.blackPlayer.makeMove() self.currentPlayer = self.whitePlayer elif self.currentPlayer == self.blackPlayer and not canBlack: self.currentPlayer = self.whitePlayer
from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI class IterativeAI: def __init__(self, ai, time): self.AI = ai self.maximum_time = time # seconds def choose_move(self, board): timeout = time.time( ) + self.maximum_time # in reality this won't suffice. I actually have to stop choose_move during its call if I want to timer-limit the algorithm. best_move = None # the move to return self.AI.depth = 0 while True: if time.time() > timeout: return best_move self.AI.depth += 1 print("Iteration with depth: " + str(self.AI.depth)) move = self.AI.choose_move(board) best_move = move if __name__ == '__main__': ai = IterativeAI(MinimaxAI(), 10) ai.choose_move()
from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame if __name__ == "__main__": # To play, uncomment one player one and one player two of the algorithm of your choice. For the depth based # algorithms, you can also change the depth. The True or False corresponds to if the player is white or black # with a "True" corresponding to white (player1) # player1 = HumanPlayer() # player2 = HumanPlayer() # player1 = RandomAI() # player2 = RandomAI() # player1 = MinimaxAI(2, True) player2 = MinimaxAI(2, False) player1 = AlphaBetaAI(2, True) # player2 = AlphaBetaAI(2, False) # player1 = IterativeDeepeningMinimaxAI(3, True) # player2 = IterativeDeepeningMinimaxAI(2, False) game = ChessGame(player1, player2) while not game.is_game_over(): print("GAME") print(game) game.make_move() game.exit_game()
from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from Iterative_Deepening_AI import Iterative_Deepening_AI from ChessGame import ChessGame import time # CREATE A PLAYER: # specify the type: minimaxAI, alphabetaAI, or Iterative_Deepening_AI # specify the depth limit: 1-5 # specify the color: chess.WHITE or chess.BLACK player_human = HumanPlayer() player_randomAI = RandomAI() player_minimax_4_white = MinimaxAI(4, chess.WHITE) player_minimax_4_black = MinimaxAI(4, chess.BLACK) player_minimax_3_white = MinimaxAI(3, chess.WHITE) player_minimax_3_black = MinimaxAI(3, chess.BLACK) player_minimax_2_white = MinimaxAI(2, chess.WHITE) player_minimax_2_black = MinimaxAI(2, chess.BLACK) player_minimax_1_white = MinimaxAI(1, chess.WHITE) player_minimax_1_black = MinimaxAI(1, chess.BLACK) player_alphabeta_5_white = AlphaBetaAI(5, chess.WHITE) player_alphabeta_5_black = AlphaBetaAI(5, chess.BLACK) player_alphabeta_4_white = AlphaBetaAI(4, chess.WHITE) player_alphabeta_4_black = AlphaBetaAI(4, chess.BLACK) player_alphabeta_3_white = AlphaBetaAI(3, chess.WHITE) player_alphabeta_3_black = AlphaBetaAI(3, chess.BLACK) player_alphabeta_2_white = AlphaBetaAI(2, chess.WHITE)
def test(): player1 = MinimaxAI(3) player2 = RandomAI() game = ChessGame(player1, player2) game.make_move()
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() if __name__ == "__main__": random.seed(1) #player_ronda = RandomAI() # to do: gui does not work well with HumanPlayer, due to input() use on stdin conflict # with event loop. player1 = MinimaxAI(3) player2 = AlphaBetaAI(5) game = ChessGame(player1, player2) gui = ChessGui(player1, player2) gui.start() sys.exit(gui.app.exec_())
Board 4: game.board = chess.Board("8/pkP5/8/8/P7/6q1/3Q2p1/2R2rK1 w - - 0 1") Board 5: game.board = chess.Board("7k/8/8/Q6q/8/PPPP4/PPPP4/K7 b - - 0 1") ''' ################## # Minimax Tests: # ################## # Board 1: # # Depth 0 player1 = MinimaxAI(heuristics.std_get_material, 0) player2 = MinimaxAI(heuristics.std_get_material, 3) game = ChessGame(player1, player2) print(game) game.make_move() # Depth 1 player1 = MinimaxAI(heuristics.std_get_material, 1) player2 = MinimaxAI(heuristics.std_get_material, 3) game = ChessGame(player1, player2) print(game) game.make_move() # Depth 2
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() if __name__ == "__main__": random.seed(1) #player_ronda = RandomAI() # to do: gui does not work well with HumanPlayer, due to input() use on stdin conflict # with event loop. player1 = MinimaxAI(10) player2 = MinimaxAI(10) game = ChessGame(player1, player2) gui = ChessGui(player1, player2) gui.start() sys.exit(gui.app.exec_())
# pip3 install python-chess import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame import sys import numpy as np player1 = AlphaBetaAI(2, True) player2 = MinimaxAI(2, False) # RandomAI() game: ChessGame = ChessGame(player1, player2) while not game.is_game_over(): print(game) game.make_move() print(game) player1.end_report() player2.end_report() # print(hash(str(game.board)))
# pip3 install python-chess import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame from transposition_alphabeta import transposition_table import sys player1 = AlphaBetaAI(2) player2 = AlphaBetaAI(2) player3 = MinimaxAI(3) player4 = MinimaxAI(3) player5 = transposition_table(2) player6 = transposition_table(2) game = ChessGame(player3, player4) while not game.is_game_over(): print(game) game.make_move() print(game.board.result()) #print (player1.consider_call) #print (player2.consider_call) #print(hash(str(game.board)))
def run_test_5(): #-- Iterative Deepening Test: Make sure that the deeper you go the better answer you get. #-- Set board. board = get_random_board(26) print(board) #-- Set up players player2 = MinimaxAI(2, color=True) player3 = MinimaxAI(3, color=True) player4 = MinimaxAI(4, color=True) playera2 = AlphaBetaAI(2, color=True) playera3 = AlphaBetaAI(3, color=True) playera4 = AlphaBetaAI(4, color=True) #-- Get move scores from different depths player2.choose_move(board) player3.choose_move(board) player4.choose_move(board) print('---------------------') playera2.choose_move(board) playera3.choose_move(board) playera4.choose_move(board)
# Iain Sheerin # 1/21/19 # CS76 HW3 import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame player1 = AlphaBetaAI(4, True, True) player2 = HumanPlayer() player3 = MinimaxAI(4, True, True) # Example 1, AlphaBetaAI has mate in one as white game = ChessGame(player1, player2) game.board.clear_board() game.board.set_piece_at(0, chess.Piece(6, False)) game.board.set_piece_at(17, chess.Piece(6, True)) game.board.set_piece_at(15, chess.Piece(5, True)) while not game.is_game_over(): print(game) game.make_move() # Example 2, MinimaxAI has mate in one as white game = ChessGame(player3, player2) game.board.clear_board() game.board.set_piece_at(0, chess.Piece(6, False)) game.board.set_piece_at(17, chess.Piece(6, True)) game.board.set_piece_at(15, chess.Piece(5, True))
# pip3 install python-chess import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame import sys player1 = HumanPlayer() player2 = MinimaxAI(3, 0) game = ChessGame(player1, player2) #print(str(game.board)) #print(game) #print(player2.get_utility(game.board)) print(int(game.board.turn)) while not game.is_game_over(): print(game) game.make_move() print(game)
# pip3 install python-chess import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame import sys #all the potential playesr player1 = HumanPlayer() player2 = RandomAI() player3 = MinimaxAI(4) player4 = AlphaBetaAI(3) #starting the game with correct players game = ChessGame(player1, player4) #keeping the game going while not game.is_game_over(): print(game) game.make_move()