-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_Tic_Tac_Toe.py
More file actions
232 lines (148 loc) · 5.84 KB
/
1_Tic_Tac_Toe.py
File metadata and controls
232 lines (148 loc) · 5.84 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
print('\n'*12)
print('Welcome to my first Python Milestone Project : \n This is a Tic Tac Toe Game')
def creating_board(board): #1 --------------------------------------------------------------
print('\n'*100)
print(f' {board[7]} | {board[8]} | {board[9]} ')
print(' ____|____|____')
print(' | | ')
print(f' {board[4]} | {board[5]} | {board[6]} ')
print(' ____|____|____')
print(' | | ')
print(f' {board[1]} | {board[2]} | {board[3]} ')
print('\n'*10)
def Player1_pick(): #2 --------------------------------------------------------------------
x = 'wrong'
options = ['X', 'O']
while x not in options :
x = input('Select your Symbol for the game (Player_1) : (X/O)')
if x not in options:
print("Please select only from X and O !!!")
return x
def row_key1(): #3 -------------------------------------------------------------------------
position = ['1','2','3','4','5','6','7','8','9']
input_position = 'wrong'
while input_position not in position:
input_position = input('Enter position- player1 : ')
if input_position not in position:
print('Please choose a correct position !!!')
return int(input_position)
def row_key2(): #4 -------------------------------------------------------------------------
position = ['1','2','3','4','5','6','7','8','9']
input_position = 'wrong'
while input_position not in position:
input_position = input('Enter position- player2 : ')
if input_position not in position:
print('Please choose a correct position !!!')
return int(input_position)
def game_on_choice(): #5 -------------------------------------------------------------------
choice = 'wrong'
lis = ['Y','N']
while choice not in lis:
choice = input("Want to keep playing? (Y/N) : ")
if choice not in lis:
print("sorry, Please reply in (Y/N) !!!")
if choice == 'Y':
print('lets get started again !!')
return True
else:
print('Thanks for playing !!!')
return False
def game_draw(board): #6 ------------------------------------------------------------------------
draw = False
x = 0
for i in board:
if i == 'X' or i == 'O':
x += 1
else:
pass
if x == 9 :
draw = True
else:
pass
return draw
def game_win1(board): #7------------------------------------------------------------------------
win = False
if board[1] == board[2] == board[3] == 'X' or board[1] == board[4] == board[7] == 'X':
win = True
elif board[4] == board[5] == board[6] == 'X' or board[2] == board[5] == board[8] == 'X':
win = True
elif board[7] == board[8] == board[9] == 'X' or board[3] == board[6] == board[9] == 'X':
win = True
elif board[7] == board[5] == board[3] == 'X' or board[9] == board[5] == board[1] == 'X':
win = True
else:
win = False
return win
def game_win2(board): #8------------------------------------------------------------------------
win = False
if board[1] == board[2] == board[3] == 'O' or board[1] == board[4] == board[7] == 'O':
win = True
elif board[4] == board[5] == board[6] == 'O' or board[2] == board[5] == board[8] == 'O':
win = True
elif board[7] == board[8] == board[9] == 'O' or board[3] == board[6] == board[9] == 'O':
win = True
elif board[7] == board[5] == board[3] == 'O' or board[9] == board[5] == board[1] == 'O':
win = True
else:
win = False
return win
#---------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------
#----------------------------------------MAIN-------------------------------------------------
next_game = True
while next_game:
game_on = True
next_game = True
board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
creating_board(board)
player1 = Player1_pick()
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
while game_on:
creating_board(board)
player_input = row_key1()
board[player_input] = player1
creating_board(board)
win1 = game_win1(board)
win2 = game_win2(board)
draw = game_draw(board)
if draw == True:
print('sorry the match has drawn...')
game_on = False
break
elif win1 == True:
print("congratulations!! Player with 'X' has won the match")
game_on = False
break
elif win2 == True:
print("congratulations!! Player with 'O' has won the match")
game_on = False
break
player_input = row_key2()
board[player_input] = player2
creating_board(board)
win1 = game_win1(board)
win2 = game_win2(board)
draw = game_draw(board)
if draw == True:
print('sorry the match has drawn...')
game_on = False
break
elif win1 == True:
print("congratulations!! Player with 'X' has won the match")
game_on = False
break
elif win2 == True:
print("congratulations!! Player with 'O' has won the match")
game_on = False
break
next_game = game_on_choice()
if next_game == True:
game_on = True
else:
game_on = False
break
# TOTAL 8 FUCTIONS CREATED...