Skip to content

Tic Tac Toe Assignment

mattcallanan edited this page Apr 25, 2012 · 3 revisions

This assignment comes from Tony Morris:

Other links:


TicTacToe

Write an API for the tic-tac-toe game. Do not use variables -- they are not permitted. This includes libraries that expose in-line updates. No exceptions (or non-termination) in exposed functions -- all functions return a consistent value for every element of their domain. The follow API methods should exist:

  • move: takes a tic-tac-toe board and position and moves to that position (if not occupied) returning a new board. This function can only be called on a board that is in-play. Calling move on a game board that is finished is a compile-time type error.

  • whoWon: takes a tic-tac-toe board and returns the player that won the game (or a draw if neither). This function can only be called on a board that is finished. Calling move on a game board that is in-play is a compile-time type error.

  • takeBack: takes either a finished board or a board in-play that has had at least one move and returns a board in-play. It is a compile-time type error to call this function on an empty board.

  • playerAt: takes a tic-tac-toe board and position and returns the (possible) player at a given position. This function works on any type of board.

  • Other API functions that you may see fit. These can be determined by also writing an interactive console application that uses the API -- other useful functions are likely to arise.

You should write automated tests for your API. For example, the following universally quantified property holds true:

forall Board b. forall Position p. such that (not (positionIsOccupied p b)). takeBack(move(p, b)) == b

You should encode this property in an automated specification test. For Scala, use ScalaCheck. For Haskell, QuickCheck. For Java, consider Functional Java. For other languages such as C# or F#, you may need to search around.

Haskell-specific

If you choose to use Haskell, also take advantage of its superior tooling:

Extra-curricular

  • Write an opponent that never loses
  • Write an opponent with easy, medium, hard difficulty levels

The Answer

https://github.com/tonymorris/course-answers/tree/master/src/TicTacToe/haskell

Clone this wiki locally