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
BlueO.h@2:da0f01fbd25c, 2015-12-04 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
soapy12312 | 2:da0f01fbd25c | 1 | /******************************************************* |
soapy12312 | 2:da0f01fbd25c | 2 | * This header program declares all of the functions and |
soapy12312 | 2:da0f01fbd25c | 3 | * variables used by the blue O player in Super |
soapy12312 | 2:da0f01fbd25c | 4 | * Tic-Tac-Toe. |
soapy12312 | 2:da0f01fbd25c | 5 | *******************************************************/ |
soapy12312 | 2:da0f01fbd25c | 6 | |
soapy12312 | 2:da0f01fbd25c | 7 | |
soapy12312 | 2:da0f01fbd25c | 8 | #define FORWARDSLASH 0 |
soapy12312 | 2:da0f01fbd25c | 9 | #define BACKWARDSLASH 1 |
soapy12312 | 2:da0f01fbd25c | 10 | #define VERTICAL 2 |
soapy12312 | 2:da0f01fbd25c | 11 | #define HORIZONTAL 3 |
soapy12312 | 2:da0f01fbd25c | 12 | #define BOMB "/sd/wavfiles/bomb.wav" |
soapy12312 | 2:da0f01fbd25c | 13 | #define WIN "/sd/win.wav" |
soapy12312 | 2:da0f01fbd25c | 14 | #define MEH "/sd/Meh.wav" |
soapy12312 | 2:da0f01fbd25c | 15 | |
soapy12312 | 2:da0f01fbd25c | 16 | |
soapy12312 | 2:da0f01fbd25c | 17 | /******************************************************* |
soapy12312 | 2:da0f01fbd25c | 18 | * This structure is used to represent the individual |
soapy12312 | 2:da0f01fbd25c | 19 | * blocks within a small grid. |
soapy12312 | 2:da0f01fbd25c | 20 | *******************************************************/ |
soapy12312 | 2:da0f01fbd25c | 21 | struct smallGrid { |
soapy12312 | 2:da0f01fbd25c | 22 | char individualGrid[3][3]; |
soapy12312 | 2:da0f01fbd25c | 23 | char done; |
soapy12312 | 2:da0f01fbd25c | 24 | }; |
soapy12312 | 2:da0f01fbd25c | 25 | |
soapy12312 | 2:da0f01fbd25c | 26 | /******************************************************* |
soapy12312 | 2:da0f01fbd25c | 27 | * This structure is used to represent the small |
soapy12312 | 2:da0f01fbd25c | 28 | * blocks within the whole grid. |
soapy12312 | 2:da0f01fbd25c | 29 | *******************************************************/ |
soapy12312 | 2:da0f01fbd25c | 30 | struct largeGrid { |
soapy12312 | 2:da0f01fbd25c | 31 | smallGrid smallGrid[3][3]; |
soapy12312 | 2:da0f01fbd25c | 32 | char done; |
soapy12312 | 2:da0f01fbd25c | 33 | }; |
soapy12312 | 2:da0f01fbd25c | 34 | |
soapy12312 | 2:da0f01fbd25c | 35 | |
soapy12312 | 2:da0f01fbd25c | 36 | class BlueO { |
soapy12312 | 2:da0f01fbd25c | 37 | public: |
soapy12312 | 2:da0f01fbd25c | 38 | // Player O constructor |
soapy12312 | 2:da0f01fbd25c | 39 | BlueO(DigitalOut &reset, InterruptIn &input, Mpr121 &MPR121, RGBLED &RGB, SDFileSystem &sd, Serial &XBee, uLCD_4DGL &uLCD, wave_player &waver) : |
soapy12312 | 2:da0f01fbd25c | 40 | reset(reset), input(input), MPR121(MPR121), RGB(RGB), sd(sd), XBee(XBee), uLCD(uLCD), waver(waver) { |
soapy12312 | 2:da0f01fbd25c | 41 | uLCD.baudrate(MAXBAUDRATE); |
soapy12312 | 2:da0f01fbd25c | 42 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 43 | |
soapy12312 | 2:da0f01fbd25c | 44 | // Set up XBee |
soapy12312 | 2:da0f01fbd25c | 45 | reset = 0; |
soapy12312 | 2:da0f01fbd25c | 46 | wait(0.001); |
soapy12312 | 2:da0f01fbd25c | 47 | reset = 1; |
soapy12312 | 2:da0f01fbd25c | 48 | wait(0.001); |
soapy12312 | 2:da0f01fbd25c | 49 | } |
soapy12312 | 2:da0f01fbd25c | 50 | |
soapy12312 | 2:da0f01fbd25c | 51 | // Play the game |
soapy12312 | 2:da0f01fbd25c | 52 | void playSuperTicTacToe() { |
soapy12312 | 2:da0f01fbd25c | 53 | initializeInterrupt(); |
soapy12312 | 2:da0f01fbd25c | 54 | displayInstructions(); |
soapy12312 | 2:da0f01fbd25c | 55 | initializeGrid(); |
soapy12312 | 2:da0f01fbd25c | 56 | |
soapy12312 | 2:da0f01fbd25c | 57 | XBee.putc(true); |
soapy12312 | 2:da0f01fbd25c | 58 | bool readyToPlay = false; |
soapy12312 | 2:da0f01fbd25c | 59 | bool print = true; |
soapy12312 | 2:da0f01fbd25c | 60 | |
soapy12312 | 2:da0f01fbd25c | 61 | while (readyToPlay == false) { |
soapy12312 | 2:da0f01fbd25c | 62 | if (XBee.readable() == true) { |
soapy12312 | 2:da0f01fbd25c | 63 | readyToPlay = XBee.getc(); |
soapy12312 | 2:da0f01fbd25c | 64 | print = false; |
soapy12312 | 2:da0f01fbd25c | 65 | } |
soapy12312 | 2:da0f01fbd25c | 66 | |
soapy12312 | 2:da0f01fbd25c | 67 | if (print == true) { |
soapy12312 | 2:da0f01fbd25c | 68 | uLCD.cls(); |
soapy12312 | 2:da0f01fbd25c | 69 | uLCD.printf("Waiting for other player..."); |
soapy12312 | 2:da0f01fbd25c | 70 | print = false; |
soapy12312 | 2:da0f01fbd25c | 71 | } |
soapy12312 | 2:da0f01fbd25c | 72 | } |
soapy12312 | 2:da0f01fbd25c | 73 | |
soapy12312 | 2:da0f01fbd25c | 74 | uLCD.cls(); |
soapy12312 | 2:da0f01fbd25c | 75 | drawStage(); |
soapy12312 | 2:da0f01fbd25c | 76 | |
soapy12312 | 2:da0f01fbd25c | 77 | // False: player one's turn; true: player two's turn |
soapy12312 | 2:da0f01fbd25c | 78 | bool turn = true; |
soapy12312 | 2:da0f01fbd25c | 79 | |
soapy12312 | 2:da0f01fbd25c | 80 | // Loop through the game |
soapy12312 | 2:da0f01fbd25c | 81 | while (grid.done == (char)(-1)) { |
soapy12312 | 2:da0f01fbd25c | 82 | turn = !turn; |
soapy12312 | 2:da0f01fbd25c | 83 | takeTurn(turn); |
soapy12312 | 2:da0f01fbd25c | 84 | gameWin(turn); |
soapy12312 | 2:da0f01fbd25c | 85 | } |
soapy12312 | 2:da0f01fbd25c | 86 | |
soapy12312 | 2:da0f01fbd25c | 87 | uLCD.cls(); |
soapy12312 | 2:da0f01fbd25c | 88 | |
soapy12312 | 2:da0f01fbd25c | 89 | if (grid.done == 0) { |
soapy12312 | 2:da0f01fbd25c | 90 | uLCD.printf("The other player\nwins."); |
soapy12312 | 2:da0f01fbd25c | 91 | playSound(BOMB); |
soapy12312 | 2:da0f01fbd25c | 92 | } else if (grid.done == 1) { |
soapy12312 | 2:da0f01fbd25c | 93 | uLCD.printf("You win!"); |
soapy12312 | 2:da0f01fbd25c | 94 | playSound(WIN); |
soapy12312 | 2:da0f01fbd25c | 95 | } else if (grid.done == 128) { |
soapy12312 | 2:da0f01fbd25c | 96 | uLCD.printf("It's a draw!"); |
soapy12312 | 2:da0f01fbd25c | 97 | playSound(MEH); |
soapy12312 | 2:da0f01fbd25c | 98 | } |
soapy12312 | 2:da0f01fbd25c | 99 | |
soapy12312 | 2:da0f01fbd25c | 100 | wait(3); |
soapy12312 | 2:da0f01fbd25c | 101 | uLCD.cls(); |
soapy12312 | 2:da0f01fbd25c | 102 | } |
soapy12312 | 2:da0f01fbd25c | 103 | |
soapy12312 | 2:da0f01fbd25c | 104 | |
soapy12312 | 2:da0f01fbd25c | 105 | private: |
soapy12312 | 2:da0f01fbd25c | 106 | DigitalOut &reset; |
soapy12312 | 2:da0f01fbd25c | 107 | InterruptIn &input; |
soapy12312 | 2:da0f01fbd25c | 108 | Mpr121 &MPR121; |
soapy12312 | 2:da0f01fbd25c | 109 | RGBLED &RGB; |
soapy12312 | 2:da0f01fbd25c | 110 | SDFileSystem &sd; |
soapy12312 | 2:da0f01fbd25c | 111 | Serial &XBee; |
soapy12312 | 2:da0f01fbd25c | 112 | uLCD_4DGL &uLCD; |
soapy12312 | 2:da0f01fbd25c | 113 | wave_player &waver; |
soapy12312 | 2:da0f01fbd25c | 114 | bool keyAllowed; |
soapy12312 | 2:da0f01fbd25c | 115 | volatile int key; |
soapy12312 | 2:da0f01fbd25c | 116 | largeGrid grid; |
soapy12312 | 2:da0f01fbd25c | 117 | |
soapy12312 | 2:da0f01fbd25c | 118 | // Initialize the interrupt |
soapy12312 | 2:da0f01fbd25c | 119 | void initializeInterrupt() { |
soapy12312 | 2:da0f01fbd25c | 120 | input.fall(this, &BlueO::keyInterrupt); |
soapy12312 | 2:da0f01fbd25c | 121 | wait(0.01); |
soapy12312 | 2:da0f01fbd25c | 122 | input.mode(PullUp); |
soapy12312 | 2:da0f01fbd25c | 123 | wait(0.01); |
soapy12312 | 2:da0f01fbd25c | 124 | } |
soapy12312 | 2:da0f01fbd25c | 125 | |
soapy12312 | 2:da0f01fbd25c | 126 | // Determine value of the key pressed |
soapy12312 | 2:da0f01fbd25c | 127 | void keyInterrupt() { |
soapy12312 | 2:da0f01fbd25c | 128 | if (keyAllowed == true) { |
soapy12312 | 2:da0f01fbd25c | 129 | int value = MPR121.read(0x00); |
soapy12312 | 2:da0f01fbd25c | 130 | value += MPR121.read(0x01) << 8; |
soapy12312 | 2:da0f01fbd25c | 131 | |
soapy12312 | 2:da0f01fbd25c | 132 | for (int i = 0; i < 12; i++) { |
soapy12312 | 2:da0f01fbd25c | 133 | if (((value >> i) & 0x01) == 1) { |
soapy12312 | 2:da0f01fbd25c | 134 | key = i; |
soapy12312 | 2:da0f01fbd25c | 135 | } |
soapy12312 | 2:da0f01fbd25c | 136 | } |
soapy12312 | 2:da0f01fbd25c | 137 | |
soapy12312 | 2:da0f01fbd25c | 138 | // Ununsed keys |
soapy12312 | 2:da0f01fbd25c | 139 | if ((key == 3) || (key == 7) || (key == 11)) { |
soapy12312 | 2:da0f01fbd25c | 140 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 141 | } |
soapy12312 | 2:da0f01fbd25c | 142 | } else { |
soapy12312 | 2:da0f01fbd25c | 143 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 144 | } |
soapy12312 | 2:da0f01fbd25c | 145 | } |
soapy12312 | 2:da0f01fbd25c | 146 | |
soapy12312 | 2:da0f01fbd25c | 147 | // Initialize the grid |
soapy12312 | 2:da0f01fbd25c | 148 | void initializeGrid() { |
soapy12312 | 2:da0f01fbd25c | 149 | grid.done = (char)(-1); |
soapy12312 | 2:da0f01fbd25c | 150 | |
soapy12312 | 2:da0f01fbd25c | 151 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 152 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 153 | grid.smallGrid[i][j].done = (char)(-1); |
soapy12312 | 2:da0f01fbd25c | 154 | |
soapy12312 | 2:da0f01fbd25c | 155 | for (int k = 0; k < 3; k++) { |
soapy12312 | 2:da0f01fbd25c | 156 | for (int l = 0; l < 3; l++) { |
soapy12312 | 2:da0f01fbd25c | 157 | grid.smallGrid[i][j].individualGrid[k][l] = (char)(-1); |
soapy12312 | 2:da0f01fbd25c | 158 | } |
soapy12312 | 2:da0f01fbd25c | 159 | } |
soapy12312 | 2:da0f01fbd25c | 160 | } |
soapy12312 | 2:da0f01fbd25c | 161 | } |
soapy12312 | 2:da0f01fbd25c | 162 | } |
soapy12312 | 2:da0f01fbd25c | 163 | |
soapy12312 | 2:da0f01fbd25c | 164 | // Display instructions |
soapy12312 | 2:da0f01fbd25c | 165 | void displayInstructions() { |
soapy12312 | 2:da0f01fbd25c | 166 | uLCD.printf("Hello, and welcome"); |
soapy12312 | 2:da0f01fbd25c | 167 | uLCD.printf("to a game of Super"); |
soapy12312 | 2:da0f01fbd25c | 168 | uLCD.printf("Tic-Tac-Toe. Use "); |
soapy12312 | 2:da0f01fbd25c | 169 | uLCD.printf("the bottom three "); |
soapy12312 | 2:da0f01fbd25c | 170 | uLCD.printf("rows of the touch "); |
soapy12312 | 2:da0f01fbd25c | 171 | uLCD.printf("keypad to select "); |
soapy12312 | 2:da0f01fbd25c | 172 | uLCD.printf("the large grid "); |
soapy12312 | 2:da0f01fbd25c | 173 | uLCD.printf("followed by the "); |
soapy12312 | 2:da0f01fbd25c | 174 | uLCD.printf("small grid. You "); |
soapy12312 | 2:da0f01fbd25c | 175 | uLCD.printf("are the blue O.\n\n"); |
soapy12312 | 2:da0f01fbd25c | 176 | uLCD.printf("Press any key to "); |
soapy12312 | 2:da0f01fbd25c | 177 | uLCD.printf("begin playing."); |
soapy12312 | 2:da0f01fbd25c | 178 | wait(0.1); |
soapy12312 | 2:da0f01fbd25c | 179 | |
soapy12312 | 2:da0f01fbd25c | 180 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 181 | keyAllowed = true; |
soapy12312 | 2:da0f01fbd25c | 182 | |
soapy12312 | 2:da0f01fbd25c | 183 | while (key == -1) { |
soapy12312 | 2:da0f01fbd25c | 184 | wait(0.01); |
soapy12312 | 2:da0f01fbd25c | 185 | } |
soapy12312 | 2:da0f01fbd25c | 186 | |
soapy12312 | 2:da0f01fbd25c | 187 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 188 | uLCD.cls(); |
soapy12312 | 2:da0f01fbd25c | 189 | } |
soapy12312 | 2:da0f01fbd25c | 190 | |
soapy12312 | 2:da0f01fbd25c | 191 | // Play a sound file |
soapy12312 | 2:da0f01fbd25c | 192 | void playSound(char *wav) { |
soapy12312 | 2:da0f01fbd25c | 193 | // Open sound file |
soapy12312 | 2:da0f01fbd25c | 194 | FILE *wave_file; |
soapy12312 | 2:da0f01fbd25c | 195 | wave_file = fopen(wav, "r"); |
soapy12312 | 2:da0f01fbd25c | 196 | |
soapy12312 | 2:da0f01fbd25c | 197 | // Play wav file |
soapy12312 | 2:da0f01fbd25c | 198 | waver.play(wave_file); |
soapy12312 | 2:da0f01fbd25c | 199 | |
soapy12312 | 2:da0f01fbd25c | 200 | // Close wav file |
soapy12312 | 2:da0f01fbd25c | 201 | fclose(wave_file); |
soapy12312 | 2:da0f01fbd25c | 202 | } |
soapy12312 | 2:da0f01fbd25c | 203 | |
soapy12312 | 2:da0f01fbd25c | 204 | // Draw the stage |
soapy12312 | 2:da0f01fbd25c | 205 | void drawStage() { |
soapy12312 | 2:da0f01fbd25c | 206 | for (int i = 15; i < 120; i += 14) { |
soapy12312 | 2:da0f01fbd25c | 207 | uLCD.line(i, 2, i, 127, YELLOW); |
soapy12312 | 2:da0f01fbd25c | 208 | uLCD.line(2, i, 127, i, YELLOW); |
soapy12312 | 2:da0f01fbd25c | 209 | } |
soapy12312 | 2:da0f01fbd25c | 210 | |
soapy12312 | 2:da0f01fbd25c | 211 | for (int i = 43; i < 120; i += 42) { |
soapy12312 | 2:da0f01fbd25c | 212 | uLCD.line(i, 2, i, 127, WHITE); |
soapy12312 | 2:da0f01fbd25c | 213 | uLCD.line(2, i, 127, i, WHITE); |
soapy12312 | 2:da0f01fbd25c | 214 | } |
soapy12312 | 2:da0f01fbd25c | 215 | } |
soapy12312 | 2:da0f01fbd25c | 216 | |
soapy12312 | 2:da0f01fbd25c | 217 | // Draw small X |
soapy12312 | 2:da0f01fbd25c | 218 | void drawSmallX(int largeX, int largeY, int smallX, int smallY) { |
soapy12312 | 2:da0f01fbd25c | 219 | int xCoordinate = 1 + (largeX * 42) + (smallX * 14) + 7; |
soapy12312 | 2:da0f01fbd25c | 220 | int yCoordinate = 1 + (largeY * 42) + (smallY * 14) + 7; |
soapy12312 | 2:da0f01fbd25c | 221 | |
soapy12312 | 2:da0f01fbd25c | 222 | int x1 = xCoordinate - 6; |
soapy12312 | 2:da0f01fbd25c | 223 | int x2 = xCoordinate + 6; |
soapy12312 | 2:da0f01fbd25c | 224 | int y1 = yCoordinate - 6; |
soapy12312 | 2:da0f01fbd25c | 225 | int y2 = yCoordinate + 6; |
soapy12312 | 2:da0f01fbd25c | 226 | |
soapy12312 | 2:da0f01fbd25c | 227 | uLCD.line(x1 + 1, y1 + 1, x2, y2, RED); |
soapy12312 | 2:da0f01fbd25c | 228 | uLCD.line(x1 + 1, y2 - 1, x2, y1, RED); |
soapy12312 | 2:da0f01fbd25c | 229 | } |
soapy12312 | 2:da0f01fbd25c | 230 | |
soapy12312 | 2:da0f01fbd25c | 231 | // Draw small O |
soapy12312 | 2:da0f01fbd25c | 232 | void drawSmallO(int largeX, int largeY, int smallX, int smallY) { |
soapy12312 | 2:da0f01fbd25c | 233 | int xCoordinate = 1 + (largeX * 42) + (smallX * 14) + 7; |
soapy12312 | 2:da0f01fbd25c | 234 | int yCoordinate = 1 + (largeY * 42) + (smallY * 14) + 7; |
soapy12312 | 2:da0f01fbd25c | 235 | |
soapy12312 | 2:da0f01fbd25c | 236 | uLCD.circle(xCoordinate, yCoordinate, 5, BLUE); |
soapy12312 | 2:da0f01fbd25c | 237 | } |
soapy12312 | 2:da0f01fbd25c | 238 | |
soapy12312 | 2:da0f01fbd25c | 239 | // Draw large X |
soapy12312 | 2:da0f01fbd25c | 240 | void drawLargeX(int largeX, int largeY) { |
soapy12312 | 2:da0f01fbd25c | 241 | int xCoordinate = 1 + (largeX * 42) + 21; |
soapy12312 | 2:da0f01fbd25c | 242 | int yCoordinate = 1 + (largeY * 42) + 21; |
soapy12312 | 2:da0f01fbd25c | 243 | |
soapy12312 | 2:da0f01fbd25c | 244 | int x1 = xCoordinate - 20; |
soapy12312 | 2:da0f01fbd25c | 245 | int x2 = xCoordinate + 20; |
soapy12312 | 2:da0f01fbd25c | 246 | int y1 = yCoordinate - 20; |
soapy12312 | 2:da0f01fbd25c | 247 | int y2 = yCoordinate + 20; |
soapy12312 | 2:da0f01fbd25c | 248 | |
soapy12312 | 2:da0f01fbd25c | 249 | uLCD.filled_rectangle(x1, y1, x2, y2, BLACK); |
soapy12312 | 2:da0f01fbd25c | 250 | |
soapy12312 | 2:da0f01fbd25c | 251 | uLCD.line(x1 + 1, y1 + 1, x2, y2, RED); |
soapy12312 | 2:da0f01fbd25c | 252 | uLCD.line(x1 + 1, y2 - 1, x2, y1, RED); |
soapy12312 | 2:da0f01fbd25c | 253 | } |
soapy12312 | 2:da0f01fbd25c | 254 | |
soapy12312 | 2:da0f01fbd25c | 255 | // Draw large O |
soapy12312 | 2:da0f01fbd25c | 256 | void drawLargeO(int largeX, int largeY) { |
soapy12312 | 2:da0f01fbd25c | 257 | int xCoordinate = 1 + (largeX * 42) + 21; |
soapy12312 | 2:da0f01fbd25c | 258 | int yCoordinate = 1 + (largeY * 42) + 21; |
soapy12312 | 2:da0f01fbd25c | 259 | |
soapy12312 | 2:da0f01fbd25c | 260 | int x1 = xCoordinate - 20; |
soapy12312 | 2:da0f01fbd25c | 261 | int x2 = xCoordinate + 20; |
soapy12312 | 2:da0f01fbd25c | 262 | int y1 = yCoordinate - 20; |
soapy12312 | 2:da0f01fbd25c | 263 | int y2 = yCoordinate + 20; |
soapy12312 | 2:da0f01fbd25c | 264 | |
soapy12312 | 2:da0f01fbd25c | 265 | uLCD.filled_rectangle(x1, y1, x2, y2, BLACK); |
soapy12312 | 2:da0f01fbd25c | 266 | |
soapy12312 | 2:da0f01fbd25c | 267 | uLCD.circle(xCoordinate, yCoordinate, 19, BLUE); |
soapy12312 | 2:da0f01fbd25c | 268 | } |
soapy12312 | 2:da0f01fbd25c | 269 | |
soapy12312 | 2:da0f01fbd25c | 270 | // Find x-coordinate based on key input |
soapy12312 | 2:da0f01fbd25c | 271 | int xGrid() { |
soapy12312 | 2:da0f01fbd25c | 272 | int x; |
soapy12312 | 2:da0f01fbd25c | 273 | |
soapy12312 | 2:da0f01fbd25c | 274 | if ((key == 0) || (key == 1) || (key == 2)) { |
soapy12312 | 2:da0f01fbd25c | 275 | x = 0; |
soapy12312 | 2:da0f01fbd25c | 276 | } else if ((key == 4) || (key == 5) || (key == 6)) { |
soapy12312 | 2:da0f01fbd25c | 277 | x = 1; |
soapy12312 | 2:da0f01fbd25c | 278 | } else if ((key == 8) || (key == 9) || (key == 10)) { |
soapy12312 | 2:da0f01fbd25c | 279 | x = 2; |
soapy12312 | 2:da0f01fbd25c | 280 | } else { |
soapy12312 | 2:da0f01fbd25c | 281 | x = -1; |
soapy12312 | 2:da0f01fbd25c | 282 | } |
soapy12312 | 2:da0f01fbd25c | 283 | |
soapy12312 | 2:da0f01fbd25c | 284 | return x; |
soapy12312 | 2:da0f01fbd25c | 285 | } |
soapy12312 | 2:da0f01fbd25c | 286 | |
soapy12312 | 2:da0f01fbd25c | 287 | // Find y-coordinate based on key input |
soapy12312 | 2:da0f01fbd25c | 288 | int yGrid() { |
soapy12312 | 2:da0f01fbd25c | 289 | int y; |
soapy12312 | 2:da0f01fbd25c | 290 | |
soapy12312 | 2:da0f01fbd25c | 291 | if ((key == 2) || (key == 6) || (key == 10)) { |
soapy12312 | 2:da0f01fbd25c | 292 | y = 0; |
soapy12312 | 2:da0f01fbd25c | 293 | } else if ((key == 1) || (key == 5) || (key == 9)) { |
soapy12312 | 2:da0f01fbd25c | 294 | y = 1; |
soapy12312 | 2:da0f01fbd25c | 295 | } else if ((key == 0) || (key == 4) || (key == 8)) { |
soapy12312 | 2:da0f01fbd25c | 296 | y = 2; |
soapy12312 | 2:da0f01fbd25c | 297 | } else { |
soapy12312 | 2:da0f01fbd25c | 298 | y = -1; |
soapy12312 | 2:da0f01fbd25c | 299 | } |
soapy12312 | 2:da0f01fbd25c | 300 | |
soapy12312 | 2:da0f01fbd25c | 301 | return y; |
soapy12312 | 2:da0f01fbd25c | 302 | } |
soapy12312 | 2:da0f01fbd25c | 303 | |
soapy12312 | 2:da0f01fbd25c | 304 | // Display a certain small grid in a set color |
soapy12312 | 2:da0f01fbd25c | 305 | void drawSmallGrid(int x, int y, int color) { |
soapy12312 | 2:da0f01fbd25c | 306 | int x1 = 1 + (x * 42) + 1; |
soapy12312 | 2:da0f01fbd25c | 307 | int x2 = x1 + 40; |
soapy12312 | 2:da0f01fbd25c | 308 | int y1 = 1 + (y * 42) + 1; |
soapy12312 | 2:da0f01fbd25c | 309 | int y2 = y1 + 40; |
soapy12312 | 2:da0f01fbd25c | 310 | |
soapy12312 | 2:da0f01fbd25c | 311 | for (int i = x1; i < x2; i += 14) { |
soapy12312 | 2:da0f01fbd25c | 312 | for (int j = y1; j < y2; j += 14) { |
soapy12312 | 2:da0f01fbd25c | 313 | uLCD.filled_rectangle(i, j, i + 12, j + 12, color); |
soapy12312 | 2:da0f01fbd25c | 314 | } |
soapy12312 | 2:da0f01fbd25c | 315 | } |
soapy12312 | 2:da0f01fbd25c | 316 | |
soapy12312 | 2:da0f01fbd25c | 317 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 318 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 319 | if (grid.smallGrid[x][y].individualGrid[i][j] == 0) { |
soapy12312 | 2:da0f01fbd25c | 320 | drawSmallX(x, y, i, j); |
soapy12312 | 2:da0f01fbd25c | 321 | } else if (grid.smallGrid[x][y].individualGrid[i][j] == 1) { |
soapy12312 | 2:da0f01fbd25c | 322 | drawSmallO(x, y, i, j); |
soapy12312 | 2:da0f01fbd25c | 323 | } |
soapy12312 | 2:da0f01fbd25c | 324 | } |
soapy12312 | 2:da0f01fbd25c | 325 | } |
soapy12312 | 2:da0f01fbd25c | 326 | } |
soapy12312 | 2:da0f01fbd25c | 327 | |
soapy12312 | 2:da0f01fbd25c | 328 | // Draw a line if a player takes a small grid |
soapy12312 | 2:da0f01fbd25c | 329 | void drawSmallWinLine(int x1, int y1, int orientation) { |
soapy12312 | 2:da0f01fbd25c | 330 | int x2 = x1; |
soapy12312 | 2:da0f01fbd25c | 331 | int y2 = y1; |
soapy12312 | 2:da0f01fbd25c | 332 | |
soapy12312 | 2:da0f01fbd25c | 333 | if (orientation == FORWARDSLASH) { |
soapy12312 | 2:da0f01fbd25c | 334 | x2 = x1 - 41; |
soapy12312 | 2:da0f01fbd25c | 335 | y2 = y1 + 41; |
soapy12312 | 2:da0f01fbd25c | 336 | } else if (orientation == BACKWARDSLASH) { |
soapy12312 | 2:da0f01fbd25c | 337 | x2 = x1 + 41; |
soapy12312 | 2:da0f01fbd25c | 338 | y2 = y1 + 41; |
soapy12312 | 2:da0f01fbd25c | 339 | } else if (orientation == VERTICAL) { |
soapy12312 | 2:da0f01fbd25c | 340 | y2 = y1 + 41; |
soapy12312 | 2:da0f01fbd25c | 341 | } else if (orientation == HORIZONTAL) { |
soapy12312 | 2:da0f01fbd25c | 342 | x2 = x1 + 41; |
soapy12312 | 2:da0f01fbd25c | 343 | } |
soapy12312 | 2:da0f01fbd25c | 344 | |
soapy12312 | 2:da0f01fbd25c | 345 | uLCD.line(x1, y1, x2, y2, GREEN); |
soapy12312 | 2:da0f01fbd25c | 346 | wait(1); |
soapy12312 | 2:da0f01fbd25c | 347 | } |
soapy12312 | 2:da0f01fbd25c | 348 | |
soapy12312 | 2:da0f01fbd25c | 349 | // Check whether a player has taken a small grid |
soapy12312 | 2:da0f01fbd25c | 350 | bool smallGridWin(int largeX, int largeY, int smallX, int smallY) { |
soapy12312 | 2:da0f01fbd25c | 351 | bool taken = false; |
soapy12312 | 2:da0f01fbd25c | 352 | char individual[3][3]; |
soapy12312 | 2:da0f01fbd25c | 353 | |
soapy12312 | 2:da0f01fbd25c | 354 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 355 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 356 | individual[i][j] = grid.smallGrid[largeX][largeY].individualGrid[i][j]; |
soapy12312 | 2:da0f01fbd25c | 357 | } |
soapy12312 | 2:da0f01fbd25c | 358 | } |
soapy12312 | 2:da0f01fbd25c | 359 | |
soapy12312 | 2:da0f01fbd25c | 360 | int x1 = 1 + (largeX * 42) + 1; |
soapy12312 | 2:da0f01fbd25c | 361 | int y1 = 1 + (largeY * 42) + 1; |
soapy12312 | 2:da0f01fbd25c | 362 | int orientation = -1; |
soapy12312 | 2:da0f01fbd25c | 363 | |
soapy12312 | 2:da0f01fbd25c | 364 | if ((individual[0][0] == individual[1][1]) && (individual[1][1] == individual[2][2]) && |
soapy12312 | 2:da0f01fbd25c | 365 | (individual[0][0] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 366 | orientation = BACKWARDSLASH; |
soapy12312 | 2:da0f01fbd25c | 367 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 368 | } else if ((individual[2][0] == individual[1][1]) && (individual[1][1] == individual[0][2]) && |
soapy12312 | 2:da0f01fbd25c | 369 | (individual[2][0] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 370 | orientation = FORWARDSLASH; |
soapy12312 | 2:da0f01fbd25c | 371 | x1 += 40; |
soapy12312 | 2:da0f01fbd25c | 372 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 373 | } else if ((individual[0][0] == individual[1][0]) && (individual[1][0] == individual[2][0]) && |
soapy12312 | 2:da0f01fbd25c | 374 | (individual[0][0] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 375 | orientation = HORIZONTAL; |
soapy12312 | 2:da0f01fbd25c | 376 | y1 += 6; |
soapy12312 | 2:da0f01fbd25c | 377 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 378 | } else if ((individual[0][1] == individual[1][1]) && (individual[1][1] == individual[2][1]) && |
soapy12312 | 2:da0f01fbd25c | 379 | (individual[0][1] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 380 | orientation = HORIZONTAL; |
soapy12312 | 2:da0f01fbd25c | 381 | y1 += 20; |
soapy12312 | 2:da0f01fbd25c | 382 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 383 | } else if ((individual[0][2] == individual[1][2]) && (individual[1][2] == individual[2][2]) && |
soapy12312 | 2:da0f01fbd25c | 384 | (individual[0][2] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 385 | orientation = HORIZONTAL; |
soapy12312 | 2:da0f01fbd25c | 386 | y1 += 34; |
soapy12312 | 2:da0f01fbd25c | 387 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 388 | } else if ((individual[0][0] == individual[0][1]) && (individual[0][1] == individual[0][2]) && |
soapy12312 | 2:da0f01fbd25c | 389 | (individual[0][0] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 390 | orientation = VERTICAL; |
soapy12312 | 2:da0f01fbd25c | 391 | x1 += 6; |
soapy12312 | 2:da0f01fbd25c | 392 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 393 | } else if ((individual[1][0] == individual[1][1]) && (individual[1][1] == individual[1][2]) && |
soapy12312 | 2:da0f01fbd25c | 394 | (individual[1][0] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 395 | orientation = VERTICAL; |
soapy12312 | 2:da0f01fbd25c | 396 | x1 += 20; |
soapy12312 | 2:da0f01fbd25c | 397 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 398 | } else if ((individual[2][0] == individual[2][1]) && (individual[2][1] == individual[2][2]) && |
soapy12312 | 2:da0f01fbd25c | 399 | (individual[2][0] != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 400 | orientation = VERTICAL; |
soapy12312 | 2:da0f01fbd25c | 401 | x1 += 34; |
soapy12312 | 2:da0f01fbd25c | 402 | taken = true; |
soapy12312 | 2:da0f01fbd25c | 403 | } else { |
soapy12312 | 2:da0f01fbd25c | 404 | orientation = -1; |
soapy12312 | 2:da0f01fbd25c | 405 | taken = false; |
soapy12312 | 2:da0f01fbd25c | 406 | } |
soapy12312 | 2:da0f01fbd25c | 407 | |
soapy12312 | 2:da0f01fbd25c | 408 | // Check to see if there is a draw |
soapy12312 | 2:da0f01fbd25c | 409 | if (taken == false) { |
soapy12312 | 2:da0f01fbd25c | 410 | bool draw = true; |
soapy12312 | 2:da0f01fbd25c | 411 | |
soapy12312 | 2:da0f01fbd25c | 412 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 413 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 414 | if (individual[i][j] != (char)(-1)) { |
soapy12312 | 2:da0f01fbd25c | 415 | draw = false; |
soapy12312 | 2:da0f01fbd25c | 416 | } |
soapy12312 | 2:da0f01fbd25c | 417 | } |
soapy12312 | 2:da0f01fbd25c | 418 | } |
soapy12312 | 2:da0f01fbd25c | 419 | |
soapy12312 | 2:da0f01fbd25c | 420 | if (draw == true) { |
soapy12312 | 2:da0f01fbd25c | 421 | grid.smallGrid[largeX][largeY].done = 128; |
soapy12312 | 2:da0f01fbd25c | 422 | } |
soapy12312 | 2:da0f01fbd25c | 423 | } |
soapy12312 | 2:da0f01fbd25c | 424 | |
soapy12312 | 2:da0f01fbd25c | 425 | drawSmallWinLine(x1, y1, orientation); |
soapy12312 | 2:da0f01fbd25c | 426 | |
soapy12312 | 2:da0f01fbd25c | 427 | return taken; |
soapy12312 | 2:da0f01fbd25c | 428 | } |
soapy12312 | 2:da0f01fbd25c | 429 | |
soapy12312 | 2:da0f01fbd25c | 430 | // Take a turn |
soapy12312 | 2:da0f01fbd25c | 431 | void takeTurn(bool turn) { |
soapy12312 | 2:da0f01fbd25c | 432 | // Player selects small grid, then individual grid |
soapy12312 | 2:da0f01fbd25c | 433 | int keyInputs[2] = {-1, -1}; |
soapy12312 | 2:da0f01fbd25c | 434 | |
soapy12312 | 2:da0f01fbd25c | 435 | // If it is player one's turn |
soapy12312 | 2:da0f01fbd25c | 436 | if (turn == false) { |
soapy12312 | 2:da0f01fbd25c | 437 | RGB = RED; |
soapy12312 | 2:da0f01fbd25c | 438 | |
soapy12312 | 2:da0f01fbd25c | 439 | // Allow user input |
soapy12312 | 2:da0f01fbd25c | 440 | keyAllowed = true; |
soapy12312 | 2:da0f01fbd25c | 441 | |
soapy12312 | 2:da0f01fbd25c | 442 | int largeX = 0; |
soapy12312 | 2:da0f01fbd25c | 443 | int largeY = 0; |
soapy12312 | 2:da0f01fbd25c | 444 | |
soapy12312 | 2:da0f01fbd25c | 445 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 446 | |
soapy12312 | 2:da0f01fbd25c | 447 | // Ready to proceed |
soapy12312 | 2:da0f01fbd25c | 448 | XBee.putc(true); |
soapy12312 | 2:da0f01fbd25c | 449 | |
soapy12312 | 2:da0f01fbd25c | 450 | // Wait for player to select small grid |
soapy12312 | 2:da0f01fbd25c | 451 | while ((keyInputs[0] == -1) || (grid.smallGrid[largeX][largeY].done != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 452 | // Read in the value through the XBee |
soapy12312 | 2:da0f01fbd25c | 453 | if (XBee.readable() == true) { |
soapy12312 | 2:da0f01fbd25c | 454 | key = XBee.getc(); |
soapy12312 | 2:da0f01fbd25c | 455 | keyInputs[0] = key; |
soapy12312 | 2:da0f01fbd25c | 456 | largeX = xGrid(); |
soapy12312 | 2:da0f01fbd25c | 457 | largeY = yGrid(); |
soapy12312 | 2:da0f01fbd25c | 458 | } |
soapy12312 | 2:da0f01fbd25c | 459 | } |
soapy12312 | 2:da0f01fbd25c | 460 | |
soapy12312 | 2:da0f01fbd25c | 461 | // Highlight selected grid |
soapy12312 | 2:da0f01fbd25c | 462 | drawSmallGrid(largeX, largeY, PINK); |
soapy12312 | 2:da0f01fbd25c | 463 | |
soapy12312 | 2:da0f01fbd25c | 464 | playSound("/sd/select.wav"); |
soapy12312 | 2:da0f01fbd25c | 465 | |
soapy12312 | 2:da0f01fbd25c | 466 | // Allow user input |
soapy12312 | 2:da0f01fbd25c | 467 | keyAllowed = true; |
soapy12312 | 2:da0f01fbd25c | 468 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 469 | |
soapy12312 | 2:da0f01fbd25c | 470 | // Ready to proceed |
soapy12312 | 2:da0f01fbd25c | 471 | XBee.putc(true); |
soapy12312 | 2:da0f01fbd25c | 472 | |
soapy12312 | 2:da0f01fbd25c | 473 | // Wait for player to select individual grid |
soapy12312 | 2:da0f01fbd25c | 474 | while (keyInputs[1] == -1) { |
soapy12312 | 2:da0f01fbd25c | 475 | wait(0.01); |
soapy12312 | 2:da0f01fbd25c | 476 | |
soapy12312 | 2:da0f01fbd25c | 477 | // Read in the value through the XBee |
soapy12312 | 2:da0f01fbd25c | 478 | if (XBee.readable() == true) { |
soapy12312 | 2:da0f01fbd25c | 479 | key = XBee.getc(); |
soapy12312 | 2:da0f01fbd25c | 480 | keyInputs[1] = key; |
soapy12312 | 2:da0f01fbd25c | 481 | } |
soapy12312 | 2:da0f01fbd25c | 482 | } |
soapy12312 | 2:da0f01fbd25c | 483 | |
soapy12312 | 2:da0f01fbd25c | 484 | int smallX = xGrid(); |
soapy12312 | 2:da0f01fbd25c | 485 | int smallY = yGrid(); |
soapy12312 | 2:da0f01fbd25c | 486 | |
soapy12312 | 2:da0f01fbd25c | 487 | grid.smallGrid[largeX][largeY].individualGrid[smallX][smallY] = 0; |
soapy12312 | 2:da0f01fbd25c | 488 | drawSmallGrid(largeX, largeY, BLACK); |
soapy12312 | 2:da0f01fbd25c | 489 | drawSmallX(largeX, largeY, smallX, smallY); |
soapy12312 | 2:da0f01fbd25c | 490 | |
soapy12312 | 2:da0f01fbd25c | 491 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 492 | playSound("/sd/select.wav"); |
soapy12312 | 2:da0f01fbd25c | 493 | |
soapy12312 | 2:da0f01fbd25c | 494 | // Check if player one has taken a small grid |
soapy12312 | 2:da0f01fbd25c | 495 | if (smallGridWin(largeX, largeY, smallX, smallY) == true) { |
soapy12312 | 2:da0f01fbd25c | 496 | grid.smallGrid[largeX][largeY].done = 0; |
soapy12312 | 2:da0f01fbd25c | 497 | drawLargeX(largeX, largeY); |
soapy12312 | 2:da0f01fbd25c | 498 | } else { |
soapy12312 | 2:da0f01fbd25c | 499 | bool draw = true; |
soapy12312 | 2:da0f01fbd25c | 500 | |
soapy12312 | 2:da0f01fbd25c | 501 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 502 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 503 | if (grid.smallGrid[largeX][largeY].individualGrid[i][j] == (char)(-1)) { |
soapy12312 | 2:da0f01fbd25c | 504 | draw = false; |
soapy12312 | 2:da0f01fbd25c | 505 | } |
soapy12312 | 2:da0f01fbd25c | 506 | } |
soapy12312 | 2:da0f01fbd25c | 507 | } |
soapy12312 | 2:da0f01fbd25c | 508 | |
soapy12312 | 2:da0f01fbd25c | 509 | if (draw == true) { |
soapy12312 | 2:da0f01fbd25c | 510 | grid.smallGrid[largeX][largeY].done = 128; |
soapy12312 | 2:da0f01fbd25c | 511 | } |
soapy12312 | 2:da0f01fbd25c | 512 | } |
soapy12312 | 2:da0f01fbd25c | 513 | |
soapy12312 | 2:da0f01fbd25c | 514 | // Ready to proceed |
soapy12312 | 2:da0f01fbd25c | 515 | XBee.putc(true); |
soapy12312 | 2:da0f01fbd25c | 516 | // If it is player two's turn |
soapy12312 | 2:da0f01fbd25c | 517 | } else { |
soapy12312 | 2:da0f01fbd25c | 518 | RGB = BLUE; |
soapy12312 | 2:da0f01fbd25c | 519 | |
soapy12312 | 2:da0f01fbd25c | 520 | // Allow user input |
soapy12312 | 2:da0f01fbd25c | 521 | keyAllowed = true; |
soapy12312 | 2:da0f01fbd25c | 522 | |
soapy12312 | 2:da0f01fbd25c | 523 | int largeX = 0; |
soapy12312 | 2:da0f01fbd25c | 524 | int largeY = 0; |
soapy12312 | 2:da0f01fbd25c | 525 | |
soapy12312 | 2:da0f01fbd25c | 526 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 527 | |
soapy12312 | 2:da0f01fbd25c | 528 | // Check if the other player is ready to proceed |
soapy12312 | 2:da0f01fbd25c | 529 | bool ready = false; |
soapy12312 | 2:da0f01fbd25c | 530 | |
soapy12312 | 2:da0f01fbd25c | 531 | while (ready == false) { |
soapy12312 | 2:da0f01fbd25c | 532 | if (XBee.readable() == true) { |
soapy12312 | 2:da0f01fbd25c | 533 | ready = XBee.getc(); |
soapy12312 | 2:da0f01fbd25c | 534 | } |
soapy12312 | 2:da0f01fbd25c | 535 | } |
soapy12312 | 2:da0f01fbd25c | 536 | |
soapy12312 | 2:da0f01fbd25c | 537 | // Wait for player to select small grid |
soapy12312 | 2:da0f01fbd25c | 538 | while ((keyInputs[0] == -1) || (grid.smallGrid[largeX][largeY].done != (char)(-1))) { |
soapy12312 | 2:da0f01fbd25c | 539 | wait(0.01); |
soapy12312 | 2:da0f01fbd25c | 540 | keyInputs[0] = key; |
soapy12312 | 2:da0f01fbd25c | 541 | largeX = xGrid(); |
soapy12312 | 2:da0f01fbd25c | 542 | largeY = yGrid(); |
soapy12312 | 2:da0f01fbd25c | 543 | } |
soapy12312 | 2:da0f01fbd25c | 544 | |
soapy12312 | 2:da0f01fbd25c | 545 | // Send out the small grid value through the XBee |
soapy12312 | 2:da0f01fbd25c | 546 | XBee.putc(keyInputs[0]); |
soapy12312 | 2:da0f01fbd25c | 547 | |
soapy12312 | 2:da0f01fbd25c | 548 | // Highlight selected grid |
soapy12312 | 2:da0f01fbd25c | 549 | drawSmallGrid(largeX, largeY, CYAN); |
soapy12312 | 2:da0f01fbd25c | 550 | |
soapy12312 | 2:da0f01fbd25c | 551 | playSound("/sd/select.wav"); |
soapy12312 | 2:da0f01fbd25c | 552 | |
soapy12312 | 2:da0f01fbd25c | 553 | // Allow user input |
soapy12312 | 2:da0f01fbd25c | 554 | keyAllowed = true; |
soapy12312 | 2:da0f01fbd25c | 555 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 556 | |
soapy12312 | 2:da0f01fbd25c | 557 | // Check if the other player is ready to proceed |
soapy12312 | 2:da0f01fbd25c | 558 | ready = false; |
soapy12312 | 2:da0f01fbd25c | 559 | |
soapy12312 | 2:da0f01fbd25c | 560 | while (ready == false) { |
soapy12312 | 2:da0f01fbd25c | 561 | if (XBee.readable() == true) { |
soapy12312 | 2:da0f01fbd25c | 562 | ready = XBee.getc(); |
soapy12312 | 2:da0f01fbd25c | 563 | } |
soapy12312 | 2:da0f01fbd25c | 564 | } |
soapy12312 | 2:da0f01fbd25c | 565 | |
soapy12312 | 2:da0f01fbd25c | 566 | wait(0.5); |
soapy12312 | 2:da0f01fbd25c | 567 | |
soapy12312 | 2:da0f01fbd25c | 568 | // Wait for player to select individual grid |
soapy12312 | 2:da0f01fbd25c | 569 | while (keyInputs[1] == -1) { |
soapy12312 | 2:da0f01fbd25c | 570 | wait(0.01); |
soapy12312 | 2:da0f01fbd25c | 571 | |
soapy12312 | 2:da0f01fbd25c | 572 | if (grid.smallGrid[largeX][largeY].individualGrid[xGrid()][yGrid()] == (char)(-1)) { |
soapy12312 | 2:da0f01fbd25c | 573 | keyInputs[1] = key; |
soapy12312 | 2:da0f01fbd25c | 574 | } |
soapy12312 | 2:da0f01fbd25c | 575 | } |
soapy12312 | 2:da0f01fbd25c | 576 | |
soapy12312 | 2:da0f01fbd25c | 577 | // Send out the individual grid value through the XBee |
soapy12312 | 2:da0f01fbd25c | 578 | XBee.putc(keyInputs[1]); |
soapy12312 | 2:da0f01fbd25c | 579 | |
soapy12312 | 2:da0f01fbd25c | 580 | int smallX = xGrid(); |
soapy12312 | 2:da0f01fbd25c | 581 | int smallY = yGrid(); |
soapy12312 | 2:da0f01fbd25c | 582 | |
soapy12312 | 2:da0f01fbd25c | 583 | grid.smallGrid[largeX][largeY].individualGrid[smallX][smallY] = 1; |
soapy12312 | 2:da0f01fbd25c | 584 | drawSmallGrid(largeX, largeY, BLACK); |
soapy12312 | 2:da0f01fbd25c | 585 | drawSmallO(largeX, largeY, smallX, smallY); |
soapy12312 | 2:da0f01fbd25c | 586 | |
soapy12312 | 2:da0f01fbd25c | 587 | key = -1; |
soapy12312 | 2:da0f01fbd25c | 588 | playSound("/sd/select.wav"); |
soapy12312 | 2:da0f01fbd25c | 589 | |
soapy12312 | 2:da0f01fbd25c | 590 | // Check if player two has taken a small grid |
soapy12312 | 2:da0f01fbd25c | 591 | if (smallGridWin(largeX, largeY, smallX, smallY) == true) { |
soapy12312 | 2:da0f01fbd25c | 592 | grid.smallGrid[largeX][largeY].done = 1; |
soapy12312 | 2:da0f01fbd25c | 593 | drawLargeO(largeX, largeY); |
soapy12312 | 2:da0f01fbd25c | 594 | } else { |
soapy12312 | 2:da0f01fbd25c | 595 | bool draw = true; |
soapy12312 | 2:da0f01fbd25c | 596 | |
soapy12312 | 2:da0f01fbd25c | 597 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 598 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 599 | if (grid.smallGrid[largeX][largeY].individualGrid[i][j] == (char)(-1)) { |
soapy12312 | 2:da0f01fbd25c | 600 | draw = false; |
soapy12312 | 2:da0f01fbd25c | 601 | } |
soapy12312 | 2:da0f01fbd25c | 602 | } |
soapy12312 | 2:da0f01fbd25c | 603 | } |
soapy12312 | 2:da0f01fbd25c | 604 | |
soapy12312 | 2:da0f01fbd25c | 605 | if (draw == true) { |
soapy12312 | 2:da0f01fbd25c | 606 | grid.smallGrid[largeX][largeY].done = 128; |
soapy12312 | 2:da0f01fbd25c | 607 | } |
soapy12312 | 2:da0f01fbd25c | 608 | } |
soapy12312 | 2:da0f01fbd25c | 609 | |
soapy12312 | 2:da0f01fbd25c | 610 | // Check if the other player is ready to proceed |
soapy12312 | 2:da0f01fbd25c | 611 | ready = false; |
soapy12312 | 2:da0f01fbd25c | 612 | |
soapy12312 | 2:da0f01fbd25c | 613 | while (ready == false) { |
soapy12312 | 2:da0f01fbd25c | 614 | if (XBee.readable() == true) { |
soapy12312 | 2:da0f01fbd25c | 615 | ready = XBee.getc(); |
soapy12312 | 2:da0f01fbd25c | 616 | } |
soapy12312 | 2:da0f01fbd25c | 617 | } |
soapy12312 | 2:da0f01fbd25c | 618 | } |
soapy12312 | 2:da0f01fbd25c | 619 | } |
soapy12312 | 2:da0f01fbd25c | 620 | |
soapy12312 | 2:da0f01fbd25c | 621 | // Draw final line when a player wins |
soapy12312 | 2:da0f01fbd25c | 622 | void drawLargeWinLine(int x1, int y1, int orientation) { |
soapy12312 | 2:da0f01fbd25c | 623 | int x2 = 127; |
soapy12312 | 2:da0f01fbd25c | 624 | int y2 = 127; |
soapy12312 | 2:da0f01fbd25c | 625 | |
soapy12312 | 2:da0f01fbd25c | 626 | if (orientation == FORWARDSLASH) { |
soapy12312 | 2:da0f01fbd25c | 627 | x2 = 0; |
soapy12312 | 2:da0f01fbd25c | 628 | } else if (orientation == HORIZONTAL) { |
soapy12312 | 2:da0f01fbd25c | 629 | y2 = y1; |
soapy12312 | 2:da0f01fbd25c | 630 | } else if (orientation == VERTICAL) { |
soapy12312 | 2:da0f01fbd25c | 631 | x2 = x1; |
soapy12312 | 2:da0f01fbd25c | 632 | } |
soapy12312 | 2:da0f01fbd25c | 633 | |
soapy12312 | 2:da0f01fbd25c | 634 | wait(1); |
soapy12312 | 2:da0f01fbd25c | 635 | uLCD.line(x1, y1, x2, y2, GREEN); |
soapy12312 | 2:da0f01fbd25c | 636 | wait(3); |
soapy12312 | 2:da0f01fbd25c | 637 | } |
soapy12312 | 2:da0f01fbd25c | 638 | |
soapy12312 | 2:da0f01fbd25c | 639 | // Check whether a player has won |
soapy12312 | 2:da0f01fbd25c | 640 | void gameWin(bool turn) { |
soapy12312 | 2:da0f01fbd25c | 641 | bool won = false; |
soapy12312 | 2:da0f01fbd25c | 642 | char small[3][3]; |
soapy12312 | 2:da0f01fbd25c | 643 | |
soapy12312 | 2:da0f01fbd25c | 644 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 645 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 646 | small[i][j] = grid.smallGrid[i][j].done; |
soapy12312 | 2:da0f01fbd25c | 647 | } |
soapy12312 | 2:da0f01fbd25c | 648 | } |
soapy12312 | 2:da0f01fbd25c | 649 | |
soapy12312 | 2:da0f01fbd25c | 650 | int x1 = 2; |
soapy12312 | 2:da0f01fbd25c | 651 | int y1 = 2; |
soapy12312 | 2:da0f01fbd25c | 652 | int orientation = -1; |
soapy12312 | 2:da0f01fbd25c | 653 | |
soapy12312 | 2:da0f01fbd25c | 654 | if ((small[0][0] == small[1][1]) && (small[1][1] == small[2][2]) && |
soapy12312 | 2:da0f01fbd25c | 655 | (small[0][0] != (char)(-1)) && (small[0][0] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 656 | orientation = BACKWARDSLASH; |
soapy12312 | 2:da0f01fbd25c | 657 | won = true; |
soapy12312 | 2:da0f01fbd25c | 658 | } else if ((small[2][0] == small[1][1]) && (small[1][1] == small[0][2]) && |
soapy12312 | 2:da0f01fbd25c | 659 | (small[2][0] != (char)(-1)) && (small[2][0] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 660 | orientation = FORWARDSLASH; |
soapy12312 | 2:da0f01fbd25c | 661 | x1 = 126; |
soapy12312 | 2:da0f01fbd25c | 662 | won = true; |
soapy12312 | 2:da0f01fbd25c | 663 | } else if ((small[0][0] == small[1][0]) && (small[1][0] == small[2][0]) && |
soapy12312 | 2:da0f01fbd25c | 664 | (small[0][0] != (char)(-1)) && (small[0][0] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 665 | orientation = HORIZONTAL; |
soapy12312 | 2:da0f01fbd25c | 666 | y1 += 21; |
soapy12312 | 2:da0f01fbd25c | 667 | won = true; |
soapy12312 | 2:da0f01fbd25c | 668 | } else if ((small[0][1] == small[1][1]) && (small[1][1] == small[2][1]) && |
soapy12312 | 2:da0f01fbd25c | 669 | (small[0][1] != (char)(-1)) && (small[0][1] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 670 | orientation = HORIZONTAL; |
soapy12312 | 2:da0f01fbd25c | 671 | y1 += 63; |
soapy12312 | 2:da0f01fbd25c | 672 | won = true; |
soapy12312 | 2:da0f01fbd25c | 673 | } else if ((small[0][2] == small[1][2]) && (small[1][2] == small[2][2]) && |
soapy12312 | 2:da0f01fbd25c | 674 | (small[0][2] != (char)(-1)) && (small[0][2] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 675 | orientation = HORIZONTAL; |
soapy12312 | 2:da0f01fbd25c | 676 | y1 += 105; |
soapy12312 | 2:da0f01fbd25c | 677 | won = true; |
soapy12312 | 2:da0f01fbd25c | 678 | } else if ((small[0][0] == small[0][1]) && (small[0][1] == small[0][2]) && |
soapy12312 | 2:da0f01fbd25c | 679 | (small[0][0] != (char)(-1)) && (small[0][0] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 680 | orientation = VERTICAL; |
soapy12312 | 2:da0f01fbd25c | 681 | x1 += 21; |
soapy12312 | 2:da0f01fbd25c | 682 | won = true; |
soapy12312 | 2:da0f01fbd25c | 683 | } else if ((small[1][0] == small[1][1]) && (small[1][1] == small[1][2]) && |
soapy12312 | 2:da0f01fbd25c | 684 | (small[1][0] != (char)(-1)) && (small[1][0] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 685 | orientation = VERTICAL; |
soapy12312 | 2:da0f01fbd25c | 686 | x1 += 63; |
soapy12312 | 2:da0f01fbd25c | 687 | won = true; |
soapy12312 | 2:da0f01fbd25c | 688 | } else if ((small[2][0] == small[2][1]) && (small[2][1] == small[2][2]) && |
soapy12312 | 2:da0f01fbd25c | 689 | (small[2][0] != (char)(-1)) && (small[2][0] != 128)) { |
soapy12312 | 2:da0f01fbd25c | 690 | orientation = VERTICAL; |
soapy12312 | 2:da0f01fbd25c | 691 | x1 += 105; |
soapy12312 | 2:da0f01fbd25c | 692 | won = true; |
soapy12312 | 2:da0f01fbd25c | 693 | } else { |
soapy12312 | 2:da0f01fbd25c | 694 | orientation = -1; |
soapy12312 | 2:da0f01fbd25c | 695 | won = false; |
soapy12312 | 2:da0f01fbd25c | 696 | } |
soapy12312 | 2:da0f01fbd25c | 697 | |
soapy12312 | 2:da0f01fbd25c | 698 | // Check to see if there is a draw |
soapy12312 | 2:da0f01fbd25c | 699 | if (won == false) { |
soapy12312 | 2:da0f01fbd25c | 700 | bool draw = true; |
soapy12312 | 2:da0f01fbd25c | 701 | |
soapy12312 | 2:da0f01fbd25c | 702 | for (int i = 0; i < 3; i++) { |
soapy12312 | 2:da0f01fbd25c | 703 | for (int j = 0; j < 3; j++) { |
soapy12312 | 2:da0f01fbd25c | 704 | if (small[i][j] == (char)(-1)) { |
soapy12312 | 2:da0f01fbd25c | 705 | draw = false; |
soapy12312 | 2:da0f01fbd25c | 706 | } |
soapy12312 | 2:da0f01fbd25c | 707 | } |
soapy12312 | 2:da0f01fbd25c | 708 | } |
soapy12312 | 2:da0f01fbd25c | 709 | |
soapy12312 | 2:da0f01fbd25c | 710 | if (draw == true) { |
soapy12312 | 2:da0f01fbd25c | 711 | grid.done = 128; |
soapy12312 | 2:da0f01fbd25c | 712 | wait(3); |
soapy12312 | 2:da0f01fbd25c | 713 | } |
soapy12312 | 2:da0f01fbd25c | 714 | } else { |
soapy12312 | 2:da0f01fbd25c | 715 | if (turn == false) { |
soapy12312 | 2:da0f01fbd25c | 716 | grid.done = 0; |
soapy12312 | 2:da0f01fbd25c | 717 | } else { |
soapy12312 | 2:da0f01fbd25c | 718 | grid.done = 1; |
soapy12312 | 2:da0f01fbd25c | 719 | } |
soapy12312 | 2:da0f01fbd25c | 720 | |
soapy12312 | 2:da0f01fbd25c | 721 | drawLargeWinLine(x1, y1, orientation); |
soapy12312 | 2:da0f01fbd25c | 722 | } |
soapy12312 | 2:da0f01fbd25c | 723 | } |
soapy12312 | 2:da0f01fbd25c | 724 | }; |