Eric Xu / Mbed 2 deprecated Arcade_RedX

Dependencies:   uLCD_4DGL_SE PinDetect SDFileSystem mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Simon.h Source File

Simon.h

00001 /******************************************************
00002  * This header program declares all the functions and
00003  * variables used by one of the players in Simon.
00004  ******************************************************/
00005 
00006 
00007 #define E3              "/sd/wavfiles/trumpet_E3.wav"
00008 #define A3              "/sd/wavfiles/trumpet_A3.wav"
00009 #define C4              "/sd/wavfiles/trumpet_Cs.wav"
00010 #define E4              "/sd/wavfiles/trumpet_E4.wav"
00011 #define BUZZER          "/sd/wavfiles/BUZZER.wav"
00012 #define DING            "/sd/wavfiles/DING.wav"
00013 #define BOMB            "/sd/wavfiles/bomb.wav"
00014 #define WIN             "/sd/win.wav"
00015 #define EASY            1
00016 #define NORMAL          2
00017 #define HEROIC          3
00018 #define LEGENDARY       4
00019 #define MAXLENGTH       7
00020 
00021 
00022 // Simon Says class
00023 class Simon {
00024     public:
00025         // Simon Says constructor
00026         Simon(AnalogIn &noise, DigitalOut &reset, PinDetect &button1, PinDetect &button2, PinDetect &button3, PinDetect &button4, SDFileSystem &sd, Serial &XBee, uLCD_4DGL &uLCD,
00027             wave_player &waver) : noise(noise), reset(reset), button1(button1), button2(button2), button3(button3), button4(button4), sd(sd), XBee(XBee), uLCD(uLCD), waver(waver) {
00028             // Maximize the uLCD baud rate
00029             uLCD.baudrate(MAXBAUDRATE);
00030 
00031             // Set up interrupts
00032             button1.mode(PullUp);
00033             button2.mode(PullUp);
00034             button3.mode(PullUp);
00035             button4.mode(PullUp);
00036             wait(0.1);        
00037             button1.attach_deasserted(this, &Simon::button1Hit);
00038             button2.attach_deasserted(this, &Simon::button2Hit);
00039             button3.attach_deasserted(this, &Simon::button3Hit);
00040             button4.attach_deasserted(this, &Simon::button4Hit);
00041             wait(0.1);        
00042             button1.setSampleFrequency();
00043             button2.setSampleFrequency();
00044             button3.setSampleFrequency();
00045             button4.setSampleFrequency();
00046             wait(0.1);
00047 
00048             // Set up XBee
00049             reset = 0;
00050             wait(0.001);
00051             reset = 1;
00052             wait(0.001);
00053 
00054             oneHit = false;
00055             twoHit = false;
00056             threeHit = false;
00057             fourHit = false;
00058         
00059             resetSequence(sequence, MAXLENGTH);
00060             resetSequence(playerSequence, MAXLENGTH);
00061 
00062             otherPlayerWin = false;
00063         }
00064 
00065         // Simulate the entire game
00066         void playSimon() {
00067             displayInstructions();
00068             selectDifficulty();
00069             countdown();
00070             playGame();
00071             gameOver();
00072         }
00073 
00074 
00075     private:
00076         AnalogIn &noise;
00077         DigitalOut &reset;
00078         PinDetect &button1;
00079         PinDetect &button2;
00080         PinDetect &button3;
00081         PinDetect &button4;
00082         SDFileSystem &sd;
00083         Serial &XBee;
00084         uLCD_4DGL &uLCD;
00085         wave_player &waver;
00086         volatile bool oneHit;
00087         volatile bool twoHit;
00088         volatile bool threeHit;
00089         volatile bool fourHit;
00090         int difficulty;
00091         int sequence[MAXLENGTH];
00092         int playerSequence[MAXLENGTH];
00093         bool otherPlayerWin;
00094 
00095         // Color the background of one of the four corner
00096         void drawBack(int position, int color) {
00097             int x11, x12, x21, x22, y11, y12, y21, y22;
00098 
00099             switch (position) {
00100                 case 3:
00101                     x11 = 64;
00102                     x12 = 127;
00103                     x21 = 85;
00104                     x22 = 127;
00105                     y11 = 85;
00106                     y12 = 127;
00107                     y21 = 64;
00108                     y22 = 84;
00109                     break;
00110                 case 2:
00111                     x11 = 0;
00112                     x12 = 63;
00113                     x21 = 0;
00114                     x22 = 42;
00115                     y11 = 85;
00116                     y12 = 127;
00117                     y21 = 64;
00118                     y22 = 84;
00119                     break;
00120                 case 1:
00121                     x11 = 0;
00122                     x12 = 63;
00123                     x21 = 0;
00124                     x22 = 42;
00125                     y11 = 0;
00126                     y12 = 42;
00127                     y21 = 43;
00128                     y22 = 63;
00129                     break;
00130                 default:
00131                     x11 = 64;
00132                     x12 = 127;
00133                     x21 = 85;
00134                     x22 = 127;
00135                     y11 = 0;
00136                     y12 = 42;
00137                     y21 = 43;
00138                     y22 = 63;
00139             }
00140 
00141             uLCD.filled_rectangle(x11, y11, x12, y12, color);
00142             uLCD.filled_rectangle(x21, y21, x22, y22, color);
00143         }
00144 
00145         // Color the foreground of one of the four corners
00146         void drawFore(int position, int color) {
00147             int x11, x12, x21, x22, y11, y12, y21, y22;
00148 
00149             switch (position) {
00150                 case 3:
00151                     x11 = 68;
00152                     x12 = 123;
00153                     x21 = 89;
00154                     x22 = 123;
00155                     y11 = 89;
00156                     y12 = 123;
00157                     y21 = 68;
00158                     y22 = 89;
00159                     break;
00160                 case 2:
00161                     x11 = 4;
00162                     x12 = 59;
00163                     x21 = 4;
00164                     x22 = 38;
00165                     y11 = 89;
00166                     y12 = 123;
00167                     y21 = 68;
00168                     y22 = 89;
00169                     break;
00170                 case 1:
00171                     x11 = 4;
00172                     x12 = 59;
00173                     x21 = 4;
00174                     x22 = 38;
00175                     y11 = 4;
00176                     y12 = 38;
00177                     y21 = 39;
00178                     y22 = 59;
00179                     break;
00180                 default:
00181                     x11 = 68;
00182                     x12 = 123;
00183                     x21 = 89;
00184                     x22 = 123;
00185                     y11 = 4;
00186                     y12 = 38;
00187                     y21 = 39;
00188                     y22 = 59;
00189             }
00190 
00191             uLCD.filled_rectangle(x11, y11, x12, y12, color);
00192             uLCD.filled_rectangle(x21, y21, x22, y22, color);
00193         }
00194 
00195         // Print a number in the middle of the screen
00196         void drawNum(int i) {
00197             uLCD.text_width(3);
00198             uLCD.text_height(3);
00199             uLCD.color(BLACK);
00200             uLCD.locate(2, 2);
00201             uLCD.printf("%2D", i);
00202         }
00203 
00204         // Create a random sequence
00205         void createSequence(int *seq, int length) {
00206             // Seed using noise from analog pin
00207             srand((int)(noise * 50000 / 43));
00208         
00209             for (int i = 0; i < length; i++) {
00210                 seq[i] = rand() % 4;
00211             }
00212         
00213             for (int i = length; i < MAXLENGTH; i++) {
00214                 seq[i] = -1;
00215             }
00216         }
00217 
00218         // Reset the hits to false
00219         void resetHits() {
00220             oneHit = false;
00221             twoHit = false;
00222             threeHit = false;
00223             fourHit = false;
00224         }
00225 
00226         // Determine if all hits are false
00227         bool noHits() {
00228             return ((oneHit == false) && (twoHit == false) && (threeHit == false) && (fourHit == false));            
00229         }
00230 
00231         // Wait for player input to go to next page
00232         void nextPage() {
00233             resetHits();
00234 
00235             while (noHits() == true) {
00236             }
00237 
00238             uLCD.cls();
00239             resetHits();
00240         }
00241 
00242         // Push button 1 interrupt routine
00243         void button1Hit() {
00244             oneHit = true;
00245         }
00246 
00247         // Push button 2 interrupt routine
00248         void button2Hit() {
00249             twoHit = true;
00250         }
00251 
00252         // Push button 3 interrupt routine
00253         void button3Hit() {
00254             threeHit = true;
00255         }
00256 
00257         // Push button 4 interrupt routine
00258         void button4Hit() {
00259             fourHit = true;
00260         }
00261 
00262         // Determine whether the player wants to view instructions
00263         bool viewInstructions() {
00264             bool viewInstructions = false;
00265 
00266             resetHits();
00267 
00268             // Wait for pb1 or pb2 to be pressed
00269             while ((oneHit == false) && (twoHit == false)) {
00270                 wait(1);
00271 
00272                 // View instructions
00273                 if (oneHit == true) {
00274                     uLCD.cls();
00275                     viewInstructions = true;
00276                 // Skip instructions
00277                 } else if (twoHit == true) {
00278                     uLCD.cls();
00279                     viewInstructions = false;
00280                 } else {
00281                     resetHits();
00282                 }
00283             }
00284 
00285             return viewInstructions;
00286         }
00287 
00288         // Play the sequence according to the difficulty
00289         void playSequence(int *sequence) {
00290             // Interval between sequence elements
00291             float interval;
00292 
00293             // Set interval based on difficulty
00294             if (difficulty == EASY) {
00295                 interval = 1.5f;
00296             } else {
00297                 interval = 1.0f;
00298             }
00299 
00300             // On Easy or Normal difficulty
00301             if (difficulty == EASY || difficulty == NORMAL) {
00302                 int i = 0;
00303 
00304                 // Loop through the sequence
00305                 while (i < MAXLENGTH && sequence[i] != -1) {
00306                     // Display position, color, and play sound at corresponding intervals
00307                     if (sequence[i] == 0) {
00308                         drawFore(0, RED);
00309                         playSound(A3);
00310                         wait(interval - 0.25);
00311                         drawBack(0, WHITE);
00312                         wait(0.25);
00313                     } else if (sequence[i] == 1) {
00314                         drawFore(1, GREEN);
00315                         playSound(E3);
00316                         wait(interval - 0.25);
00317                         drawBack(1, WHITE);
00318                         wait(0.25);
00319                     } else if (sequence[i] == 2) {
00320                         drawFore(2, YELLOW);
00321                         playSound(C4);
00322                         wait(interval - 0.25);
00323                         drawBack(2, WHITE);
00324                         wait(0.25);
00325                     } else {
00326                         drawFore(3, BLUE);
00327                         playSound(E4);
00328                         wait(interval - 0.25);
00329                         drawBack(3, WHITE);
00330                         wait(0.25);
00331                     }
00332 
00333                     i++;
00334                 }
00335                 // On Heroic difficulty
00336             } else if (difficulty == HEROIC) {
00337                 int i = 0;
00338 
00339                 // Loop through the sequence
00340                 while (i < MAXLENGTH && sequence[i] != -1) {
00341                     // Display color with random position and sound at 1.0-second intervals
00342                     if (sequence[i] == 0) {
00343                         int position = rand() % 4;
00344                         int sound = rand() % 4;
00345                         drawFore(position, RED);
00346 
00347                         if (sound == 0) {
00348                             playSound(E3);
00349                         } else if (sound == 1) {
00350                             playSound(A3);
00351                         } else if (sound == 2) {
00352                             playSound(C4);
00353                         } else {
00354                             playSound(E4);
00355                         }
00356 
00357                         wait(interval - 0.25);
00358                         drawBack(position, WHITE);
00359                         wait(0.25);
00360                     } else if (sequence[i] == 1) {
00361                         int position = rand() % 4;
00362                         int sound = rand() % 4;
00363                         drawFore(position, GREEN);
00364 
00365                         if (sound == 0) {
00366                             playSound(E3);
00367                         } else if (sound == 1) {
00368                             playSound(A3);
00369                         } else if (sound == 2) {
00370                             playSound(C4);
00371                         } else {
00372                             playSound(E4);
00373                         }
00374 
00375                         wait(interval - 0.25);
00376                         drawBack(position, WHITE);
00377                         wait(0.25);
00378                     } else if (sequence[i] == 2) {
00379                         int position = rand() % 4;
00380                         int sound = rand() % 4;
00381                         drawFore(position, YELLOW);
00382 
00383                         if (sound == 0) {
00384                             playSound(E3);
00385                         } else if (sound == 1) {
00386                             playSound(A3);
00387                         } else if (sound == 2) {
00388                             playSound(C4);
00389                         } else {
00390                             playSound(E4);
00391                         }
00392 
00393                         wait(interval - 0.25);
00394                         drawBack(position, WHITE);
00395                         wait(0.25);
00396                     } else {
00397                         int position = rand() % 4;
00398                         int sound = rand() % 4;
00399                         drawFore(position, BLUE);
00400 
00401                         if (sound == 0) {
00402                             playSound(E3);
00403                         } else if (sound == 1) {
00404                             playSound(A3);
00405                         } else if (sound == 2) {
00406                             playSound(C4);
00407                         } else {
00408                             playSound(E4);
00409                         }
00410 
00411                         wait(interval - 0.25);
00412                         drawBack(position, WHITE);
00413                         wait(0.25);
00414                     }
00415 
00416                     i++;
00417                 }
00418                 // On Legendary difficulty
00419             } else {
00420                 int i = 0;
00421 
00422                 // Loop through the sequence
00423                 while (i < MAXLENGTH && sequence[i] != -1) {
00424                     // Play sound with random color and position at 1.0-second intervals
00425                     if (sequence[i] == 0) {
00426                         int position = rand() % 4;
00427                         int color = rand() % 4;
00428 
00429                         if (color == 0) {
00430                             drawFore(position, RED);
00431                         } else if (color == 1) {
00432                             drawFore(position, GREEN);
00433                         } else if (color == 2) {
00434                             drawFore(position, YELLOW);
00435                         } else {
00436                             drawFore(position, BLUE);
00437                         }
00438 
00439                         playSound(A3);
00440                         wait(interval - 0.25);
00441                         drawBack(position, WHITE);
00442                         wait(0.25);
00443                     } else if (sequence[i] == 1) {
00444                         int position = rand() % 4;
00445                         int color = rand() % 4;
00446 
00447                         if (color == 0) {
00448                             drawFore(position, RED);
00449                         } else if (color == 1) {
00450                             drawFore(position, GREEN);
00451                         } else if (color == 2) {
00452                             drawFore(position, YELLOW);
00453                         } else {
00454                             drawFore(position, BLUE);
00455                         }
00456 
00457                         playSound(E3);
00458                         wait(interval - 0.25);
00459                         drawBack(position, WHITE);
00460                         wait(0.25);
00461                     } else if (sequence[i] == 2) {
00462                         int position = rand() % 4;
00463                         int color = rand() % 4;
00464 
00465                         if (color == 0) {
00466                             drawFore(position, RED);
00467                         } else if (color == 1) {
00468                             drawFore(position, GREEN);
00469                         } else if (color == 2) {
00470                             drawFore(position, YELLOW);
00471                         } else {
00472                             drawFore(position, BLUE);
00473                         }
00474 
00475                         playSound(C4);
00476                         wait(interval - 0.25);
00477                         drawBack(position, WHITE);
00478                         wait(0.25);
00479                     } else {
00480                         int position = rand() % 4;
00481                         int color = rand() % 4;
00482 
00483                         if (color == 0) {
00484                             drawFore(position, RED);
00485                         } else if (color == 1) {
00486                             drawFore(position, GREEN);
00487                         } else if (color == 2) {
00488                             drawFore(position, YELLOW);
00489                         } else {
00490                             drawFore(position, BLUE);
00491                         }
00492 
00493                         playSound(E4);
00494                         wait(interval - 0.25);
00495                         drawBack(position, WHITE);
00496                         wait(0.25);
00497                     }
00498 
00499                     i++;
00500                 }
00501             }
00502         }
00503 
00504         // Play a sound file
00505         void playSound(char *wav) {
00506             // Open sound file
00507             FILE *wave_file;
00508             wave_file = fopen(wav, "r");
00509 
00510             // Play wav file
00511             waver.play(wave_file);
00512 
00513             // Close wav file
00514             fclose(wave_file);
00515         }
00516 
00517         // Reset the values in the sequence
00518         void resetSequence(int *sequence, int length) {
00519             for (int i = 0; i < length; i++) {
00520                 sequence[i] = -1;
00521             }
00522         }
00523 
00524         // Display instructions
00525         void displayInstructions() {
00526             uLCD.printf("Hello, and welcometo a game of\nmultiplayer Simon.");
00527             uLCD.printf("First player to   ");
00528             uLCD.printf("reach %d points    ", MAXLENGTH);
00529             uLCD.printf("wins!\n\n");
00530             uLCD.printf("Press pb1 to view instructions or\npb2 to skip to\ndifficulty select-ion.");
00531         
00532             // If player chooses to view instructions
00533             if (viewInstructions() == true) {
00534                 // Gameplay instructions
00535                 uLCD.printf("Sequences will be-gin with only one element. The numb-er of elements\n");
00536                 uLCD.printf("increases by one\nupon earning a po-int, and vise ver-sa. ");
00537                 uLCD.printf("Remember, you must correctly getALL the values in a ");
00538                 uLCD.printf("sequence to earna point.\n\n");
00539                 uLCD.printf("Press any button\nto continue.");
00540         
00541                 nextPage();
00542         
00543                 uLCD.printf("Each push button\nhas its own uniqueposition, color,\nand sound.\n\n");
00544                 uLCD.printf("Press any button\nto continue.");
00545         
00546                 nextPage();
00547         
00548                 // Push button 1
00549                 uLCD.printf("pb1: top right,\nred, note A3\n\n");
00550                 uLCD.printf("Press pb1 for a\ndemonstration.");
00551         
00552                 while (oneHit == false) {
00553                 }
00554         
00555                 uLCD.background_color(WHITE);
00556                 uLCD.cls();
00557                 wait(0.5);
00558         
00559                 for (int i = 0; i < 5; i++) {
00560                     drawFore(0, RED);
00561                     playSound(A3);
00562                     wait(1);
00563                     drawBack(0, WHITE);
00564                     wait(0.5);
00565                 }
00566         
00567                 uLCD.background_color(BLACK);
00568                 uLCD.cls();
00569                 uLCD.printf("pb1: top right,\nred, note A3\n\n");
00570                 uLCD.printf("Press any button\nto continue.");
00571         
00572                 nextPage();
00573         
00574                 // Push button 2
00575                 uLCD.printf("pb2: top left, gr-een, note E3\n\n");
00576                 uLCD.printf("Press pb2 for a\ndemonstration.");
00577         
00578                 while (twoHit == false) {
00579                 }
00580         
00581                 uLCD.background_color(WHITE);
00582                 uLCD.cls();
00583                 wait(0.5);
00584         
00585                 for (int i = 0; i < 5; i++) {
00586                     drawFore(1, GREEN);
00587                     playSound(E3);
00588                     wait(1);
00589                     drawBack(1, WHITE);
00590                     wait(0.5);
00591                 }
00592         
00593                 uLCD.background_color(BLACK);
00594                 uLCD.cls();
00595                 uLCD.printf("pb2: top left, gr-een, note E3\n\n");
00596                 uLCD.printf("Press any button\nto continue.");
00597         
00598                 nextPage();
00599         
00600                 // Push button 3
00601                 uLCD.printf("pb3: bottom left,\nyellow, note C4\n\n");
00602                 uLCD.printf("Press pb3 for a\ndemonstration.");
00603         
00604                 while (threeHit == false) {
00605                 }
00606         
00607                 uLCD.background_color(WHITE);
00608                 uLCD.cls();
00609                 wait(0.5);
00610         
00611                 for (int i = 0; i < 5; i++) {
00612                     drawFore(2, YELLOW);
00613                     playSound(C4);
00614                     wait(1);
00615                     drawBack(2, WHITE);
00616                     wait(0.5);
00617                 }
00618         
00619                 uLCD.background_color(BLACK);
00620                 uLCD.cls();
00621                 uLCD.printf("pb3: bottom left,\nyellow, note C4\n\n");
00622                 uLCD.printf("Press any button\nto continue.");
00623         
00624                 nextPage();
00625         
00626                 // Push button 4
00627                 uLCD.printf("pb4: bottom right,blue, note E4\n\n");
00628                 uLCD.printf("Press pb4 for a\ndemonstration.");
00629         
00630                 while (fourHit == false) {
00631                 }
00632         
00633                 uLCD.background_color(WHITE);
00634                 uLCD.cls();
00635                 wait(0.5);
00636         
00637                 for (int i = 0; i < 5; i++) {
00638                     drawFore(3, BLUE);
00639                     playSound(E4);
00640                     wait(1);
00641                     drawBack(3, WHITE);
00642                     wait(0.5);
00643                 }
00644         
00645                 uLCD.background_color(BLACK);
00646                 uLCD.cls();
00647                 uLCD.printf("pb4: bottom right,blue, note E4\n\n");
00648                 uLCD.printf("Press any button\nto continue.");
00649         
00650                 nextPage();
00651         
00652                 uLCD.printf("There are 4 diffi-culties to choose from:\n\n");
00653                 uLCD.printf("Easy, Normal,\nHeroic, and\nLegendary.\n\n");
00654                 uLCD.printf("Press any button\nto continue.");
00655         
00656                 nextPage();
00657         
00658                 // Different difficulties
00659                 uLCD.printf("Easy difficulty:\n\nThe timing betweenthe values of a\n");
00660                 uLCD.printf("sequence is kept\nat constant 1.5-\nsecond intervals.\n\n");
00661                 uLCD.printf("Press any button\nto continue.");
00662         
00663                 nextPage();
00664         
00665                 uLCD.printf("Normal difficulty:\nThe timing betweenthe values of a\n");
00666                 uLCD.printf("sequence is kept\nat constant 1.0-\nsecond intervals.\n\n");
00667                 uLCD.printf("Press any button\nto continue.");
00668         
00669                 nextPage();
00670         
00671                 uLCD.printf("Heroic difficulty:\nYou must match thepush button to the");
00672                 uLCD.printf("right COLOR that\nis displayed at a random position\nwith a ");
00673                 uLCD.printf("random sou-nd. Timing betweenvalues is 1.0 sec-onds.\n\n");
00674                 uLCD.printf("Press any button\nto continue.");
00675         
00676                 nextPage();
00677         
00678                 uLCD.printf("Legendary\ndifficulty:\n\nYou must match thepush button to the");
00679                 uLCD.printf("right SOUND that\nis displayed at a random position\nwith a ");
00680                 uLCD.printf("random col-or. Timing betweenvalues is 1.0 sec-onds.\n\n");
00681                 uLCD.printf("Press any button\nto continue.");
00682         
00683                 nextPage();
00684             }
00685         }
00686 
00687         // Select difficulty
00688         void selectDifficulty() {
00689             difficultySelection:
00690             uLCD.cls();
00691 
00692             uLCD.printf("Select difficulty:\npb1: Easy\npb2: Normal\npb3: Heroic\npb4: Legendary");
00693 
00694             resetHits();
00695 
00696             while (noHits() == true) {
00697                 wait(1);
00698 
00699                 if (oneHit == true) {
00700                     difficulty = EASY;
00701                 } else if (twoHit == true) {
00702                     difficulty = NORMAL;
00703                 } else if (threeHit == true) {
00704                     difficulty = HEROIC;
00705                 } else if (fourHit == true) {
00706                     difficulty = LEGENDARY;
00707                 } else {
00708                     resetHits();
00709                 }
00710             }
00711 
00712             XBee.putc(difficulty);
00713 
00714             char otherDifficulty = (char)(-1);
00715             bool print = true;
00716 
00717             while (otherDifficulty == (char)(-1)) {
00718                 if (XBee.readable() == true) {
00719                     otherDifficulty = XBee.getc();
00720                     print = false;
00721                 }
00722 
00723                 if (print == true) {
00724                     uLCD.cls();
00725                     uLCD.printf("Waiting for other player...");
00726                     print = false;
00727                 }
00728             }
00729 
00730             uLCD.cls();
00731 
00732             if (otherDifficulty != difficulty) {
00733                 uLCD.cls();
00734                 uLCD.printf("The other player  ");
00735                 uLCD.printf("selected another  ");
00736                 uLCD.printf("difficulty. Please");
00737                 uLCD.printf("select again.     ");
00738                 wait(3);
00739                 uLCD.cls();
00740                 goto difficultySelection;
00741             }
00742 
00743             uLCD.cls();
00744 
00745             if (difficulty == EASY) {
00746                 uLCD.printf("You are playing onEasy difficulty.\n\n");
00747             } else if (difficulty == NORMAL) {
00748                 uLCD.printf("You are playing onNormal difficulty.\n");
00749             } else if (difficulty == HEROIC) {
00750                 uLCD.printf("You are playing onHeroic difficulty.\n");
00751             } else {
00752                 uLCD.printf("You are playing onLegendary\ndifficulty.\n\n");
00753             }
00754 
00755             uLCD.printf("Starting the\ngame...");
00756             wait(1.5);
00757         }
00758 
00759         // Countdown to start the game
00760         void countdown() {
00761             uLCD.filled_rectangle(0, 0, 127, 127, WHITE);
00762             uLCD.textbackground_color(WHITE);
00763             uLCD.background_color(WHITE);
00764             uLCD.cls();
00765 
00766             for (int i = 3; i > 0; i--) {
00767                 drawNum(i);
00768                 wait(1);
00769             }
00770         }
00771 
00772         // Play the game
00773         void playGame() {
00774             // Initialize the score to be 0
00775             int score = 0;
00776 
00777             // Loop through the game
00778             do {
00779                 // Display score
00780                 drawNum(score);
00781                 // Create a sequence
00782                 createSequence(sequence, score + 1);
00783                 // Play sequence
00784                 playSequence(sequence);
00785 
00786                 resetHits();
00787 
00788                 // Loop through the player sequence
00789                 for (int i = 0; i < score + 1; i++) {
00790                     while (noHits() == true) {
00791                     }
00792 
00793                     // Track player inputs
00794                     if (oneHit == true) {
00795                         playerSequence[i] = 0;
00796         
00797                         // On Easy difficulty
00798                         if (difficulty == EASY) {
00799                             // Display position, color, and play sound
00800                             drawFore(0, RED);
00801                             playSound(A3);
00802                             drawBack(0, WHITE);
00803                         // On Normal difficulty
00804                         } else if (difficulty == NORMAL) {
00805                             // Display position, color, and play sound
00806                             drawFore(0, RED);
00807                             playSound(A3);
00808                             drawBack(0, WHITE);
00809                         // On Heroic difficulty
00810                         } else if (difficulty == HEROIC) {
00811                             // Display color
00812                             drawFore(0, RED);
00813                             drawFore(1, RED);
00814                             drawFore(2, RED);
00815                             drawFore(3, RED);
00816                             wait(0.5);
00817                             drawBack(0, WHITE);
00818                             drawBack(1, WHITE);
00819                             drawBack(2, WHITE);
00820                             drawBack(3, WHITE);
00821                         // On Legendary difficulty
00822                         } else {
00823                             // Play sound
00824                             playSound(A3);
00825                         }
00826                     } else if (twoHit == true) {
00827                         playerSequence[i] = 1;
00828 
00829                         // On Easy difficulty
00830                         if (difficulty == EASY) {
00831                             // Display position, color, and play sound
00832                             drawFore(1, GREEN);
00833                             playSound(E3);
00834                             drawBack(1, WHITE);
00835                         // On Normal difficulty
00836                         } else if (difficulty == NORMAL) {
00837                             // Display position, color, and play sound
00838                             drawFore(1, GREEN);
00839                             playSound(E3);
00840                             drawBack(1, WHITE);
00841                         // On Heroic difficulty
00842                         } else if (difficulty == HEROIC) {
00843                             // Display color
00844                             drawFore(0, GREEN);
00845                             drawFore(1, GREEN);
00846                             drawFore(2, GREEN);
00847                             drawFore(3, GREEN);
00848                             wait(0.5);
00849                             drawBack(0, WHITE);
00850                             drawBack(1, WHITE);
00851                             drawBack(2, WHITE);
00852                             drawBack(3, WHITE);
00853                         // On Legendary difficulty
00854                         } else {
00855                             // Play sound
00856                             playSound(E3);
00857                         }
00858                     } else if (threeHit == true) {
00859                         playerSequence[i] = 2;
00860 
00861                         // On Easy difficulty
00862                         if (difficulty == EASY) {
00863                             // Display position, color, and play sound
00864                             drawFore(2, YELLOW);
00865                             playSound(C4);
00866                             drawBack(2, WHITE);
00867                         // On Normal difficulty
00868                         } else if (difficulty == NORMAL) {
00869                             // Display position, color, and play sound
00870                             drawFore(2, YELLOW);
00871                             playSound(C4);
00872                             drawBack(2, WHITE);
00873                         // On Heroic difficulty
00874                         } else if (difficulty == HEROIC) {
00875                             drawFore(0, YELLOW);
00876                             drawFore(1, YELLOW);
00877                             drawFore(2, YELLOW);
00878                             drawFore(3, YELLOW);
00879                             wait(0.5);
00880                             drawBack(0, WHITE);
00881                             drawBack(1, WHITE);
00882                             drawBack(2, WHITE);
00883                             drawBack(3, WHITE);
00884                         // On Legendary difficulty
00885                         } else {
00886                             // Play sound
00887                             playSound(C4);
00888                             //wait(1);
00889                         }
00890                     } else if (fourHit == true) {
00891                         playerSequence[i] = 3;
00892 
00893                         // On Easy difficulty
00894                         if (difficulty == EASY) {
00895                             // Display position, color, and play sound
00896                             drawFore(3, BLUE);
00897                             playSound(E4);
00898                             drawBack(3, WHITE);
00899                             // On Normal difficulty
00900                         } else if (difficulty == NORMAL) {
00901                             // Display position, color, and play sound
00902                             drawFore(3, BLUE);
00903                             playSound(E4);
00904                             drawBack(3, WHITE);
00905                             // On Heroic difficulty
00906                         } else if (difficulty == HEROIC) {
00907                             // Display color
00908                             drawFore(0, BLUE);
00909                             drawFore(1, BLUE);
00910                             drawFore(2, BLUE);
00911                             drawFore(3, BLUE);
00912                             wait(0.5);
00913                             drawBack(0, WHITE);
00914                             drawBack(1, WHITE);
00915                             drawBack(2, WHITE);
00916                             drawBack(3, WHITE);
00917                             // On Legendary difficulty
00918                         } else {
00919                             // Play sound
00920                             playSound(E4);
00921                         }
00922                     }
00923         
00924                     resetHits();
00925                 }
00926 
00927                 // Determine whether the sequences match
00928                 bool correct = true;
00929 
00930                 for (int i = 0; i < score + 1; i++) {
00931                     if (sequence[i] != playerSequence[i]) {
00932                         correct = false;
00933                     }
00934                 }
00935 
00936                 // Play sound
00937                 if (correct == true) {
00938                     playSound(DING);
00939                 } else {
00940 
00941                     playSound(BUZZER);
00942                 }
00943 
00944                 // Check if the other player has won
00945                 if (XBee.readable() == true) {
00946                     otherPlayerWin = XBee.getc();
00947                 }
00948 
00949                 // If the other player has won, end the game
00950                 if (otherPlayerWin == true) {
00951                     goto endGame;
00952                 }
00953 
00954                 // Update the score
00955                 if (correct == true) {
00956                     score++;
00957                 } else {
00958                     if (score > 0) {
00959                         score--;
00960                     }
00961                 }
00962 
00963                 // Reset sequences
00964                 resetSequence(sequence, MAXLENGTH);
00965                 resetSequence(playerSequence, MAXLENGTH);
00966             } while (score < MAXLENGTH);
00967 
00968             endGame:
00969             uLCD.pixel(68, 68, WHITE);
00970         }
00971 
00972         // Game over
00973         void gameOver() {
00974             XBee.putc(true);
00975 
00976             uLCD.cls();
00977             uLCD.text_width(1);
00978             uLCD.text_height(1);
00979             uLCD.textbackground_color(BLACK);
00980             uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
00981             uLCD.background_color(BLACK);
00982             uLCD.color(GREEN);
00983             uLCD.locate(0, 0);
00984 
00985             if (otherPlayerWin == false) {
00986                 uLCD.printf("You win!");
00987                 playSound(WIN);
00988             } else {
00989                 uLCD.printf("The other player\n");
00990                 uLCD.printf("wins.");
00991                 playSound(BOMB);
00992             }
00993         }
00994 };