-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt.py
170 lines (140 loc) · 3.3 KB
/
ttt.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
import sys
import logging
from itertools import chain
a = 0
b = 0
class board:
b = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
def print_board():
print(" 0 1 2")
for count, x in enumerate(board.b):
print(count, x)
def user_input():
global a
global b
a = input("enter column name\n")
b = input('enter row number \n')
return [int(a), int(b)]
def game_engine1():
print('for user 1')
x = user_input()
# modify_board(a, b)
if spot_checker() == True:
board.b[int(b)][int(a)] = 1
print_board()
game_logic()
else:
print("can't place there")
game_engine1()
def player2():
print('for user 2')
x = user_input()
# modify_board(a, b)
if spot_checker() == True:
board.b[int(b)][int(a)] = 2
print_board()
game_logic()
else:
print("can't place there")
player2()
def board_check():
c = 0
flat_list = []
for sublist in board.b:
for item in sublist:
flat_list.append(item)
if c not in flat_list:
game_over()
else:
return True
def spot_checker():
global a, b
if board_check() == True:
if board.b[int(b)][int(a)] == 0:
return True
else:
return False
def game_logic():
if horizontal_check() == 1:
print('player 1 won')
sys.exit()
elif horizontal_check() == 2:
print("player 2 won")
sys.exit()
else:
pass
if vertical_check() == 1:
print('player 1 won')
sys.exit()
elif vertical_check() == 2:
print("player 2 won")
sys.exit()
elif diagonal_check() == 1:
print('player 1 won')
sys.exit()
elif diagonal_check() == 2:
print("player 2 won")
sys.exit()
else:
pass
def horizontal_check():
for r in board.b:
result1 = all(elem == 1 for elem in r)
result2 = all(elem == 2 for elem in r)
if result1 == True:
return 1
else:
pass
if result2 == True:
return 2
else:
pass
def vertical_check():
flat_list = []
c1 = []
c2 = []
c3 = []
for sublist in board.b:
for item in sublist:
flat_list.append(item)
c1 = flat_list[0::3]
c2 = flat_list[1::3]
c3 = flat_list[2::3]
result1 = all(elem == 1 for elem in c1) or all(
elem == 1 for elem in c2) or all(elem == 1 for elem in c3)
result2 = all(elem == 2 for elem in c1) or all(
elem == 2 for elem in c2) or all(elem == 2 for elem in c3)
if result1 == True:
return 1
else:
pass
if result2 == True:
return 2
else:
pass
def diagonal_check():
flat_list = []
d1 = []
d2 = []
for sublist in board.b:
for item in sublist:
flat_list.append(item)
d1 = flat_list[0::4]
spots = [2, 4, 8]
d2 = [flat_list[i] for i in spots]
result1 = all(elem == 1 for elem in d1)
result2 = all(elem == 2 for elem in d2)
if result1 == True:
return 1
elif result2 == True:
return 2
else:
pass
def game_over():
print("game_over()")
if __name__ == "__main__":
x = game_logic()
print_board()
while x != True:
game_engine1()
player2()