-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (106 loc) · 4.62 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
import random
import os
import time
welcome = '''
╔╦╦╦═╦╗╔═╦═╦══╦═╗
║║║║╚╣╚╣═╣║║║║║╚╣
╚══╩═╩═╩═╩═╩╩╩╩═╝
'''
def print_tic_tac_toe(tic_tac_toe):
os.system('cls' if os.name == 'nt' else 'clear')
print("Tic Tac Toe")
print()
print(f'{tic_tac_toe[0][0]} | {tic_tac_toe[0][1]} | {tic_tac_toe[0][2]}')
print('---------')
print(f'{tic_tac_toe[1][0]} | {tic_tac_toe[1][1]} | {tic_tac_toe[1][2]}')
print('---------')
print(f'{tic_tac_toe[2][0]} | {tic_tac_toe[2][1]} | {tic_tac_toe[2][2]}')
if tic_tac_toe[0][0] == tic_tac_toe[0][1] == tic_tac_toe[0][2] and tic_tac_toe[0][0] != "":
print(tic_tac_toe[0][0], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[1][0] == tic_tac_toe[1][1] == tic_tac_toe[1][2] and tic_tac_toe[1][0] != "":
print(tic_tac_toe[1][0], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[2][0] == tic_tac_toe[2][1] == tic_tac_toe[2][2] and tic_tac_toe[2][0] != "":
print(tic_tac_toe[2][0], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[0][0] == tic_tac_toe[1][0] == tic_tac_toe[2][0] and tic_tac_toe[0][0] != "":
print(tic_tac_toe[0][0], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[0][1] == tic_tac_toe[1][1] == tic_tac_toe[2][1] and tic_tac_toe[0][1] != "":
print(tic_tac_toe[0][1], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[0][2] == tic_tac_toe[1][2] == tic_tac_toe[2][2] and tic_tac_toe[0][2] != "":
print(tic_tac_toe[0][2], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[0][0] == tic_tac_toe[1][1] == tic_tac_toe[2][2] and tic_tac_toe[0][0] != "":
print(tic_tac_toe[0][0], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
elif tic_tac_toe[0][2] == tic_tac_toe[1][1] == tic_tac_toe[2][0] and tic_tac_toe[0][2] != "":
print(tic_tac_toe[0][2], "won the match")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
def start_tic_tac_toe():
player_x = 'X'
player_o = 'O'
tic_tac_toe = [["", "", ""],
["", "", ""],
["", "", ""]]
game_is_start = True
user_response = input("For start The game Please enter any key or press q for quit: ").lower()
if user_response == 'q':
game_is_start = False
while game_is_start:
os.system('cls' if os.name == 'nt' else 'clear')
print(welcome)
for i in range(1, 10):
if i % 2 != 0:
player = player_x
row = random.randint(0, 2)
column = random.randint(0, 2)
while tic_tac_toe[row][column] != "":
row = random.randint(0, 2)
column = random.randint(0, 2)
else:
player = player_o
valid = False
while not valid:
try:
row = int(input("user O row: "))
column = int(input('user O column: '))
valid = True
except:
print("Please enter valid input")
while row > 2 or row < 0 or column > 2 or column < 0:
print("Please enter coordinates between 0-2")
row = int(input("user O row: "))
column = int(input('user O column: '))
while tic_tac_toe[row][column] != "":
print("Please enter another coordinates")
row = int(input("user O row: "))
column = int(input('user O column: '))
tic_tac_toe[row][column] = player
print_tic_tac_toe(tic_tac_toe)
for row in range(3):
for column in range(3):
if tic_tac_toe[row][column] != "":
print("Draw")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
start_tic_tac_toe()
start_tic_tac_toe()