-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-teo-p2p.py
109 lines (91 loc) · 2.92 KB
/
tic-tac-teo-p2p.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
def print_mat(p1, p2, v=" "):
if p1 != -1:
matrix[p1][p2] = v
print("**********")
print(f" {matrix[0][0]} | {matrix[0][1]} | {matrix[0][2]}")
print(f"_ _|_ _|_ _")
print(f" {matrix[1][0]} | {matrix[1][1]} | {matrix[1][2]}")
print(f"_ _|_ _|_ _")
print(f" {matrix[2][0]} | {matrix[2][1]} | {matrix[2][2]}")
print(f" | | ")
print("**********")
# Checking all possible wining rows
def check_winner():
if matrix[0][0] == matrix[0][1] == matrix[0][2] != " ":
return matrix[0][0]
elif matrix[1][0] == matrix[1][1] == matrix[1][2] != " ":
return matrix[1][0]
elif matrix[2][0] == matrix[2][1] == matrix[2][2] != " ":
return matrix[2][0]
elif matrix[0][0] == matrix[1][0] == matrix[2][0] != " ":
return matrix[0][0]
elif matrix[0][1] == matrix[1][1] == matrix[2][1] != " ":
return matrix[0][1]
elif matrix[0][2] == matrix[1][2] == matrix[2][2] != " ":
return matrix[0][2]
elif matrix[0][0] == matrix[1][1] == matrix[2][2] != " ":
return matrix[0][0]
elif matrix[2][0] == matrix[1][1] == matrix[0][2] != " ":
return matrix[2][0]
else:
return -1
# Printing winner
def p_winner(s):
if s == u1:
print(f"The Winner is {user1}")
else:
print(f"The Winner is {user2}")
exit(0)
# To stop replacing data in matrix
def check_error(k1, k2):
if matrix[k1][k2] == " ":
return 1
else:
return -1
# Number of filled block's
filledBlocks = 0
print("Welcome to the tic tac toe game!\n")
user1 = input("User1 name : ")
user2 = input("User1 name : ")
matrix = [[" " for i in range(3)] for j in range(3)]
print_mat(-1, -1)
print(f"{user1} select your symbol ")
u1 = input('\nselect your symbol (X | O) : ')
u2 = "x" if u1 == "o" else "o"
print(f'{user1} symbol is "{u1}"')
print(f'{user2} symbol is "{u2}"')
# filling the data
while filledBlocks < 10:
# user input
while True:
pos = input(f"{user1} enter the position (x y): ")
if check_error(int(pos[0]), int(pos[1])) == 1:
break
else:
print("This position is already filled, try another.")
print(f"{user1} input ")
print_mat(int(pos[0]), int(pos[1]), u1)
filledBlocks += 1
# Checking for winner
if filledBlocks > 3:
winD = check_winner()
if winD != -1:
p_winner(winD)
if filledBlocks >= 9:
print("\n******Draw********")
exit(0)
# User2 input
while True:
cPos = input(f"{user2} enter the position (x y): ")
if check_error(int(cPos[0]), int(cPos[1])) == 1:
break
else:
print("This position is already filled, try another.")
# printing grid for User2
print(f"{user2} input ")
filledBlocks += 1
print_mat(int(cPos[0]), int(cPos[1]), u2)
if filledBlocks > 2:
winD = check_winner()
if winD != -1:
p_winner(winD)