Scala exercise with types and abstraction
Write an API for playing tic-tac-toe. There should be no side-effects (or variables), at all, for real. The move function will take a game state, a Position and it will return a data type that you write yourself.
The following functions (at least) must be supported:
move(as mentioned)whoseTurn(returns which player’s turn it is)whoWon(returns who won or if a draw)playerAt(returns which player, if any, is at a given position)
Importantly, the following must be true:
- It is a compile-time error to call
moveorwhoseTurnon a game that has been completed - It is a compile-time error to call
whoWonon a game that has not been completed - The
playerAtfunction must be supported on both complete and in-play games
Good luck and may the types be with you.
January 9th, 2011 at 10:34 am
[...] a data type that you write yourself. The following functions (at least) must be supported:… [full post] Tony Morris λ Tony’s blog λ programming 0 0 0 0 [...]
January 9th, 2011 at 10:53 am
[...] λ Tony’s blog λ » Blog Archive » Scala exercise with types and abstraction blog.tmorris.net/scala-exercise-with-types-and-abstraction/ – view page – cached Write an API for playing tic-tac-toe. There should be no side-effects, at all, for real. The move function will take a game state, a Position and it will return a data type that you write yourself. [...]
January 10th, 2011 at 3:48 am
Hi, here is my solution (with small extras, so you can also play it):
https://gist.github.com/771842
What do you think about it?
January 17th, 2011 at 11:37 am
[...] recently gave a beginner API design quiz. I use it in programming classes that I teach, where many people struggle with it. When these [...]
March 15th, 2011 at 4:27 am
Angel, nice example.
For Tony, could you do an experiment with a toy language. “Write a toy language that has the following rules…”
May 26th, 2011 at 5:38 am
[...] read the delightful article on a type safe tic-tac-toe API recently. It got me thinking how one might do this for an application with a complex GUI and data [...]