Multi-game multiplayer arcade gaming system meant for the blue O when playing Super Tic-Tac-Toe.

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player

Committer:
soapy12312
Date:
Fri Dec 04 23:00:42 2015 +0000
Revision:
2:da0f01fbd25c
Multi-game multiplayer arcade gaming system meant for the blue O when playing Super Tic-Tac-Toe.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
soapy12312 2:da0f01fbd25c 1 /******************************************************
soapy12312 2:da0f01fbd25c 2 * This header program declares all the functions and
soapy12312 2:da0f01fbd25c 3 * variables used by one of the players in Simon.
soapy12312 2:da0f01fbd25c 4 ******************************************************/
soapy12312 2:da0f01fbd25c 5
soapy12312 2:da0f01fbd25c 6
soapy12312 2:da0f01fbd25c 7 #define E3 "/sd/wavfiles/trumpet_E3.wav"
soapy12312 2:da0f01fbd25c 8 #define A3 "/sd/wavfiles/trumpet_A3.wav"
soapy12312 2:da0f01fbd25c 9 #define C4 "/sd/wavfiles/trumpet_Cs.wav"
soapy12312 2:da0f01fbd25c 10 #define E4 "/sd/wavfiles/trumpet_E4.wav"
soapy12312 2:da0f01fbd25c 11 #define BUZZER "/sd/wavfiles/BUZZER.wav"
soapy12312 2:da0f01fbd25c 12 #define DING "/sd/wavfiles/DING.wav"
soapy12312 2:da0f01fbd25c 13 #define BOMB "/sd/wavfiles/bomb.wav"
soapy12312 2:da0f01fbd25c 14 #define WIN "/sd/win.wav"
soapy12312 2:da0f01fbd25c 15 #define EASY 1
soapy12312 2:da0f01fbd25c 16 #define NORMAL 2
soapy12312 2:da0f01fbd25c 17 #define HEROIC 3
soapy12312 2:da0f01fbd25c 18 #define LEGENDARY 4
soapy12312 2:da0f01fbd25c 19 #define MAXLENGTH 7
soapy12312 2:da0f01fbd25c 20
soapy12312 2:da0f01fbd25c 21
soapy12312 2:da0f01fbd25c 22 // Simon Says class
soapy12312 2:da0f01fbd25c 23 class Simon {
soapy12312 2:da0f01fbd25c 24 public:
soapy12312 2:da0f01fbd25c 25 // Simon Says constructor
soapy12312 2:da0f01fbd25c 26 Simon(AnalogIn &noise, DigitalOut &reset, PinDetect &button1, PinDetect &button2, PinDetect &button3, PinDetect &button4, SDFileSystem &sd, Serial &XBee, uLCD_4DGL &uLCD,
soapy12312 2:da0f01fbd25c 27 wave_player &waver) : noise(noise), reset(reset), button1(button1), button2(button2), button3(button3), button4(button4), sd(sd), XBee(XBee), uLCD(uLCD), waver(waver) {
soapy12312 2:da0f01fbd25c 28 // Maximize the uLCD baud rate
soapy12312 2:da0f01fbd25c 29 uLCD.baudrate(MAXBAUDRATE);
soapy12312 2:da0f01fbd25c 30
soapy12312 2:da0f01fbd25c 31 // Set up interrupts
soapy12312 2:da0f01fbd25c 32 button1.mode(PullUp);
soapy12312 2:da0f01fbd25c 33 button2.mode(PullUp);
soapy12312 2:da0f01fbd25c 34 button3.mode(PullUp);
soapy12312 2:da0f01fbd25c 35 button4.mode(PullUp);
soapy12312 2:da0f01fbd25c 36 wait(0.1);
soapy12312 2:da0f01fbd25c 37 button1.attach_deasserted(this, &Simon::button1Hit);
soapy12312 2:da0f01fbd25c 38 button2.attach_deasserted(this, &Simon::button2Hit);
soapy12312 2:da0f01fbd25c 39 button3.attach_deasserted(this, &Simon::button3Hit);
soapy12312 2:da0f01fbd25c 40 button4.attach_deasserted(this, &Simon::button4Hit);
soapy12312 2:da0f01fbd25c 41 wait(0.1);
soapy12312 2:da0f01fbd25c 42 button1.setSampleFrequency();
soapy12312 2:da0f01fbd25c 43 button2.setSampleFrequency();
soapy12312 2:da0f01fbd25c 44 button3.setSampleFrequency();
soapy12312 2:da0f01fbd25c 45 button4.setSampleFrequency();
soapy12312 2:da0f01fbd25c 46 wait(0.1);
soapy12312 2:da0f01fbd25c 47
soapy12312 2:da0f01fbd25c 48 // Set up XBee
soapy12312 2:da0f01fbd25c 49 reset = 0;
soapy12312 2:da0f01fbd25c 50 wait(0.001);
soapy12312 2:da0f01fbd25c 51 reset = 1;
soapy12312 2:da0f01fbd25c 52 wait(0.001);
soapy12312 2:da0f01fbd25c 53
soapy12312 2:da0f01fbd25c 54 oneHit = false;
soapy12312 2:da0f01fbd25c 55 twoHit = false;
soapy12312 2:da0f01fbd25c 56 threeHit = false;
soapy12312 2:da0f01fbd25c 57 fourHit = false;
soapy12312 2:da0f01fbd25c 58
soapy12312 2:da0f01fbd25c 59 resetSequence(sequence, MAXLENGTH);
soapy12312 2:da0f01fbd25c 60 resetSequence(playerSequence, MAXLENGTH);
soapy12312 2:da0f01fbd25c 61
soapy12312 2:da0f01fbd25c 62 otherPlayerWin = false;
soapy12312 2:da0f01fbd25c 63 }
soapy12312 2:da0f01fbd25c 64
soapy12312 2:da0f01fbd25c 65 // Simulate the entire game
soapy12312 2:da0f01fbd25c 66 void playSimon() {
soapy12312 2:da0f01fbd25c 67 displayInstructions();
soapy12312 2:da0f01fbd25c 68 selectDifficulty();
soapy12312 2:da0f01fbd25c 69 countdown();
soapy12312 2:da0f01fbd25c 70 playGame();
soapy12312 2:da0f01fbd25c 71 gameOver();
soapy12312 2:da0f01fbd25c 72 }
soapy12312 2:da0f01fbd25c 73
soapy12312 2:da0f01fbd25c 74
soapy12312 2:da0f01fbd25c 75 private:
soapy12312 2:da0f01fbd25c 76 AnalogIn &noise;
soapy12312 2:da0f01fbd25c 77 DigitalOut &reset;
soapy12312 2:da0f01fbd25c 78 PinDetect &button1;
soapy12312 2:da0f01fbd25c 79 PinDetect &button2;
soapy12312 2:da0f01fbd25c 80 PinDetect &button3;
soapy12312 2:da0f01fbd25c 81 PinDetect &button4;
soapy12312 2:da0f01fbd25c 82 SDFileSystem &sd;
soapy12312 2:da0f01fbd25c 83 Serial &XBee;
soapy12312 2:da0f01fbd25c 84 uLCD_4DGL &uLCD;
soapy12312 2:da0f01fbd25c 85 wave_player &waver;
soapy12312 2:da0f01fbd25c 86 volatile bool oneHit;
soapy12312 2:da0f01fbd25c 87 volatile bool twoHit;
soapy12312 2:da0f01fbd25c 88 volatile bool threeHit;
soapy12312 2:da0f01fbd25c 89 volatile bool fourHit;
soapy12312 2:da0f01fbd25c 90 int difficulty;
soapy12312 2:da0f01fbd25c 91 int sequence[MAXLENGTH];
soapy12312 2:da0f01fbd25c 92 int playerSequence[MAXLENGTH];
soapy12312 2:da0f01fbd25c 93 bool otherPlayerWin;
soapy12312 2:da0f01fbd25c 94
soapy12312 2:da0f01fbd25c 95 // Color the background of one of the four corner
soapy12312 2:da0f01fbd25c 96 void drawBack(int position, int color) {
soapy12312 2:da0f01fbd25c 97 int x11, x12, x21, x22, y11, y12, y21, y22;
soapy12312 2:da0f01fbd25c 98
soapy12312 2:da0f01fbd25c 99 switch (position) {
soapy12312 2:da0f01fbd25c 100 case 3:
soapy12312 2:da0f01fbd25c 101 x11 = 64;
soapy12312 2:da0f01fbd25c 102 x12 = 127;
soapy12312 2:da0f01fbd25c 103 x21 = 85;
soapy12312 2:da0f01fbd25c 104 x22 = 127;
soapy12312 2:da0f01fbd25c 105 y11 = 85;
soapy12312 2:da0f01fbd25c 106 y12 = 127;
soapy12312 2:da0f01fbd25c 107 y21 = 64;
soapy12312 2:da0f01fbd25c 108 y22 = 84;
soapy12312 2:da0f01fbd25c 109 break;
soapy12312 2:da0f01fbd25c 110 case 2:
soapy12312 2:da0f01fbd25c 111 x11 = 0;
soapy12312 2:da0f01fbd25c 112 x12 = 63;
soapy12312 2:da0f01fbd25c 113 x21 = 0;
soapy12312 2:da0f01fbd25c 114 x22 = 42;
soapy12312 2:da0f01fbd25c 115 y11 = 85;
soapy12312 2:da0f01fbd25c 116 y12 = 127;
soapy12312 2:da0f01fbd25c 117 y21 = 64;
soapy12312 2:da0f01fbd25c 118 y22 = 84;
soapy12312 2:da0f01fbd25c 119 break;
soapy12312 2:da0f01fbd25c 120 case 1:
soapy12312 2:da0f01fbd25c 121 x11 = 0;
soapy12312 2:da0f01fbd25c 122 x12 = 63;
soapy12312 2:da0f01fbd25c 123 x21 = 0;
soapy12312 2:da0f01fbd25c 124 x22 = 42;
soapy12312 2:da0f01fbd25c 125 y11 = 0;
soapy12312 2:da0f01fbd25c 126 y12 = 42;
soapy12312 2:da0f01fbd25c 127 y21 = 43;
soapy12312 2:da0f01fbd25c 128 y22 = 63;
soapy12312 2:da0f01fbd25c 129 break;
soapy12312 2:da0f01fbd25c 130 default:
soapy12312 2:da0f01fbd25c 131 x11 = 64;
soapy12312 2:da0f01fbd25c 132 x12 = 127;
soapy12312 2:da0f01fbd25c 133 x21 = 85;
soapy12312 2:da0f01fbd25c 134 x22 = 127;
soapy12312 2:da0f01fbd25c 135 y11 = 0;
soapy12312 2:da0f01fbd25c 136 y12 = 42;
soapy12312 2:da0f01fbd25c 137 y21 = 43;
soapy12312 2:da0f01fbd25c 138 y22 = 63;
soapy12312 2:da0f01fbd25c 139 }
soapy12312 2:da0f01fbd25c 140
soapy12312 2:da0f01fbd25c 141 uLCD.filled_rectangle(x11, y11, x12, y12, color);
soapy12312 2:da0f01fbd25c 142 uLCD.filled_rectangle(x21, y21, x22, y22, color);
soapy12312 2:da0f01fbd25c 143 }
soapy12312 2:da0f01fbd25c 144
soapy12312 2:da0f01fbd25c 145 // Color the foreground of one of the four corners
soapy12312 2:da0f01fbd25c 146 void drawFore(int position, int color) {
soapy12312 2:da0f01fbd25c 147 int x11, x12, x21, x22, y11, y12, y21, y22;
soapy12312 2:da0f01fbd25c 148
soapy12312 2:da0f01fbd25c 149 switch (position) {
soapy12312 2:da0f01fbd25c 150 case 3:
soapy12312 2:da0f01fbd25c 151 x11 = 68;
soapy12312 2:da0f01fbd25c 152 x12 = 123;
soapy12312 2:da0f01fbd25c 153 x21 = 89;
soapy12312 2:da0f01fbd25c 154 x22 = 123;
soapy12312 2:da0f01fbd25c 155 y11 = 89;
soapy12312 2:da0f01fbd25c 156 y12 = 123;
soapy12312 2:da0f01fbd25c 157 y21 = 68;
soapy12312 2:da0f01fbd25c 158 y22 = 89;
soapy12312 2:da0f01fbd25c 159 break;
soapy12312 2:da0f01fbd25c 160 case 2:
soapy12312 2:da0f01fbd25c 161 x11 = 4;
soapy12312 2:da0f01fbd25c 162 x12 = 59;
soapy12312 2:da0f01fbd25c 163 x21 = 4;
soapy12312 2:da0f01fbd25c 164 x22 = 38;
soapy12312 2:da0f01fbd25c 165 y11 = 89;
soapy12312 2:da0f01fbd25c 166 y12 = 123;
soapy12312 2:da0f01fbd25c 167 y21 = 68;
soapy12312 2:da0f01fbd25c 168 y22 = 89;
soapy12312 2:da0f01fbd25c 169 break;
soapy12312 2:da0f01fbd25c 170 case 1:
soapy12312 2:da0f01fbd25c 171 x11 = 4;
soapy12312 2:da0f01fbd25c 172 x12 = 59;
soapy12312 2:da0f01fbd25c 173 x21 = 4;
soapy12312 2:da0f01fbd25c 174 x22 = 38;
soapy12312 2:da0f01fbd25c 175 y11 = 4;
soapy12312 2:da0f01fbd25c 176 y12 = 38;
soapy12312 2:da0f01fbd25c 177 y21 = 39;
soapy12312 2:da0f01fbd25c 178 y22 = 59;
soapy12312 2:da0f01fbd25c 179 break;
soapy12312 2:da0f01fbd25c 180 default:
soapy12312 2:da0f01fbd25c 181 x11 = 68;
soapy12312 2:da0f01fbd25c 182 x12 = 123;
soapy12312 2:da0f01fbd25c 183 x21 = 89;
soapy12312 2:da0f01fbd25c 184 x22 = 123;
soapy12312 2:da0f01fbd25c 185 y11 = 4;
soapy12312 2:da0f01fbd25c 186 y12 = 38;
soapy12312 2:da0f01fbd25c 187 y21 = 39;
soapy12312 2:da0f01fbd25c 188 y22 = 59;
soapy12312 2:da0f01fbd25c 189 }
soapy12312 2:da0f01fbd25c 190
soapy12312 2:da0f01fbd25c 191 uLCD.filled_rectangle(x11, y11, x12, y12, color);
soapy12312 2:da0f01fbd25c 192 uLCD.filled_rectangle(x21, y21, x22, y22, color);
soapy12312 2:da0f01fbd25c 193 }
soapy12312 2:da0f01fbd25c 194
soapy12312 2:da0f01fbd25c 195 // Print a number in the middle of the screen
soapy12312 2:da0f01fbd25c 196 void drawNum(int i) {
soapy12312 2:da0f01fbd25c 197 uLCD.text_width(3);
soapy12312 2:da0f01fbd25c 198 uLCD.text_height(3);
soapy12312 2:da0f01fbd25c 199 uLCD.color(BLACK);
soapy12312 2:da0f01fbd25c 200 uLCD.locate(2, 2);
soapy12312 2:da0f01fbd25c 201 uLCD.printf("%2D", i);
soapy12312 2:da0f01fbd25c 202 }
soapy12312 2:da0f01fbd25c 203
soapy12312 2:da0f01fbd25c 204 // Create a random sequence
soapy12312 2:da0f01fbd25c 205 void createSequence(int *seq, int length) {
soapy12312 2:da0f01fbd25c 206 // Seed using noise from analog pin
soapy12312 2:da0f01fbd25c 207 srand((int)(noise * 50000 / 43));
soapy12312 2:da0f01fbd25c 208
soapy12312 2:da0f01fbd25c 209 for (int i = 0; i < length; i++) {
soapy12312 2:da0f01fbd25c 210 seq[i] = rand() % 4;
soapy12312 2:da0f01fbd25c 211 }
soapy12312 2:da0f01fbd25c 212
soapy12312 2:da0f01fbd25c 213 for (int i = length; i < MAXLENGTH; i++) {
soapy12312 2:da0f01fbd25c 214 seq[i] = -1;
soapy12312 2:da0f01fbd25c 215 }
soapy12312 2:da0f01fbd25c 216 }
soapy12312 2:da0f01fbd25c 217
soapy12312 2:da0f01fbd25c 218 // Reset the hits to false
soapy12312 2:da0f01fbd25c 219 void resetHits() {
soapy12312 2:da0f01fbd25c 220 oneHit = false;
soapy12312 2:da0f01fbd25c 221 twoHit = false;
soapy12312 2:da0f01fbd25c 222 threeHit = false;
soapy12312 2:da0f01fbd25c 223 fourHit = false;
soapy12312 2:da0f01fbd25c 224 }
soapy12312 2:da0f01fbd25c 225
soapy12312 2:da0f01fbd25c 226 // Determine if all hits are false
soapy12312 2:da0f01fbd25c 227 bool noHits() {
soapy12312 2:da0f01fbd25c 228 return ((oneHit == false) && (twoHit == false) && (threeHit == false) && (fourHit == false));
soapy12312 2:da0f01fbd25c 229 }
soapy12312 2:da0f01fbd25c 230
soapy12312 2:da0f01fbd25c 231 // Wait for player input to go to next page
soapy12312 2:da0f01fbd25c 232 void nextPage() {
soapy12312 2:da0f01fbd25c 233 resetHits();
soapy12312 2:da0f01fbd25c 234
soapy12312 2:da0f01fbd25c 235 while (noHits() == true) {
soapy12312 2:da0f01fbd25c 236 }
soapy12312 2:da0f01fbd25c 237
soapy12312 2:da0f01fbd25c 238 uLCD.cls();
soapy12312 2:da0f01fbd25c 239 resetHits();
soapy12312 2:da0f01fbd25c 240 }
soapy12312 2:da0f01fbd25c 241
soapy12312 2:da0f01fbd25c 242 // Push button 1 interrupt routine
soapy12312 2:da0f01fbd25c 243 void button1Hit() {
soapy12312 2:da0f01fbd25c 244 oneHit = true;
soapy12312 2:da0f01fbd25c 245 }
soapy12312 2:da0f01fbd25c 246
soapy12312 2:da0f01fbd25c 247 // Push button 2 interrupt routine
soapy12312 2:da0f01fbd25c 248 void button2Hit() {
soapy12312 2:da0f01fbd25c 249 twoHit = true;
soapy12312 2:da0f01fbd25c 250 }
soapy12312 2:da0f01fbd25c 251
soapy12312 2:da0f01fbd25c 252 // Push button 3 interrupt routine
soapy12312 2:da0f01fbd25c 253 void button3Hit() {
soapy12312 2:da0f01fbd25c 254 threeHit = true;
soapy12312 2:da0f01fbd25c 255 }
soapy12312 2:da0f01fbd25c 256
soapy12312 2:da0f01fbd25c 257 // Push button 4 interrupt routine
soapy12312 2:da0f01fbd25c 258 void button4Hit() {
soapy12312 2:da0f01fbd25c 259 fourHit = true;
soapy12312 2:da0f01fbd25c 260 }
soapy12312 2:da0f01fbd25c 261
soapy12312 2:da0f01fbd25c 262 // Determine whether the player wants to view instructions
soapy12312 2:da0f01fbd25c 263 bool viewInstructions() {
soapy12312 2:da0f01fbd25c 264 bool viewInstructions = false;
soapy12312 2:da0f01fbd25c 265
soapy12312 2:da0f01fbd25c 266 resetHits();
soapy12312 2:da0f01fbd25c 267
soapy12312 2:da0f01fbd25c 268 // Wait for pb1 or pb2 to be pressed
soapy12312 2:da0f01fbd25c 269 while ((oneHit == false) && (twoHit == false)) {
soapy12312 2:da0f01fbd25c 270 wait(1);
soapy12312 2:da0f01fbd25c 271
soapy12312 2:da0f01fbd25c 272 // View instructions
soapy12312 2:da0f01fbd25c 273 if (oneHit == true) {
soapy12312 2:da0f01fbd25c 274 uLCD.cls();
soapy12312 2:da0f01fbd25c 275 viewInstructions = true;
soapy12312 2:da0f01fbd25c 276 // Skip instructions
soapy12312 2:da0f01fbd25c 277 } else if (twoHit == true) {
soapy12312 2:da0f01fbd25c 278 uLCD.cls();
soapy12312 2:da0f01fbd25c 279 viewInstructions = false;
soapy12312 2:da0f01fbd25c 280 } else {
soapy12312 2:da0f01fbd25c 281 resetHits();
soapy12312 2:da0f01fbd25c 282 }
soapy12312 2:da0f01fbd25c 283 }
soapy12312 2:da0f01fbd25c 284
soapy12312 2:da0f01fbd25c 285 return viewInstructions;
soapy12312 2:da0f01fbd25c 286 }
soapy12312 2:da0f01fbd25c 287
soapy12312 2:da0f01fbd25c 288 // Play the sequence according to the difficulty
soapy12312 2:da0f01fbd25c 289 void playSequence(int *sequence) {
soapy12312 2:da0f01fbd25c 290 // Interval between sequence elements
soapy12312 2:da0f01fbd25c 291 float interval;
soapy12312 2:da0f01fbd25c 292
soapy12312 2:da0f01fbd25c 293 // Set interval based on difficulty
soapy12312 2:da0f01fbd25c 294 if (difficulty == EASY) {
soapy12312 2:da0f01fbd25c 295 interval = 1.5f;
soapy12312 2:da0f01fbd25c 296 } else {
soapy12312 2:da0f01fbd25c 297 interval = 1.0f;
soapy12312 2:da0f01fbd25c 298 }
soapy12312 2:da0f01fbd25c 299
soapy12312 2:da0f01fbd25c 300 // On Easy or Normal difficulty
soapy12312 2:da0f01fbd25c 301 if (difficulty == EASY || difficulty == NORMAL) {
soapy12312 2:da0f01fbd25c 302 int i = 0;
soapy12312 2:da0f01fbd25c 303
soapy12312 2:da0f01fbd25c 304 // Loop through the sequence
soapy12312 2:da0f01fbd25c 305 while (i < MAXLENGTH && sequence[i] != -1) {
soapy12312 2:da0f01fbd25c 306 // Display position, color, and play sound at corresponding intervals
soapy12312 2:da0f01fbd25c 307 if (sequence[i] == 0) {
soapy12312 2:da0f01fbd25c 308 drawFore(0, RED);
soapy12312 2:da0f01fbd25c 309 playSound(A3);
soapy12312 2:da0f01fbd25c 310 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 311 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 312 wait(0.25);
soapy12312 2:da0f01fbd25c 313 } else if (sequence[i] == 1) {
soapy12312 2:da0f01fbd25c 314 drawFore(1, GREEN);
soapy12312 2:da0f01fbd25c 315 playSound(E3);
soapy12312 2:da0f01fbd25c 316 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 317 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 318 wait(0.25);
soapy12312 2:da0f01fbd25c 319 } else if (sequence[i] == 2) {
soapy12312 2:da0f01fbd25c 320 drawFore(2, YELLOW);
soapy12312 2:da0f01fbd25c 321 playSound(C4);
soapy12312 2:da0f01fbd25c 322 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 323 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 324 wait(0.25);
soapy12312 2:da0f01fbd25c 325 } else {
soapy12312 2:da0f01fbd25c 326 drawFore(3, BLUE);
soapy12312 2:da0f01fbd25c 327 playSound(E4);
soapy12312 2:da0f01fbd25c 328 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 329 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 330 wait(0.25);
soapy12312 2:da0f01fbd25c 331 }
soapy12312 2:da0f01fbd25c 332
soapy12312 2:da0f01fbd25c 333 i++;
soapy12312 2:da0f01fbd25c 334 }
soapy12312 2:da0f01fbd25c 335 // On Heroic difficulty
soapy12312 2:da0f01fbd25c 336 } else if (difficulty == HEROIC) {
soapy12312 2:da0f01fbd25c 337 int i = 0;
soapy12312 2:da0f01fbd25c 338
soapy12312 2:da0f01fbd25c 339 // Loop through the sequence
soapy12312 2:da0f01fbd25c 340 while (i < MAXLENGTH && sequence[i] != -1) {
soapy12312 2:da0f01fbd25c 341 // Display color with random position and sound at 1.0-second intervals
soapy12312 2:da0f01fbd25c 342 if (sequence[i] == 0) {
soapy12312 2:da0f01fbd25c 343 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 344 int sound = rand() % 4;
soapy12312 2:da0f01fbd25c 345 drawFore(position, RED);
soapy12312 2:da0f01fbd25c 346
soapy12312 2:da0f01fbd25c 347 if (sound == 0) {
soapy12312 2:da0f01fbd25c 348 playSound(E3);
soapy12312 2:da0f01fbd25c 349 } else if (sound == 1) {
soapy12312 2:da0f01fbd25c 350 playSound(A3);
soapy12312 2:da0f01fbd25c 351 } else if (sound == 2) {
soapy12312 2:da0f01fbd25c 352 playSound(C4);
soapy12312 2:da0f01fbd25c 353 } else {
soapy12312 2:da0f01fbd25c 354 playSound(E4);
soapy12312 2:da0f01fbd25c 355 }
soapy12312 2:da0f01fbd25c 356
soapy12312 2:da0f01fbd25c 357 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 358 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 359 wait(0.25);
soapy12312 2:da0f01fbd25c 360 } else if (sequence[i] == 1) {
soapy12312 2:da0f01fbd25c 361 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 362 int sound = rand() % 4;
soapy12312 2:da0f01fbd25c 363 drawFore(position, GREEN);
soapy12312 2:da0f01fbd25c 364
soapy12312 2:da0f01fbd25c 365 if (sound == 0) {
soapy12312 2:da0f01fbd25c 366 playSound(E3);
soapy12312 2:da0f01fbd25c 367 } else if (sound == 1) {
soapy12312 2:da0f01fbd25c 368 playSound(A3);
soapy12312 2:da0f01fbd25c 369 } else if (sound == 2) {
soapy12312 2:da0f01fbd25c 370 playSound(C4);
soapy12312 2:da0f01fbd25c 371 } else {
soapy12312 2:da0f01fbd25c 372 playSound(E4);
soapy12312 2:da0f01fbd25c 373 }
soapy12312 2:da0f01fbd25c 374
soapy12312 2:da0f01fbd25c 375 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 376 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 377 wait(0.25);
soapy12312 2:da0f01fbd25c 378 } else if (sequence[i] == 2) {
soapy12312 2:da0f01fbd25c 379 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 380 int sound = rand() % 4;
soapy12312 2:da0f01fbd25c 381 drawFore(position, YELLOW);
soapy12312 2:da0f01fbd25c 382
soapy12312 2:da0f01fbd25c 383 if (sound == 0) {
soapy12312 2:da0f01fbd25c 384 playSound(E3);
soapy12312 2:da0f01fbd25c 385 } else if (sound == 1) {
soapy12312 2:da0f01fbd25c 386 playSound(A3);
soapy12312 2:da0f01fbd25c 387 } else if (sound == 2) {
soapy12312 2:da0f01fbd25c 388 playSound(C4);
soapy12312 2:da0f01fbd25c 389 } else {
soapy12312 2:da0f01fbd25c 390 playSound(E4);
soapy12312 2:da0f01fbd25c 391 }
soapy12312 2:da0f01fbd25c 392
soapy12312 2:da0f01fbd25c 393 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 394 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 395 wait(0.25);
soapy12312 2:da0f01fbd25c 396 } else {
soapy12312 2:da0f01fbd25c 397 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 398 int sound = rand() % 4;
soapy12312 2:da0f01fbd25c 399 drawFore(position, BLUE);
soapy12312 2:da0f01fbd25c 400
soapy12312 2:da0f01fbd25c 401 if (sound == 0) {
soapy12312 2:da0f01fbd25c 402 playSound(E3);
soapy12312 2:da0f01fbd25c 403 } else if (sound == 1) {
soapy12312 2:da0f01fbd25c 404 playSound(A3);
soapy12312 2:da0f01fbd25c 405 } else if (sound == 2) {
soapy12312 2:da0f01fbd25c 406 playSound(C4);
soapy12312 2:da0f01fbd25c 407 } else {
soapy12312 2:da0f01fbd25c 408 playSound(E4);
soapy12312 2:da0f01fbd25c 409 }
soapy12312 2:da0f01fbd25c 410
soapy12312 2:da0f01fbd25c 411 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 412 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 413 wait(0.25);
soapy12312 2:da0f01fbd25c 414 }
soapy12312 2:da0f01fbd25c 415
soapy12312 2:da0f01fbd25c 416 i++;
soapy12312 2:da0f01fbd25c 417 }
soapy12312 2:da0f01fbd25c 418 // On Legendary difficulty
soapy12312 2:da0f01fbd25c 419 } else {
soapy12312 2:da0f01fbd25c 420 int i = 0;
soapy12312 2:da0f01fbd25c 421
soapy12312 2:da0f01fbd25c 422 // Loop through the sequence
soapy12312 2:da0f01fbd25c 423 while (i < MAXLENGTH && sequence[i] != -1) {
soapy12312 2:da0f01fbd25c 424 // Play sound with random color and position at 1.0-second intervals
soapy12312 2:da0f01fbd25c 425 if (sequence[i] == 0) {
soapy12312 2:da0f01fbd25c 426 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 427 int color = rand() % 4;
soapy12312 2:da0f01fbd25c 428
soapy12312 2:da0f01fbd25c 429 if (color == 0) {
soapy12312 2:da0f01fbd25c 430 drawFore(position, RED);
soapy12312 2:da0f01fbd25c 431 } else if (color == 1) {
soapy12312 2:da0f01fbd25c 432 drawFore(position, GREEN);
soapy12312 2:da0f01fbd25c 433 } else if (color == 2) {
soapy12312 2:da0f01fbd25c 434 drawFore(position, YELLOW);
soapy12312 2:da0f01fbd25c 435 } else {
soapy12312 2:da0f01fbd25c 436 drawFore(position, BLUE);
soapy12312 2:da0f01fbd25c 437 }
soapy12312 2:da0f01fbd25c 438
soapy12312 2:da0f01fbd25c 439 playSound(A3);
soapy12312 2:da0f01fbd25c 440 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 441 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 442 wait(0.25);
soapy12312 2:da0f01fbd25c 443 } else if (sequence[i] == 1) {
soapy12312 2:da0f01fbd25c 444 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 445 int color = rand() % 4;
soapy12312 2:da0f01fbd25c 446
soapy12312 2:da0f01fbd25c 447 if (color == 0) {
soapy12312 2:da0f01fbd25c 448 drawFore(position, RED);
soapy12312 2:da0f01fbd25c 449 } else if (color == 1) {
soapy12312 2:da0f01fbd25c 450 drawFore(position, GREEN);
soapy12312 2:da0f01fbd25c 451 } else if (color == 2) {
soapy12312 2:da0f01fbd25c 452 drawFore(position, YELLOW);
soapy12312 2:da0f01fbd25c 453 } else {
soapy12312 2:da0f01fbd25c 454 drawFore(position, BLUE);
soapy12312 2:da0f01fbd25c 455 }
soapy12312 2:da0f01fbd25c 456
soapy12312 2:da0f01fbd25c 457 playSound(E3);
soapy12312 2:da0f01fbd25c 458 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 459 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 460 wait(0.25);
soapy12312 2:da0f01fbd25c 461 } else if (sequence[i] == 2) {
soapy12312 2:da0f01fbd25c 462 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 463 int color = rand() % 4;
soapy12312 2:da0f01fbd25c 464
soapy12312 2:da0f01fbd25c 465 if (color == 0) {
soapy12312 2:da0f01fbd25c 466 drawFore(position, RED);
soapy12312 2:da0f01fbd25c 467 } else if (color == 1) {
soapy12312 2:da0f01fbd25c 468 drawFore(position, GREEN);
soapy12312 2:da0f01fbd25c 469 } else if (color == 2) {
soapy12312 2:da0f01fbd25c 470 drawFore(position, YELLOW);
soapy12312 2:da0f01fbd25c 471 } else {
soapy12312 2:da0f01fbd25c 472 drawFore(position, BLUE);
soapy12312 2:da0f01fbd25c 473 }
soapy12312 2:da0f01fbd25c 474
soapy12312 2:da0f01fbd25c 475 playSound(C4);
soapy12312 2:da0f01fbd25c 476 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 477 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 478 wait(0.25);
soapy12312 2:da0f01fbd25c 479 } else {
soapy12312 2:da0f01fbd25c 480 int position = rand() % 4;
soapy12312 2:da0f01fbd25c 481 int color = rand() % 4;
soapy12312 2:da0f01fbd25c 482
soapy12312 2:da0f01fbd25c 483 if (color == 0) {
soapy12312 2:da0f01fbd25c 484 drawFore(position, RED);
soapy12312 2:da0f01fbd25c 485 } else if (color == 1) {
soapy12312 2:da0f01fbd25c 486 drawFore(position, GREEN);
soapy12312 2:da0f01fbd25c 487 } else if (color == 2) {
soapy12312 2:da0f01fbd25c 488 drawFore(position, YELLOW);
soapy12312 2:da0f01fbd25c 489 } else {
soapy12312 2:da0f01fbd25c 490 drawFore(position, BLUE);
soapy12312 2:da0f01fbd25c 491 }
soapy12312 2:da0f01fbd25c 492
soapy12312 2:da0f01fbd25c 493 playSound(E4);
soapy12312 2:da0f01fbd25c 494 wait(interval - 0.25);
soapy12312 2:da0f01fbd25c 495 drawBack(position, WHITE);
soapy12312 2:da0f01fbd25c 496 wait(0.25);
soapy12312 2:da0f01fbd25c 497 }
soapy12312 2:da0f01fbd25c 498
soapy12312 2:da0f01fbd25c 499 i++;
soapy12312 2:da0f01fbd25c 500 }
soapy12312 2:da0f01fbd25c 501 }
soapy12312 2:da0f01fbd25c 502 }
soapy12312 2:da0f01fbd25c 503
soapy12312 2:da0f01fbd25c 504 // Play a sound file
soapy12312 2:da0f01fbd25c 505 void playSound(char *wav) {
soapy12312 2:da0f01fbd25c 506 // Open sound file
soapy12312 2:da0f01fbd25c 507 FILE *wave_file;
soapy12312 2:da0f01fbd25c 508 wave_file = fopen(wav, "r");
soapy12312 2:da0f01fbd25c 509
soapy12312 2:da0f01fbd25c 510 // Play wav file
soapy12312 2:da0f01fbd25c 511 waver.play(wave_file);
soapy12312 2:da0f01fbd25c 512
soapy12312 2:da0f01fbd25c 513 // Close wav file
soapy12312 2:da0f01fbd25c 514 fclose(wave_file);
soapy12312 2:da0f01fbd25c 515 }
soapy12312 2:da0f01fbd25c 516
soapy12312 2:da0f01fbd25c 517 // Reset the values in the sequence
soapy12312 2:da0f01fbd25c 518 void resetSequence(int *sequence, int length) {
soapy12312 2:da0f01fbd25c 519 for (int i = 0; i < length; i++) {
soapy12312 2:da0f01fbd25c 520 sequence[i] = -1;
soapy12312 2:da0f01fbd25c 521 }
soapy12312 2:da0f01fbd25c 522 }
soapy12312 2:da0f01fbd25c 523
soapy12312 2:da0f01fbd25c 524 // Display instructions
soapy12312 2:da0f01fbd25c 525 void displayInstructions() {
soapy12312 2:da0f01fbd25c 526 uLCD.printf("Hello, and welcometo a game of\nmultiplayer Simon.");
soapy12312 2:da0f01fbd25c 527 uLCD.printf("First player to ");
soapy12312 2:da0f01fbd25c 528 uLCD.printf("reach %d points ", MAXLENGTH);
soapy12312 2:da0f01fbd25c 529 uLCD.printf("wins!\n\n");
soapy12312 2:da0f01fbd25c 530 uLCD.printf("Press pb1 to view instructions or\npb2 to skip to\ndifficulty select-ion.");
soapy12312 2:da0f01fbd25c 531
soapy12312 2:da0f01fbd25c 532 // If player chooses to view instructions
soapy12312 2:da0f01fbd25c 533 if (viewInstructions() == true) {
soapy12312 2:da0f01fbd25c 534 // Gameplay instructions
soapy12312 2:da0f01fbd25c 535 uLCD.printf("Sequences will be-gin with only one element. The numb-er of elements\n");
soapy12312 2:da0f01fbd25c 536 uLCD.printf("increases by one\nupon earning a po-int, and vise ver-sa. ");
soapy12312 2:da0f01fbd25c 537 uLCD.printf("Remember, you must correctly getALL the values in a ");
soapy12312 2:da0f01fbd25c 538 uLCD.printf("sequence to earna point.\n\n");
soapy12312 2:da0f01fbd25c 539 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 540
soapy12312 2:da0f01fbd25c 541 nextPage();
soapy12312 2:da0f01fbd25c 542
soapy12312 2:da0f01fbd25c 543 uLCD.printf("Each push button\nhas its own uniqueposition, color,\nand sound.\n\n");
soapy12312 2:da0f01fbd25c 544 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 545
soapy12312 2:da0f01fbd25c 546 nextPage();
soapy12312 2:da0f01fbd25c 547
soapy12312 2:da0f01fbd25c 548 // Push button 1
soapy12312 2:da0f01fbd25c 549 uLCD.printf("pb1: top right,\nred, note A3\n\n");
soapy12312 2:da0f01fbd25c 550 uLCD.printf("Press pb1 for a\ndemonstration.");
soapy12312 2:da0f01fbd25c 551
soapy12312 2:da0f01fbd25c 552 while (oneHit == false) {
soapy12312 2:da0f01fbd25c 553 }
soapy12312 2:da0f01fbd25c 554
soapy12312 2:da0f01fbd25c 555 uLCD.background_color(WHITE);
soapy12312 2:da0f01fbd25c 556 uLCD.cls();
soapy12312 2:da0f01fbd25c 557 wait(0.5);
soapy12312 2:da0f01fbd25c 558
soapy12312 2:da0f01fbd25c 559 for (int i = 0; i < 5; i++) {
soapy12312 2:da0f01fbd25c 560 drawFore(0, RED);
soapy12312 2:da0f01fbd25c 561 playSound(A3);
soapy12312 2:da0f01fbd25c 562 wait(1);
soapy12312 2:da0f01fbd25c 563 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 564 wait(0.5);
soapy12312 2:da0f01fbd25c 565 }
soapy12312 2:da0f01fbd25c 566
soapy12312 2:da0f01fbd25c 567 uLCD.background_color(BLACK);
soapy12312 2:da0f01fbd25c 568 uLCD.cls();
soapy12312 2:da0f01fbd25c 569 uLCD.printf("pb1: top right,\nred, note A3\n\n");
soapy12312 2:da0f01fbd25c 570 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 571
soapy12312 2:da0f01fbd25c 572 nextPage();
soapy12312 2:da0f01fbd25c 573
soapy12312 2:da0f01fbd25c 574 // Push button 2
soapy12312 2:da0f01fbd25c 575 uLCD.printf("pb2: top left, gr-een, note E3\n\n");
soapy12312 2:da0f01fbd25c 576 uLCD.printf("Press pb2 for a\ndemonstration.");
soapy12312 2:da0f01fbd25c 577
soapy12312 2:da0f01fbd25c 578 while (twoHit == false) {
soapy12312 2:da0f01fbd25c 579 }
soapy12312 2:da0f01fbd25c 580
soapy12312 2:da0f01fbd25c 581 uLCD.background_color(WHITE);
soapy12312 2:da0f01fbd25c 582 uLCD.cls();
soapy12312 2:da0f01fbd25c 583 wait(0.5);
soapy12312 2:da0f01fbd25c 584
soapy12312 2:da0f01fbd25c 585 for (int i = 0; i < 5; i++) {
soapy12312 2:da0f01fbd25c 586 drawFore(1, GREEN);
soapy12312 2:da0f01fbd25c 587 playSound(E3);
soapy12312 2:da0f01fbd25c 588 wait(1);
soapy12312 2:da0f01fbd25c 589 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 590 wait(0.5);
soapy12312 2:da0f01fbd25c 591 }
soapy12312 2:da0f01fbd25c 592
soapy12312 2:da0f01fbd25c 593 uLCD.background_color(BLACK);
soapy12312 2:da0f01fbd25c 594 uLCD.cls();
soapy12312 2:da0f01fbd25c 595 uLCD.printf("pb2: top left, gr-een, note E3\n\n");
soapy12312 2:da0f01fbd25c 596 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 597
soapy12312 2:da0f01fbd25c 598 nextPage();
soapy12312 2:da0f01fbd25c 599
soapy12312 2:da0f01fbd25c 600 // Push button 3
soapy12312 2:da0f01fbd25c 601 uLCD.printf("pb3: bottom left,\nyellow, note C4\n\n");
soapy12312 2:da0f01fbd25c 602 uLCD.printf("Press pb3 for a\ndemonstration.");
soapy12312 2:da0f01fbd25c 603
soapy12312 2:da0f01fbd25c 604 while (threeHit == false) {
soapy12312 2:da0f01fbd25c 605 }
soapy12312 2:da0f01fbd25c 606
soapy12312 2:da0f01fbd25c 607 uLCD.background_color(WHITE);
soapy12312 2:da0f01fbd25c 608 uLCD.cls();
soapy12312 2:da0f01fbd25c 609 wait(0.5);
soapy12312 2:da0f01fbd25c 610
soapy12312 2:da0f01fbd25c 611 for (int i = 0; i < 5; i++) {
soapy12312 2:da0f01fbd25c 612 drawFore(2, YELLOW);
soapy12312 2:da0f01fbd25c 613 playSound(C4);
soapy12312 2:da0f01fbd25c 614 wait(1);
soapy12312 2:da0f01fbd25c 615 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 616 wait(0.5);
soapy12312 2:da0f01fbd25c 617 }
soapy12312 2:da0f01fbd25c 618
soapy12312 2:da0f01fbd25c 619 uLCD.background_color(BLACK);
soapy12312 2:da0f01fbd25c 620 uLCD.cls();
soapy12312 2:da0f01fbd25c 621 uLCD.printf("pb3: bottom left,\nyellow, note C4\n\n");
soapy12312 2:da0f01fbd25c 622 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 623
soapy12312 2:da0f01fbd25c 624 nextPage();
soapy12312 2:da0f01fbd25c 625
soapy12312 2:da0f01fbd25c 626 // Push button 4
soapy12312 2:da0f01fbd25c 627 uLCD.printf("pb4: bottom right,blue, note E4\n\n");
soapy12312 2:da0f01fbd25c 628 uLCD.printf("Press pb4 for a\ndemonstration.");
soapy12312 2:da0f01fbd25c 629
soapy12312 2:da0f01fbd25c 630 while (fourHit == false) {
soapy12312 2:da0f01fbd25c 631 }
soapy12312 2:da0f01fbd25c 632
soapy12312 2:da0f01fbd25c 633 uLCD.background_color(WHITE);
soapy12312 2:da0f01fbd25c 634 uLCD.cls();
soapy12312 2:da0f01fbd25c 635 wait(0.5);
soapy12312 2:da0f01fbd25c 636
soapy12312 2:da0f01fbd25c 637 for (int i = 0; i < 5; i++) {
soapy12312 2:da0f01fbd25c 638 drawFore(3, BLUE);
soapy12312 2:da0f01fbd25c 639 playSound(E4);
soapy12312 2:da0f01fbd25c 640 wait(1);
soapy12312 2:da0f01fbd25c 641 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 642 wait(0.5);
soapy12312 2:da0f01fbd25c 643 }
soapy12312 2:da0f01fbd25c 644
soapy12312 2:da0f01fbd25c 645 uLCD.background_color(BLACK);
soapy12312 2:da0f01fbd25c 646 uLCD.cls();
soapy12312 2:da0f01fbd25c 647 uLCD.printf("pb4: bottom right,blue, note E4\n\n");
soapy12312 2:da0f01fbd25c 648 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 649
soapy12312 2:da0f01fbd25c 650 nextPage();
soapy12312 2:da0f01fbd25c 651
soapy12312 2:da0f01fbd25c 652 uLCD.printf("There are 4 diffi-culties to choose from:\n\n");
soapy12312 2:da0f01fbd25c 653 uLCD.printf("Easy, Normal,\nHeroic, and\nLegendary.\n\n");
soapy12312 2:da0f01fbd25c 654 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 655
soapy12312 2:da0f01fbd25c 656 nextPage();
soapy12312 2:da0f01fbd25c 657
soapy12312 2:da0f01fbd25c 658 // Different difficulties
soapy12312 2:da0f01fbd25c 659 uLCD.printf("Easy difficulty:\n\nThe timing betweenthe values of a\n");
soapy12312 2:da0f01fbd25c 660 uLCD.printf("sequence is kept\nat constant 1.5-\nsecond intervals.\n\n");
soapy12312 2:da0f01fbd25c 661 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 662
soapy12312 2:da0f01fbd25c 663 nextPage();
soapy12312 2:da0f01fbd25c 664
soapy12312 2:da0f01fbd25c 665 uLCD.printf("Normal difficulty:\nThe timing betweenthe values of a\n");
soapy12312 2:da0f01fbd25c 666 uLCD.printf("sequence is kept\nat constant 1.0-\nsecond intervals.\n\n");
soapy12312 2:da0f01fbd25c 667 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 668
soapy12312 2:da0f01fbd25c 669 nextPage();
soapy12312 2:da0f01fbd25c 670
soapy12312 2:da0f01fbd25c 671 uLCD.printf("Heroic difficulty:\nYou must match thepush button to the");
soapy12312 2:da0f01fbd25c 672 uLCD.printf("right COLOR that\nis displayed at a random position\nwith a ");
soapy12312 2:da0f01fbd25c 673 uLCD.printf("random sou-nd. Timing betweenvalues is 1.0 sec-onds.\n\n");
soapy12312 2:da0f01fbd25c 674 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 675
soapy12312 2:da0f01fbd25c 676 nextPage();
soapy12312 2:da0f01fbd25c 677
soapy12312 2:da0f01fbd25c 678 uLCD.printf("Legendary\ndifficulty:\n\nYou must match thepush button to the");
soapy12312 2:da0f01fbd25c 679 uLCD.printf("right SOUND that\nis displayed at a random position\nwith a ");
soapy12312 2:da0f01fbd25c 680 uLCD.printf("random col-or. Timing betweenvalues is 1.0 sec-onds.\n\n");
soapy12312 2:da0f01fbd25c 681 uLCD.printf("Press any button\nto continue.");
soapy12312 2:da0f01fbd25c 682
soapy12312 2:da0f01fbd25c 683 nextPage();
soapy12312 2:da0f01fbd25c 684 }
soapy12312 2:da0f01fbd25c 685 }
soapy12312 2:da0f01fbd25c 686
soapy12312 2:da0f01fbd25c 687 // Select difficulty
soapy12312 2:da0f01fbd25c 688 void selectDifficulty() {
soapy12312 2:da0f01fbd25c 689 difficultySelection:
soapy12312 2:da0f01fbd25c 690 uLCD.cls();
soapy12312 2:da0f01fbd25c 691
soapy12312 2:da0f01fbd25c 692 uLCD.printf("Select difficulty:\npb1: Easy\npb2: Normal\npb3: Heroic\npb4: Legendary");
soapy12312 2:da0f01fbd25c 693
soapy12312 2:da0f01fbd25c 694 resetHits();
soapy12312 2:da0f01fbd25c 695
soapy12312 2:da0f01fbd25c 696 while (noHits() == true) {
soapy12312 2:da0f01fbd25c 697 wait(1);
soapy12312 2:da0f01fbd25c 698
soapy12312 2:da0f01fbd25c 699 if (oneHit == true) {
soapy12312 2:da0f01fbd25c 700 difficulty = EASY;
soapy12312 2:da0f01fbd25c 701 } else if (twoHit == true) {
soapy12312 2:da0f01fbd25c 702 difficulty = NORMAL;
soapy12312 2:da0f01fbd25c 703 } else if (threeHit == true) {
soapy12312 2:da0f01fbd25c 704 difficulty = HEROIC;
soapy12312 2:da0f01fbd25c 705 } else if (fourHit == true) {
soapy12312 2:da0f01fbd25c 706 difficulty = LEGENDARY;
soapy12312 2:da0f01fbd25c 707 } else {
soapy12312 2:da0f01fbd25c 708 resetHits();
soapy12312 2:da0f01fbd25c 709 }
soapy12312 2:da0f01fbd25c 710 }
soapy12312 2:da0f01fbd25c 711
soapy12312 2:da0f01fbd25c 712 XBee.putc(difficulty);
soapy12312 2:da0f01fbd25c 713
soapy12312 2:da0f01fbd25c 714 char otherDifficulty = (char)(-1);
soapy12312 2:da0f01fbd25c 715 bool print = true;
soapy12312 2:da0f01fbd25c 716
soapy12312 2:da0f01fbd25c 717 while (otherDifficulty == (char)(-1)) {
soapy12312 2:da0f01fbd25c 718 if (XBee.readable() == true) {
soapy12312 2:da0f01fbd25c 719 otherDifficulty = XBee.getc();
soapy12312 2:da0f01fbd25c 720 print = false;
soapy12312 2:da0f01fbd25c 721 }
soapy12312 2:da0f01fbd25c 722
soapy12312 2:da0f01fbd25c 723 if (print == true) {
soapy12312 2:da0f01fbd25c 724 uLCD.cls();
soapy12312 2:da0f01fbd25c 725 uLCD.printf("Waiting for other player...");
soapy12312 2:da0f01fbd25c 726 print = false;
soapy12312 2:da0f01fbd25c 727 }
soapy12312 2:da0f01fbd25c 728 }
soapy12312 2:da0f01fbd25c 729
soapy12312 2:da0f01fbd25c 730 uLCD.cls();
soapy12312 2:da0f01fbd25c 731
soapy12312 2:da0f01fbd25c 732 if (otherDifficulty != difficulty) {
soapy12312 2:da0f01fbd25c 733 uLCD.cls();
soapy12312 2:da0f01fbd25c 734 uLCD.printf("The other player ");
soapy12312 2:da0f01fbd25c 735 uLCD.printf("selected another ");
soapy12312 2:da0f01fbd25c 736 uLCD.printf("difficulty. Please");
soapy12312 2:da0f01fbd25c 737 uLCD.printf("select again. ");
soapy12312 2:da0f01fbd25c 738 wait(3);
soapy12312 2:da0f01fbd25c 739 uLCD.cls();
soapy12312 2:da0f01fbd25c 740 goto difficultySelection;
soapy12312 2:da0f01fbd25c 741 }
soapy12312 2:da0f01fbd25c 742
soapy12312 2:da0f01fbd25c 743 uLCD.cls();
soapy12312 2:da0f01fbd25c 744
soapy12312 2:da0f01fbd25c 745 if (difficulty == EASY) {
soapy12312 2:da0f01fbd25c 746 uLCD.printf("You are playing onEasy difficulty.\n\n");
soapy12312 2:da0f01fbd25c 747 } else if (difficulty == NORMAL) {
soapy12312 2:da0f01fbd25c 748 uLCD.printf("You are playing onNormal difficulty.\n");
soapy12312 2:da0f01fbd25c 749 } else if (difficulty == HEROIC) {
soapy12312 2:da0f01fbd25c 750 uLCD.printf("You are playing onHeroic difficulty.\n");
soapy12312 2:da0f01fbd25c 751 } else {
soapy12312 2:da0f01fbd25c 752 uLCD.printf("You are playing onLegendary\ndifficulty.\n\n");
soapy12312 2:da0f01fbd25c 753 }
soapy12312 2:da0f01fbd25c 754
soapy12312 2:da0f01fbd25c 755 uLCD.printf("Starting the\ngame...");
soapy12312 2:da0f01fbd25c 756 wait(1.5);
soapy12312 2:da0f01fbd25c 757 }
soapy12312 2:da0f01fbd25c 758
soapy12312 2:da0f01fbd25c 759 // Countdown to start the game
soapy12312 2:da0f01fbd25c 760 void countdown() {
soapy12312 2:da0f01fbd25c 761 uLCD.filled_rectangle(0, 0, 127, 127, WHITE);
soapy12312 2:da0f01fbd25c 762 uLCD.textbackground_color(WHITE);
soapy12312 2:da0f01fbd25c 763 uLCD.background_color(WHITE);
soapy12312 2:da0f01fbd25c 764 uLCD.cls();
soapy12312 2:da0f01fbd25c 765
soapy12312 2:da0f01fbd25c 766 for (int i = 3; i > 0; i--) {
soapy12312 2:da0f01fbd25c 767 drawNum(i);
soapy12312 2:da0f01fbd25c 768 wait(1);
soapy12312 2:da0f01fbd25c 769 }
soapy12312 2:da0f01fbd25c 770 }
soapy12312 2:da0f01fbd25c 771
soapy12312 2:da0f01fbd25c 772 // Play the game
soapy12312 2:da0f01fbd25c 773 void playGame() {
soapy12312 2:da0f01fbd25c 774 // Initialize the score to be 0
soapy12312 2:da0f01fbd25c 775 int score = 0;
soapy12312 2:da0f01fbd25c 776
soapy12312 2:da0f01fbd25c 777 // Loop through the game
soapy12312 2:da0f01fbd25c 778 do {
soapy12312 2:da0f01fbd25c 779 // Display score
soapy12312 2:da0f01fbd25c 780 drawNum(score);
soapy12312 2:da0f01fbd25c 781 // Create a sequence
soapy12312 2:da0f01fbd25c 782 createSequence(sequence, score + 1);
soapy12312 2:da0f01fbd25c 783 // Play sequence
soapy12312 2:da0f01fbd25c 784 playSequence(sequence);
soapy12312 2:da0f01fbd25c 785
soapy12312 2:da0f01fbd25c 786 resetHits();
soapy12312 2:da0f01fbd25c 787
soapy12312 2:da0f01fbd25c 788 // Loop through the player sequence
soapy12312 2:da0f01fbd25c 789 for (int i = 0; i < score + 1; i++) {
soapy12312 2:da0f01fbd25c 790 while (noHits() == true) {
soapy12312 2:da0f01fbd25c 791 }
soapy12312 2:da0f01fbd25c 792
soapy12312 2:da0f01fbd25c 793 // Track player inputs
soapy12312 2:da0f01fbd25c 794 if (oneHit == true) {
soapy12312 2:da0f01fbd25c 795 playerSequence[i] = 0;
soapy12312 2:da0f01fbd25c 796
soapy12312 2:da0f01fbd25c 797 // On Easy difficulty
soapy12312 2:da0f01fbd25c 798 if (difficulty == EASY) {
soapy12312 2:da0f01fbd25c 799 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 800 drawFore(0, RED);
soapy12312 2:da0f01fbd25c 801 playSound(A3);
soapy12312 2:da0f01fbd25c 802 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 803 // On Normal difficulty
soapy12312 2:da0f01fbd25c 804 } else if (difficulty == NORMAL) {
soapy12312 2:da0f01fbd25c 805 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 806 drawFore(0, RED);
soapy12312 2:da0f01fbd25c 807 playSound(A3);
soapy12312 2:da0f01fbd25c 808 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 809 // On Heroic difficulty
soapy12312 2:da0f01fbd25c 810 } else if (difficulty == HEROIC) {
soapy12312 2:da0f01fbd25c 811 // Display color
soapy12312 2:da0f01fbd25c 812 drawFore(0, RED);
soapy12312 2:da0f01fbd25c 813 drawFore(1, RED);
soapy12312 2:da0f01fbd25c 814 drawFore(2, RED);
soapy12312 2:da0f01fbd25c 815 drawFore(3, RED);
soapy12312 2:da0f01fbd25c 816 wait(0.5);
soapy12312 2:da0f01fbd25c 817 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 818 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 819 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 820 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 821 // On Legendary difficulty
soapy12312 2:da0f01fbd25c 822 } else {
soapy12312 2:da0f01fbd25c 823 // Play sound
soapy12312 2:da0f01fbd25c 824 playSound(A3);
soapy12312 2:da0f01fbd25c 825 }
soapy12312 2:da0f01fbd25c 826 } else if (twoHit == true) {
soapy12312 2:da0f01fbd25c 827 playerSequence[i] = 1;
soapy12312 2:da0f01fbd25c 828
soapy12312 2:da0f01fbd25c 829 // On Easy difficulty
soapy12312 2:da0f01fbd25c 830 if (difficulty == EASY) {
soapy12312 2:da0f01fbd25c 831 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 832 drawFore(1, GREEN);
soapy12312 2:da0f01fbd25c 833 playSound(E3);
soapy12312 2:da0f01fbd25c 834 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 835 // On Normal difficulty
soapy12312 2:da0f01fbd25c 836 } else if (difficulty == NORMAL) {
soapy12312 2:da0f01fbd25c 837 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 838 drawFore(1, GREEN);
soapy12312 2:da0f01fbd25c 839 playSound(E3);
soapy12312 2:da0f01fbd25c 840 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 841 // On Heroic difficulty
soapy12312 2:da0f01fbd25c 842 } else if (difficulty == HEROIC) {
soapy12312 2:da0f01fbd25c 843 // Display color
soapy12312 2:da0f01fbd25c 844 drawFore(0, GREEN);
soapy12312 2:da0f01fbd25c 845 drawFore(1, GREEN);
soapy12312 2:da0f01fbd25c 846 drawFore(2, GREEN);
soapy12312 2:da0f01fbd25c 847 drawFore(3, GREEN);
soapy12312 2:da0f01fbd25c 848 wait(0.5);
soapy12312 2:da0f01fbd25c 849 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 850 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 851 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 852 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 853 // On Legendary difficulty
soapy12312 2:da0f01fbd25c 854 } else {
soapy12312 2:da0f01fbd25c 855 // Play sound
soapy12312 2:da0f01fbd25c 856 playSound(E3);
soapy12312 2:da0f01fbd25c 857 }
soapy12312 2:da0f01fbd25c 858 } else if (threeHit == true) {
soapy12312 2:da0f01fbd25c 859 playerSequence[i] = 2;
soapy12312 2:da0f01fbd25c 860
soapy12312 2:da0f01fbd25c 861 // On Easy difficulty
soapy12312 2:da0f01fbd25c 862 if (difficulty == EASY) {
soapy12312 2:da0f01fbd25c 863 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 864 drawFore(2, YELLOW);
soapy12312 2:da0f01fbd25c 865 playSound(C4);
soapy12312 2:da0f01fbd25c 866 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 867 // On Normal difficulty
soapy12312 2:da0f01fbd25c 868 } else if (difficulty == NORMAL) {
soapy12312 2:da0f01fbd25c 869 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 870 drawFore(2, YELLOW);
soapy12312 2:da0f01fbd25c 871 playSound(C4);
soapy12312 2:da0f01fbd25c 872 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 873 // On Heroic difficulty
soapy12312 2:da0f01fbd25c 874 } else if (difficulty == HEROIC) {
soapy12312 2:da0f01fbd25c 875 drawFore(0, YELLOW);
soapy12312 2:da0f01fbd25c 876 drawFore(1, YELLOW);
soapy12312 2:da0f01fbd25c 877 drawFore(2, YELLOW);
soapy12312 2:da0f01fbd25c 878 drawFore(3, YELLOW);
soapy12312 2:da0f01fbd25c 879 wait(0.5);
soapy12312 2:da0f01fbd25c 880 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 881 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 882 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 883 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 884 // On Legendary difficulty
soapy12312 2:da0f01fbd25c 885 } else {
soapy12312 2:da0f01fbd25c 886 // Play sound
soapy12312 2:da0f01fbd25c 887 playSound(C4);
soapy12312 2:da0f01fbd25c 888 //wait(1);
soapy12312 2:da0f01fbd25c 889 }
soapy12312 2:da0f01fbd25c 890 } else if (fourHit == true) {
soapy12312 2:da0f01fbd25c 891 playerSequence[i] = 3;
soapy12312 2:da0f01fbd25c 892
soapy12312 2:da0f01fbd25c 893 // On Easy difficulty
soapy12312 2:da0f01fbd25c 894 if (difficulty == EASY) {
soapy12312 2:da0f01fbd25c 895 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 896 drawFore(3, BLUE);
soapy12312 2:da0f01fbd25c 897 playSound(E4);
soapy12312 2:da0f01fbd25c 898 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 899 // On Normal difficulty
soapy12312 2:da0f01fbd25c 900 } else if (difficulty == NORMAL) {
soapy12312 2:da0f01fbd25c 901 // Display position, color, and play sound
soapy12312 2:da0f01fbd25c 902 drawFore(3, BLUE);
soapy12312 2:da0f01fbd25c 903 playSound(E4);
soapy12312 2:da0f01fbd25c 904 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 905 // On Heroic difficulty
soapy12312 2:da0f01fbd25c 906 } else if (difficulty == HEROIC) {
soapy12312 2:da0f01fbd25c 907 // Display color
soapy12312 2:da0f01fbd25c 908 drawFore(0, BLUE);
soapy12312 2:da0f01fbd25c 909 drawFore(1, BLUE);
soapy12312 2:da0f01fbd25c 910 drawFore(2, BLUE);
soapy12312 2:da0f01fbd25c 911 drawFore(3, BLUE);
soapy12312 2:da0f01fbd25c 912 wait(0.5);
soapy12312 2:da0f01fbd25c 913 drawBack(0, WHITE);
soapy12312 2:da0f01fbd25c 914 drawBack(1, WHITE);
soapy12312 2:da0f01fbd25c 915 drawBack(2, WHITE);
soapy12312 2:da0f01fbd25c 916 drawBack(3, WHITE);
soapy12312 2:da0f01fbd25c 917 // On Legendary difficulty
soapy12312 2:da0f01fbd25c 918 } else {
soapy12312 2:da0f01fbd25c 919 // Play sound
soapy12312 2:da0f01fbd25c 920 playSound(E4);
soapy12312 2:da0f01fbd25c 921 }
soapy12312 2:da0f01fbd25c 922 }
soapy12312 2:da0f01fbd25c 923
soapy12312 2:da0f01fbd25c 924 resetHits();
soapy12312 2:da0f01fbd25c 925 }
soapy12312 2:da0f01fbd25c 926
soapy12312 2:da0f01fbd25c 927 // Determine whether the sequences match
soapy12312 2:da0f01fbd25c 928 bool correct = true;
soapy12312 2:da0f01fbd25c 929
soapy12312 2:da0f01fbd25c 930 for (int i = 0; i < score + 1; i++) {
soapy12312 2:da0f01fbd25c 931 if (sequence[i] != playerSequence[i]) {
soapy12312 2:da0f01fbd25c 932 correct = false;
soapy12312 2:da0f01fbd25c 933 }
soapy12312 2:da0f01fbd25c 934 }
soapy12312 2:da0f01fbd25c 935
soapy12312 2:da0f01fbd25c 936 // Play sound
soapy12312 2:da0f01fbd25c 937 if (correct == true) {
soapy12312 2:da0f01fbd25c 938 playSound(DING);
soapy12312 2:da0f01fbd25c 939 } else {
soapy12312 2:da0f01fbd25c 940
soapy12312 2:da0f01fbd25c 941 playSound(BUZZER);
soapy12312 2:da0f01fbd25c 942 }
soapy12312 2:da0f01fbd25c 943
soapy12312 2:da0f01fbd25c 944 // Check if the other player has won
soapy12312 2:da0f01fbd25c 945 if (XBee.readable() == true) {
soapy12312 2:da0f01fbd25c 946 otherPlayerWin = XBee.getc();
soapy12312 2:da0f01fbd25c 947 }
soapy12312 2:da0f01fbd25c 948
soapy12312 2:da0f01fbd25c 949 // If the other player has won, end the game
soapy12312 2:da0f01fbd25c 950 if (otherPlayerWin == true) {
soapy12312 2:da0f01fbd25c 951 goto endGame;
soapy12312 2:da0f01fbd25c 952 }
soapy12312 2:da0f01fbd25c 953
soapy12312 2:da0f01fbd25c 954 // Update the score
soapy12312 2:da0f01fbd25c 955 if (correct == true) {
soapy12312 2:da0f01fbd25c 956 score++;
soapy12312 2:da0f01fbd25c 957 } else {
soapy12312 2:da0f01fbd25c 958 if (score > 0) {
soapy12312 2:da0f01fbd25c 959 score--;
soapy12312 2:da0f01fbd25c 960 }
soapy12312 2:da0f01fbd25c 961 }
soapy12312 2:da0f01fbd25c 962
soapy12312 2:da0f01fbd25c 963 // Reset sequences
soapy12312 2:da0f01fbd25c 964 resetSequence(sequence, MAXLENGTH);
soapy12312 2:da0f01fbd25c 965 resetSequence(playerSequence, MAXLENGTH);
soapy12312 2:da0f01fbd25c 966 } while (score < MAXLENGTH);
soapy12312 2:da0f01fbd25c 967
soapy12312 2:da0f01fbd25c 968 endGame:
soapy12312 2:da0f01fbd25c 969 uLCD.pixel(68, 68, WHITE);
soapy12312 2:da0f01fbd25c 970 }
soapy12312 2:da0f01fbd25c 971
soapy12312 2:da0f01fbd25c 972 // Game over
soapy12312 2:da0f01fbd25c 973 void gameOver() {
soapy12312 2:da0f01fbd25c 974 XBee.putc(true);
soapy12312 2:da0f01fbd25c 975
soapy12312 2:da0f01fbd25c 976 uLCD.cls();
soapy12312 2:da0f01fbd25c 977 uLCD.text_width(1);
soapy12312 2:da0f01fbd25c 978 uLCD.text_height(1);
soapy12312 2:da0f01fbd25c 979 uLCD.textbackground_color(BLACK);
soapy12312 2:da0f01fbd25c 980 uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
soapy12312 2:da0f01fbd25c 981 uLCD.background_color(BLACK);
soapy12312 2:da0f01fbd25c 982 uLCD.color(GREEN);
soapy12312 2:da0f01fbd25c 983 uLCD.locate(0, 0);
soapy12312 2:da0f01fbd25c 984
soapy12312 2:da0f01fbd25c 985 if (otherPlayerWin == false) {
soapy12312 2:da0f01fbd25c 986 uLCD.printf("You win!");
soapy12312 2:da0f01fbd25c 987 playSound(WIN);
soapy12312 2:da0f01fbd25c 988 } else {
soapy12312 2:da0f01fbd25c 989 uLCD.printf("The other player\n");
soapy12312 2:da0f01fbd25c 990 uLCD.printf("wins.");
soapy12312 2:da0f01fbd25c 991 playSound(BOMB);
soapy12312 2:da0f01fbd25c 992 }
soapy12312 2:da0f01fbd25c 993 }
soapy12312 2:da0f01fbd25c 994 };