-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.py
More file actions
249 lines (188 loc) · 5.94 KB
/
Copy pathSample.py
File metadata and controls
249 lines (188 loc) · 5.94 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# TicTacToe
# TODO
# Optimize code
import random
LINE_UP = "\033[1A"
LINE_CLEAR = "\x1b[2K"
def resetGame () -> None:
global gameAssets
global players
global turn
global winner
gameAssets = [0] * 9
players = [[], []]
turn = 0
winner = 0
resetGame()
winCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
AI_enabled = True
difficult = 1
AI_start = False
def clearTerminal (length: int) -> None:
for o in range(length):
print(LINE_UP, end=LINE_CLEAR)
# \u2502 Vertical
# \u2500 Horizontal
# \u253c Vertical and Horizontal
# print(u" A \u2502 A \u2502 A")
# print(u"\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500")
# print(u" A \u2502 A \u2502 A")
# print(u"\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500")
# print(u" A \u2502 A \u2502 A")
def printBoard () -> None:
print(" A B C\n")
for index, value in enumerate(gameAssets):
label = [" ", "X", "O"][value]
if index % 3 == 0:
print("%i %s" %(index / 3 + 1, label), end="")
else:
print(u" \u2502 %s" %label, end="\n" if index % 3 == 2 else "")
if index in [2, 5]:
print(u" \u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500")
print()
def checkWinner () -> int:
for player, playerCombo in enumerate(players):
for combo in winCombos:
counter = 0
for value in combo:
if value in playerCombo:
counter += 1
if counter >= 3:
return player + 1
if all(gameAssets):
return 3
return 0
def exe () -> int:
def whatever ():
rand = random.randint(0, 8)
while gameAssets[rand] != 0:
rand = random.randint(0, 8)
return rand
playersList = list(reversed(players))
if AI_enabled and AI_start:
playersList = players
# Normal and Impossible
# TODO
# Make it mark in places that still being possible to win and hasnt being blocked
# Bug found at turn Ai's turn 2
if difficult >= 1:
for playerCombo in playersList:
for combo in winCombos:
counter = 0
for value in combo:
if value in playerCombo:
counter += 1
if counter == 2:
for value in combo:
if gameAssets[value] == 0:
return value
for combo in winCombos:
counter = 0
counter_AI = 0
for value in playersList[1]:
if value in combo:
counter += 1
for value in playersList[0]:
# Maybe remove ?!
if value in combo:
counter_AI += 1
if counter == 0 and counter_AI >= 1:
while True:
position = combo[random.randint(0, 2)]
if gameAssets[position] == 0:
return position
# Impossible
if difficult == 2:
if turn == 1:
playerInput = playersList[1][0]
# Side
if playerInput in [0, 2, 6, 8]:
return 4
# Middle
elif playerInput == 4:
combo = [0, 2, 6, 8]
# Middle sides
elif playerInput == 1:
combo = [0, 2, 4, 7]
elif playerInput == 3:
combo = [0, 6, 4, 5]
elif playerInput == 3:
combo = [2, 8, 4, 3]
else:
combo = [6, 8, 4, 1]
return combo[random.randint(0, 3)]
# Dumb mode (Easy)
return whatever()
# Game Preferences
while True:
clearTerminal(7)
print("Select an option [1-4]:")
print(" [1] Agaist AI: %s" %AI_enabled)
print(" [2] Modify AI difficulty: %s" %["Easy", "Normal", "Impossible"][difficult])
print(" [3] Start with AI: %s" %AI_start)
print(" [4] Game style: Modern")
print(" [Enter] Start")
command = input().rstrip()
if command == "":
break
if command == "1":
AI_enabled = not AI_enabled
elif command == "2":
difficult = (difficult + 1) % 3
elif command == "3":
AI_start = not AI_start
# TODO make it run after restarts
if AI_start and AI_enabled:
position = exe()
gameAssets[position] = turn % 2 + 1
players[turn % 2].append(position)
turn += 1
while True:
clearTerminal(9)
printBoard()
if winner:
if winner in [1, 2]:
print ("Player", "XO"[winner - 1], "won!")
else:
print ("Tie!")
if input("Do you want to continue? [Y/n] ").lower().rstrip() in ["y", ""]:
clearTerminal(1)
resetGame()
continue
else:
break
playin = input("Play in: ").upper().rstrip()
if playin == "RESET":
resetGame()
continue
if playin == "KILL":
break
if len(playin) != 2:
continue
if not playin in "A1A2A3A1B2B3B1C2C3C1":
continue
x = "ABC".find(playin[1 if playin[0].isdecimal() else 0])
y = int(playin[0 if playin[0].isdecimal() else 1]) - 1
position = x + y * 3
if gameAssets[position] != 0:
continue
gameAssets[position] = turn % 2 + 1
players[turn % 2].append(position)
turn += 1
winner = checkWinner()
# AI's Turn
if not winner and AI_enabled:
position = exe()
gameAssets[position] = turn % 2 + 1
players[turn % 2].append(position)
turn += 1
winner = checkWinner()