Wednesday, March 30, 2011

A Display Function

So, today I'm writing a powershell function that will display all the sticks in a board in a human-readable fashion.

The first thing I was going to do was open the file with the C# code and check which one of the methods I would need to access to do this.

Then I remembered I was using PowerShell.

new Board(19) | get-member


TypeName: Board


Name MemberType Definition

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

addStick Method bool addStick(int position)

Equals Method bool Equals(System.Object obj)

GetHashCode Method int GetHashCode()

getPositionValue Method bool getPositionValue(int position)

GetType Method type GetType()

moveStick Method bool moveStick(int origin, int destination)

removeStick Method bool removeStick(int position)

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;}


Get-member displays all the public members of an object. PowerShell is object oriented.


OK, the one I want is getPositionValue:


$x.getPositionValue(8)

False


So, I spent fifteen minutes futzing around with it and got this:

function early ($a) {


$outString = "o"


for ($b = 1; $b -lt 6; $b++)

{

if ($a.getPositionValue($b))

{

$tempString = '-'

}

else {

$tempString = ' '

}

$outString = $outString + $tempString + 'o'


}


$outString


}


Which gives this result:

early $x

o-o-o-o-o-o


I loaded stick positions 1 through 5, so there would be something to display.


OK, all I need to do now is do the rest.


I'm not going to post the whole code – because I'm not. I renamed it “display” and it seems to work:


> $test = new Board(60)

> for ($kj = 1; $kj -lt 61; $kj++){ $test.addStick($kj)}


…

> display $test

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


Tomorrow, I'll start reworking the board to get rid of the grid[] array.

No comments:

Post a Comment