   ## Shift, Rattle and Roll

Rotation is a very simple thing to implement. This work consisted of a simple pair of test functions (one to test the rotations and one that would dump out the group of doors). While I probably didn’t need to write the test code, I figured having a dump of the rotation code working would be a good way of making sure that the code does in fact work thereby saving myself debugging work later if it doesn’t work The test simply loops through the ten rooms rotating each room to the left five times and then to the right 5 times. The actual rotation code is also very simple. Here is the rotateLeft function.

• function rotateLeft(n)
• {
• var temp = roomArray[n];
• for (var cntr = 0; cntr < 4; ++cntr)
• roomArray[n][cntr] = roomArray[n][cntr+1];
• roomArray[n] = temp;
• }

As you can see, this is a very simple function. First, we hold a copy of the left-most door for the indicated room to shift. We then move the value from the door to the right for each of the first four doors. Finally, we use the stored value to replace the right-most door value. The rotateRight function works the same way, except it does the shifting in the opposite direction.

After running the test function, it is quickly and thoroughly verified that the shifting works. Actually having the game support the shifting modes is very simple. First, I add a variable that I call farthestRoom. In the resetGame function, the value is set to zero. The purpose of this variable is simply to hold the number of the room which the player has reached. This way, the game won’t shift a room if the player hasn’t found his or her way past that room yet. After all, what is the point of having the doors shift if the player doesn’t even know what the correct door was?

Deciding which way to shift is done by simply choosing a random number between 0 and 2 inclusive. A result of 0 calls the rotateLeft function, while a result of 2 calls the RotateRight function. A result of 1 does nothing. The appropriate flag is set to true so the LCD will show the appropriate arrow if the doors are shifting.

As explained earlier, testing can be done using a sheet of paper with a grid drawn on it. Pennies mark the correct doors. When a room shifts, you slide the penny in the appropriate direction. With testing confirming that the second mode works, we are ready to tackle the final mode.

 Previous Making Deadly Doors Contents Page 8 of 10 Deadly Doors Next