Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
The sequence should be different at each instance of the game, and so the program should generate random sequences.
Example: Lets say the computer chooses a sequence of
(red, red, blue, yellow, green)
If the player presses (red, red, blue, yellow, green), s/he has won (otherwise lost) and a new game is started.
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
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.
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.