In the first version of this program, there was no class for modeling sticks. Instead, the board was simply an array of booleans.
This time around, I'm going to implement a class to wrap a Boolean. This seems like overkill, but I was thinking – what if I want to revise the program to have some other characteristic of sticks, such as color, or to track an individual stick on the board. It's easy to lay the groundwork for this now, early in the design process.
While I'm doing this, I'm going to save some stub code for classes – some stuff I've been re-typing up until now.
Here it is:
public class
{
// FIELDS
// CONSTRUCTORS
// PROPERTIES
// METHODS
// TESTING CODE
// OVERRIDES
public override int GetHashCode()
{
// STUB
return base.GetHashCode();
}
public override bool Equals(System.Object obj)
{
// STUB
return base.Equals(obj);
}
}
Adding a field is easy:
private bool stickValue = false;
And a property to access it:
public bool Value
{
get {
return stickValue;
}
set {
stickValue = !stickValue;
}
}
I don't know whether this works, so let's write some testing code to find out:
# Testing Stick Value Property
#
# Checks whether get and set for the Stick class works as expected:
$testStick = new Stick
$testStick.Value
Running this should get False, since the stickValue is false by default, and it does.
The next two lines, also, run fine:
$testStick.Value = "J"
$testStick.Value
(At first I assumed that, since my code doesn't use the value given it to set the value of the stick, it doesn't matter what that value is or even its type. Actually, it's a PowerShell thing: it interprets any string as True. It interprets 0 as False and 1 as True, which I use in the testing below.)
How about this: a value of 0 sets the stick to False, a value of 1 sets it to True, a value of 2 sets it to the opposite of what it is, and any other value throws an exception. Type mis-match or something. Hold on.
How about an ArgumentOutOfRangeException?
(Oh, the reason I want to be able to set the value directly (0 for false and such) is in case I want to write code that clears the board or that fills the board without preserving the number of sticks. The reason I chose 2, not -1, is so I would only have to pass a single character in case I write tests for this.)
set {
if (value == 0)
{
stickValue = false;
}
else if (value == 1)
{
stickValue = true;
}
else if (value == 2)
{
stickValue = !stickValue;
}
else
{
throw new ArgumentOutOfRangeException();
}
}
Beautiful, isn't it?
Doesn't work. The value item is a bool. I guess C# knows better than to let such hackery go on.
Well, we know what to do about that.
public bool Value
{
get {
return stickValue;
}
set {
stickValue = value;
}
// METHODS
public void flip()
{
stickValue = !stickValue;
}
I can get both kinds of functionality this way. Sorry I can't use the exception.
Let's tidy up.
Here's my working test code:
$testStick = new Stick
$testStick.Value
$testStick.Value = 0
$testStick.Value # expected: false
$testStick.Value = 1
$testStick.Value # expected: True
$testStick.flip()
$testStick.Value # expected: False
$testStick.flip()
$testStick.Value # expected: true
When I run it, it all works. Now I just need to turn it into conditionals using 0 and 1 as False and True:
$testStick = new Stick
if ($testStick.Value -eq 1) {"FAIL: Default stick value should be FALSE."}
$testStick.Value = 0
if ($testStick.Value -eq 1) {"FAIL: Value should be set to FALSE."}
$testStick.Value = 1
if ($testStick.Value -eq 0) {"FAIL: Value should be set to TRUE."}
$testStick.flip()
if ($testStick.Value -eq 1) {"FAIL: Value should be set to FALSE."}
$testStick.flip()
if ($testStick.Value -eq 0) {"FAIL: Value should be set to TRUE."}
Running it now shows no results.
Next up: Whatever else needs to be done on the Stick class.
No comments:
Post a Comment