I spent some extra time this morning thinking about the representation of the board. One idea that I had was to try to write class that can be modified in obvious ways later – made larger without changing much code.
If I specify the moves directly, this won't work.
Still, I don't want to get hung up on finding the perfect design when there so much else to do, so I'm just going to push ahead.
A move specifies a pair of stick-positions. In order for a move to be valid, two things have to be true:
- The first position must be occupied.
- The second position must be open.
- The two positions must share a center point and be 90 degrees different in orientation.
A method that checks for the first two is simple:
private bool validContent(int origin, int destination)
{
return (board[origin] && !(board[destination]));
}
Specifying the positions is a little harder. Well, not harder – just longer and more complicated.
private bool validPosition(int origin, int destination)
{
if ((origin == 0) && (destination == 2)) return true;
if ((origin == 0) && (destination == 3)) return true;
if ((origin == 1) && (destination == 3)) return true;
…
if ((origin == 11) && (destination == 8)) return true;
if ((origin == 11) && (destination == 9)) return true;
return false;
}
What? I know it's ugly. It should work. Stop bugging me about how ugly it is.
OK, so this is the code for the actual move:
public bool moveStick(int origin, int destination)
{
if (origin <>
if (origin > 11) return false;
if (destination <>
if (destination > 11) return false;
if (!(validContent(origin, destination))) return false;
if (!(validPosition(origin, destination))) return false;
board[origin] = false;
board[destination] = true;
return true;
}
(I slipped in a remove stick method, while you weren't looking.)
OK, next up, writing the tests for the methods.
$testRemove = new Sticks
if ($testRemove.getType().Name -ne "Sticks") {"FAIL: test object is not a Sticks object."}
if ($testRemove.removeStick(4)) {"FAIL: No sticks to remove."}
if ($testRemove.removeStick(-1)) {"FAIL: Outside board."}
if ($testRemove.removeStick(12)) {"FAIL: Outside board."}
if (!($testRemove.addStick(4))) {"FAIL: Should be able to add stick to position 4."}
if (!($testRemove.removeStick(4))) {"FAIL: Should be able to remove stick from position 4."}
$testMove = new Sticks;
if ($testMove.getType().Name -ne "Sticks") {"FAIL: test object is not a Sticks object."}
if (!($testMove.addStick(0))) {"FAIL: Should be able to add stick to position 0."}
if (!($testMove.addStick(2))) {"FAIL: Should be able to add stick to position 2."}
if (!($testMove.addStick(10))) {"FAIL: Should be able to add stick to position 10."}
if (!($testMove.moveStick(2, 5))) {"FAIL: Should be able to move stick from 2 to 5."}
if (!($testMove.moveStick(0, 2))) {"FAIL: Should be able to move stick from 0 to 2."}
if ($testMove.moveStick(2, 9)) {"FAIL: Should not be able to move stick from 2 to 9."}
if ($testMove.moveStick(2, 5)) {"FAIL: Should not be able to move stick from 2 to 5 (5 is occupied)."}
I think this is all the basic cases. Of course, maybe I should test each possible move. That might be testing overkill, if there is such a thing.
No comments:
Post a Comment