Back to 'score'
Checking our todo list, we see that the score method is still
pending. Let's reenable that test (and include our refactored
configure_score method).
# file: testnet.rb
...
def test_score_simple_board
configure_score
assert_equal 1, @net.score(:BLACK)
end
...
|
As expected, the tests report that we returned a 0, but we want a 1.
Its time to implement score for real. It seems obvious that we need
to iterate over each of the locations on the board and check for a
score. Here's the code ...
# file: net.rb
...
def score(player)
(1..MAXSIZE).each { |x|
(1..MAXSIZE).each { |y|
return 1 if score_at(x, y, player) > 0
}
}
end
...
|
Our tests give us a surprising result ...
| | | ...
Failure occurred in test_score_empty_board(TestNet) [testnet.rb:49]: Expected <0> but was <1..5>
...
|
It seems we are returning a range for the value of the score. Oops,
we forget to have score return a zero for the fall-through
condition. Fixing that gives us ...
# file: net.rb
...
def score(player)
(1..MAXSIZE).each { |x|
(1..MAXSIZE).each { |y|
return 1 if score_at(x, y, player) > 0
}
}
0
end
...
|
And the tests all pass again.