Even as I wrote it, though, I knew the code was extremely brittle. If the problem is changed, in any way, the underlying code needs to be re-worked, a lot. These are the main problems I see with the code.
- Verification of input is hard-coded.
- The positions in the board are numbed sequentially, which means the X axis is different from the Y axis.
- What a valid move is is hard-coded, making that part of the class irrelevant to any future version.
Well, I can fix the first item in a pretty straight-forward manner.
For right now, I'm just supporting an easier way do verification - a position must be between 0 and 11. No new tests, since the old tests are testing the right thing.
private int size;
// CONSTRUCTORS
public Sticks(int maximum)
{
max = maximum;
size = 12;
}
The size variable is set only in the constructor.
So, we need code to check whether a number is outside this range:
private bool beyondSize(int candidate)
{
if (candidate <>
else if (candidate > (size - 1)) return false;
else {return true;}
}
And replace (position -lt beyondSize. So, I ran this and it all blew up in my face. A bunch of my tests failed. In the middle of writing the code, I changed the approach from checking whether the position fell in the range to whether it fell outside the range. The earlier code was expecting a fail to be false. This did the exact opposite.
private bool beyondSize(int candidate)
{
if (candidate <>
else if (candidate > (size - 1)) return true;
else {return false;}
}
That's better.
Next, I need to figure out how to make moves more generalized.
No comments:
Post a Comment