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.?