C++代写 – Lab #8 (Option 2): Implementing the Simon Game

Lab #8 (Option 2): Implementing the Simon Game
Problem Summary
This option allows you to get up to 140% on this lab (Please select and submit either option 1 or option 2, not both.). Note that the bonus marks will not allow you to get more than 100% on your “overall” lab mark, it will help you though to improve your marks of earlier labs (for example lab 7, or/and lab 5, …). Please add a comment at the top of your program indicating that it is the Simon game and follow the instructions on the deliverables document for the .c file name.
You are to implement the Simon game in C using the DAQ simulator. The setup number value for initializing the simulator is 6 (setupNum of 6). The following figure shows a snapshot of the simulator:
When turned on, the LEDs from 0 to 3 are of the following colours: Green, Red, Yellow, and Blue, respectively. LED0 is associated with PushButton0, LED1 with PushButton1, and so forth.
Simon Game
Simon is a simple memory skills electronic game that goes back to the early era of electronic games.
 A sequence of colors are shown to the player. The player should memorize those and reproduce the same sequence by pushing the corresponding switches.
 The displayed sequence begins with a single colour (displayed on the corresponding LED by blinking once).
 If the player follows successfully (i.e. remembers the correct colour and pushes the corresponding button correctly), another randomly chosen colour is added to the end of the sequence each time and this new sequence is displayed (displayed on the corresponding LEDs
one after the other by blinking each once). So the sequence becomes longer each time (and so harder to memorize).
 The game ends if the player makes a mistake, or if the player wins (following the pattern all the way up to the (pre-set) maximum length
of the sequence.) You program though should start a new game right after that, until the use quits. You can see http://en.wikipedia.org/wiki/Simon_(game) for more info.

We want to develop a program that implements a simple version of the game.
 There are 4 color LEDs.
 There are 4 corresponding switches. Note that these switches are push-button switches (like the keys on a keyboard). So you need to
implement reading from them.
 The maximum sequence length is fixed at 5 (although it could be easily modified to be longer).
The sequence should be different at each instance of the game, and so the program should generate random sequences. Example: Let’s say the computer chooses a sequence of
(red, red, blue, yellow, green)
 First the red LED is turned on (flashed).
 If the player presses the red button, then the sequence (red, red) is displayed (otherwise, the player has made a mistake and a new
game is started.)
 If the player presses (red, red), then the sequence (red, red, blue) is displayed (otherwise, the player has made a mistake and a new
game is started.) …
 If the player presses (red, red, blue, yellow, green), s/he has won (otherwise lost) and a new game is started.

Writing the Program
Now that we know what we expect from the program (clear problem statement), we should start implementing it.
 We first devise an algorithm for implementing this program.
 Then we write the primary structure of the program based on the general framework.
 Then we implement the required functions:
o a function to implement the functionality of the Simon game, o a function to generate the required random sequence,
o and any other required functions.
 Finally (and periodically), we debug and test the program Algorithm
One possible algorithm for implementing the simple Simon game is:
– generate a random sequence of 5 numbers between 0 and 3 (inclusive, representing the colours), (sequence length is 5)
– start with 1 LED (N=1)
– continue while the user wants to play
 turn on (flash) the first N LEDs in the sequence one by one
 read the user’s input (i.e. read the buttons)
 if the user gets it correctly
o if N equals to the sequence length
 the user wins (flash the green LED three times to indicate a win)  start a new game
o else
 increment N
 else
o the user loses (flash the red LED three times to indicate a lose) o start a new game
Use practical and reasonable timing (not too long nor too short): Allow some short time before the game starts/restarts (allowing the player to get ready), use practical LED flashing times, the same goes with the interval between displaying each colour of the random sequence to the player, …
Program Structure
As before the main skeleton of your program will be:
/* include required header files */
/* define input/output channels */
/* define other symbolic constants, e.g. state of the switches and LEDs */
void runSimon ( void ) ;
/* other function prototypes here*/
int main( void ) {
/* using our usual main function */ }
void runSimon ( void ) {
/* your code here */
/* call other functions that you write here */ }
/* other function definitions here*/
Programming Challenges
1) Reading from push-button switches
In this program, we are reading from push-button switches as opposed to the toggle switches (that we used in earlier exercises). So you need to implement it.
Note that when you change the state of a toggle switch (like in Lab 7), it keeps its state until you change it again. So you could just call digitalRead one time (some time after this change of state) and know its new status. For the push-button, when you press it, its state changes to on, but as soon as you release it, it goes back to its default state (off).
You simply need to consider this procedure, when you implement reading from the push buttons: a) your program must sense that the push button is pressed (i.e. it is on now) and b) it should wait until it is released (back to off again) -> that constitutes one click of that push button.
– Your program still has to be responsive if the user quits the program.
– The push button must be responsive to a brief click as well as a longer press.
2) Generating Random Sequences (Numbers)
The sequence should be different at each instance of the game, and so the program should generate random sequences. We need to write a function that generates the sequence of random numbers for our game. How do we generate random integers between 0 and 3?
The C library stdlib.h declares the function:
int rand(void);
 Returns a pseudo-random integer in the range 0 to RAND_MAX inclusive, where RAND_MAX is a system-dependent constant (often 32,767).
 The pseudo-random integers are equally likely to occur (statistically speaking, they have a uniform distribution.)
To initialize (seed) the random number generator, we use the following call to the function srand() (just use it exactly as below, you need to
include the time.h header file):
srand( (unsigned) time( NULL ) ) ;
This function call uses the current time (in seconds) from the system clock as the seed for the random number generator.
Assuming we run the program at different times, different sequences of pseudo-random numbers will be generated. The time function is declared in time.h. The srand function is declared in stdlib.h.
Always seed the random number generator before using it!
int randInt(int lower, int upper) {
/* your code here */ }
void generateSequence(int length, int data[ ] ) {
/* use randInt here */ }
See section 4.5 of the textbook for more info.

Leave a Reply

Your email address will not be published. Required fields are marked *