Chapter 15. A Simple Game

This example involves refactoring and test-driven design.[1]

[1] The source code for this example is at http://github.com/kevinrutherford/rrwb-code.

Suppose we’ve decided to develop an application to play games in the tic-tac-toe family: squares occupied by different markers. In tic-tac-toe you have a 3 × 3 grid, and you try to put your mark in three boxes in a row. In Connect Four by Hasbro you have a rectangular grid and try to get four boxes in a row, but columns have to be filled from bottom to top. We’ll start with a simplified version of tic-tac-toe and work our way up to the general case.

Code

Here are some tests and the first version of the code:

Notice that the winner method is simplified: You win by ...

Get Refactoring in Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.