This exercise is Part 3 of 4 of the Tic Tac Toe exercise series. The other exercises are: Part 1, Part 2, and Part 4.
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 X
s and O
s are in the board, to know whether player 1 or player 2 (or whoever is X
and O
won).
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:
X
and player 2 (the second player) will always be O
.Bonus:
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!