
So you should first test for a win, and only then for a draw. Even on a full board, it could be that the last move was a winning move. In minimax you are testing too soon whether the board is full. Return -10 if maximizing else 10 # opponent won the game Realize that it is not the current player that won the game, but the previous player, so maximizing is not reflecting who won the game. In minimax you have this code: if is_win(temp_list):īut the value you return is opposite to what it should be. In find_best_move you have not mirrored temp_list = 1 with temp_list = 0 as you have correctly done in minimax. I guess this could be just a problem you introduced in this question. Near the end of your code there is an indentation issue: three statements need to be indented more to become part of the if is_win(board_list) block. So it is not picking up the winning move. The expected behavior is that bot player should choose 8 cell so it will be a win. While not winner and not is_board_full(board_list):īest_move = int(input("Enter your move (1-9): ")) - 1Ĭurrent_player = 2 if current_player = 1 else 1 Score = minimax(temp_list, depth + 1, True) Score = minimax(temp_list, depth + 1, False) def minimax(board_list, depth, maximizing): I have implemented the algorithm and it's working but it is not picking up the best move. But I want to improve the bot player to pick the best move using minimax algorithm. Bot player is assumed to be 1 which is "O" and human player is assumed to be 2 which is "X". I have build a small python tic tac toe game.
