-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
184 lines (140 loc) · 4.08 KB
/
tictactoe.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Tic Tac Toe Player
"""
import math
import copy
X = "X"
O = "O"
EMPTY = None
def initial_state():
"""
Returns starting state of the board.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
"""
Returns player who has the next turn on a board.
"""
moves = 0
if terminal(board):
return EMPTY
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] != EMPTY:
moves += 1
if moves % 2 == 0:
return X
else:
return O
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
if terminal(board):
return EMPTY
actions = set()
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == EMPTY:
actions.add((i,j))
else:
continue
return actions
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
board_copy = copy.deepcopy(board)
if board[action[0]][action[1]] in {O, X}:
raise NameError("Invalid Action!")
elif board[action[0]][action[1]] == EMPTY:
pass
if player(board) == X:
board_copy[action[0]][action[1]] == X
elif player(board) == O:
board_copy[action[0]][action[1]] == O
return board_copy
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
# Horizontal
for i in range(len(board)):
if board[i][0] == X and board[i][1] == X and board[i][2] == X:
return X
elif board[i][0] == O and board[i][1] == O and board[i][2] == O:
return O
# Vertical
for i in range(len(board)):
if board[0][i] == X and board[1][i] == X and board[2][i] == X:
return X
elif board[0][i] == O and board[1][i] == O and board[2][i] == O:
return O
# Diagonal
if board[0][0] == X and board[1][1] == X and board[2][2] == X:
return X
elif board[0][2] == X and board[1][1] == X and board[2][0] == X:
return X
elif board[0][0] == O and board[1][1] == O and board[2][2] == O:
return O
elif board[0][2] == O and board[1][1] == O and board[2][0] == O:
return O
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
moves = 0
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] != EMPTY:
moves += 1
if winner(board) != None:
return True
elif moves == 9:
return True
else:
return False
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == X:
return 1
elif winner(board) == O:
return -1
elif winner(board) == None:
return 0
def minimax(board):
Actions = actions(board)
Player = player(board)
action_score = {}
if X in Player:
action_score = {}
for Action in Actions:
action_score[Action] = calculate_score_for_board(board, Action)
max_score = -10
for Action in Actions:
if max_score < action_score[Action]:
max_score = action_score[Action]
return action_score[max_score]
elif O in Player:
action_score = {}
for Action in Actions:
action_score[Action] = calculate_score_for_board(board, Action)
min_score = 10
for Action in Actions:
if min_score > action_score[Action]:
min_score = action_score[Action]
return action_score[min_score]
def calculate_score_for_board(board, action):
new_board_state = result(board, action)
if terminal(new_board_state):
return utility(new_board_state)
else:
Actions = actions(new_board_state)
for Action in Actions:
score = calculate_score_for_board(new_board_state, Action)
return score