CPMSE Worked Examples
What is the problem asking us to do?
Imagine a tic-tac-toe game where each player has to enter the row and column of the board where she/he wants to play. The first turn would look like:


Now, the second player turn would look like...
What data structure should we use to store the board?
A matrix seems to be a good approach. Now, we need to create three methods: winnner, board and twoplayer.
-
winner: This method will receive a board as parameter. It will check all the possible ways a game can be won by a player. Then it will return the winner identifier in case it exists, or a predefined code in case no one has won yet.
-
board: This method should simply display the board to the user. We already chose a matrix to store the board. Now we need to go through it and print it.
-
twoplayer: This method will keep the game running until there is a winner or all the possibilities are over. Once this event happens, this method will display the winner.
And so on.