-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe.py
145 lines (118 loc) · 3.63 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
"""
Tic Tac Toe Player
"""
import copy
import math
X = "X"
O = "O"
EMPTY = None
answer=(0,0)
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.
"""
if board==initial_state():
return X
if sum(row.count(X) for row in board) > sum(row.count(O) for row in board):
return O
else:
return X
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
moves=[]
for i in range(3):
for j in range (3):
if board[i][j] == EMPTY:
moves.append((i,j))
return moves
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
copyy = copy.deepcopy(board)
if player(board)==X:
copyy[action[0]][action[1]]=X
else:
copyy[action[0]][action[1]]=O
return copyy
def winner(board):
if (board[0][0] == 'X' and board[1][1] == 'X' and board[2][2] == 'X') or \
(board[0][2] == 'X' and board[1][1] == 'X' and board[2][0] == 'X') or \
(board[0] == ['X', 'X', 'X'] or board[1] == ['X', 'X', 'X'] or board[2] == ['X', 'X', 'X']) or \
(board[0][0] == 'X' and board[1][0] == 'X' and board[2][0] == 'X') or \
(board[0][1] == 'X' and board[1][1] == 'X' and board[2][1] == 'X') or \
(board[2][2] == 'X' and board[0][2] == 'X' and board[1][2] == 'X'):
return X
elif (board[0][0]=='O' and board[1][1]=='O' and board[2][2]=='O') or\
(board[0][2]=='O' and board[1][1]=='O' and board[2][0]=='O') or\
(board[0]==['O','O','O'] or board[1]==['O','O','O'] or board[2]==['O','O','O']) or\
(board[0][0]=='O' and board[1][0]=='O' and board[2][0]=='O') or\
(board[0][1]=='O' and board[1][1]=='O' and board[2][1]=='O') or\
(board[2][2]=='O' and board[0][2]=='O' and board[1][2]=='O'):
return O
else:
return None
raise NotImplementedError
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board)==X or winner(board)==O:
return True
elif sum(row.count(EMPTY) for row in board)==0:
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
else:
return 0
raise NotImplementedError
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
def max_value(board):
if terminal(board):
return utility(board)
v=-100000000
for action in actions(board):
v=max(v,min_value(result(board,action)))
return v
def min_value(board):
if terminal(board):
return utility(board)
v = 100000000
for action in actions(board):
v = min(v, max_value(result(board, action)))
return v
if player(board)==X:
v = -math.inf
for action in actions(board):
k = min_value(result(board, action))
if k > v:
v = k
best_move = action
else:
v = math.inf
for action in actions(board):
k = max_value(result(board, action)) # FIXED
if k < v:
v = k
best_move = action
return best_move