Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3942f7

Browse files
Update Tic-Tac-Toe-Game.py
Removed repeated code and instead created a function. Added cosmetics.
1 parent 55b1e1f commit f3942f7

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

‎Tic-Tac-Toe-Game/Tic-Tac-Toe-Game.py

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,38 @@
99
'4': ' ' , '5': ' ' , '6': ' ' ,
1010
'1': ' ' , '2': ' ' , '3': ' ' }
1111

12-
board_keys = []
13-
14-
for key in theBoard:
15-
board_keys.append(key)
12+
board_keys = [key for key in theBoard]
1613

1714
''' We will have to print the updated board after every move in the game and
1815
thus we will make a function in which we'll define the printBoard function
1916
so that we can easily print the board everytime by calling this function. '''
2017

2118
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("\nGame Over.\n")
32+
print(f" **** {winner} won. ****")
33+
2834
# Now we'll write the main function which has all the gameplay functionality.
2935
def game():
3036

3137
turn = 'X'
3238
count = 0
3339

34-
3540
for i in range(10):
3641
printBoard(theBoard)
37-
print("It's your turn," + turn + ".Move to which place?")
3842

39-
move = input()
43+
move = input(f"It's your turn, {turn}. Move to which place?")
4044

4145
if theBoard[move] == ' ':
4246
theBoard[move] = turn
@@ -48,64 +52,47 @@ def game():
4852
# Now we will check if player X or O has won,for every move after 5 moves.
4953
if count >= 5:
5054
if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top
51-
printBoard(theBoard)
52-
print("\nGame Over.\n")
53-
print(" **** " +turn + " won. ****")
55+
win_message(turn)
5456
break
5557
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
56-
printBoard(theBoard)
57-
print("\nGame Over.\n")
58-
print(" **** " +turn + " won. ****")
58+
win_message(turn)
5959
break
6060
elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the bottom
61-
printBoard(theBoard)
62-
print("\nGame Over.\n")
63-
print(" **** " +turn + " won. ****")
61+
win_message(turn)
6462
break
6563
elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the left side
66-
printBoard(theBoard)
67-
print("\nGame Over.\n")
68-
print(" **** " +turn + " won. ****")
64+
win_message(turn)
6965
break
7066
elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the middle
71-
printBoard(theBoard)
72-
print("\nGame Over.\n")
73-
print(" **** " +turn + " won. ****")
67+
win_message(turn)
7468
break
7569
elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right side
76-
printBoard(theBoard)
77-
print("\nGame Over.\n")
78-
print(" **** " +turn + " won. ****")
70+
win_message(turn)
7971
break
8072
elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
81-
printBoard(theBoard)
82-
print("\nGame Over.\n")
83-
print(" **** " +turn + " won. ****")
73+
win_message(turn)
8474
break
8575
elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
86-
printBoard(theBoard)
87-
print("\nGame Over.\n")
88-
print(" **** " +turn + " won. ****")
76+
win_message(turn)
8977
break
9078

9179
# If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
9280
if count == 9:
93-
print("\nGame Over.\n")
94-
print("It's a Tie!!")
81+
print("\nGame Over.\nIt's a Tie!!")
9582

9683
# Now we have to change the player after every move.
97-
if turn =='X':
84+
if turn =='X':
9885
turn = 'O'
9986
else:
10087
turn = 'X'
10188

10289
# 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"orrestart=="Y":
90+
restart = input("Do want to play Again?(y/n)")
91+
if restart in ('y', 'Y'):
10592
for key in board_keys:
10693
theBoard[key] = " "
10794

10895
game()
10996

11097
if __name__ == "__main__":
111-
game()
98+
game()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /