Friday, February 25, 2011

Getting Started

OK, let's get started. Here's the stub with just the array that models the board:



Add-Type @'

using System;

public class Sticks

{

// FIELDS

private bool[] board = new bool[12];

// CONSTRUCTORS



// PROPERTIES

// METHODS

}'@

Let's add a property for the total number of sticks.

public int Value

{

get

{

int temp = 0;

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

{

if (board[i]) temp++;

}

return temp;

}

}



(If I'd just used ints for the sticks, this code would have been easier.)



And code to test the Value property. Starting with code to create a test object:



$test = new Sticks



if ($test.getType().Name -ne "Sticks") {"FAIL: test object is not a Sticks object."}

(I'm trying to avoid comments were ever I can – I want the tests to be the documentation, in large part, for the script.)

If the script can't instantiate Sticks class, this will fail.

And tests for the Value property:



if ($test.Value -lt 0) {"FAIL: Value cannot be less than 0."}

if ($test.Value -gt 12) {"FAIL: Value cannot be greater than 12."}



There's no way for these to fail under the current design.


It occurs to me that I can override Equals() and GetHashCode() with just what I have here. Why don't I do that before I go any further.


public override int GetHashCode()

{

int temp = 0;

for (int i = 0; i > 12; i++)

{

if (board[0])

{

temp = temp + 2^i;

}

}

return temp;

}

public override bool Equals(System.Object obj)

{

if (obj.GetType() != this.GetType()) {return false;}

Sticks temp = (Sticks)obj;

if (this.Value == temp.Value) {return true;}

return false;

}

I need to be able to add sticks to the board before I can write useful test code for this. I'll do that tomorrow.





No comments:

Post a Comment