In this chapter, we are going to create the game of Simon using our Aisoy1 and the Scratch programming tool.
Rules of the Game
In the game of Simon, we have to recite the correct sequence of colors displayed by the bot relying on the Aisoy1 voice recognition to capture our answers. The game will start simply by displaying one color and progress with incrementally longer color sequences displayed at faster speeds. The game ends when the user fails to recite a color sequence correctly.
The Program
So how can we accomplish this task? It’s easy using Scratch. First we will explain the variables used in this program.
-
active_grammar: Internal flag variable.
-
continue: Indicates if the game is finished (value = 1) or not (value = 2). We will set the default value to 0.
-
end: Enables aborting the main loop if the player chooses to do so.
-
hearing color: Set to allow bot to listen (1 when it is listening, 0 when it is not).
-
hearing confirmation: Set when bot is listening and a “Yes” or “No” answer is expected from the player. These variables allow us to manage the expected words to recognize in context.
-
iterator: Supports iterating through loops.
-
latch: Internal flag variable.
-
lose: Set to 1 if player fails to recite colors in the correct sequence. Default value is 0.
-
new color: Set to represent the new color in the sequence (a new color is added with each iteration).
-
points: Records points player has achieved (1 point for each sequence iteration).
-
time: Stores the amount of time that we set to display the colors (we decrement this value as we proceed to display the colors faster and make the game more difficult).
And now, the lists:
-
colors: This list will contain all the colors that the robot will display.
-
grammar: This list contains the vocabulary that we want the bot recognize (BLUE, RED, GREEN, YELLOW, YES, NO).
Now that we understand the different variables used in the program, we are going to review the process of constructing the game using Scratch programming blocks:
These first blocks initialize variables setting them to specific values. Note that time is set to 1.5 representing the delay time in seconds between color displays. We will decrease this value dynamically. In addition, we clear the "colors" list to remove the values from previous runs.
The first step is set the bot to the normal state (eyebrows, eyes, mouth and hearth color). Here is the block that will accomplish this:
Now, we turn off the Aisoy1’s heart color to enable the players to more easily see the change. Finally, we set the grammar or vocabulary of the robot using “grammar” variable.
These are the messages the bot will recite and that Scratch will display on screen to indicate that it will play the Simon game. Now we can investigate the core code of the program.
We set up a loop to repeat the game indefinitely while the "end" variable is set to 0 (this value will change when the player wants to end the program).
Then the "if" statement checks to see if the number of historic iterations is odd or even. If it is even we decrease the time delay by 25% (effectively making the sequencing faster every other iteration).
Will set the “lose” variable to 0 when we restart the loop. We are in a new iteration and want to add a new color to display. We generate a random number between 1 and 4 and add the corresponding color to the colors list (1 = BLUE, 2 = red, 3 = GREEN, 4 = YELLOW).
This block just tells the player to pay attention because we are about to display the colors.
This is the loop that displays the colors stored in the “colors” list. This list is the sequence that the player must repeat correctly in order to continue. Note that "secs wait time" block uses variable "time" to drive the amount of time to display each color.
Now we check to see if the player recites the displayed colors properly. Will reuse the variable "iterator" to count how many times the player has said a word (a color in this case). Therefore we must to restart and set the iterator to 1. Do you see that we use the “hearing color” variable to control that it’s hearing a color (view next image)?
Now, the bot asks the player to repeat the displayed sequence and waits until the player has recited all the correct colors or makes a mistake (setting the “lose” variable to 1).
We use these blocks to manage the evaluation of recited colors:
The code is exactly the same for the case for each of the four colors just differing in the associated value. For each iteration we check that the spoken value corresponds to that stored (so shown by the bot) in “colors” variable.
An incorrectly recited color results in failure. Then the "lose" variable will be set to 1 and the game ends.
After the previous “wait until” block, we continue with:
First, we find an “if-else” block that checks the value of lose” (1 indicates a fail).
If “lose” is not 1, the code will run the “else” sentences. The game continues and the player’s points are increased by 1 (the colors sequence was guessed correclty).
However, if “lose” is equal to 1, we set the bot to a sad state.
Next we inform the player of the failure and tell him or her the number of points that he or she scored.
At this point, the game is over. Then the bot will ask player if he or she wants to start a new game and wait for an answer. The “continue” value results from the player’s answer (“YES”=1 or “NO”=2).
If the answer is “NO”, the program will set the “continue” variable to 2 and the bot will thank the player for playing.
However, if the answer is “YES”, the bot will tell us the game will restart and set “continue” to 1. The “time” variable is reset to the default value (1.5) when we start a new game. We also turn off the heart color at the start of a new game.
Now we are back to the point where the loop had been waiting for the player’s reply.
Basically we check if the "continue" variable is set to 2, which would mean that we have decided not to play and end the program. If so, we set the value of the "end" variable to 1, which would take the program out of the loop for the next iteration and finish.
The other actions reset variables and lists to their default values. We have to do this step if the player answered "YES" to continue and restart the game.
We have finished the SIMON game for our Aisoy1. The final step is to connect our bot and launch the program by clicking the green flag.
Cheers to all and we hope you like it!
Comments
0 comments
Please sign in to leave a comment.