-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
277 lines (218 loc) · 5.42 KB
/
main.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from operator import xor
import sys
import os
import re
import random
# Modes:
# 1 - vs AI
# 2 - Hotspot (2 player mode)
mode = 1
difficulty = 0
currentPlayer = 1
playerVictory = False
gameRunning = True
board = [1,3,5,7]
aiMovesHistory = []
playerMovesHistory = []
def clearConsole():
os.system('cls' if os.name == 'nt' else 'clear')
def difficultySetup():
clearConsole()
global difficulty
print('______')
print('*Please choose % chance of winning (difficulty) level 0 - 100:')
print('0 - Hardest')
print('100 - Easiest')
choice = int(input('Type difficulty level: '))
if not re.match('^[0-9]+$', str(choice)) and choice <= 0 and choice >= 100:
print("Error! Only in the range 0-100 please!")
difficultySetup()
else:
difficulty = choice
def modeSetup():
global mode
print('______')
print('**Please choose mode 1 or 2:')
print('1 - vs AI')
print('2 - vs Player')
choice = int(input('Type mode: '))
if not re.match('^[1-2]+$', str(choice)):
print("Error! There are 2 modes!")
modeSetup()
else:
mode = choice
if choice == 1:
difficultySetup()
def changePlayer():
global currentPlayer
if currentPlayer == 1:
currentPlayer = 2
else:
currentPlayer = 1
def logLastMove(target):
if(target == 'ai'):
if(len(aiMovesHistory) > 0):
print('\033[1;32;40m' + aiMovesHistory[-1])
print('\033[1;37;40m')
else:
if(len(playerMovesHistory) > 0):
print(playerMovesHistory[-1])
def isBalanced(board_to_check):
balanced = (board_to_check[0] ^ board_to_check[1]) ^ (board_to_check[2] ^ board_to_check[3])
if balanced == 0:
return True
else:
return False
def drawGame(board_to_check):
count = 1
# Draw the board_to_check
for el in board_to_check:
print(count, ': ', end='')
count = count + 1
for _ in range(el):
print(' O ', end='')
print('')
print('\n')
# print('Balanced: ', isBalanced(board_to_check))
def getCorrectRow():
row = int(input('--> Row to remove from: '))
if not re.match('^[1-4]+$', str(row)):
print("Error! There are 4 rows!")
getCorrectRow()
else:
if(board[row-1] < 1):
print("Error! That row is empty!")
getCorrectRow()
else:
return row-1
def getCorrectAmount(row):
amount = int(input('--> Amount to remove: '))
if amount < 1 or amount > board[row]:
print("Error! Illegal amount!")
getCorrectAmount(row)
else:
return amount
def remove():
print('Player ' + str(currentPlayer) + '\'s turn!')
drawGame(board)
row = getCorrectRow()
amount = getCorrectAmount(row)
board[row] -= amount or 0
playerMovemessage = 'Player removed: ' + str(amount) + ' From row: ' + str(row)
playerMovesHistory.append(playerMovemessage)
def checkWin():
global gameRunning
for value in range(4):
if board[value] > 0:
changePlayer()
return
print('----------------------------')
if(currentPlayer == 1):
print('Player 1 Won!')
else:
if mode == 1:
print('AI Won!')
else:
print('Player 2 Won!')
print('----------------------------')
gameRunning = False
def aiMove():
global board
global playerVictory
tempList = board.copy()
foundBalanced = False
rowTraversed = 0
if random.randint(0,100) > difficulty:
# print('DIFFICULTY: ', difficulty)
if(isBalanced(tempList)):
# print('Stupid move needed')
if(tempList[0]) > 0:
tempList[0] -= 1
board = tempList.copy()
# print('Move made 1')
elif(tempList[1] > 0):
tempList[1] -= 1
board = tempList.copy()
# print('Move made 2')
elif(tempList[2] > 0):
tempList[2] -= 1
board = tempList.copy()
# print('Move made 3')
elif(tempList[3] > 0):
tempList[3] -= 1
board = tempList.copy()
# print('Move made 4')
else:
# print('Balancing attempt')
for row in range(4):
amount = 0
rowTraversed += 1
if(tempList[row] > 0):
for v in range(tempList[row]):
tempList[row] -= 1
amount += 1
# print('Checking: ', tempList[0], tempList[1], tempList[2], tempList[3])
if(isBalanced(tempList)):
foundBalanced = True
aiMoveMessave = 'AI removed: ' + str(amount) + ' From row: ' + str(rowTraversed)
aiMovesHistory.append(aiMoveMessave)
board = tempList.copy()
# print('break 1')
break
# else:
# print('Failed attempt')
if(tempList[row] == 0):
# Reseting row
# print('Reseting row')
tempList = board.copy()
# print('Should be reset: ', tempList[0], tempList[1], tempList[2], tempList[3])
if(foundBalanced):
break
amount = 0
logLastMove('ai')
else:
# print('WRONG CHOICE: ', difficulty)
if(isBalanced(tempList)):
# print('Stupid move needed')
if(tempList[0]) > 0:
tempList[0] -= 1
board = tempList.copy()
# print('Move made 1')
elif(tempList[1] > 0):
tempList[1] -= 1
board = tempList.copy()
# print('Move made 2')
elif(tempList[2] > 0):
tempList[2] -= 1
board = tempList.copy()
# print('Move made 3')
elif(tempList[3] > 0):
tempList[3] -= 1
board = tempList.copy()
# print('Move made 4')
# ------------------------------------
# GAME START
# ------------------------------------
# Choose the mode
modeSetup()
# Draw result board
if(mode == 1):
# vs AI setup
while gameRunning:
if(currentPlayer == 1):
remove()
else:
# Clear old consonsole
clearConsole()
aiMove()
checkWin()
else:
# 2 player mode setup
while gameRunning:
if(currentPlayer == 1):
remove()
else:
# Clear old consonsole
clearConsole()
remove()
checkWin()