From fae51028b1f4f3b1b528a4e515f3b24b63873134 Mon Sep 17 00:00:00 2001 From: P Gautam <92343715+PGautam27@users.noreply.github.com> Date: Fri, 7 Oct 2022 19:09:56 +0530 Subject: [PATCH 1/4] Added miniMax --- scripts/MiniMaxAlgo/player.py | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/scripts/MiniMaxAlgo/player.py b/scripts/MiniMaxAlgo/player.py index 2ffe8cc..34e997c 100644 --- a/scripts/MiniMaxAlgo/player.py +++ b/scripts/MiniMaxAlgo/player.py @@ -36,3 +36,46 @@ class RandomComputerPlayer(Player): def get_move(self, game): square = random.choice(game.available_moves()) return square + +class SmartComputerPlayer(Player): + def __init__(self, letter): + super().__init__(letter) + + def get_move(self, game): + if len(game.available_moves()) == 9: + square = random.choice(game.available_moves()) + else: + square = self.minimax(game, self.letter)['position'] + return square + + def minimax(self, state, player): + max_player = self.letter # yourself + other_player = 'O' if player == 'X' else 'X' + + # first we want to check if the previous move is a winner + if state.current_winner == other_player: + return {'position': None, 'score': 1 * (state.num_empty_squares() + 1) if other_player == max_player else -1 * ( + state.num_empty_squares() + 1)} + elif not state.empty_squares(): + return {'position': None, 'score': 0} + + if player == max_player: + best = {'position': None, 'score': -math.inf} # each score should maximize + else: + best = {'position': None, 'score': math.inf} # each score should minimize + for possible_move in state.available_moves(): + state.make_move(possible_move, player) + sim_score = self.minimax(state, other_player) # simulate a game after making that move + + # undo move + state.board[possible_move] = ' ' + state.current_winner = None + sim_score['position'] = possible_move # this represents the move optimal next move + + if player == max_player: # X is max player + if sim_score['score'] > best['score']: + best = sim_score + else: + if sim_score['score'] < best['score']: + best = sim_score + return best From 18c9ccc1c5762af84ae0211eb1fd082f228358c6 Mon Sep 17 00:00:00 2001 From: P Gautam <92343715+PGautam27@users.noreply.github.com> Date: Fri, 7 Oct 2022 19:10:47 +0530 Subject: [PATCH 2/4] Uses SmartComputer --- scripts/MiniMaxAlgo/TicTacToe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/MiniMaxAlgo/TicTacToe.py b/scripts/MiniMaxAlgo/TicTacToe.py index 5e9545a..271eb3a 100644 --- a/scripts/MiniMaxAlgo/TicTacToe.py +++ b/scripts/MiniMaxAlgo/TicTacToe.py @@ -1,6 +1,6 @@ import math import time -from player import HumanPlayer, RandomComputerPlayer +from player import HumanPlayer, SmartComputerPlayer class TicTacToe(): From d4dbbd9ed59115a26aa044723e293c9a60dc460b Mon Sep 17 00:00:00 2001 From: P Gautam <92343715+PGautam27@users.noreply.github.com> Date: Fri, 7 Oct 2022 19:11:28 +0530 Subject: [PATCH 3/4] Removed random ai --- scripts/MiniMaxAlgo/player.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/scripts/MiniMaxAlgo/player.py b/scripts/MiniMaxAlgo/player.py index 34e997c..30cbea5 100644 --- a/scripts/MiniMaxAlgo/player.py +++ b/scripts/MiniMaxAlgo/player.py @@ -27,15 +27,6 @@ class HumanPlayer(Player): except ValueError: print('Invalid square. Try again.') return val - - -class RandomComputerPlayer(Player): - def __init__(self, letter): - super().__init__(letter) - - def get_move(self, game): - square = random.choice(game.available_moves()) - return square class SmartComputerPlayer(Player): def __init__(self, letter): From 5736b3fce22f0402cf18b924546a13f8aae5b1cd Mon Sep 17 00:00:00 2001 From: P Gautam <92343715+PGautam27@users.noreply.github.com> Date: Fri, 7 Oct 2022 19:15:44 +0530 Subject: [PATCH 4/4] Added Readme.md --- scripts/MiniMaxAlgo/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 scripts/MiniMaxAlgo/README.md diff --git a/scripts/MiniMaxAlgo/README.md b/scripts/MiniMaxAlgo/README.md new file mode 100644 index 0000000..68e40db --- /dev/null +++ b/scripts/MiniMaxAlgo/README.md @@ -0,0 +1,5 @@ +## MiniMax Algorithm + +It's implemented in such way that the AI minimizes it's loss and maximizes it's winning chances. + +![minimax](https://user-images.githubusercontent.com/92343715/194568346-bc6c78c3-fe22-43b9-ba25-e2f429b1b5e8.png)