Friday, February 25, 2011

Add a Stick

Time to Add Sticks –

This method allows you to add one stick at a time to the board.


public bool addStick(int position)

{

if (position < 0) return false;

if (position > 11) return false;

if (board[position]) return false;

board[position] = true;

return true;

}


There are three conditions.


  • You can't add a stick to a position less than 0.
  • You can't add a stick to a position greater than 11.
  • You can't add a stick to a position that already has a stick.


In each of these three cases, the method returns FALSE, but doesn't change anything.


Coding this was where I was getting a little uncomfortable with my design. After all, if I want to make a bigger board, I have to deal with a lot of hard-coded values.


But, for now, I'm going to charge ahead.


I wrote tests for the three conditions.


$testAdd = new Sticks


if ($testAdd.addStick(-1)) {"FAIL: Should not be able to add stick to position -1."}

if ($testAdd.addStick(12)) {"FAIL: Should not be able to add stick to position 12."}


if !($testAdd.addStick(9)) {"FAIL: Should be able to add stick to position 9."}

if ($testAdd.addStick(9)) {"FAIL: Should not be able to add stick to position 9 (which is already filled)."}


One of the other features of the problem, as I described it, was to have a maximum number of sticks in the board. I could have implemented this before, but there was no way for that to have an effect before I had an addStick method.


Here's some code:


private int max;

// CONSTRUCTORS

public Sticks(int maximum)

{

max = maximum;

}

public Sticks() : this(12)

{

}


There's a private field for this and (since the number of sticks is established at instantiation) I need to define the constructor (including a default constructor, so I don't have to throw away my existing testing code.)


Oh, and here's the code added to addStick to prevent an additional stick from being added.



if (this.Value == max) return false;


New testing code:


$testMax = new Sticks(4);


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


if (!($testMax.addStick(0))) {"FAIL: Should be able to add stick to position 0."}

if (!($testMax.addStick(1))) {"FAIL: Should be able to add stick to position 1."}

if (!($testMax.addStick(2))) {"FAIL: Should be able to add stick to position 2."}

if (!($testMax.addStick(3))) {"FAIL: Should be able to add stick to position 3."}

if ($testMax.addStick(4)) {"FAIL: Should not be able to add fifth stick."}


And it seems to work.


Well, the next big hurdle is moving sticks. We'll start that tomorrow.

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.





Modeling Movement

OK, so I was going back and forth about how to implement the sticks.

In the pilot version of the program, I just had an array of 0 to 11 bools. Not so bad, until I tried to implement moving. Here's a valid move:

._ _
|_ _|
._|_


._
|_|_|
._|_

I model this as 12 numbered positions:

01
234
56

789

JK

(J = 10, K = 11 -- A and B are already spoken for, see below.)

Here, stick 1 moves to position 3.

I was starting to implement this procedurally, but it turned into an ugly set of if-thens pretty quick, so I thought, how about a class with the bool for the stick and then the corners that the sticks belong to:

A B C
D E F

G H I

So, 0 and 2 belong to A, 0, 1 and 3 belong to B, 1 and 4 belong to C. The values for the corners would be calculated when a stick was added or moved and then referred to.

The problem with this is that it maintains, as a property, something that changes and something that depends only on outside info.

So, I think I might have been right with my original approach -- which corner a stick belong to is a fact about the board, not the sticks. What I need is a member of Sticks that does the look-up in a clear way.

Unfortunately, I don't see how to make something that isn't kind of ugly. Here are my notes:

0 2 A
0 1 3 B

1 4 C

2 5 7 D

3 5 6 8 E

4 6 9 F

7 10 G

8 10 11 H

9 11 I

Param1, Param2

does Param1 belong to A?

false: continue

true: Does Param2 belong to A?

- false: continue

- true: return true


This procedure, as a loop, would be repeated for B to I.

Although the number of items is small, this is a computationally difficult problem. This is a multiple to multiple relationship: A has two sticks, 0 and 2, but 0 has two corners, A and B. I can't just use a hash table and do a unique look-up on anything.

And if I implement a solution along the procedural lines I outlined in my quasi-code, it won't be scalable. ( I thought of being able to add more squares in a later version. A good design would let me do this.)

So, the blog plan is:
Tomorrow, I'll start re-implementing the array-based board I used in the pilot project. Once that is done, with testing code in place, I'll just write the method I described for figuring out whether a move is a possible move.

Thursday, February 24, 2011

Testing in a PS Script

So, how do you use PowerShell to do .NET development?

Well, simple:

Add-Type @'

using System;

public class Sticks


{



// FIELDS



// CONSTRUCTORS



// PROPERTIES



// METHODS



}


'@

I entered this into my PowerShell IDE and hit "Run Script". This was the result:

WARNING: The generated type defines no public methods or properties.

Fair enough.

TIP: Add the following line to your profile:

set-alias new New-Object

Once I added this, I can do this:

PS Prompt> $test = new Sticks

PS Prompt> $test | get-member
TypeName: Sticks

Name MemberType Definition

---- ---------- ----------


Equals Method bool Equals(System.Object obj)


GetHashCode Method int GetHashCode()


GetType Method type GetType()


ToString Method string ToString()



As I understand, my new type is a .NET object added to this PowerShell session. The get-member cmdlet gives information about the public members of the class.

I added a property and a method from my first draft (which I am otherwise abandoning) to show how tests can be added. Here's a get-member on the revised class:

Name MemberType Definition

---- ---------- ----------


addStick Method bool addStick(int position)
<--

Equals Method bool Equals(System.Object obj)


GetHashCode Method int GetHashCode()


GetType Method type GetType()


ToString Method string ToString()


Value Property System.Int32 Value {get;}
<-- And here's the test code that covers that: $test = new Sticks

if ($test.addStick(-1)) { "Broken: Accepted position value of -1."}


if ($test.addStick(12)) { "Broken: Accepted position value of 12."}


If it's working OK, the script will run without printing anything. The methods in my first run were bools. They return FALSE if they fail and MAKE NO CHANGES TO THE BOARD. A result of TRUE means they made the change expected (here, it means a stick was added.) (That approach is something I picked up doing C code.)

Here's the full script as it stands:



Add-Type @'

using System;


public class Sticks


{



// FIELDS


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



// CONSTRUCTORS



public Sticks()

{



}


// PROPERTIES


// adds up the number of matchsticks


public int Value


{


get


{


int temp = 0;


for (int i = 0; i <>

{


if (board[i]) temp++;


}


return temp;


}


}





// METHODS


public bool addStick(int position)


{


if (position <>

if (position > 11) return false;


if (board[position]) return true;


board[position] = true;


return true;


}




}

'@



$test = new Sticks


# value cannot be less than 0"Broken: Value cannot be less than 0."


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




#value cannot be greater than 11


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





if ($test.addStick(-1)) { "Broken: Accepted position value of -1."}




if ($test.addStick(12)) { "Broken: Accepted position value of 12."}



if ($test.Value -ne 0) {"Broken: Failed value assignment changed value."}



But I am throwing out this approach tomorrow. It got ugly really fast. I think a nested class will do what I want -- and cleanly, to boot.

The Problem

Imagine the following situation: You have a chess board with four squares and from 0 to 12 sticks - like wooden matches or toothpicks. You can place one match along the edge of any square or between any two squares:

_ _
|_|_|
|_|_|

When you have a match on the board, you can move it by rotating it 90 degrees from either end, providing the spot you are rotating it into is open.

One other wrinkle: when you create the board, you do so with a specified number of sticks. For instance, you might start with four sticks. You could add up to four sticks to the board and move them any way you wanted to and remove them, but you could never add a fifth stick.

Oh, what's this for?

Well, I've been playing with some C# in the last few days. I spent a lot of time learning C and even some assembly language. That was a lot of fun, but it meant I neglected the things that object oriented languages are good at and useful for: object design.

I realized this, anew, over the last couple of days, when I tried to implement the exercise above. Once I got to some still simple behaviors using my basic design, it got harder and harder to get the program to do what I had set out for it to do.

Since I'm going to spend time on this problem, anyway, I may as well blog it.

This time, I won't have a specific time limit, as I did with the assembly blog.

Oh, and one other thing: I intend to write tests at the same time I write the code. I made a stab at this, as I got started.

I'm working in the PowerShell IDE, not Visual Studio. Maybe I'll explain why tomorrow.