Wednesday, March 16, 2011

Constructor for Node Class

Each node is going to know that it's associated with up to 4 positions. So, when the node object is initialized, it needs to know which positions it is connected to.


Node numbers:

0 1 2 3 4 5

6 7 8 9 10 11

12 13 14 15 16 17

18 19 20 21 22 23

24 25 26 27 28 29

30 31 32 33 34 35



Stick Positions:

.01 .02 .03 .04 .05 .

06 07 08 09 10 11

.12 .13 .14 .15 .16 .

17 18 19 20 21 22

.23 .24 .25 .26 .27 .

28 29 30 31 32 33

.34 .35 .36 .37 .38 .

39 40 41 42 43 44

.45 .46 .47 .48 .49 .

50 51 52 53 54 55

.56 .57 .58 .59 .60 .


After some playing around, I found the following handy equation to calculate the number of the stick position above each node:

node number + (((node number/5) – 1) * 5)

(This only works for integers variables. The division is whole number division.)

The other values can be calculated from the value of the position above. Here's the code:


position[0] = location + (((location / 5) - 1) + 5);

position[1] = position[0] + 6;

position[1] = position[0] + 11;

position[1] = position[0] + 5;


From a mile away, I'm sure you saw the flaw in this: it doesn't work correctly for any corner or edge position. Those are going to have to be done as separate cases:


// EDGE CASES: TOP EDGE

if (position[0] <>

// BOTTOM EDGE

if (position[2] > 60) position[2] = -1;


The left and right edges are less pretty. I use the modulo operator (%) to figure out whether something is on the edge:


//LEFT EDGE

if (location % 6 == 0) position[3] = -1;

//RIGHT EDGE

if ((location + 1) % 6 == 0) position[1] = -1;


And does it compile?


It does! (Although that doesn't prove anything – all my tests failed, since not enough of the program is running for them to make sense.)


Next up: a method to tell whether a position is occupied with a stick.

No comments:

Post a Comment