mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-24 12:31:11 +00:00
Merge pull request #1 from PGautam27/master
Added readme and the MiniMaxAlgo
This commit is contained in:
commit
0be5a79b66
5
scripts/MiniMaxAlgo/README.md
Normal file
5
scripts/MiniMaxAlgo/README.md
Normal file
|
@ -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)
|
|
@ -1,6 +1,6 @@
|
||||||
import math
|
import math
|
||||||
import time
|
import time
|
||||||
from player import HumanPlayer, RandomComputerPlayer
|
from player import HumanPlayer, SmartComputerPlayer
|
||||||
|
|
||||||
|
|
||||||
class TicTacToe():
|
class TicTacToe():
|
||||||
|
|
|
@ -28,11 +28,45 @@ class HumanPlayer(Player):
|
||||||
print('Invalid square. Try again.')
|
print('Invalid square. Try again.')
|
||||||
return val
|
return val
|
||||||
|
|
||||||
|
class SmartComputerPlayer(Player):
|
||||||
class RandomComputerPlayer(Player):
|
|
||||||
def __init__(self, letter):
|
def __init__(self, letter):
|
||||||
super().__init__(letter)
|
super().__init__(letter)
|
||||||
|
|
||||||
def get_move(self, game):
|
def get_move(self, game):
|
||||||
|
if len(game.available_moves()) == 9:
|
||||||
square = random.choice(game.available_moves())
|
square = random.choice(game.available_moves())
|
||||||
|
else:
|
||||||
|
square = self.minimax(game, self.letter)['position']
|
||||||
return square
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user