So, yesterday afternoon, I was looking at the blog again and trying to figure out where to go next with the fall method.
I realized why the approach was not working as expected. The calling code in the Board class uses a foreach loop to go through all the nodes looking for the right one to address to do whatever it's doing. For a fall, that means that – until it gets a true returned – it will check the other nodes, starting with node 7, for drop possibilities.
The thing that I realized next was that my expectations were wrong with respect to my design. Here's the board that I was getting from adding and dropping stick 6:
Starting board:
o o o o o o
o-o o o o o
|
o o o o o o
o o o o o o
o o o o o o
o o o o o o
Actual:
o o o o o o
o o o o o o
| |
o o o o o o
o o o o o o
o o o o o o
o o o o o o
Expected:
o o o o o o
o_o o o o o
|
o o o o o o
o o o o o o
o o o o o o
o o o o o o
What's wrong with this?
The expected result has a higher energy level than the actual result. The stick actually falls one space in the actual result. It can't fall in the expected result. The expected result requires a fall with respect to a single node.
This is not what I wanted from the fall. The connection of a stick to a node is incidental and can change at any time.
I was saying in my code comments that the fall method in the Node class seemed too complicated. That's because it was. It tried to do too much, even if it was a simple as it could be to do what it was doing.
So, I'm going to scrap the current tests and modify the fall method to cause the specified stick to fall one space – from 0 to 1 or 3, from 1 to 2, or from 3 to 2. I'll pass the location by reference so the output can be used to cause the stick to fall farther.
Here's my code:
/*
Implements the fall. If the stick can fall, it falls a single level down:
from position 0 to position 1 or 3, from position 1 to position 2, or from
position 3 to position 2.
Returns a value of false if the stick cannot fall. Otherwise, returns a value of true.
location: Location of stick to fall. Passed by reference to pass back current
location of stick for future falls.
returns: True if stick can fall. Otherwise, false.
*/
public bool fall(ref int location)
{
// Does this location belong to this node?
if (!(currentNode(location))) return false;
// Does this location have a stick?
if (!isFilled(location, board)) return false;
switch (nodePosition(location))
{
case 0:
// Can it fall to position 1?
if (!isFilled(position[1], board))
{
doMove(position[0], position[1]);
location = position[1]; // set location to the new position
return true;
}
// Can it fall to position 3?
else if (!isFilled(position[3], board))
{
doMove(position[0], position[3]);
location = position[3]; // set location to the new position
return true;
}
break;
case 1:
if (!isFilled(position[2], board))
{
doMove(position[1], position[2]);
location = position[2]; //// set location to the new position
return true;
}
break;
case 2:
break;
case 3:
if (!isFilled(position[2], board))
{
doMove(position[3], position[2]);
location = position[2]; //// set location to the new position
return true;
}
break;
default:
break;
}
return false;
}
Does it work? Well, it compiles. My tests shouldn't work for it, but let's run them anyway, see if they are salvageable.
I went back through and reset the expected values to the new expected values. Passes all tests.
So, what about doing a “drop all the way” method?
Here's my first run at it:
public bool dropStickRecurse(int location)
{
bool temp = true;
if (!(isValid(location))) return false;
while (temp) {
temp = false;
foreach (Node x in nodes)
{
if (x.fall(ref location))
{
temp = true;
break;
}
}
}
return false;
}
This code is adapted from the dropStick. I've turned that into testing code (now test_dropStick) because it doesn't expose behavior that I want to be part of the design.
And – after changing the tests to call test_dropStick, all the tests run as before.
I'm going to finish today by doing a sanity check on the new method – seem if it seems to be working.
blog> $x = new Board(60)
blog> $x.addStick(3)
True
blog> display $x
o o o-o o o
o o o o o o
o o o o o o
o o o o o o
o o o o o o
o o o o o o
blog> $x | get-member
TypeName: Board
Name MemberType Definition
---- ---------- ----------
addStick Method bool addStick(int location)
dropStickRecurse Method bool dropStickRecurse(int location)
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
getPositionValue Method bool getPositionValue(int location)
getStick Method Stick getStick(int location)
GetType Method type GetType()
moveStick Method bool moveStick(int origin, int destination)
removeStick Method bool removeStick(int location)
test_dropStick Method bool test_dropStick(int location)
test_getNode Method Node test_getNode(int loc)
test_getStick Method Stick test_getStick(int loc)
ToString Method string ToString()
Value Property System.Int32 Value {get;}
I forgot what I called the method, so I used get-member to look it up instead of digging through the code. Isn't PowerShell nifty?
blog> $x.dropStickRecurse(3)
False
blog> display $x
o o o o o o
o o o o o o
o o o o o o
o o o o o o
o o o o o o
o o o-o o o
Well, that is what I expected. It fell right down from 3 to 58, presumably through 8, 14, 19, 25, 30, 36, 41, 47, and 52.
Next time: tests for the new dropStickRecurse method.
Oh, and the method should return true if a stick moves and false if it doesn't. I think I can fix that next time.Have a good weekend!
No comments:
Post a Comment