-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttt.py
224 lines (198 loc) · 7.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import random
progress = True
result = ""
choice = ""
#2d array
board = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
#Functions
def check(progress, board):
#Game over
loopx = 0
loopy = 0
#Check horizontal
while loopy < 3:
if board[loopy][0] == "X " and board[loopy][1] == "X " and board[loopy][2] == "X ":
result = "win"
progress = False
elif board[loopy][0] == "O " and board[loopy][1] == "O " and board[loopy][2] == "O ":
result = "lose"
progress = False
loopy = loopy + 1
#Check vertical
while loopx < 3:
if board[0][loopx] == "X " and board[1][loopx] == "X " and board[2][loopx] == "X ":
result = "win"
progress = False
elif board[0][loopx] == "O " and board[1][loopx] == "O " and board[2][loopx] == "O ":
result = "lose"
progress = False
loopx = loopx + 1
#Check diagonal
if board[0][0] == "X " and board[1][1] == "X " and board[2][2] == "X ":
result = "win"
progress = False
elif board[0][0] == "O " and board[1][1] == "O " and board[2][2] == "O ":
result = "lose"
progress = False
elif board[0][2] == "X " and board[1][1] == "X " and board[2][0] == "X ":
result = "win"
progress = False
elif board[0][2] == "O " and board[1][1] == "O " and board[2][0] == "O ":
result = "lose"
progress = False
if progress == False:
print(" —— —— ——\n|"+board[0][0]+"|"+board[0][1]+"|"+board[0][2]+"|\n —— —— ——\n|"+board[1][0]+"|"+board[1][1]+"|"+board[1][2]+"|\n —— —— ——\n|"+board[2][0]+"|"+board[2][1]+"|"+board[2][2]+"|\n —— —— ——")
if result == "win":
print("You won!")
exit()
elif result == "lose":
print("You lost!")
exit()
return progress
def valid(progress, board):
#Check draw
if progress == True and board[0][0] != " " and board[0][1] != " " and board[0][2] != " " and board[1][0] != " " and board[1][1] != " " and board[1][2] != " " and board[2][0] != " " and board[2][1] != " " and board[2][2] != " ":
result = "draw"
progress = False
if progress == False:
print(" —— —— ——\n|"+board[0][0]+"|"+board[0][1]+"|"+board[0][2]+"|\n —— —— ——\n|"+board[1][0]+"|"+board[1][1]+"|"+board[1][2]+"|\n —— —— ——\n|"+board[2][0]+"|"+board[2][1]+"|"+board[2][2]+"|\n —— —— ——")
if result == "draw":
print("Its a draw!")
exit()
return progress
#Main loop
while progress:
confirm = False
position = int(input("Select a square (1-9): "))-1
x = 0
y = 0
#Update board
if position > 5:
x = position - 6
y = 2
elif position > 2:
x = position - 3
y = 1
else:
x = position
y = 0
if board[y][x] == " ":
board[y][x] = "X "
else:
print("That square has already been occupied!")
confirm = True
check(progress, board)
valid(progress, board)
#Response
loopx = 0
loopy = 0
move = 0
#Solve horizontal
while loopy < 3:
if confirm == False and board[loopy][0] == " " and board[loopy][1] == "O " and board[loopy][2] == "O ":
board[loopy][0] = "O "
confirm = True
elif confirm == False and board[loopy][1] == " " and board[loopy][0] == "O " and board[loopy][2] == "O ":
board[loopy][1] = "O "
confirm = True
elif confirm == False and board[loopy][2] == " " and board[loopy][0] == "O " and board[loopy][1] == "O ":
board[loopy][2] = "O "
confirm = True
loopy = loopy + 1
#Solve vertical
while loopx < 3:
if confirm == False and board[0][loopx] == " " and board[1][loopx] == "O " and board[2][loopx] == "O ":
board[0][loopx] = "O "
confirm = True
elif confirm == False and board[1][loopx] == " " and board[0][loopx] == "O " and board[2][loopx] == "O ":
board[1][loopx] = "O "
confirm = True
elif confirm == False and board[2][loopx] == " " and board[0][loopx] == "O " and board[1][loopx] == "O ":
board[2][loopx] = "O "
confirm = True
loopx = loopx + 1
#Solve diagonal
if confirm == False and board[0][0] == " " and board[1][1] == "O " and board[2][2] == "O ":
board[0][0] = "O "
confirm = True
elif confirm == False and board[1][1] == " " and board[0][0] == "O " and board[2][2] == "O ":
board[1][1] = "O "
confirm = True
elif confirm == False and board[2][2] == " " and board[0][0] == "O " and board[1][1] == "O ":
board[2][2] = "O "
confirm = True
elif confirm == False and board[0][2] == " " and board[1][1] == "O " and board[2][0] == "O ":
board[0][2] = "O "
confirm = True
elif confirm == False and board[1][1] == " " and board[0][2] == "O " and board[2][0] == "O ":
board[1][1] = "O "
confirm = True
elif confirm == False and board[2][0] == " " and board[0][2] == "O " and board[1][1] == "O ":
board[2][0] = "O "
confirm = True
#Reset loops
loopx = 0
loopy = 0
#Block horizontal
while loopy < 3:
if confirm == False and board[loopy][0] == " " and board[loopy][1] == "X " and board[loopy][2] == "X ":
board[loopy][0] = "O "
confirm = True
elif confirm == False and board[loopy][1] == " " and board[loopy][0] == "X " and board[loopy][2] == "X ":
board[loopy][1] = "O "
confirm = True
elif confirm == False and board[loopy][2] == " " and board[loopy][0] == "X " and board[loopy][1] == "X ":
board[loopy][2] = "O "
confirm = True
loopy = loopy + 1
#Block vertical
while loopx < 3:
if confirm == False and board[0][loopx] == " " and board[1][loopx] == "X " and board[2][loopx] == "X ":
board[0][loopx] = "O "
confirm = True
elif confirm == False and board[1][loopx] == " " and board[0][loopx] == "X " and board[2][loopx] == "X ":
board[1][loopx] = "O "
confirm = True
elif confirm == False and board[2][loopx] == " " and board[0][loopx] == "X " and board[1][loopx] == "X ":
board[2][loopx] = "O "
confirm = True
loopx = loopx + 1
#Block diagonal
if confirm == False and board[0][0] == " " and board[1][1] == "X " and board[2][2] == "X ":
board[0][0] = "O "
confirm = True
elif confirm == False and board[1][1] == " " and board[0][0] == "X " and board[2][2] == "X ":
board[1][1] = "O "
confirm = True
elif confirm == False and board[2][2] == " " and board[0][0] == "X " and board[1][1] == "X ":
board[2][2] = "O "
confirm = True
elif confirm == False and board[0][2] == " " and board[1][1] == "X " and board[2][0] == "X ":
board[0][2] = "O "
confirm = True
elif confirm == False and board[1][1] == " " and board[0][2] == "X " and board[2][0] == "X ":
board[1][1] = "O "
confirm = True
elif confirm == False and board[2][0] == " " and board[0][2] == "X " and board[1][1] == "X ":
board[2][0] = "O "
confirm = True
#Anyhow place
while confirm == False:
move = random.randint(0,8)
if move > 5:
x = move - 6
y = 2
elif move > 2:
x = move - 3
y = 1
else:
x = move
y = 0
if board[y][x] == " ":
board[y][x] = "O "
confirm = True
check(progress, board)
valid(progress, board)
print(" —— —— ——\n|"+board[0][0]+"|"+board[0][1]+"|"+board[0][2]+"|\n —— —— ——\n|"+board[1][0]+"|"+board[1][1]+"|"+board[1][2]+"|\n —— —— ——\n|"+board[2][0]+"|"+board[2][1]+"|"+board[2][2]+"|\n —— —— ——")