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