def searchbestmoves(self, playerTile, opponentTile): """ search for the best moves using min-max algorithms with alpha-beta pruning implemented in the Reversi_AI.py Args: playerTile(str): a string that represents player's tile opponentTile(str): a string that represents player's opponent's tile Returns: suggestMove((x,y)): best move for player """ AI = Reversi_AI() suggestMove = AI.getBestMove(self.board, 3, playerTile, opponentTile) return suggestMove
def searchbestmoves(self, playerTile, opponentTile): """ search for the best moves using min-max algorithms with alpha-beta pruning implemented in the Reversi_AI.py Args: playerTile(str): a string that represents player's tile opponentTile(str): a string that represents player's opponent's tile Returns: suggestMove((x,y)): best move for player """ AI = Reversi_AI() suggestMove = AI.getBestMove(self.board,3,playerTile,opponentTile) return suggestMove
def run_game(self,reversi): """ run_game controls the flow of the game Args: reversi(object): contains all functions necessary for running the game Returns: restart(boolean): true if user wants to restart the game and false otherwise """ restart = False end = False board = reversi.getBoard() # initialize game board AI = Reversi_AI() end = self.drawBoard(reversi,0) # draw the game board yield_ = 0 waitForComputer = False waitForUser = True # let user go first loop = True while loop: for event in pygame.event.get(): if end: # if end of the game print("end of this round") if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: restart = True print("down key") loop = False # end the game elif event.type is pygame.QUIT: sys.exit() elif event.type is pygame.MOUSEBUTTONDOWN: (mouseX,mouseY) = pygame.mouse.get_pos() if waitForUser is True: # player's turn if len(AI.searchAllmoves(board,"X","0")) > 0: # if there are valid moves for player yield_ = 0 if self.checkValid(mouseX,mouseY,board,"X","0"): # if user chooses a valid move (userX,userY) = self.convertCoordinate(1,(mouseX,mouseY)) self.updatePos(1,userX+1,userY+1) board = reversi.updateboard(userX,userY,self.userTile) # update board according to new move end = self.drawBoard(reversi,yield_) pygame.display.flip() pygame.time.wait(200) board = reversi.reverse(userX,userY,self.userTile,self.computerTile) # reverse tiles end = self.drawBoard(reversi,yield_) pygame.display.flip() pygame.time.wait(200) waitForComputer = True # computer's turn waitForUser = False else: print("invalid move") else: # if no valid move for player print("yield to computer") yield_ = 2 waitForComputer = True # pass play to computer waitForUser = False elif waitForComputer is True: # computer's turn bestMove = reversi.searchbestmoves(self.computerTile, self.userTile) # get best move for computer if bestMove != None: yield_ = 0 board = reversi.updateboard(bestMove[0],bestMove[1],self.computerTile) self.updatePos(0,bestMove[0]+1,bestMove[1]+1) # update board according to new move end = self.drawBoard(reversi,yield_) pygame.display.flip() pygame.time.wait(200) print("waitForComputer") board = reversi.reverse(bestMove[0],bestMove[1],self.computerTile,self.userTile) # reverse tiles else: print("yield to user") yield_ = 1 # pass play to computer waitForComputer = False waitForUser = True else: # user has not clicked on the board at user's turn if len(AI.searchAllmoves(board,"X","0")) == 0: waitForComputer = True waitForUser = False yield_ = 2 # if no valid move for user, pass play to computer print("yield to computer") # update board end = self.drawBoard(reversi,yield_) pygame.display.flip() if yield_ != 0: pygame.time.wait(1000) else: pygame.time.wait(200) print("out of the loop") return restart
def run_game(self, reversi): """ run_game controls the flow of the game Args: reversi(object): contains all functions necessary for running the game Returns: restart(boolean): true if user wants to restart the game and false otherwise """ restart = False end = False board = reversi.getBoard() # initialize game board AI = Reversi_AI() end = self.drawBoard(reversi, 0) # draw the game board yield_ = 0 waitForComputer = False waitForUser = True # let user go first loop = True while loop: for event in pygame.event.get(): if end: # if end of the game print("end of this round") if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: restart = True print("down key") loop = False # end the game elif event.type is pygame.QUIT: sys.exit() elif event.type is pygame.MOUSEBUTTONDOWN: (mouseX, mouseY) = pygame.mouse.get_pos() if waitForUser is True: # player's turn if len(AI.searchAllmoves(board, "X", "0") ) > 0: # if there are valid moves for player yield_ = 0 if self.checkValid( mouseX, mouseY, board, "X", "0"): # if user chooses a valid move (userX, userY) = self.convertCoordinate( 1, (mouseX, mouseY)) self.updatePos(1, userX + 1, userY + 1) board = reversi.updateboard( userX, userY, self.userTile ) # update board according to new move end = self.drawBoard(reversi, yield_) pygame.display.flip() pygame.time.wait(200) board = reversi.reverse( userX, userY, self.userTile, self.computerTile) # reverse tiles end = self.drawBoard(reversi, yield_) pygame.display.flip() pygame.time.wait(200) waitForComputer = True # computer's turn waitForUser = False else: print("invalid move") else: # if no valid move for player print("yield to computer") yield_ = 2 waitForComputer = True # pass play to computer waitForUser = False elif waitForComputer is True: # computer's turn bestMove = reversi.searchbestmoves( self.computerTile, self.userTile) # get best move for computer if bestMove != None: yield_ = 0 board = reversi.updateboard(bestMove[0], bestMove[1], self.computerTile) self.updatePos(0, bestMove[0] + 1, bestMove[1] + 1) # update board according to new move end = self.drawBoard(reversi, yield_) pygame.display.flip() pygame.time.wait(200) print("waitForComputer") board = reversi.reverse(bestMove[0], bestMove[1], self.computerTile, self.userTile) # reverse tiles else: print("yield to user") yield_ = 1 # pass play to computer waitForComputer = False waitForUser = True else: # user has not clicked on the board at user's turn if len(AI.searchAllmoves(board, "X", "0")) == 0: waitForComputer = True waitForUser = False yield_ = 2 # if no valid move for user, pass play to computer print("yield to computer") # update board end = self.drawBoard(reversi, yield_) pygame.display.flip() if yield_ != 0: pygame.time.wait(1000) else: pygame.time.wait(200) print("out of the loop") return restart