Hardware Reference
In-Depth Information
selfPlay() checks to see if the current level is equal to the last level, and then randomly chooses to push the
virtual button by comparing a randomly generated number to a chosen number and when the numbers match the
virtual button is pressed. A perfect game will be played if the selfPlay() function can only press the virtual button
when the current level is perfectly aligned with the last level even with randomly deciding when to press the virtual
button. To add the feel of imperfection to the selfplay() function the same method used as to determine when
to press the virtual button to randomly be off by one so that a perfect game is not guaranteed and selfPlay()
can lose. When the game is playing, the first level will never equal the foundation level, and the virtual button call
will never be activated. selfplay() has to has to trigger the virtual button at a random point to proceed from the
first level. The random numbers are generated from seed that is created by reading the Arduino's analog pin 0 while
it is not connected and is electrically floating. When a generated random number is checked against a set number,
corresponding events will be triggered in the self-play mode.
The move sound is turned off when the game is in self-play mode so that the game does not get irritating to
people when it is idle. selfPlay() displays the splash screen every 51 virtual button presses, or about every three to
five selfplay() games. The selfPlay() function attaches a different interrupt function to the physical button so that
the self-play can be exited and the game can return to a playable state when a player wants to play it. A few things
need to be set up in the beginning of the sketch to enable self-play. Two variables need to be initialized so that the
program will know when to play the splash screen and to keep track of whether a player is not at the game. One of the
variables is incremented when the self-play is called and is initialized to a value of 300 so the self-play functionality
starts when the game is turned on. The other variable is incremented when the self-play presses the virtual button.
Both variables are reset when a player engages the game. Add the following two variables to the global declarations
after the library includes:
int logoCount = 0;
int selfPlayCount = 300;
A reset of the self-play count ( selfPlayCount = 0 ) is added to the beginning of the buttonInterrupt() function
so that the self-play will not be engaged while the player is in the middle of a game. Finally, a call is made to GD.ascii()
b efore the call to StackIt() in the setup() function, allowing the game to use the standard Gameduino font. The font
is used so that a string can be printed to the top-left corner of the display to inform a prospective player on how to
start a new game.
Part 2 is the virtual button the self-play mode uses to advance the game. A tone is played that is similar when
the physical button is pressed. The virtual button makes a call to WinState() to check if the self-play has matched
the prior level. The self-play mode uses all the game play mechanisms and mimics an actual player. Self-play will
not always win or play the same game every time. logoCount is incremented within this function to signal the splash
screen to be displayed.
Listing 11-6. Self-Play for Stack It, Part 2 of 3
void selfPlayButton() {
GD.voice(0, 0, 5000, 254, 254);
delay (50);
WinState();
logoCount++;
GD.voice(0,0,0,0,0);
} // end self play button
The game will return to a normal play mode because the self-play changes the interrupt function. Part 3 is for
handling the returning to normal play when a player presses the button while the self-play mode is activated. The
string is removed for the top of the screen, play is reset, the move sound is turned back on, and the counts are set to
the appropriate states. The logo count is set to 51 so that self-play will execute after the game goes idle.
Search WWH ::




Custom Search