1+ #### TIC TAC TOE ####
2+
3+ #START;
4+
5+
6+ #FUNCTIONS;
7+
8+ def default ():
9+ #To be printed as Default;
10+ print ("\n Welcome! Let's play TIC TAC TOE!\n " )
11+
12+
13+ def rules ():
14+ print ("The board will look like this!" )
15+ print ("The positions of this 3 x 3 board is same as the right side of your key board.\n " )
16+ print (" 7 | 8 | 9 " )
17+ print ("-----------" )
18+ print (" 4 | 5 | 6 " )
19+ print ("-----------" )
20+ print (" 1 | 2 | 3 " )
21+ print ("\n You just have to input the position(1-9)." )
22+
23+
24+ def play ():
25+ #Asking if the player is ready;
26+ return input ("\n Are you ready to play the game? Enter [Y]es or [N]o.\t " ).upper ().startswith ('Y' )
27+
28+
29+ def names ():
30+ #Player names input;
31+
32+ p1_name = input ("\n Enter NAME of PLAYER 1:\t " ).capitalize ()
33+ p2_name = input ("Enter NAME of PLAYER 2:\t " ).capitalize ()
34+ return (p1_name , p2_name )
35+
36+
37+ def choice ():
38+ #Player choice input;
39+ p1_choice = ' '
40+ p2_choice = ' '
41+ while p1_choice != 'X' or p1_choice != 'O' : #while loop; if the entered value isn't X or O;
42+
43+ #WHILE LOOP STARTS
44+
45+ p1_choice = input (f"\n { p1_name } , Do you want to be X or O?\t " )[0 ].upper ()
46+ #The input above has [0].upper() in the end;
47+ #So the user can enter x, X, xxxx or XXX; the input will always be taken as X;
48+ #Thereby, increasing the user input window;
49+
50+ if p1_choice == 'X' or p1_choice == 'O' :
51+ #if entered value is X or O; get out of the loop;
52+ break
53+ print ("INVALID INPUT! Please Try Again!" )
54+ #if the entered value isn't X or O, re-run the while loop;
55+
56+ #WHILE LOOP ENDS
57+ #Assigning the value to p2 and then diplaying the values;
58+ if p1_choice == 'X' :
59+ p2_choice = 'O'
60+ elif p1_choice == 'O' :
61+ p2_choice = 'X'
62+
63+ return (p1_choice , p2_choice )
64+
65+
66+
67+ def first_player ():
68+ #This function will randomly decide who will go first;
69+ import random
70+ return random .choice ((0 , 1 ))
71+
72+
73+ def display_board (board , avail ):
74+ print (" " + " {} | {} | {} " .format (board [7 ],board [8 ],board [9 ]) + " " + " {} | {} | {} " .format (avail [7 ],avail [8 ],avail [9 ]))
75+ print (" " + "-----------" + " " + "-----------" )
76+ print (" " + " {} | {} | {} " .format (board [4 ],board [5 ],board [6 ]) + " " + " {} | {} | {} " .format (avail [4 ],avail [5 ],avail [6 ]))
77+ print (" " + "-----------" + " " + "-----------" )
78+ print (" " + " {} | {} | {} " .format (board [1 ],board [2 ],board [3 ]) + " " + " {} | {} | {} " .format (avail [1 ],avail [2 ],avail [3 ]))
79+
80+
81+ def player_choice (board , name , choice ):
82+ position = 0
83+ #Initialising position as 0^; so it passes through the while loop;
84+ while position not in [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ] or not space_check (board , position ):
85+ position = int (input (f'\n { name } ({ choice } ), Choose your next position: (1-9) \t ' ))
86+
87+ if position not in [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ] or not space_check (board , position ) or position == "" :
88+ #To check whether the given position is in the set [1-9] or whether it is empty or occupied;
89+ print (f"INVALID INPUT. Please Try Again!\n " )
90+ print ("\n " )
91+ return position
92+
93+
94+ # THIS IS THEFUNCTION WHERE AI IS ADDED:
95+ def CompAI (board , name , choice ):
96+ position = 0
97+ possibilities = [x for x , letter in enumerate (board ) if letter == ' ' and x != 0 ]
98+
99+ # including both X and O, since if computer will win, he will place a choice there, but if the component will win --> we have to block that move
100+ for let in ['O' , 'X' ]:
101+ for i in possibilities :
102+ # Creating a copy of the board everytime, placing the move and checking if it wins;
103+ # Creating a copy like this and not this boardCopy = board, since changes to boardCopy changes the original board;
104+ boardCopy = board [:]
105+ boardCopy [i ] = let
106+ if (win_check (boardCopy , let )):
107+ position = i
108+ return position
109+
110+ openCorners = [x for x in possibilities if x in [1 , 3 , 7 , 9 ]]
111+
112+ if len (openCorners ) > 0 :
113+ position = selectRandom (openCorners )
114+ return position
115+
116+ if 5 in possibilities :
117+ position = 5
118+ return position
119+
120+ openEdges = [x for x in possibilities if x in [2 , 4 , 6 , 8 ]]
121+
122+ if len (openEdges ) > 0 :
123+ position = selectRandom (openEdges )
124+ return position
125+
126+
127+
128+ def selectRandom (board ):
129+ import random
130+ ln = len (board )
131+ r = random .randrange (0 ,ln )
132+ return board [r ]
133+
134+
135+ def place_marker (board , avail , choice , position ):
136+ #To mark/replace the position on the board list;
137+ board [position ] = choice
138+ avail [position ] = ' '
139+
140+
141+ def space_check (board , position ):
142+ #To check whether the given position is empty or occupied;
143+ return board [position ] == ' '
144+
145+
146+ def full_board_check (board ):
147+ #To check if the board is full, then the game is a draw;
148+ for i in range (1 ,10 ):
149+ if space_check (board , i ):
150+ return False
151+ return True
152+
153+
154+ def win_check (board , choice ):
155+ #To check if one of the following patterns are true; then the respective player has won!;
156+
157+ #HORIZONTAL CHECK;
158+ return (
159+ ( board [1 ] == choice and board [2 ] == choice and board [3 ] == choice )
160+ or ( board [4 ] == choice and board [5 ] == choice and board [6 ] == choice )
161+ or ( board [7 ] == choice and board [8 ] == choice and board [9 ] == choice )
162+ #VERTICAL CHECK;
163+ or ( board [1 ] == choice and board [4 ] == choice and board [7 ] == choice )
164+ or ( board [2 ] == choice and board [5 ] == choice and board [8 ] == choice )
165+ or ( board [3 ] == choice and board [6 ] == choice and board [9 ] == choice )
166+ #DIAGONAL CHECK;
167+ or ( board [1 ] == choice and board [5 ] == choice and board [9 ] == choice )
168+ or ( board [3 ] == choice and board [5 ] == choice and board [7 ] == choice ) )
169+
170+ def delay (mode ):
171+ if mode == 2 :
172+ import time
173+ time .sleep (2 )
174+
175+ def replay ():
176+ #If the users want to play the game again?
177+ return input ('\n Do you want to play again? Enter [Y]es or [N]o: ' ).lower ().startswith ('y' )
178+
179+
180+
181+
182+
183+ #MAIN PROGRAM STARTS;
184+
185+ print ("\n \t \t NAMASTE! \n " )
186+ input ("Press ENTER to start!" )
187+
188+ default ()
189+ rules ()
190+
191+
192+ while True :
193+ ####################################################################################
194+
195+ #Creating the board as a list; to be kept replacing it with user input;
196+ theBoard = [' ' ]* 10
197+
198+ #Creating the available options on the board:
199+ available = [str (num ) for num in range (0 ,10 )] # a List Comprehension
200+ #available = '0123456789'
201+
202+
203+
204+ print ("\n [0]. Player vs. Computer" )
205+ print ("[1]. Player vs. Player" )
206+ print ("[2]. Computer vs. Computer" )
207+ mode = int (input ("\n Select an option [0]-[2]: " ))
208+ if mode == 1 :
209+ #Asking Names;
210+ p1_name , p2_name = names ()
211+ # Asking Choices; Printing choices; X or O;
212+ p1_choice , p2_choice = choice ()
213+ print (f"\n { p1_name } :" , p1_choice )
214+ print (f"{ p2_name } :" , p2_choice )
215+
216+ elif mode == 0 :
217+ p1_name = input ("\n Enter NAME of PLAYER who will go against the Computer:\t " ).capitalize ()
218+ p2_name = "Computer"
219+ # Asking Choices; Printing choices; X or O;
220+ p1_choice , p2_choice = choice ()
221+ print (f"\n { p1_name } :" , p1_choice )
222+ print (f"{ p2_name } :" , p2_choice )
223+
224+ else :
225+ p1_name = "Computer1"
226+ p2_name = "Computer2"
227+ p1_choice , p2_choice = "X" , "O"
228+ print (f"\n { p1_name } :" , p1_choice )
229+ print (f"\n { p2_name } :" , p2_choice )
230+
231+
232+
233+ #Printing randomly who will go first;
234+ if first_player ():
235+ turn = p2_name
236+ else :
237+ turn = p1_name
238+
239+ print (f"\n { turn } will go first!" )
240+
241+ #Asking the user, if ready to play the game; Output will be True or False;
242+ if (mode == 1 ):
243+ play_game = play ()
244+ else :
245+ ent = input ("\n This is going to be fast! Press Enter for the battle to begin!\n " )
246+ play_game = 1
247+
248+ while play_game :
249+
250+ ############################
251+ #PLAYER1
252+ if turn == p1_name :
253+
254+ #Displaying the board;
255+ display_board (theBoard , available )
256+
257+ #Position of the input;
258+ if mode == 1 :
259+ position = player_choice (theBoard , p2_name , p2_choice )
260+ else :
261+ position = CompAI (theBoard , p2_name , p2_choice )
262+ print (f'\n { p2_name } ({ p2_choice } ) has placed on { position } \n ' )
263+
264+ #Replacing the ' ' at *position* to *p1_choice* in *theBoard* list;
265+ place_marker (theBoard , available , p1_choice , position )
266+
267+ #To check if Player 1 has won after the current input;
268+ if win_check (theBoard , p1_choice ):
269+ display_board (theBoard , available )
270+ print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" )
271+ if (mode ):
272+ print (f'\n \n CONGRATULATIONS { p1_name } ! YOU HAVE WON THE GAME!\n \n ' )
273+ else :
274+ print ('\n \n THE Computer HAS WON THE GAME!\n \n ' )
275+ print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" )
276+ play_game = False
277+
278+ else :
279+ #To check if the board is full; if yes, the game is a draw;
280+ if full_board_check (theBoard ):
281+ display_board (theBoard , available )
282+ print ("~~~~~~~~~~~~~~~~~~" )
283+ print ('\n The game is a DRAW!\n ' )
284+ print ("~~~~~~~~~~~~~~~~~~" )
285+ break
286+ #If none of the above is possible, next turn of Player 2;
287+ else :
288+ turn = p2_name
289+
290+
291+ ############################
292+ #PLAYER2
293+ elif turn == p2_name :
294+
295+ #Displaying the board;
296+ display_board (theBoard , available )
297+
298+ #Position of the input;
299+ if (mode == 1 ):
300+ position = player_choice (theBoard , p2_name , p2_choice )
301+ else :
302+ position = CompAI (theBoard , p2_name , p2_choice )
303+ print (f'\n { p2_name } ({ p2_choice } ) has placed on { position } \n ' )
304+
305+ #Replacing the ' ' at *position* to *p2_choice* in *theBoard* list;
306+ place_marker (theBoard , available , p2_choice , position )
307+
308+ #To check if Player 2 has won after the current input;
309+ if win_check (theBoard , p2_choice ):
310+ display_board (theBoard , available )
311+ print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" )
312+ if (mode ):
313+ print (f'\n \n CONGRATULATIONS { p2_name } ! YOU HAVE WON THE GAME!\n \n ' )
314+ else :
315+ print ('\n \n THE Computer HAS WON THE GAME!\n \n ' )
316+ print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" )
317+ play_game = False
318+
319+ else :
320+ #To check if the board is full; if yes, the game is a draw;
321+ if full_board_check (theBoard ):
322+ display_board (theBoard , available )
323+ print ("~~~~~~~~~~~~~~~~~~" )
324+ print ('\n The game is a DRAW!\n ' )
325+ print ("~~~~~~~~~~~~~~~~~~" )
326+ break
327+ #If none of the above is possible, next turn of Player 2;
328+ else :
329+ turn = p1_name
330+
331+
332+ #If the users want to play the game again?
333+ if replay ():
334+ #if Yes;
335+ continue
336+ else :
337+ #if No;
338+ break
339+
340+ ####################################################################################
341+
342+ print ("\n \n \t \t \t THE END!" )
343+
344+
345+
346+ #END
0 commit comments