-
Notifications
You must be signed in to change notification settings - Fork 92
/
Tic-Tac-Toe-python
166 lines (131 loc) · 4.92 KB
/
Tic-Tac-Toe-python
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
#Global Variables
player1 = "X"
player2 = "O"
#Game_Board Design
board = [ "-","-","-",
"-","-","-",
"-","-","-"]
player = "X"
count = 0
game_over = False
#display the layout of the game
def display_board():
print("\n")
print( " "+board[0] + " | " + board[1] + " | " + board[2]+"\t "+" 1 | 2 | 3 ")
print( " "+board[3] + " | " + board[4] + " | " + board[5]+"\t "+" 4 | 5 | 6 ")
print( " "+board[6] + " | " + board[7] + " | " + board[8]+"\t "+" 7 | 8 | 9 ")
print("\n")
def winsituation():
#check rows
#check conditions for player1
if((board[0] == board[1]) and (board[1]== board[2]) and (board[0] == player1)):
print(player1 + "\t" + "wins the game!")
game_over = True
return game_over
elif(((board[3] == board[4]) and (board[4]== board[5]) and board[3] == player1)):
print(player1 + "\t" + "wins the game!")
game_over = True
return game_over
elif(((board[6] == board[7]) and (board[7]== board[8]) and board[6] == player1)):
print(player1 + "\t" + " wins the game!")
game_over = True
return game_over
#check conditions for player2
elif(((board[0] == board[1]) and (board[1]== board[2]) and board[0] == player2)):
print(player2 + "\t" + " wins the game!")
game_over = True
return game_over
elif(((board[3] == board[4]) and (board[4]== board[5]) and board[3] == player2)):
print(player2 + "\t" + " wins the game!")
game_over = True
return game_over
elif(((board[6] == board[7]) and (board[7]== board[8]) and board[6] == player2)):
print(player2 + "\t" + " wins the game!")
game_over = True
return game_over
#check columns
#check conditions for player1
if((board[0] == board[3]) and (board[3]== board[6]) and (board[0] == player1)):
print(player1 + "\t" + " wins the game!")
game_over = True
return game_over
elif(((board[1] == board[4]) and (board[4]== board[7]) and board[1] == player1)):
print(player1 + "\t" + " wins the game!")
game_over = True
return game_over
elif(((board[2] == board[5]) and (board[5]== board[8]) and board[2] == player1)):
print(player1 + "\t" + " wins the game!")
game_over = True
return game_over
#check conditions for player2
if((board[0] == board[3]) and (board[3]== board[6]) and (board[0] == player2)):
print(player2 + "\t" + "wins the game!")
game_over = True
return game_over
elif(((board[1] == board[4]) and (board[4]== board[7]) and board[1] == player2)):
print(player2 + "\t" + "wins the game!")
game_over = True
return game_over
elif(((board[2] == board[5]) and (board[5]== board[8]) and board[2] == player2)):
print(player2 + "\t"+" wins the game!")
game_over = True
return game_over
#check diagonals
#check conditions for player1
if((board[0] == board[4]) and (board[4]== board[8]) and (board[0] == player1)):
print(player1 + "\t"+"wins the game!")
game_over = True
return game_over
elif(((board[2] == board[4]) and (board[4]== board[6]) and board[2] == player1)):
print(player1 + "\t"+"wins the game!")
game_over = True
return game_over
#check conditions for player1
if((board[0] == board[4]) and (board[4]== board[8]) and (board[0] == player2)):
print(player2 + "\t"+"wins the game!")
game_over = True
return game_over
elif(((board[2] == board[4]) and (board[4]== board[6]) and board[2] == player2)):
print(player2 + "\t"+"wins the game!")
game_over = True
return game_over
def tictactoe():
#Show the initial layout of the board.
print("Welcome to the command line version of TIC TAC TOE game")
num=0
display_board()
while not game_over:
ret = winsituation()
if (ret == True):
break
handler()
t=tie_game()
if(t == True):
break
display_board()
def handler():
print("Choose a position between 1-9 where you want to place your move:")
pos = input()
if pos not in ["1","2","3","4","5","6","7","8","9"]:
pos= input("Enter a valid number")
position = int(pos)-1
if ( board[position] != "-" ):
print("Choose another place")
handler()
global player
if(player == "X"):
board[position] = player
player = "O"
elif(player == "O"):
board[position] = player
player = "X"
def tie_game():
global game_over
if "-" not in board:
game_over = True
print("The game is over!IT'S A TIE")
return game_over
else:
game_over = False
return game_over
tictactoe()