-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
200 lines (161 loc) · 4.72 KB
/
TicTacToe.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
import math
import random
import os
# game board
game = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# play with Computer or 2 players
game_mode = 0
# current player x by default
current_player = "x"
# get current position
position = -1
# check if its computer turn
computer_turn = False
# check if it is draw by counting each turns
turn_steps = 0
# clear console for both windows and linux OS
def clearConsole():
command = 'clear'
if os.name in ('nt', 'dos'): # If Machine is running on Windows, use cls
command = 'cls'
os.system(command)
# display the game board
def board():
counter = 1
clearConsole()
print('-------------')
for row in game:
for item in row:
print(f"| {item} ", end="")
if counter % 3 == 0:
print("|", end="")
counter = counter + 1
print("")
counter = 0
print('-------------')
# check if given input is numeric
def is_num(num):
if not num.isnumeric():
return False
else:
return True
# check if given input is in right range(1 - 9)
def is_in_range(num):
if 1 <= int(num) <= 9:
return True
else:
return False
# check if a place is already taken by another player
def is_taken(row, col):
if game[row][col] == "x" or game[row][col] == "o":
return True
else:
return False
def check_if_draw():
if turn_steps >= 8:
return True
else:
return False
# check if any players win
def check_if_win(row):
win = False
if check_row(row) or check_column() or check_diagonal():
win = True
return win
# check if a player win by row
def check_row(row):
win_row = True
for pos in game[row]:
if pos != current_player:
win_row = False
break
return win_row
# check if a player win by column
def check_column():
for i in range(3):
win_column = True
for j in range(3):
if game[j][i] != current_player:
win_column = False
break
if win_column:
return True
return False
# check if a player win by diagonal
def check_diagonal():
if game[0][0] == current_player and game[1][1] == current_player and game[2][2] == current_player:
return True
elif game[0][2] == current_player and game[1][1] == current_player and game[2][0] == current_player:
return True
else:
return False
# if X turn finished then go to O turn
def switch_player():
global current_player
if current_player == "x":
current_player = "o"
elif current_player == "o":
current_player = "x"
def computer_player():
while True:
temp = random.randint(1,9)
row = int((int(temp) - 1) / 3)
col = int((int(temp) - 1) % 3)
if not is_taken(row, col):
return str(temp)
break
def get_position():
global position, computer_turn
if game_mode == "1":
position = computer_player()
computer_turn = False
elif game_mode == "2":
position = input("Choice number: ")
def game_mode():
# 1 for computer , 2 for 2 players
global game_mode
game_mode = input("Choose a game mode:\n\n1 - play with Computer\n2 - play with 2 Players\n> ")
# computer mode
if game_mode == "1":
print("Computer Mode Selected")
elif game_mode == "2":
print("2 Players Mode Selected")
else:
print("Invalid choice, select either 1 or 2")
def tic_tac_toe():
global computer_turn, position, turn_steps
game_mode()
while True:
board()
if computer_turn:
get_position()
else:
position = input("Choice number: ")
computer_turn = True
if is_num(position) and is_in_range(position):
row = int((int(position) - 1) / 3)
col = int((int(position) - 1) % 3)
if not is_taken(row, col):
game[row][col] = current_player
if check_if_win(row):
board()
print(f"{current_player} Winned!")
break
elif check_if_draw():
board()
print(f"Draw ")
break
else:
switch_player()
turn_steps = turn_steps + 1
else:
print('position is taken, choose another')
continue
else:
print('Invalid choice, Enter from 1 to 9')
continue
tic_tac_toe()