CPMSE Worked Examples
Let's start by the "winner" method...
How do I know if there is a tic-tac-toe winner?

We need to check if three values are equal in the following configurations:
-
row=col (e.g., 1,1; 2,2; 3,3)
-
row fixed (e.g, 1,1; 1,2; 1, 3)
-
col fixed (e.g, 1,2; 2,2; 3, 2)
These are the conditions we need to check for in the method "winner"
Now, the method "board"...
We just neet to print the board matrix. However, it will depend on the values you have stored to represent each possible value in the matrix (i.e., X, O, or an empty spot).
Remember that a matrix in MATLAB is usually numeric, so it might be a good idea to use number to represent each of those values.
How about X=1, O=-1 and Empty=0? How would you print it then?
Finally, let's think about the twoplayer method...
This method needs to perform two tasks: (1) Keep the game going; and (2) Announce the winner.
(1) It seems as if we need to keep something going until certain condition is satisfied. What are the conditions you need to keep playing?
a. No one has won yet. We have a method to know this, remember? (hint: winner)
b. There are still places to play in. Since there are nine spots, we could use counter to know how many sports are still available.
What should we do if the condition has not been satisfied yet? Ask to the next player to play!.
So, we ask the player to enter row and column values to make her/his move.
Now we display the current status of the board (using the method "board") and go back to ask for the conditions in (1). This looks more and more like a loop!.
(2) Once the game is over, we just need to invoke the method winner again to know who won or if there is a tie!!
This is only one approach to solve this problem, can you think on something else?