reset password
Author Message
rabbott
Posts: 1649
Posted 20:12 Oct 30, 2016 |

I know this will make some of you unhappy, but I wanted to make a Player type and not just use Char as the type. So:

type Player = Maybe Char
e = Nothing  :: Player -- the empty cell
o = Just 'O' :: Player -- the cell occupied by player X
x = Just 'X' :: Player -- the cell occupied by player O

This required a few other changes in the code, but nothing very significant. For example, now

whoseMove :: Game -> Player

whereas formerly 

whoseMove :: Game -> Char

As before, if you want to stay with the version of the code you have been studying, that's fine with me. 

-----------------

What is gained by doing this? Not too much. It is a bit more abstract, and it separates the Player type from how players are represented. It also distinguishes between cells that are occupied (Just <something>) from those that aren't (Nothing).

On the other hand, it is a bit more complex. For example, since we are using safeHead of a list of Players, the result may be Just (Just p), which looks pretty ugly. (This occurs in gameOver.)

Is it worth it? For tutorial purposes I think it is. But, as I said, you can stick with the previous version.
 

 

Last edited by rabbott at 20:23 Oct 30, 2016.