In a previous exercise we explored the idea of using a list of lists as a “data structure” to store information about a tic tac toe game. In a tic tac toe game, the “game server” needs to know where the
Os are in the board, to know whether player 1 or player 2 (or whoever is
There has also been an exercise about drawing the actual tic tac toe gameboard using text characters.
The next logical step is to deal with handling user input. When a player (say player 1, who is
X) wants to place an
X on the screen, they can’t just click on a terminal. So we are going to approximate this clicking simply by asking the user for a coordinate of where they want to place their piece.
As a reminder, our tic tac toe game is really a list of lists. The game starts out with an empty game board like this:
The computer asks Player 1 (X) what their move is (in the format
row,col), and say they type
1,3. Then the game would print out
And ask Player 2 for their move, printing an
O in that place.
Things to note:
Xand player 2 (the second player) will always be
Here is one solution that does not fully complete the exercise, but gives a bit of food for thought. The
drawboard function is a general function that can be used to draw the game board with any given inputs.
Another solution uses lots of functions, and also checks whether the game has ended!
This solution adds some bonus functions, like checking for a winner!
Happy hacking!comments powered by Disqus