Wednesday, March 23, 2011

The Board Class

I did a Stick class, so the next thing to do is create a board. That is, the area where you can lay down the sticks. This is the class called Sticks in the preliminary version. I think Board is a better name for the class.


Here's my first error of the day:

The name 'board' does not exist in the current context

Since I started with the old code and am renaming the class, I have to follow up on some re-factoring stuff.

Some design thoughts:

The sticks are dealt with as a 1-based array, not 0-based. The user deals with the setup only through the numbers assigned to the sticks. I decided early on to exclude 0. Up until now, that hasn't had much of an effect, but it could be a place to trip up later. I decided to add a position field to the Stick class. Here's the code around that change:

private int location = 0;

// CONSTRUCTORS

public Stick (int temp)

{

location = temp;

}

// PROPERTIES

public int Location

{

get {

return location;

}

}

I also had to make a change to the testing code since the class no longer has a default constructor.

Here's the testing code for the Location property:

$testStick = new Stick (1)


if ($testStick.Location -ne 1) {"FAIL: Location should be 1."}


try {

$testStick.Location = 12

{"FAIL: Location should be a read-only property."}

}

catch {

}

OK, this should be enough to create a board. How can I test whether it works?

I started by putting in some testing code to let me examine the objects (nodes and sticks) associated with the board:

// TESTING CODE

public Stick getStick(int loc)

{

return grid[loc];

}

public Node getNode(int loc)

{

return node[loc];

}


Let's write some tests:

$testBoard = new Board(60)


try {

$b = $testBoard.getNode(-1)

"FAIL: -1 is not a valid value."

}

catch { }



for ($a = 0; $a -lt 36; $a++) {

try {

$b = $testBoard.getNode($a)

}

catch { "FAIL: Node missing from board."}

}



try {

$b = $testBoard.getNode(36)

"FAIL: 36 should not be a valid value."

}

catch { }



try {

$b = $testBoard.getStick(-1)

"FAIL: -1 is not a valid value."

}

catch { }



for ($a = 0; $a -lt 60; $a++) {

try {

$b = $testBoard.getStick($a)

}

catch { "FAIL: Stick missing from board."}

}



try {

$b = $testBoard.getStick(60)

"FAIL: 60 should not be a valid value."

}

catch { }

These test whether the C# throws an error when you ask for something outside the array.

More tests tomorrow.


No comments:

Post a Comment