1+ import os
2+ import random
3+
4+ def display_board (board ):
5+ """
6+ Display the Tic Tac Toe Board on screen
7+
8+ """
9+ os .system ("cls" )
10+ print ("\t \t **Tic Tac Toe Board**\n " )
11+ print (" | | " )
12+ print (" {} | {} | {} " .format (board [7 ],board [8 ],board [9 ]))
13+ print (" | | " )
14+ print ("--------------------" )
15+ print (" | | " )
16+ print (" {} | {} | {} " .format (board [4 ],board [5 ],board [6 ]))
17+ print (" | | " )
18+ print ("--------------------" )
19+ print (" | | " )
20+ print (" {} | {} | {} " .format (board [1 ],board [2 ],board [3 ]))
21+ print (" | | " )
22+ print ("\n " )
23+
24+
25+ def player_input ():
26+ """
27+ OUTPUT : (player 1 marker, player 2 marker)
28+ """
29+
30+ marker = ""
31+ while not ( marker == 'X' or marker == 'O' ):
32+ marker = input ("Player 1 choose 'X' or 'O' : " ).upper ()
33+
34+ if marker == 'O' :
35+ return ('O' ,'X' )
36+ else :
37+ return ('X' ,'O' )
38+
39+
40+ def place_marker (board ,marker ,position ):
41+ '''
42+ Place your marker on the board at the given position
43+ '''
44+ board [position ] = marker
45+
46+
47+ def win_check (board , mark ):
48+ """
49+ WIN TIC TAC TOE ?
50+
51+ """
52+ return ((board [7 ] == mark and board [8 ] == mark and board [9 ] == mark ) or # across the top
53+ (board [4 ] == mark and board [5 ] == mark and board [6 ] == mark ) or # across the middle
54+ (board [1 ] == mark and board [2 ] == mark and board [3 ] == mark ) or # across the bottom
55+ (board [7 ] == mark and board [4 ] == mark and board [1 ] == mark ) or # down the middle
56+ (board [8 ] == mark and board [5 ] == mark and board [2 ] == mark ) or # down the middle
57+ (board [9 ] == mark and board [6 ] == mark and board [3 ] == mark ) or # down the right side
58+ (board [7 ] == mark and board [5 ] == mark and board [3 ] == mark ) or # diagonal
59+ (board [9 ] == mark and board [5 ] == mark and board [1 ] == mark )) # diagonal
60+
61+
62+ def choose_first ():
63+ """
64+ Randomly choose the player who goes first
65+ """
66+
67+ flip = random .randint (0 ,1 )
68+
69+ if flip == 0 :
70+ return 'Player 1'
71+ else :
72+ return 'Player 2'
73+
74+
75+ def space_check (board ,position ):
76+ """
77+ Check for empty space
78+ """
79+
80+ return board [position ] == " "
81+
82+
83+ def full_board_check (board ):
84+ """
85+ Check for the board is full or not
86+ """
87+
88+ for i in range (1 ,10 ):
89+ if (space_check (board ,i )):
90+ return False
91+ return True
92+
93+
94+ def player_choice (board ):
95+ position = 0
96+ while position not in [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ] and not space_check (board ,position ):
97+ position = int (input ("Choose position : (1-9) " ))
98+
99+ return position
100+
101+
102+ def replay ():
103+ choice = input ("Do You want to play Again? Yes or No : " )
104+ return choice == 'Yes'
105+
106+
107+
108+ if __name__ == "__main__" :
109+ print ("\t \t Welcome to Tic Tac Toe Game\n " )
110+ while True :
111+ the_board = [" " ]* 10
112+ player1_marker , player2_marker = player_input ()
113+
114+ turn = choose_first ()
115+ print (turn , " will go first \n " )
116+ play_game = input ("Ready to play? y or n :" )
117+ if play_game == 'y' :
118+ game_on = True
119+ else :
120+ game_on = False
121+
122+ while game_on :
123+ if turn == 'Player 1' :
124+
125+ display_board (the_board )
126+ position = player_choice (the_board )
127+ place_marker (the_board ,player1_marker ,position )
128+ if win_check (the_board ,player1_marker ):
129+ display_board (the_board )
130+ print ("PLAYER 1 WON..!!\n " )
131+ game_on = False
132+ else :
133+ if full_board_check (the_board ):
134+ display_board (the_board )
135+ print ("TIE GAME" )
136+ game_on = False
137+ else :
138+ turn = 'Player 2'
139+ else :
140+
141+ display_board (the_board )
142+ position = player_choice (the_board )
143+ place_marker (the_board ,player2_marker ,position )
144+ if win_check (the_board ,player2_marker ):
145+ display_board (the_board )
146+ print ("PLAYER 2 WON..!!\n " )
147+ game_on = False
148+ else :
149+ if full_board_check (the_board ):
150+ display_board (the_board )
151+ print ("TIE GAME" )
152+ game_on = False
153+ else :
154+ turn = 'Player 1'
155+
156+ if not replay ():
157+ break
158+
0 commit comments