def makeMoveBlack(move, is_late_game): if display.player_color == "B": board.push_uci(move) else: # The depth attribute has to be even if is_late_game: black = minimax(depth + 2, False, MIN, MAX, board, True) else: black = minimax(depth, False, MIN, MAX, board, True) board.push(black) display.update(board)
def makeMoveWhite(move, is_late_game): if display.player_color == "W": board.push_uci(move) else: # The depth attribute has to be odd if is_late_game: white = minimax(depth + 1, True, MIN, MAX, board, True) else: white = minimax(depth + 1, True, MIN, MAX, board, True) board.push(white) display.update(board)
def play(pos, who, text_count): player = who if player: #Max player's move if text_count > 1: pos, text_count = read_Y(pos, text_count) #given the current position, a depth, whos turn and checkmate, try to use minimax score, move = minimax(pos, 1, True, player) if score == 99999: config.TOTAL_PIECE -= 1 #engine check for player win if pos.score <= -MATE_LOWER: print("PLAYER Y has won!") return True #call to make a move, save and show the move pos = showMove(pos, True, move, text_count) text_count += 1 else: #Minimizing PLAYER MOVE pos, text_count = read_X(pos, text_count) #rotate board for library to work properly pos = pos.rotate() #call minimax to determine the move score, move = minimax(pos, 1, False, player) if score == -1001: config.TOTAL_PIECE -= 1 #This engine's check for player win if pos.score <= -MATE_LOWER: print("Player X has won!") return True #call to make a move, save and show the move pos = showMove(pos, False, move, text_count) text_count += 1 return pos, False, text_count
def minimaxBclicked(self): self.gameType = "MINIMAX" print( "\n\n\n-------------------Mim=niMax Algo-------------------\n-------------------AI TURN---------------------" ) action = minimax(globalState) print( "---- (as all action have same utility value, we choose one random) ----\n*******************************************************************\n\n\n" ) temp = random.choice([1, 2, 3, 4]) self.b[temp - 1].setIcon( QIcon("/Users/rachitrathore/PycharmProjects/AI3/green.png")) self.b[temp - 1].setIconSize(QSize(50, 50)) self.b[temp - 1].setEnabled(False) coord = noToCoord(temp) globalState.machineCoord.append(coord) self.winL.setText( "Machine played first! \nHuman it's your Turn...Place your blue coin" ) self.winL.adjustSize() self.minimaxB.setEnabled(False) self.alphabetaB.setEnabled(False) for i in range(16): self.b[i].setEnabled(True)
def find_best_move(self, playboard): emptyplayboard = self.check_if_Playboard_empty(playboard) if emptyplayboard: firstmove = [True, 0, 0] return firstmove else: ismaximizing = False possible_moves = [] bestscore = -2 rowcoordinate = -1 for row in playboard: rowcoordinate += 1 spotcoordinate = -1 for spot in row: spotcoordinate += 1 if spot == "-": playboard[rowcoordinate][spotcoordinate] = self.sign score = minimax(playboard, 100, ismaximizing) playboard[rowcoordinate][spotcoordinate] = "-" move = [bestscore, rowcoordinate, spotcoordinate] if score > bestscore: bestscore = score possible_moves.clear() possible_moves.append(move) elif score == bestscore: possible_moves.append(move) else: continue else: continue bestmove = random.choice(possible_moves) return bestmove #returns a list ->[score,row,spot]
def b16clicked(self): if not possibleClicks(globalState).__contains__([3, 3]): self.winL.setText( "HUMAN TURN -- \nPlease place your coin in right position") self.winL.adjustSize() return else: self.b[15].setIcon(QIcon("blue.png")) self.b[15].setIconSize(QSize(50, 50)) self.b[15].setEnabled(False) globalState.humanCoord.append([3, 3]) if terminalTest(globalState) and utilityValue(globalState) == -1: self.winL.setText("HUMAN WON!! Please reset") self.winL.adjustSize() for i in range(16): self.b[i].setEnabled(False) return if self.gameType == "MINIMAX": action = minimax(globalState) elif self.gameType == "ALPHABETA": action = alphabeta(globalState) globalState.machineCoord.append(action) temp = coordToNo(action) self.b[temp - 1].setIcon(QIcon("green.png")) self.b[temp - 1].setIconSize(QSize(50, 50)) self.b[temp - 1].setEnabled(False) if terminalTest(globalState) and utilityValue(globalState) == 1: self.winL.setText("MACHINE WON!! Please reset") self.winL.adjustSize() for i in range(16): self.b[i].setEnabled(False) return self.winL.setText( "Machine already played ! \nHuman it's your Turn...Place your blue coin" ) self.winL.adjustSize()
def play(pos, who, text_count, lines): player = who if player: #Max player's move if pos.find_board_piece("k") == None: print("Player X has won!") return pos, True, text_count if pos.find_board_piece("K") == None: print("Player Y has won!") return pos, True, text_count if text_count > 1: pos, text_count, lines = read_Y(pos, text_count, lines) #given the current position, a depth, whos turn and checkmate, try to use minimax score, move = minimax(pos, 1, True, player) if score == 99999: pc.TOTAL_PIECE -= 1 #engine check for player win if pos.score <= -MATE_LOWER: print("PLAYER Y has won!") return pos, True, text_count if score == -1000: print("The game has ended in a draw.") return pos, True, text_count #call to make a move, save and show the move pos = showMove(pos, True, move, text_count) text_count += 1 else: pos = pos.rotate() if pos.find_board_piece("k") == None: print("Player Y has won!") pos = pos.rotate() return pos, True, text_count pos = pos.rotate() if pos.find_board_piece("k") == None: print("Player X has won!") return pos, True, text_count #Minimizing PLAYER MOVE pos, text_count, lines = read_X(pos, text_count, lines) #rotate board for library to work properly pos = pos.rotate() #call minimax to determine the move score, move = minimax(pos, 1, False, player) if score == -99999: pc.TOTAL_PIECE -= 1 #This engine's check for player win if pos.score <= -MATE_LOWER: print("Player X has won!") return pos, True, text_count if score == -1000: print("The game has ended in a draw.") return pos, True, text_count #call to make a move, save and show the move pos = showMove(pos, False, move, text_count) text_count += 1 return pos, False, text_count, lines