The Classic Tic-Tac-Toe Game in Python 3

The Classic Tic-Tac-Toe Game in Python 3

Image for post

Hello There My Gorgeous Friends On The Internet!

So, the best and the most fun way to learn any programming language for me has always been by developing a fun project like a simple game or some project that I would use in my daily life.

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.

What we are going to do?

We are going to build a two-player tic-tac-toe game, which we can play in the command-line. Initially, we?ll make an empty game board and then we?ll take inputs from the players and we?ll check for the winning condition and if the whole board gets filled and no one wins, we?ll declare the result as ?Tie? and ask users if they want to restart the game.

What will we use?

We will build this game using Python 3, so make sure you have it installed on your laptop/computer and we are good to go.

What we?ll learn?

After building this game, we can get a pretty clear idea about dictionaries in python, how to access dictionaries, how to iterate over dictionaries, for loop, if-else conditions and functions in python.

How does the game work?

The board is numbered like the keyboard?s number pad. And thus, a player can make their move in the game board by entering the number from the keyboard number pad.

Image for postThe board is numbered like the keyboard?s number pad.

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.

Initially our game board will look like this: | |-+-+- | |-+-+- | |

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.

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?

And now, we?ll ask the players if they want to play again.

And boom!! Now we have our game ready.??

The Full Code:

Play Time:

| |-+-+- | |-+-+- | |It’s your turn,X.Move to which place?7X| |-+-+- | |-+-+- | |It’s your turn,O.Move to which place?9X| |O-+-+- | |-+-+- | |It’s your turn,X.Move to which place?5X| |O-+-+- |X|-+-+- | |It’s your turn,O.Move to which place?3X| |O-+-+- |X|-+-+- | |OIt’s your turn,X.Move to which place?6X| |O-+-+- |X|X-+-+- | |OIt’s your turn,O.Move to which place?1X| |O-+-+- |X|X-+-+-O| |OIt’s your turn,X.Move to which place?4X| |O-+-+-X|X|X-+-+-O| |OGame Over.**** X won. ****

Thanks For Scrolling, I hope you liked it. Hit me up with your views and suggestions in the comment down below.?

1

No Responses

Write a response