-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
96 lines (78 loc) · 3.45 KB
/
Copy pathplayer.py
File metadata and controls
96 lines (78 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import math
import random
class Player:
def __init__(self, letter):
self.letter = letter #letter is x or o
#we want all players to get their next move given a game
def get_move(self, game):
pass
class RandomComputerPlayer(Player):
def __init__(self, letter):
super().__init__(letter)
def get_move(self, game):
square = random.choice(game.available_moves())
return square
class HumanPlayer(Player):
def __init__(self, letter):
super().__init__(letter)
def get_move(self, game):
valid_square = False
val = None
while not valid_square:
square = input(self.letter + '\'s turn. Input move (0-8):')
#we're going to check if this is a correct value by trying to cast
# it to an integer, and if it isn't, we say it is invalif
# also, if that spot is not available on the board, then it is invalid
try:
val = int(square)
if val not in game.available_moves():
raise ValueError
valid_square = True
except ValueError:
print('Invalid square. Please enter valid square')
return val
class GeniusComputerPlayer(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()) #randomly choose one
else:
#get the square based off the minimax algorithm
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
#this is our base case
if state.current_winner == other_player:
#we should return position and score b/c we need to keep track of the score
# for minimax to work
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 be larger
else:
best = {'position': None, 'score': math.inf} #each score should minimize
for possible_move in state.available_moves():
#step 1: make a move and try that spot
state.make_move(possible_move, player)
#step 2: recurse using minimax to simulate a game after making that move
sim_score = self.minimax(state, other_player) # now, we alternatr players
#step 3: undo the move
state.board[possible_move] = ' '
state.current_winner = None
sim_score['position'] = possible_move
#step 4: update the dictionaries if necessary
if player == max_player: #trying to maximize the max player
if sim_score['score'] > best['score']:
best = sim_score #replace best
else:
if sim_score['score'] < best['score']:
best = sim_score #replace best
return best