One of my ideas about how to lay out the grid for this project was to define a position and then recursively have a position object define the next position. Instead of defining everything as a grid, it would be modeled as a linked list.
I hadn't played with that kind of list in a while – you know: struct item { int x; item* next; }. I decided to brush up a little with a side project: an odometer.
This class will model a wheel on an old-fashioned odometer. It will have a possible value from 0 to 9 and every time you add 1 to a value of 9, it will roll over and also increment the next wheel upstream.
So, here's the state information:
private int state = 0;
private Wheel next = null;
Since I'm providing the initial state, I don't need to include a constructor.
I wrote a property to get the value of the whole odometer (calling each wheel, in turn, and multiplying the result by 10).
I added a method to add one to the current value and print out the value.
Here's the whole class:
using System;
public class Wheel
{
// FIELDS
private int state = 0;
private Wheel next = null;
// CONSTRUCTORS
// PROPERTIES
public int Value
{
get
{
int temp = 0;
if (next != null)
{
temp = (next.Value * 10);
}
temp = temp + state;
return temp;
}
}
//METHODS
public int addOne()
{
click();
return Value;
}
private void click()
{
state++;
if (state [GREATER THAN SYMBOL] 9)
{
if (next != null)
{
next.click();
}
else
{
next = new Wheel();
next.click();
}
state = 0;
}
}
}
Finally, I added some testing code in PowerShell to verify that the class was working as expected.
$x = new Wheel
for ($i = 0; $i -lt 10000; $i++)
{
$x.addOne()
}
So, here's my first question: can I do this with just PowerShell? After all, PS comes with programming logic.
Well, here's my first run at it:
function click ($a)
{
$e[$a]++
if ($e[$a] [GREATER THAN SYMBOL] 9) {
$e[$a] = 0
click $a+1
}
$e[$a]
}
And it includes my first mistake: I used the greater than symbol instead of -gt.
Actually, when I ran the above with the following test code, I got a perfectly OK-looking output:
$e = 0, 0, 0, 0, 0
for ($i = 0; $i -lt 10000; $i++)
{
click 0
}
It seemed to just count up from 1 to 10000. What it was doing was counting from 1 up and just ignoring the greater than symbol. When I changed it to -gt, it blew up:
0
1
2
3
4
5
6
7
8
9
Array assignment to [0+1] failed: Cannot convert value "0+1" to type "System.Int32". Error: "Input string was not in a correct format.".
At line:5 char:4
+ $e[ [LESS THAN SYMBOL][LESS THAN SYMBOL][LESS THAN SYMBOL][LESS THAN SYMBOL] $a]++
+ CategoryInfo : InvalidOperation: (1:Int32) [], RuntimeException
+ FullyQualifiedErrorId : ArrayAssignmentFailed
I needed to have parens around $a+1 in order to call the function from within the function.
Here's the fixed function:
function click ($a)
{
$e[$a]++
if ($e[$a] -gt 9) {
$e[$a] = 0
click ($a+1)
}
$e[$a]
}
When this runs, it just displays the value of each wheel as it turns it. This isn't what we want to see – so how about a function that displays the whole odometer value and on the same line. Here's the whole script as revised:
function click ($a)
{
$e[$a]++
if ($e[$a] -gt 9) {
$e[$a] = 0
click ($a+1)
}
}
function display {
$a = $e[4] * 10000 + $e[3] * 1000 + $e[2] * 100 + $e[1] * 10 + $e[0]
$a
}
$e = 0, 0, 0, 0, 0
for ($i = 0; $i -lt 999; $i++)
{
click 0
display
}
This works as expected.
It also fails if you don't have a big enough array for all your clicks, but that's expected.
I just want to fix one thing I should have done from the beginning: making the parameter optional by including a default value. That way, you can just call “click” instead of “click 0”. The change is just in the function signature:
function click ($a = 0)
Now the testing code can be changed to just use “click”:
$e = 0, 0, 0, 0
for ($i = 0; $i -lt 999; $i++)
{
click
display
}
The result is the same.
No comments:
Post a Comment