-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictacutils.py
More file actions
135 lines (95 loc) · 3.12 KB
/
tictacutils.py
File metadata and controls
135 lines (95 loc) · 3.12 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
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
#!/usr/bin/env python
"""
This file provides common functions needed for the Tic-Tac-Toe game.
Author: Abhishek N. Kulkarni (abhibp1993)
Date Modified: 7 April 2017
"""
import pickle
from copy import deepcopy
O = 'O'
X = 'X'
XWINS = 0
OWINS = 1
DRAW = 2
UNDECIDED = 3
WINNING_CONDITION = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)],
]
def unpickle_graph():
with open('tic-tac-toe-game-graph.pkl', 'r') as pkl:
grf = pickle.load(file=pkl)
pkl.close()
return grf
def unpickle_policy():
with open('tic-tac-toe-safe-policy-X.pkl', 'r') as pkl:
safe_st = pickle.load(file=pkl)
pkl.close()
return safe_st
def free_positions(state):
return [(i, j) for i in range(3) for j in range(3) if state[i][j] is None]
def label_state(state):
"""
Returns label of "X-Wins", "O-Wins", "Draw", "No Decision" for given state.
"""
for element in WINNING_CONDITION:
# Decouple indices of winning positions
(i1, j1), (i2, j2), (i3, j3) = element
# Check for Winner
if state[i1][j1] == state[i2][j2] == state[i3][j3]:
if state[i1][j1] == X:
return XWINS
if state[i1][j1] == O:
return OWINS
# If no ones winning the game, then check for a draw
if len(free_positions(state)) == 0:
return DRAW
return UNDECIDED
def pickle_policy(safe_st):
with open('tic-tac-toe-safe-policy-X.pkl', 'w') as pkl:
pickle.dump(obj=safe_st, file=pkl)
pkl.close()
def is_winning(state, player):
if player == X:
return label_state(state) == XWINS
else: # player == O:
return label_state(state) == OWINS
def is_draw(state):
return label_state(state) == DRAW
def x_positions(state):
return [(i, j) for i in range(3) for j in range(3) if state[i][j] == X]
def o_positions(state):
return [(i, j) for i in range(3) for j in range(3) if state[i][j] == O]
def turn_of(state):
if len(free_positions(state)) == 0 or len(x_positions(state)) == len(o_positions(state)):
return X
elif len(x_positions(state)) > len(o_positions(state)):
return O
def pickle_graph(G):
with open('tic-tac-toe-game-graph.pkl', 'w') as pkl:
pickle.dump(obj=G, file=pkl)
pkl.close()
def show_game(state):
print
for row in range(3):
print state[row]
print
def mark(state, cell, player):
"""
Does not mutate state.
"""
# Uncouple row, col
m, n = cell
# Copy and Convert to list
state_copy = [list(deepcopy(i)) for i in state]
# Update the mark the state
if state_copy[m][n] is not None:
raise ValueError('Cannot set value of occupied cell.')
state_copy[m][n] = player
return tuple(tuple(i) for i in state_copy)