9
9
'4' : ' ' , '5' : ' ' , '6' : ' ' ,
10
10
'1' : ' ' , '2' : ' ' , '3' : ' ' }
11
11
12
- board_keys = []
13
-
14
- for key in theBoard :
15
- board_keys .append (key )
12
+ board_keys = [key for key in theBoard ]
16
13
17
14
''' We will have to print the updated board after every move in the game and
18
15
thus we will make a function in which we'll define the printBoard function
19
16
so that we can easily print the board everytime by calling this function. '''
20
17
21
18
def printBoard (board ):
22
- print (board ['7' ] + '|' + board ['8' ] + '|' + board ['9' ])
23
- print ('-+-+-' )
24
- print (board ['4' ] + '|' + board ['5' ] + '|' + board ['6' ])
25
- print ('-+-+-' )
26
- print (board ['1' ] + '|' + board ['2' ] + '|' + board ['3' ])
27
-
19
+ metrix = f"""
20
+ board['7'] + '|' + board['8'] + '|' + board['9']
21
+ -+-+-
22
+ board['4'] + '|' + board['5'] + '|' + board['6']
23
+ -+-+-
24
+ board['1'] + '|' + board['2'] + '|' + board['3']
25
+ """ "
26
+ print (metrix )
27
+
28
+ #lets define a win function in order not to write it all over again when checking who won.
29
+ def win_message (winner ):
30
+ printBoard (theBoard )
31
+ print ("\n Game Over.\n " )
32
+ print (f" **** { winner } won. ****" )
33
+
28
34
# Now we'll write the main function which has all the gameplay functionality.
29
35
def game ():
30
36
31
37
turn = 'X'
32
38
count = 0
33
39
34
-
35
40
for i in range (10 ):
36
41
printBoard (theBoard )
37
- print ("It's your turn," + turn + ".Move to which place?" )
38
42
39
- move = input ()
43
+ move = input (f"It's your turn, { turn } . Move to which place?" )
40
44
41
45
if theBoard [move ] == ' ' :
42
46
theBoard [move ] = turn
@@ -48,64 +52,47 @@ def game():
48
52
# Now we will check if player X or O has won,for every move after 5 moves.
49
53
if count >= 5 :
50
54
if theBoard ['7' ] == theBoard ['8' ] == theBoard ['9' ] != ' ' : # across the top
51
- printBoard (theBoard )
52
- print ("\n Game Over.\n " )
53
- print (" **** " + turn + " won. ****" )
55
+ win_message (turn )
54
56
break
55
57
elif theBoard ['4' ] == theBoard ['5' ] == theBoard ['6' ] != ' ' : # across the middle
56
- printBoard (theBoard )
57
- print ("\n Game Over.\n " )
58
- print (" **** " + turn + " won. ****" )
58
+ win_message (turn )
59
59
break
60
60
elif theBoard ['1' ] == theBoard ['2' ] == theBoard ['3' ] != ' ' : # across the bottom
61
- printBoard (theBoard )
62
- print ("\n Game Over.\n " )
63
- print (" **** " + turn + " won. ****" )
61
+ win_message (turn )
64
62
break
65
63
elif theBoard ['1' ] == theBoard ['4' ] == theBoard ['7' ] != ' ' : # down the left side
66
- printBoard (theBoard )
67
- print ("\n Game Over.\n " )
68
- print (" **** " + turn + " won. ****" )
64
+ win_message (turn )
69
65
break
70
66
elif theBoard ['2' ] == theBoard ['5' ] == theBoard ['8' ] != ' ' : # down the middle
71
- printBoard (theBoard )
72
- print ("\n Game Over.\n " )
73
- print (" **** " + turn + " won. ****" )
67
+ win_message (turn )
74
68
break
75
69
elif theBoard ['3' ] == theBoard ['6' ] == theBoard ['9' ] != ' ' : # down the right side
76
- printBoard (theBoard )
77
- print ("\n Game Over.\n " )
78
- print (" **** " + turn + " won. ****" )
70
+ win_message (turn )
79
71
break
80
72
elif theBoard ['7' ] == theBoard ['5' ] == theBoard ['3' ] != ' ' : # diagonal
81
- printBoard (theBoard )
82
- print ("\n Game Over.\n " )
83
- print (" **** " + turn + " won. ****" )
73
+ win_message (turn )
84
74
break
85
75
elif theBoard ['1' ] == theBoard ['5' ] == theBoard ['9' ] != ' ' : # diagonal
86
- printBoard (theBoard )
87
- print ("\n Game Over.\n " )
88
- print (" **** " + turn + " won. ****" )
76
+ win_message (turn )
89
77
break
90
78
91
79
# If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
92
80
if count == 9 :
93
- print ("\n Game Over.\n " )
94
- print ("It's a Tie!!" )
81
+ print ("\n Game Over.\n It's a Tie!!" )
95
82
96
83
# Now we have to change the player after every move.
97
- if turn == 'X' :
84
+ if turn == 'X' :
98
85
turn = 'O'
99
86
else :
100
87
turn = 'X'
101
88
102
89
# Now we will ask if player wants to restart the game or not.
103
- restart = input ("Do want to play Again?(y/n)" )
104
- if restart == "y" or restart == "Y" :
90
+ restart = input ("Do want to play Again?(y/n)" )
91
+ if restart in ( 'y' , 'Y' ) :
105
92
for key in board_keys :
106
93
theBoard [key ] = " "
107
94
108
95
game ()
109
96
110
97
if __name__ == "__main__" :
111
- game ()
98
+ game ()
0 commit comments