Tuesday, April 5, 2011

A Fall Method

I want to do some clean-up work – updating the documentation, looking at the tests, and such.

But first, let's create gravity!

Down will be toward the bottom of the screen. If you enter a position number with a stick into a new “fall” method, the stick will move, as much as it can, down the board.

Actually, let's make it two fall routines: fall (local on a node) and drop (recursive fall).

SIDE TRIP:

Some of the methods in Node require you to pass the board into the method. This seems like unneeded effort. I'm going to refactor things a little:

New field: private Board board;

Since Board is a reference type, this is automatically a pointer.

Add this to the constructor: board = currentBoard;

And change the Board constructor:

for (int i = 0; i < 36; i++)

{

nodes[i] = new Node(i, this);

}

And fix this method call, as such:

if (x.doMove(origin, destination)) return true;


OK, this compiles. Testing is showing a lot of problems, though. One of the things I did in testing was to test nodes and sticks individually, without creating a board. This is no longer valid.

That was easy. I just added this line to the CreateNodeTestArray function:

$testNodeTestArrayBoard = new Board(60)

And changed the line that creates each node as follows:

new Node($a, $testNodeTestArrayBoard)

And that's it.


END OF SIDE TRIP

So, the fall method. Here's my psuedocode:


public bool fall(int location)

{

// Does this position belong to this node?

if (!(currentNode(location))) return false;

// Does this position have a stick?

if (!isFilled(location, board)) return false;

// Is this at position 2

if (position[2] == location) return false; // when no move takes place, return false. True means more movement possible.

// Is this at position 0?

// Is this at position 1?

// Is this at position 3?

}


So, if a stick is in position 0, it can fall to position 1 or 3 (probably will fall to 1, if it can.) A stick in position 1 or 3 can fall to 2.

OK, I just spend twenty minutes filling it out. One thing that it includes is a recursive call to fall to allow the stick to fall from position 0 to position 3.

Does it work?

I'll write tests tomorrow.

No comments:

Post a Comment