Professional Documents
Culture Documents
Tic Tac Toe
Tic Tac Toe
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 1/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
So, when I started to learn Python, I started with this No Starch Press published book
“Automate The Boring Stuff With Python” which is just awesome and If you are looking
for a book to get started learning python, I would recommend you to go through this
book. Its very beginner-friendly and it covers almost all the basic topics of python. So,
while solving the exercises in this book, I came across this TicTacToe game
implementation in python.
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 2/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
Code Time💻
First, let’s see how we are going to use a dictionary to create our game board. A
dictionary is a primitive data type in python which stores data in “key: value” format.
and thus, we’ll create a dictionary of length 9 and each key will represent a block in the
board and its corresponding value will represent the move made by a player. and we’ll
create a function printBoard() which we can use every time we want to print the
updated board in the game.
1 theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,
2 '4': ' ' , '5': ' ' , '6': ' ' ,
3 '1': ' ' , '2': ' ' , '3': ' ' }
4
5 def printBoard(board):
6 print(board['7'] + '|' + board['8'] + '|' + board['9'])
7 print('-+-+-')
8 print(board['4'] + '|' + board['5'] + '|' + board['6'])
9 print('-+-+-')
10 print(board['1'] + '|' + board['2'] + '|' + board['3'])
| |
-+-+-
| |
-+-+-
| |
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 3/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
Now, in the main function, we’ll first take the input from the player and check if the
input is a valid move or not. If the block that player requests to move to is valid, we’ll fill
that block else we’ll ask the user to choose another block.
1 def game():
2
3 turn = 'X'
4 count = 0
5
6 for i in range(10):
7 printBoard(theBoard)
8 print("It's your turn," + turn + ".Move to which place?")
9
10 move = input()
11
12 if theBoard[move] == ' ':
13 theBoard[move] = turn
14 count += 1
15 else:
16 print("That place is already filled.\nMove to which place?")
17 continue
Now, to check the winning condition, we’ll check a total of 8 conditions and whichever
player has made the last move, we’ll declare that player as a winner. And if no one wins,
we’ll declare ‘tie’
1 # Now we will check if player X or O has won,for every move after 5 moves.
2 if count >= 5:
3 if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top
4 printBoard(theBoard)
5 print("\nGame Over.\n")
6 print(" **** " +turn + " won. ****")
7 break
8 elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
9 printBoard(theBoard)
10 print("\nGame Over.\n")
11 print(" **** " +turn + " won. ****")
12 break
13 elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the bottom
14 printBoard(theBoard)
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 4/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
15 print("\nGame Over.\n")
16 print(" **** " +turn + " won. ****")
17 break
18 elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the left side
19 printBoard(theBoard)
20 print("\nGame Over.\n")
21 print(" **** " +turn + " won. ****")
22 break
23 elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the middle
24 printBoard(theBoard)
25 print("\nGame Over.\n")
26 print(" **** " +turn + " won. ****")
27 break
28 elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right side
29 printBoard(theBoard)
30 print("\nGame Over.\n")
31 print(" **** " +turn + " won. ****")
32 break
33 elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
34 printBoard(theBoard)
35 print("\nGame Over.\n")
36 print(" **** " +turn + " won. ****")
37 break
38 elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
39 printBoard(theBoard)
40 print("\nGame Over.\n")
41 print(" **** " +turn + " won. ****")
42 break
43
44 # If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
45 if count == 9:
46 print("\nGame Over.\n")
47 print("It's a Tie!!")
48
49 # we have to change the player after every move.
50 if turn =='X':
51 turn = 'O'
52 else:
53 turn = 'X'
And now, we’ll ask the players if they want to play again.
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 5/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
1 board_keys = []
2
3 for key in theBoard:
4 board_keys.append(key)
5
6 restart = input("Do want to play Again?(y/n)")
7
8 if restart == "y" or restart == "Y":
9 for key in board_keys:
10 theBoard[key] = " "
11
12 game()
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 8/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
Play Time:
| |
-+-+-
| |
-+-+-
| |
It's your turn,X.Move to which place?
7
X| |
-+-+-
| |
-+-+-
| |
It's your turn,O.Move to which place?
9
X| |O
-+-+-
| |
-+-+-
| |
It's your turn,X.Move to which place?
5
X| |O
-+-+-
|X|
-+-+-
| |
It's your turn,O.Move to which place?
3
X| |O
-+-+-
|X|
-+-+-
| |O
It's your turn,X.Move to which place?
6
X| |O
-+-+-
|X|X
-+-+-
| |O
It's your turn,O.Move to which place?
1
X| |O
-+-+-
|X|X
-+-+-
O| |O
It's your turn,X.Move to which place?
4
X| |O
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 9/10
2/28/2021 The Classic Tic-Tac-Toe Game in Python 3 | by James Shah | Byte Tales | Medium
-+-+-
X|X|X
-+-+-
O| |O
Game Over.
Thanks For Scrolling, I hope you liked it. Hit me up with your views and suggestions in
the comment down below.🙌
https://medium.com/byte-tales/the-classic-tic-tac-toe-game-in-python-3-1427c68b8874 10/10