forked from latenightx/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.V.2.py
127 lines (111 loc) · 2.56 KB
/
TicTacToe.V.2.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
Grid={'1':' ', '2':' ', '3':' ','4':' ','5':' ','6':' ','7':' ','8':' ','9':' '}
# print(Grid)
#printing Grid
def printTTT(a):
c=1
for v in a.values():
if c==3 or c==6:
print(v,'\n-+-+-')
c+=1
continue
elif c==9:
print(v)
break
print(v,end='|')
c+=1
#the basic structure
# | |
# -+-+-
# |X|
# -+-+-
# | |
# | |
# X | | O
# _____|_____|_____
# | |
# | |
# _____|_____|_____
# | |
# | |
# | |
#Game logic
def game():
player='O'
count=0
while True:
printTTT(Grid)
print("Enter position for ", player)
pos=input()
if pos!='1' and pos!='2' and pos !='3' and pos!='4'and pos!='5'and pos!='6'and pos!='7'and pos!='8'and pos!='9':
print('Invalid Response,Try Again')
continue
elif Grid[pos]==' ':
Grid[pos]=player
count+=1
else:
print("Position already taken by ", Grid[pos], "Try another position")
continue
if count>=5:
if Grid['1']==Grid['2']==Grid['3']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['4']==Grid['5']==Grid['6']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['7']==Grid['8']==Grid['9']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['1']==Grid['5']==Grid['9']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['3']==Grid['5']==Grid['7']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['1']==Grid['4']==Grid['7']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['5']==Grid['2']==Grid['8']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
elif Grid['3']==Grid['6']==Grid['9']!=' ':
printTTT(Grid)
print(player, " Has Won!!")
break
if count>8:
printTTT(Grid)
print("****TIE****")
break
if player=='O':
player='X'
else:
player='O'
game()
print("Game Over!\n Try Again?\nYes(y)/No(n)")
while True:
restart=input()
if restart=='Y' or restart=='y':
for k in Grid:
Grid[k]=' '
game()
print("Game Over!\n Try Again?\nYes(y)/No(n)")
continue
elif restart=='n' or restart=='N':
print("Okay, Bye!\n\n****THE END****")
break
else :
print("Wrong Input\nTry Again!")
continue
# TikTacToe v2 made by Zaid and Shoaib
# What's New?
# 1) Try again bug fix:
# - The code now can be run n number of times.
# - Gives alert warning when invalid input is entered.
# 2) Enhanced user friendly instructions.
# 3) Code simplified.