[ next ] [ prev ] [ contents ] XP-Cinti TDD Workshop

Code for Story Three

Ok, we're done. Well ... almost. I just noticed that the instance variable in the Net class is called @tied. That seems inappropriate now. Let's rename it to @board. We do the change and run the tests (and they all pass!).

That gives us the final result here.

Test Run

  
$ ruby testnet.rb
Loaded suite testnet
Started
.........
Finished in 0.012476 seconds.
8 tests, 33 assertions, 0 failures, 0 errors

Unit Tests

# file: testnet.rb
require 'test/unit'
require 'net'

class TestNet < Test::Unit::TestCase
  def setup
    @net = Net.new
  end

  def test_initially_empty
    assert_equal :EMPTY, @net[1,1]
  end

  def test_place_token
    @net[1,1] = :BLACK
    assert_equal :BLACK, @net[1,1]
    @net[1,1] = :WHITE
    assert_equal :WHITE, @net[1,1]
    @net[1,1] = :EMPTY
    assert_equal :EMPTY, @net[1,1]
  end

  def test_multiple_positions
    @net[1,1] = :BLACK
    assert_equal :BLACK, @net[1,1]
    assert_equal :EMPTY, @net[1,2]
    assert_equal :EMPTY, @net[2,1]
    assert_equal :EMPTY, @net[2,2]
  end

  def test_max_matrix_size
    check_location(1,1)
    check_location(1,5)
    check_location(5,1)
    check_location(5,5)
  end

  def check_location(x,y)
    assert_equal :EMPTY, @net[x,y]
    @net[x,y] = :BLACK
    assert_equal :BLACK, @net[x,y]
    @net[x,y] = :WHITE
    assert_equal :WHITE, @net[x,y]
    @net[x,y] = :EMPTY
    assert_equal :EMPTY, @net[x,y]
  end

  def test_score_empty_board
    assert_equal 0, @net.score(:BLACK)
    assert_equal 0, @net.score(:WHITE)
  end

  def test_score_simple_board
    configure_score_at(2, 2, :BLACK)
    assert_equal 1, @net.score(:BLACK)
  end

  def test_score_at
    assert_equal 0, @net.score_at(2,2,:BLACK)
    configure_score_at(2, 2, :BLACK)
    assert_equal 0, @net.score_at(2,2,:WHITE)
    assert_equal 1, @net.score_at(2,2,:BLACK)
    assert_equal 0, @net.score_at(1,1,:BLACK)
    assert_equal 0, @net.score_at(3,4,:BLACK)
  end

  def test_score_at_boundary
    @net[1,2] = :BLACK
    @net[2,1] = :BLACK
    @net[1,5] = :BLACK
    @net[5,1] = :BLACK
    assert_equal 0, @net.score_at(1,1,:BLACK)
  end

  def configure_score_at(x, y, player)
    @net[x-1,y] = player
    @net[x+1,y] = player
    @net[x,y-1] = player
    @net[x,y+1] = player
  end

end

Net Class

# file: net.rb
class Net
  MAXSIZE=5

  def initialize
    @board = (1..MAXSIZE).collect {
      (1..MAXSIZE).collect {:EMPTY}
    }
  end

  def [](x,y)
    if x<1 || y<1
      nil
    else
      @board[x-1][y-1]
    end
  end

  def []=(x, y, value)
    @board[x-1][y-1] = value
  end

  def score(player)
    (1..MAXSIZE).each { |x|
      (1..MAXSIZE).each { |y|
	return 1 if score_at(x, y, player) > 0
      }
    }
    0
  end

  def score_at(x, y, player)
    surrounded?(x, y, player) ? 1 : 0
  end

  def surrounded?(x, y, player)
    self[x-1, y] == player &&
      self[x+1, y] == player &&
      self[x, y-1] == player &&
      self[x, y+1] == player
  end
end


[ next ] [ prev ] [ contents ] Copyright 2003 by Jim Weirich.
Some Rights Reserved