-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
296 lines (225 loc) · 9.49 KB
/
logic.py
File metadata and controls
296 lines (225 loc) · 9.49 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
import random as r
from ui import UI
import time as t
interface = UI()
class LoginFunctions:
def __init__(self):
self.game_board = np.array([['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']])
def check_pos(self, pos):
for i in range(0, 3):
for j in range(0, 3):
if pos == self.game_board[i][j]:
return False
return True
def get_player(self, p_1, p_2):
if r.randint(0, 1) == 1:
return p_1
else:
return p_2
def computer_game(self, p_1, sym):
"""This function is the brain of the computer game"""
if sym == 'X':
cmp_sym = 'O'
else:
cmp_sym = 'X'
# Choose the first player to play logic:
if r.randint(0, 1) == 1:
turn = True
else:
turn = False
self.get_board()
win = False
while not self.game_board_full() and not win:
if turn:
print("\nComputer Turn .")
for i in range(0, 7):
t.sleep(0.5)
print(".")
self.computer_turn(c_sym=cmp_sym)
else:
usr_trn = True
while usr_trn:
try:
u_ch = int(input(f"\n{p_1} your turn. Enter the number where you want to play your move: "))
except ValueError:
print("\nPlease enter a number.")
continue
check_validated = self.checks(pos=u_ch, sym=sym)
if check_validated:
usr_trn = False
if self.check_win():
interface.show_congratulations() # Show congratulations message.
print(f"\n{'Computer' if turn is True else p_1}. You won the game!!")
win = True # Break out of the multiplayer game loop.
self.game_board = np.array([['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]) # Reset the board.
turn = not turn
if self.game_board_full():
print("\nIt's a tie.")
self.game_board = np.array([['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]) # Reset the board.
ch_run = True
while ch_run:
resp = input("\nDo you want to play the with computer again?\nType \n'y' to continue\n'n' to return to main menu.\nYour choice: ")
if resp == 'y':
return True
elif resp == 'n':
return False
else:
print("\nInvalid input!")
def computer_turn(self, c_sym):
# Check the correctness of the random number chosen by computer.
corr_pos = False
while not corr_pos:
cmp_ch = r.randint(1, 10)
if 1<=cmp_ch<=3:
if self.check_pos(cmp_ch):
self.game_board[0][cmp_ch-1] = c_sym
self.get_board()
corr_pos = True
elif 4<=cmp_ch<=6:
if self.check_pos(cmp_ch):
self.game_board[1][cmp_ch-4] = c_sym
self.get_board()
corr_pos = True
elif 7<=cmp_ch<=9:
if self.check_pos(cmp_ch):
self.game_board[2][cmp_ch-7] = c_sym
self.get_board()
corr_pos = True
def multiplayer_game(self, p_1, p_2):
"""This function is the brain of multiplayer game."""
# Choose the first player to play logic:
if r.randint(0, 1) == 1:
turn = True
else:
turn = False
# Show the Tic-Tac-Toe board.
self.get_board()
# win confition.
win = False
# Game Begins:
while not self.game_board_full() and not win:
# Give turn to player.
re_q = True # To maintain answer correctness.
if turn == True:
while re_q:
try:
u_ch = int(input(f"\n{p_1} your turn. Enter the number where you want to play your move: "))
except ValueError and UnboundLocalError as e:
print("\nPlease enter a number!")
else:
if u_ch in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
re_q = False
else:
print("\nInvalid number!")
else:
while re_q:
try:
u_ch = int(input(f"\n{p_2} your turn. Enter the number where you want to play your move: "))
except ValueError:
print("\nPlease enter a number!")
else:
if u_ch in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
re_q = False
else:
print("\nInvalid number!")
# Write changes on the board after passing of all the checks.
if turn == True:
check_validated = self.checks(pos=u_ch, sym='X')
else:
check_validated = self.checks(pos=u_ch, sym='O')
# Check if there is a winner.
if self.check_win():
interface.show_congratulations() # Show congratulations message.
print(f"\n{p_1 if turn is True else p_2}. You won the game!!")
win = True # Break out of the multiplayer game loop.
self.game_board = np.array([['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]) # Reset the board.
else:
if check_validated: # Check if all the checks are validated.
turn = not turn # Switch turns if checks are validated.
else:
turn = turn # Keep the same turn.
# Tie condition.
if self.game_board_full():
print("\nIt's a tie.")
self.game_board = np.array([['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]) # Reset the board.
ch_run = True
while ch_run:
resp = input("\nDo you want to play the multiplayer game again?\nType \n'y' to continue\n'n' to return to main menu.\nYour choice: ")
if resp == 'y':
return True
elif resp == 'n':
return False
else:
print("\nInvalid input!")
def game_board_full(self):
for i in range(0, 3):
for j in range(0, 3):
for val in range(1, 10):
if str(val) == self.game_board[i][j]:
return False
return True
def check_pos(self, pos):
for i in range(0, 3):
for j in range(0, 3):
if str(pos) == self.game_board[i][j]:
return True
return False
def get_board(self):
print("\n\n")
print(f" | | ")
print(f" {self.game_board[0][0]} | {self.game_board[0][1]} | {self.game_board[0][2]} ")
print(f"____________|____________|____________")
print(f" | | ")
print(f" {self.game_board[1][0]} | {self.game_board[1][1]} | {self.game_board[1][2]} ")
print(f"____________|____________|____________")
print(f" | | ")
print(f" {self.game_board[2][0]} | {self.game_board[2][1]} | {self.game_board[2][2]} ")
print(f" | | ")
def check_win(self):
if self.col_win() or self.row_win() or self.cross_win():
return True
return False
def col_win(self):
for i in range(0, 3):
if self.game_board[0][i] == self.game_board[1][i] == self.game_board[2][i]:
return True
return False
def row_win(self):
for i in range(0,3):
if self.game_board[i][0] == self.game_board[i][1] == self.game_board[i][2]:
return True
return False
def cross_win(self):
if self.game_board[0][0] == self.game_board[1][1] == self.game_board[2][2] or self.game_board[0][2] == self.game_board[1][1] == self.game_board[2][0]:
return True
return False
def checks(self, pos, sym):
correct = False
while not correct:
if 1<=pos<=3:
if self.check_pos(pos):
self.game_board[0][pos-1] = sym
self.get_board()
else:
print("\nInvalid position")
return False
elif 4<=pos<=6:
if self.check_pos(pos):
self.game_board[1][pos-4] = sym
self.get_board()
else:
print("\nInvalid position")
return False
elif 7<=pos<=9:
if self.check_pos(pos):
self.game_board[2][pos-7] = sym
self.get_board()
else:
print("\nInvalid position")
return False
else:
print("\nInvalid position")
return False
correct = True
return True