-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.py
128 lines (92 loc) · 3.43 KB
/
tic_tac_toe.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
class TicTacToe():
def __init__(self, player_symbol):
self.symbol_list = []
for i in range(9):
self.symbol_list.append(" ")
self.player_symbol = player_symbol
def restart(self):
for i in range(9):
self.symbol_list[i] = " "
def draw_grid(self):
print("\n A B C\n")
row_one = " 1 " + self.symbol_list[0]
row_one += " ║ " + self.symbol_list[1]
row_one += " ║ " + self.symbol_list[2]
print(row_one)
print(" ═══╬═══╬═══")
row_two = " 2 " + self.symbol_list[3]
row_two += " ║ " + self.symbol_list[4]
row_two += " ║ " + self.symbol_list[5]
print(row_two)
print(" ═══╬═══╬═══")
row_three = " 3 " + self.symbol_list[6]
row_three += " ║ " + self.symbol_list[7]
row_three += " ║ " + self.symbol_list[8]
print(row_three, "\n")
def edit_square(self, grid_coord):
if grid_coord[0].isdigit():
grid_coord = grid_coord[1] + grid_coord[0]
col = grid_coord[0].capitalize()
row = grid_coord[1]
grid_index = 0
if row == "1":
if col == "A":
grid_index = 0
elif col == "B":
grid_index = 1
elif col == "C":
grid_index = 2
elif row == "2":
if col == "A":
grid_index = 3
elif col == "B":
grid_index = 4
elif col == "C":
grid_index = 5
elif row == "3":
if col == "A":
grid_index = 6
elif col == "B":
grid_index = 7
elif col == "C":
grid_index = 8
else:
print("----INVALID INPUT----TURN SKIPPED---")
if self.symbol_list[grid_index] == " ":
self.symbol_list[grid_index] = self.player_symbol
else:
print("----INVALID INPUT----TURN SKIPPED---")
def update_symbol_list(self, new_symbol_list):
for i in range(9):
self.symbol_list[i] = new_symbol_list[i]
def did_win(self, player_symbol):
g = []
for i in range(9):
g.append(self.symbol_list[i])
sym = player_symbol
if g[0] == sym and g[1] == sym and g[2] == sym:
return True
elif g[3] == sym and g[4] == sym and g[5] == sym:
return True
elif g[6] == sym and g[7] == sym and g[8] == sym:
return True
elif g[0] == sym and g[3] == sym and g[6] == sym:
return True
elif g[1] == sym and g[4] == sym and g[7] == sym:
return True
elif g[2] == sym and g[5] == sym and g[8] == sym:
return True
elif g[2] == sym and g[4] == sym and g[6] == sym:
return True
elif g[0] == sym and g[4] == sym and g[8] == sym:
return True
return False
def is_draw(self):
num_blanks = 0
for i in range(9):
if self.symbol_list[i] == " ":
num_blanks += 1
if self.did_win(self.player_symbol) == False and num_blanks == 0:
return True
else:
return False