forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeTest.py
120 lines (105 loc) · 3.12 KB
/
TicTacToeTest.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
#variables
player="X"
result=None
cont= True
#starting the game
board = ['-','-','-','-','-','-','-','-','-']
print("Starting the game... [ TIC TAC TOE ]")
#board
def full_board():
print()
print(" (POSITIONS)")
print(board[0]+' |',board[1]+' |',board[2]," 1 | 2 | 3 ")
print(board[3]+' |',board[4]+' |',board[5]," 4 | 5 | 6 ")
print(board[6]+' |',board[7]+' |',board[8]," 7 | 8 | 9 ")
print()
full_board()
print("X's turn...")
#input positions
def position():
global player
global cont
pos=input("Enter your liked position from 1-9 :")
while pos not in ("1","2","3","4","5","6","7","8","9"):
pos=input("Enter your liked position from 1-9 :")
pos=int(pos)-1
if board[pos]!="-":
print("You cant go there, TRY AGAIN...")
cont=False
else:
board[pos]=player
full_board()
cont=True
#win/tie check
def game_over():
global result
result= None
row1= board[0]==board[1]==board[2]!="-"
row2= board[3]==board[4]==board[5]!="-"
row3= board[6]==board[7]==board[8]!="-"
col1= board[0]==board[3]==board[6]!="-"
col2= board[1]==board[4]==board[7]!="-"
col3= board[2]==board[5]==board[8]!="-"
dig1= board[0]==board[4]==board[8]!="-"
dig2= board[2]==board[4]==board[6]!="-"
if row1 :
result= True
print("Congratulations... Player",board[0]+"'s won")
elif row2 :
result= True
print("Congratulations... Player",board[3]+"'s won")
elif row3 :
result= True
print("Congratulations... Player",board[6]+"'s won")
elif col1 :
result= True
print("Congratulations... Player",board[0]+"'s won")
elif col2 :
result= True
print("Congratulations... Player",board[1]+"'s won")
elif col3 :
result= True
print("Congratulations... Player",board[2]+"'s won")
elif dig1 :
result= True
print("Congratulations... Player",board[0]+"'s won")
elif dig2 :
result= True
print("Congratulations... Player",board[2]+"'s won")
elif "-" not in board :
result = True
print("Game Tied")
return()
#player change
def flip():
global player
global cont
if player == "X" and result!= True and cont==True:
print("O's turn...")
player="O"
elif player == "O" and result!= True and cont==True:
print("X's turn...")
player="X"
#restaring the game
def restart():
global board
board = ['-','-','-','-','-','-','-','-','-']
print("==========================================")
print()
print("Starting the game... [ TIC TAC TOE ]")
full_board()
print("X's turn...")
#game loop
while True:
while result!= True:
position()
game_over()
flip()
print()
loop=input("You want to replay the game (y/n): ")
if loop=="Y" or loop=="y":
result=None
restart()
elif loop=="N" or loop=="n":
break
#end