-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
310 lines (244 loc) · 10.2 KB
/
Copy pathmain.py
File metadata and controls
310 lines (244 loc) · 10.2 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import random
from os import system, name
from time import sleep
# Displays the welcome Message
def welcome_msg():
print("\nWelcome to TicTacToe!")
# Gets the player's choice of game mode and returns it
def game_mode():
print("\n1. Do you want to play against the computer")
print("\n", "\t" * 2, '(or)')
print("\n2. Do you want to play against another player(2 players)")
accecptable_choice = range(1, 3)
choice = ''
while choice not in accecptable_choice:
choice = input("\nChoose your game mode : ")
if choice.isdigit():
choice = int(choice)
if choice not in accecptable_choice:
print("Sorry! It's not a valid input.")
else:
return choice
# Takes the player's information and returns a list of player's name
def player_details(mode):
if mode == 1:
player_1 = input("\nEnter your name : ")
player_2 = "Computer"
return [player_1, player_2]
else:
player_1 = input("\nPlayer 1, enter your name : ")
player_2 = input("\nPlayer 2, enter your name : ")
return [player_1, player_2]
# Clears the display screen
def clear():
if name == 'nt':
_ = system('cls')
# Displays the board
def display_board(gamelist):
clear()
print('\n' * 10)
print('\t', gamelist[7], "||", gamelist[8], "||", gamelist[9])
print("-" * 30)
print('\t', gamelist[4], "||", gamelist[5], "||", gamelist[6])
print("-" * 30)
print('\t', gamelist[1], "||", gamelist[2], "||", gamelist[3])
print('\n' * 10)
# Updates the board with user option
def place_marker(board, marker, position):
board[position] = marker
return board
# Conditon to check if the player has won
def isWin(board):
if board[1] == board[2] == board[3] != ' ':
return True
elif board[4] == board[5] == board[6] != ' ':
return True
elif board[7] == board[8] == board[9] != ' ':
return True
elif board[7] == board[8] == board[9] != ' ':
return True
elif board[1] == board[4] == board[7] != ' ':
return True
elif board[2] == board[5] == board[8] != ' ':
return True
elif board[3] == board[6] == board[9] != ' ':
return True
elif board[1] == board[5] == board[9] != ' ':
return True
elif board[3] == board[5] == board[7] != ' ':
return True
else:
return False
def isWinPossible(board, marker, position):
board[position] = marker
result = isWin(board)
board[position] = ' '
return result
# Condition to check if the position is free
def isSpacefree(board, position):
return board[position] == ' '
# Condition to check if the board is full
def isFull(board):
for pos in board:
if pos == ' ':
return False
return True
# Gets the player's choice of position
def player_choice(board, player, marker):
accepltable_range = range(1, 10)
position = '' # Initially set to empty string so that the while loop is executed
spacefree = True # Initially the position is assumed to be free since user hasn't selected a position
while position not in accepltable_range and spacefree == True:
position = input(f"\n{player} '{marker}', enter your positon (0 to 9) : ")
if position.isdigit():
position = int(position)
if position in accepltable_range:
if isSpacefree(board, position):
return position
else:
position = ''
print("\nThe position is not empty!")
else:
print("\nSorry! It's not a valid input.")
def computer_choice(board, computer_marker, player_marker):
# Scanning for all available positions
available_positions = []
for pos in range(1, 10):
if isSpacefree(board, pos):
available_positions.append(pos)
for pos in available_positions:
# Checking is computer has a win possibility
if isWinPossible(board, computer_marker, pos):
return pos
# Checking if player has a win possibility
elif isWinPossible(board, player_marker, pos):
return pos
# Return a random position if no win is possible
return random.choice(available_positions)
# Condition to check if the player want's to replay
def isReplay():
replay = '' # Initailly set to empty string so that the while loop can be executed
acceptable_choice = ['y', 'n']
while replay not in acceptable_choice:
replay = input("\nDo you want to continue playing (Y or N) : ")
replay.lower()
if replay in acceptable_choice:
return replay == 'y'
print("Sorry, its not a valid input!")
# The main Function
if __name__ == '__main__':
game_on = True
# The game loop
while game_on:
welcome_msg()
sleep(1.5)
mode = game_mode()
# Game mode 2 (2 players)
if mode == 2:
board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] # Initial board
player_names = player_details(mode)
moves_count = 0
player_1_marker = 'X'
player_2_marker = 'O'
first_turn = random.randint(0, 1) # Random selection of the first player
display_board(board)
# Assigning respective user as player1 and player2
print(f"\n{player_names[first_turn]} is 'X' and goes first!")
player_1 = player_names[first_turn]
player_names.pop(first_turn)
player_2 = player_names[0]
winning = False # Initially the winning condition is set False
# Loop until there's a winner
while not winning:
# Player_1 turn
moves_count += 1
print("\nMove : ", moves_count)
position = player_choice(board, player_1, player_1_marker)
board = place_marker(board, player_1_marker, position)
display_board(board)
# Checking if player_1 has won the game
if isWin(board):
print(f"\nCONGRATULATIONS {player_1}!!!,\nYou have won the game in {moves_count} moves!")
winning = True
break
# Condition to check if the game is a draw
if isFull(board):
print("It's a draw!")
break
# Player_2 turn
moves_count += 1
print("\nMove : ", moves_count)
position = player_choice(board, player_2, player_2_marker)
board = place_marker(board, player_2_marker, position)
display_board(board)
# Checking if player_2 has won the game
if isWin(board):
print(f"\nCONGRATULATIONS{player_2}!!!,\nYou have won the game in {moves_count} moves!")
winning = True
# Replay or Quit after the game is over
game_on = isReplay()
clear()
# Game mode 1 (1 player)
else:
board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] # Initial board
player_names = player_details(mode) # List of player names
moves_count = 0
player_1_marker = 'X'
player_2_marker = 'O'
first_turn = random.randint(0, 1) # Random selection of the first player
display_board(board)
# Assigning respective user as player1 and player2
print(f"\n{player_names[first_turn]} is 'X' and goes first!")
print('\n' * 5)
sleep(2)
player_1 = player_names[first_turn]
player_names.pop(first_turn)
player_2 = player_names[0]
winning = False # Initially the winning condition is set False
# Loop until there's a winner
while not winning:
# Player_1 turn
moves_count += 1
print("\nMove : ", moves_count)
# If Player_1 is the computer
if player_1 == 'Computer':
position = computer_choice(board, player_1_marker, player_2_marker)
# If player_1 is the player
else:
position = player_choice(board, player_1, player_1_marker)
board = place_marker(board, player_1_marker, position)
display_board(board)
# Checking if player_1 has won the game
if isWin(board):
if player_1 == 'Computer':
print("Sorry, you lost to the computer")
else:
print(f"\nCONGRATULATIONS {player_1}!!!,\nYou have won the game in {moves_count} moves!")
winning = True
break
# Condition to check if the game is a draw
if isFull(board):
print("It's a draw!")
break
# Player_2 turn
moves_count += 1
print("\nMove : ", moves_count)
# If Player_2 is the computer
if player_2 == 'Computer':
position = computer_choice(board, player_2_marker, player_1_marker)
# If player_2 is the player
else:
position = player_choice(board, player_2, player_2_marker)
board = place_marker(board, player_2_marker, position)
display_board(board)
# Checking if player_2 has won the game
if isWin(board):
if player_2 == 'Computer':
print("Sorry, you lost to the computer")
else:
print(f"\nCONGRATULATIONS {player_2}!!!,\nYou have won the game in {moves_count} moves!")
winning = True
break
# Replay or Quit after the game is over
game_on = isReplay()
clear()