Friday, March 25, 2011

The Revised moveStick Method

When I sat down to it today, I quickly wrote the removeStick method. It didn't require anything that I hadn't done for the addStick method. I was able to modify the addStick testing code to work for removeStick.

So, what about moveStick? That is the last big piece outstanding.

This time, though, I'm going to write the tests first.

Looking back at my notes, here are some valid possible moves:

1 to 6

4 to 9

9 to 3

40 to 46

46 to 51

51 to 45

45 to 40


Here are some invalid moves:

3 to 4

45 to 46

1 to 42

1 to 0

55 to 50

60 to 66


Here's the testing code I wrote:


# Testing Board moveStick Method


# Test valid moves:


$TestMoveStick = new Board(60) # reset board.


if (!($TestMoveStick.addStick(1))) {"FAIL: Position 1 is valid for a stick to be added."}

if (!($TestMoveStick.moveStick(1, 6))) {"FAIL: Move 1 to 6 should be valid."}


if (!($TestMoveStick.addStick(4))) {"FAIL: Position 4 is valid for a stick to be added."}


if (!($TestMoveStick.moveStick(4, 9))) {"FAIL: Move 4 to 9 should be valid."}

if (!($TestMoveStick.moveStick(9, 3))) {"FAIL: Move 3 to 3 should be valid."}


if (!($TestMoveStick.addStick(40))) {"FAIL: Position 40 is valid for a stick to be added."}


if (!($TestMoveStick.moveStick(40, 46))) {"FAIL: Move 40 to 46 should be valid."}

if (!($TestMoveStick.moveStick(46, 51))) {"FAIL: Move 46 to 51 should be valid."}

if (!($TestMoveStick.moveStick(51, 45))) {"FAIL: Move 51 to 45 should be valid."}

if (!($TestMoveStick.moveStick(45, 40))) {"FAIL: Move 45 to 40 should be valid."}


# Test invalid moves


$TestMoveStick = new Board(60) # reset board. I don't want the tests tripping over each other.


if (!($TestMoveStick.addStick(3))) {"FAIL: Position 3 is valid for a stick to be added."}

if (!($TestMoveStick.addStick(45))) {"FAIL: Position 45 is valid for a stick to be added."}

if (!($TestMoveStick.addStick(1))) {"FAIL: Position 1 is valid for a stick to be added."}

if (!($TestMoveStick.addStick(55))) {"FAIL: Position 55 is valid for a stick to be added."}

if (!($TestMoveStick.addStick(60))) {"FAIL: Position 60 is valid for a stick to be added."}


if ($TestMoveStick.moveStick(3, 4)) {"FAIL: Move 3 to 4 should not be valid."}

if ($TestMoveStick.moveStick(45, 46)) {"FAIL: Move 45 to 46 should not be valid."}

if ($TestMoveStick.moveStick(1, 42)) {"FAIL: Move 1 to 42 should not be valid."}

if ($TestMoveStick.moveStick(1, 0)) {"FAIL: Move 1 to 0 should not be valid."}

if ($TestMoveStick.moveStick(55, 50)) {"FAIL: Move 55 to 50 should not be valid."}

if ($TestMoveStick.moveStick(60, 66)) {"FAIL: Move 60 to 66 should not be valid."}


# Test moves for having the sticks in the right place before and after move.

# This section works on these sticks: 21 27 32 26


$TestMoveStick = new Board(60) # reset the board


if ($TestMoveStick.moveStick(21, 27)) {"FAIL: Move 21 to 27 should not be valid because 21 is empty."}


if (!($TestMoveStick.addStick(21))) {"FAIL: Position 21 is valid for a stick to be added."}

if (!($TestMoveStick.addStick(27))) {"FAIL: Position 27 is valid for a stick to be added."}


if ($TestMoveStick.moveStick(21, 27)) {"FAIL: Move 21 to 27 should not be valid because 27 is filled."}


if (!($TestMoveStick.removeStick(27))) {"FAIL: Position 27 is valid for a stick to be removed."}

if (!($TestMoveStick.moveStick(21, 27))) {"FAIL: Move 21 to 27 should be valid."}

if (!($TestMoveStick.removeStick(27))) {"FAIL: Position 27 is valid for a stick to be removed."}



if (!($TestMoveStick.addStick(21))) {"FAIL: Position 21 is valid for a stick to be added."}


if (!($TestMoveStick.moveStick(21, 27))) {"FAIL: Move 21 to 27 should be valid."}

if (!($TestMoveStick.moveStick(27, 32))) {"FAIL: Move 27 to 32 should be valid."}

if (!($TestMoveStick.moveStick(32, 26))) {"FAIL: Move 32 to 26 should be valid."}

if (!($TestMoveStick.moveStick(26, 21))) {"FAIL: Move 26 to 21 should be valid."}

#

#

# END Testing Board moveStick Method

OK, after a couple of hours of coding, I got the tests to run. The thing that held me up was that I got a couple of tests wrong for the removeStick set. I should create an output method that lets me see the whole board. Things would have gone faster if I could have done a reality check on the tests as I went.

Maybe I'll do that on Monday.




No comments:

Post a Comment