Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:912c221cc186, 2018-04-28 (annotated)
- Committer:
- bdonohue
- Date:
- Sat Apr 28 22:07:24 2018 +0000
- Revision:
- 0:912c221cc186
working final ver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bdonohue | 0:912c221cc186 | 1 | //Headers |
bdonohue | 0:912c221cc186 | 2 | #include "globals.h" |
bdonohue | 0:912c221cc186 | 3 | #include "Player.h" |
bdonohue | 0:912c221cc186 | 4 | #include "PinDetect.h" |
bdonohue | 0:912c221cc186 | 5 | #include "letters.h" |
bdonohue | 0:912c221cc186 | 6 | #include "bluetoothFunc.h" |
bdonohue | 0:912c221cc186 | 7 | |
bdonohue | 0:912c221cc186 | 8 | //Local functions |
bdonohue | 0:912c221cc186 | 9 | void buttonHit (void); |
bdonohue | 0:912c221cc186 | 10 | void bluetoothResponse(); |
bdonohue | 0:912c221cc186 | 11 | void moveCursor(); |
bdonohue | 0:912c221cc186 | 12 | void callback(); |
bdonohue | 0:912c221cc186 | 13 | |
bdonohue | 0:912c221cc186 | 14 | BusOut myled(LED1,LED2,LED3,LED4); |
bdonohue | 0:912c221cc186 | 15 | |
bdonohue | 0:912c221cc186 | 16 | //I/O |
bdonohue | 0:912c221cc186 | 17 | Serial blue(p28,p27); |
bdonohue | 0:912c221cc186 | 18 | uLCD_4DGL uLCD(p9,p10,p11); |
bdonohue | 0:912c221cc186 | 19 | PinDetect buttonLeft(p15); |
bdonohue | 0:912c221cc186 | 20 | PinDetect buttonUp(p14); |
bdonohue | 0:912c221cc186 | 21 | PinDetect buttonRight(p16); |
bdonohue | 0:912c221cc186 | 22 | PinDetect buttonDown(p13); |
bdonohue | 0:912c221cc186 | 23 | AnalogIn potentiometer(p20); |
bdonohue | 0:912c221cc186 | 24 | Serial pc(USBTX, USBRX); |
bdonohue | 0:912c221cc186 | 25 | |
bdonohue | 0:912c221cc186 | 26 | //Class initialization |
bdonohue | 0:912c221cc186 | 27 | Player player; |
bdonohue | 0:912c221cc186 | 28 | Mutex serialLock; |
bdonohue | 0:912c221cc186 | 29 | |
bdonohue | 0:912c221cc186 | 30 | //VARIABLES |
bdonohue | 0:912c221cc186 | 31 | int state; |
bdonohue | 0:912c221cc186 | 32 | int fire, dir; |
bdonohue | 0:912c221cc186 | 33 | int winOrLose; |
bdonohue | 0:912c221cc186 | 34 | int enemyHits; |
bdonohue | 0:912c221cc186 | 35 | |
bdonohue | 0:912c221cc186 | 36 | //bluetooth variables |
bdonohue | 0:912c221cc186 | 37 | int num; |
bdonohue | 0:912c221cc186 | 38 | int upOrDown; |
bdonohue | 0:912c221cc186 | 39 | int updateMove; |
bdonohue | 0:912c221cc186 | 40 | |
bdonohue | 0:912c221cc186 | 41 | //move cursor variables |
bdonohue | 0:912c221cc186 | 42 | int x, y, prevX, prevY; |
bdonohue | 0:912c221cc186 | 43 | |
bdonohue | 0:912c221cc186 | 44 | //ship placement variables |
bdonohue | 0:912c221cc186 | 45 | int newX, newY; |
bdonohue | 0:912c221cc186 | 46 | int ships[5]; |
bdonohue | 0:912c221cc186 | 47 | int shipPointer; |
bdonohue | 0:912c221cc186 | 48 | int shipLength; |
bdonohue | 0:912c221cc186 | 49 | int prevXship[5]; |
bdonohue | 0:912c221cc186 | 50 | int prevYship[5]; |
bdonohue | 0:912c221cc186 | 51 | int prevShipColor[5]; |
bdonohue | 0:912c221cc186 | 52 | int prevShipLength; |
bdonohue | 0:912c221cc186 | 53 | int layout; |
bdonohue | 0:912c221cc186 | 54 | |
bdonohue | 0:912c221cc186 | 55 | //server variables |
bdonohue | 0:912c221cc186 | 56 | char cmd[4]; |
bdonohue | 0:912c221cc186 | 57 | int clearBackground; |
bdonohue | 0:912c221cc186 | 58 | int fireX, fireY; |
bdonohue | 0:912c221cc186 | 59 | int nextHit; |
bdonohue | 0:912c221cc186 | 60 | |
bdonohue | 0:912c221cc186 | 61 | int main() { |
bdonohue | 0:912c221cc186 | 62 | //Variable setups |
bdonohue | 0:912c221cc186 | 63 | state = 1; |
bdonohue | 0:912c221cc186 | 64 | fire = dir = 0; |
bdonohue | 0:912c221cc186 | 65 | enemyHits = 0; |
bdonohue | 0:912c221cc186 | 66 | x = y = prevX = prevY = 0; |
bdonohue | 0:912c221cc186 | 67 | num = upOrDown = updateMove = 0; |
bdonohue | 0:912c221cc186 | 68 | ships[0] = 5; |
bdonohue | 0:912c221cc186 | 69 | ships[1] = 4; |
bdonohue | 0:912c221cc186 | 70 | ships[2] = 3; |
bdonohue | 0:912c221cc186 | 71 | ships[3] = 3; |
bdonohue | 0:912c221cc186 | 72 | ships[4] = 2; |
bdonohue | 0:912c221cc186 | 73 | newX = newY = shipPointer = 0; |
bdonohue | 0:912c221cc186 | 74 | shipLength = ships[shipPointer]; |
bdonohue | 0:912c221cc186 | 75 | for(int i = 0; i < 5; i++){ |
bdonohue | 0:912c221cc186 | 76 | prevXship[i] = 0; |
bdonohue | 0:912c221cc186 | 77 | prevYship[i] = 0; |
bdonohue | 0:912c221cc186 | 78 | prevShipColor[i] = 0; |
bdonohue | 0:912c221cc186 | 79 | } |
bdonohue | 0:912c221cc186 | 80 | prevShipLength = 0; |
bdonohue | 0:912c221cc186 | 81 | layout = -1; |
bdonohue | 0:912c221cc186 | 82 | clearBackground = 0; |
bdonohue | 0:912c221cc186 | 83 | |
bdonohue | 0:912c221cc186 | 84 | nextHit = 2; |
bdonohue | 0:912c221cc186 | 85 | //Screen setup |
bdonohue | 0:912c221cc186 | 86 | uLCD.set_font_size(8,8); |
bdonohue | 0:912c221cc186 | 87 | uLCD.color(0xFFFFFF); |
bdonohue | 0:912c221cc186 | 88 | uLCD.background_color(0x0000FF); |
bdonohue | 0:912c221cc186 | 89 | uLCD.cls(); |
bdonohue | 0:912c221cc186 | 90 | addText(); |
bdonohue | 0:912c221cc186 | 91 | player.PrintBoard(); |
bdonohue | 0:912c221cc186 | 92 | while(pc.readable()) { |
bdonohue | 0:912c221cc186 | 93 | pc.getc(); |
bdonohue | 0:912c221cc186 | 94 | } |
bdonohue | 0:912c221cc186 | 95 | //Main loop |
bdonohue | 0:912c221cc186 | 96 | while(1){ |
bdonohue | 0:912c221cc186 | 97 | |
bdonohue | 0:912c221cc186 | 98 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 99 | //bluetooth function |
bdonohue | 0:912c221cc186 | 100 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 101 | if(bluetoothFunc(num,upOrDown)){ |
bdonohue | 0:912c221cc186 | 102 | if(num >= 1 && num <= 4 && upOrDown){ |
bdonohue | 0:912c221cc186 | 103 | layout = num; |
bdonohue | 0:912c221cc186 | 104 | if(num == 1){ |
bdonohue | 0:912c221cc186 | 105 | fire = 1; |
bdonohue | 0:912c221cc186 | 106 | } else if(num == 2){ |
bdonohue | 0:912c221cc186 | 107 | updateMove = 1; |
bdonohue | 0:912c221cc186 | 108 | if(dir){ |
bdonohue | 0:912c221cc186 | 109 | dir = 0; |
bdonohue | 0:912c221cc186 | 110 | } else { |
bdonohue | 0:912c221cc186 | 111 | dir = 1; |
bdonohue | 0:912c221cc186 | 112 | } |
bdonohue | 0:912c221cc186 | 113 | } |
bdonohue | 0:912c221cc186 | 114 | } else if (num >= 5 && num <= 8 && upOrDown){ |
bdonohue | 0:912c221cc186 | 115 | updateMove = 1; |
bdonohue | 0:912c221cc186 | 116 | if(num == 7){ |
bdonohue | 0:912c221cc186 | 117 | x = prevX - 1; |
bdonohue | 0:912c221cc186 | 118 | if(x < 0){ |
bdonohue | 0:912c221cc186 | 119 | x = 0; |
bdonohue | 0:912c221cc186 | 120 | } |
bdonohue | 0:912c221cc186 | 121 | } else if(num == 8){ |
bdonohue | 0:912c221cc186 | 122 | x = prevX + 1; |
bdonohue | 0:912c221cc186 | 123 | if(x > 9){ |
bdonohue | 0:912c221cc186 | 124 | x = 9; |
bdonohue | 0:912c221cc186 | 125 | } |
bdonohue | 0:912c221cc186 | 126 | } else if(num == 5){ |
bdonohue | 0:912c221cc186 | 127 | y = prevY - 1; |
bdonohue | 0:912c221cc186 | 128 | if(y < 0){ |
bdonohue | 0:912c221cc186 | 129 | y = 0; |
bdonohue | 0:912c221cc186 | 130 | } |
bdonohue | 0:912c221cc186 | 131 | } else if(num == 6){ |
bdonohue | 0:912c221cc186 | 132 | y = prevY + 1; |
bdonohue | 0:912c221cc186 | 133 | if(y > 9){ |
bdonohue | 0:912c221cc186 | 134 | y = 9; |
bdonohue | 0:912c221cc186 | 135 | } |
bdonohue | 0:912c221cc186 | 136 | } |
bdonohue | 0:912c221cc186 | 137 | uLCD.rectangle(26+prevX*10,26+prevY*10,36+prevX*10,36+prevY*10,WHITE); |
bdonohue | 0:912c221cc186 | 138 | uLCD.rectangle(26+x*10,26+y*10,36+x*10,36+y*10,RED); |
bdonohue | 0:912c221cc186 | 139 | prevX = x; |
bdonohue | 0:912c221cc186 | 140 | prevY = y; |
bdonohue | 0:912c221cc186 | 141 | } |
bdonohue | 0:912c221cc186 | 142 | } |
bdonohue | 0:912c221cc186 | 143 | |
bdonohue | 0:912c221cc186 | 144 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 145 | //STATE MACHINE |
bdonohue | 0:912c221cc186 | 146 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 147 | |
bdonohue | 0:912c221cc186 | 148 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 149 | //PLACE SHIPS |
bdonohue | 0:912c221cc186 | 150 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 151 | if(state == 1){ |
bdonohue | 0:912c221cc186 | 152 | /* |
bdonohue | 0:912c221cc186 | 153 | if(player.presetShips()){ |
bdonohue | 0:912c221cc186 | 154 | state = 2; |
bdonohue | 0:912c221cc186 | 155 | } |
bdonohue | 0:912c221cc186 | 156 | */ |
bdonohue | 0:912c221cc186 | 157 | //Clear the previous ships |
bdonohue | 0:912c221cc186 | 158 | if(updateMove && clearBackground){ |
bdonohue | 0:912c221cc186 | 159 | clearBackground = 0; |
bdonohue | 0:912c221cc186 | 160 | for(int i = 0; i < shipLength; i++){ |
bdonohue | 0:912c221cc186 | 161 | newX = prevXship[i]; |
bdonohue | 0:912c221cc186 | 162 | newY = prevYship[i]; |
bdonohue | 0:912c221cc186 | 163 | if(prevShipColor[i]){ |
bdonohue | 0:912c221cc186 | 164 | if(newX < 10 && newY < 10){ |
bdonohue | 0:912c221cc186 | 165 | uLCD.filled_circle(31+newX*10,31+newY*10,4,0xFFFFFF); |
bdonohue | 0:912c221cc186 | 166 | } |
bdonohue | 0:912c221cc186 | 167 | } else { |
bdonohue | 0:912c221cc186 | 168 | if(newX < 10 && newY < 10){ |
bdonohue | 0:912c221cc186 | 169 | uLCD.filled_circle(31+newX*10,31+newY*10,4,0x0000FF); |
bdonohue | 0:912c221cc186 | 170 | } |
bdonohue | 0:912c221cc186 | 171 | } |
bdonohue | 0:912c221cc186 | 172 | } |
bdonohue | 0:912c221cc186 | 173 | } |
bdonohue | 0:912c221cc186 | 174 | |
bdonohue | 0:912c221cc186 | 175 | if(updateMove){ |
bdonohue | 0:912c221cc186 | 176 | updateMove = 0; |
bdonohue | 0:912c221cc186 | 177 | clearBackground = 1; |
bdonohue | 0:912c221cc186 | 178 | //Print the ship |
bdonohue | 0:912c221cc186 | 179 | if(dir){ |
bdonohue | 0:912c221cc186 | 180 | for(int i = x; i < x + shipLength; i++){ |
bdonohue | 0:912c221cc186 | 181 | if(i < 10){ |
bdonohue | 0:912c221cc186 | 182 | uLCD.filled_circle(31+i*10,31+y*10,4,0x00FF00); |
bdonohue | 0:912c221cc186 | 183 | } |
bdonohue | 0:912c221cc186 | 184 | } |
bdonohue | 0:912c221cc186 | 185 | for(int i = 0; i < shipLength; i++){ |
bdonohue | 0:912c221cc186 | 186 | prevXship[i] = x + i; |
bdonohue | 0:912c221cc186 | 187 | prevYship[i] = y; |
bdonohue | 0:912c221cc186 | 188 | prevShipColor[i] = player.getColor(x+i,y); |
bdonohue | 0:912c221cc186 | 189 | } |
bdonohue | 0:912c221cc186 | 190 | } else { |
bdonohue | 0:912c221cc186 | 191 | for(int i = y; i < y + shipLength; i++){ |
bdonohue | 0:912c221cc186 | 192 | if(i < 10){ |
bdonohue | 0:912c221cc186 | 193 | uLCD.filled_circle(31+x*10,31+i*10,4,0x00FF00); |
bdonohue | 0:912c221cc186 | 194 | } |
bdonohue | 0:912c221cc186 | 195 | } |
bdonohue | 0:912c221cc186 | 196 | for(int i = 0; i < shipLength; i++){ |
bdonohue | 0:912c221cc186 | 197 | prevXship[i] = x; |
bdonohue | 0:912c221cc186 | 198 | prevYship[i] = y + i; |
bdonohue | 0:912c221cc186 | 199 | prevShipColor[i] = player.getColor(x,y+i); |
bdonohue | 0:912c221cc186 | 200 | } |
bdonohue | 0:912c221cc186 | 201 | } |
bdonohue | 0:912c221cc186 | 202 | } |
bdonohue | 0:912c221cc186 | 203 | |
bdonohue | 0:912c221cc186 | 204 | //Player presses the fire button |
bdonohue | 0:912c221cc186 | 205 | if(fire){ |
bdonohue | 0:912c221cc186 | 206 | fire = 0; |
bdonohue | 0:912c221cc186 | 207 | bool canPlace = true; |
bdonohue | 0:912c221cc186 | 208 | //Check if the boat can be placed |
bdonohue | 0:912c221cc186 | 209 | for(int i = 0; i < shipLength; i++){ |
bdonohue | 0:912c221cc186 | 210 | if(player.getColor(prevXship[i],prevYship[i]) != 0){ |
bdonohue | 0:912c221cc186 | 211 | canPlace = false; |
bdonohue | 0:912c221cc186 | 212 | break; |
bdonohue | 0:912c221cc186 | 213 | } |
bdonohue | 0:912c221cc186 | 214 | if(prevXship[i] > 10 || prevYship[i] > 10){ |
bdonohue | 0:912c221cc186 | 215 | canPlace = false; |
bdonohue | 0:912c221cc186 | 216 | break; |
bdonohue | 0:912c221cc186 | 217 | } |
bdonohue | 0:912c221cc186 | 218 | } |
bdonohue | 0:912c221cc186 | 219 | //Place the boat |
bdonohue | 0:912c221cc186 | 220 | if(canPlace){ |
bdonohue | 0:912c221cc186 | 221 | for(int i = 0; i < shipLength; i++){ |
bdonohue | 0:912c221cc186 | 222 | player.PlaceShips(prevXship[i],prevYship[i]); |
bdonohue | 0:912c221cc186 | 223 | } |
bdonohue | 0:912c221cc186 | 224 | shipPointer++; |
bdonohue | 0:912c221cc186 | 225 | clearBackground = 0; |
bdonohue | 0:912c221cc186 | 226 | shipLength = ships[shipPointer]; |
bdonohue | 0:912c221cc186 | 227 | player.PrintBoard(); |
bdonohue | 0:912c221cc186 | 228 | } |
bdonohue | 0:912c221cc186 | 229 | } |
bdonohue | 0:912c221cc186 | 230 | |
bdonohue | 0:912c221cc186 | 231 | //Check if all the boats were placed |
bdonohue | 0:912c221cc186 | 232 | if(shipPointer > 4){ |
bdonohue | 0:912c221cc186 | 233 | state = 2; |
bdonohue | 0:912c221cc186 | 234 | } |
bdonohue | 0:912c221cc186 | 235 | } |
bdonohue | 0:912c221cc186 | 236 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 237 | |
bdonohue | 0:912c221cc186 | 238 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 239 | //DETERMINE WHICH PLAYER GOES FIRST |
bdonohue | 0:912c221cc186 | 240 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 241 | else if(state == 2){ |
bdonohue | 0:912c221cc186 | 242 | myled[0] = 1; |
bdonohue | 0:912c221cc186 | 243 | myled[1] = 1; |
bdonohue | 0:912c221cc186 | 244 | myled[2] = 1; |
bdonohue | 0:912c221cc186 | 245 | myled[3] = 1; |
bdonohue | 0:912c221cc186 | 246 | |
bdonohue | 0:912c221cc186 | 247 | fire = 0; |
bdonohue | 0:912c221cc186 | 248 | |
bdonohue | 0:912c221cc186 | 249 | for (int i = 0; i < 4; i++) { |
bdonohue | 0:912c221cc186 | 250 | cmd[i] = pc.getc(); |
bdonohue | 0:912c221cc186 | 251 | } |
bdonohue | 0:912c221cc186 | 252 | |
bdonohue | 0:912c221cc186 | 253 | if(cmd[0] == 'a'){ |
bdonohue | 0:912c221cc186 | 254 | player.PrintEnemyBoard(); |
bdonohue | 0:912c221cc186 | 255 | addAttack(); |
bdonohue | 0:912c221cc186 | 256 | state = 3; |
bdonohue | 0:912c221cc186 | 257 | } else { |
bdonohue | 0:912c221cc186 | 258 | player.PrintBoard(); |
bdonohue | 0:912c221cc186 | 259 | addDefense(); |
bdonohue | 0:912c221cc186 | 260 | state = 4; |
bdonohue | 0:912c221cc186 | 261 | } |
bdonohue | 0:912c221cc186 | 262 | myled[0] = 0; |
bdonohue | 0:912c221cc186 | 263 | myled[1] = 0; |
bdonohue | 0:912c221cc186 | 264 | myled[2] = 0; |
bdonohue | 0:912c221cc186 | 265 | myled[3] = 0; |
bdonohue | 0:912c221cc186 | 266 | } |
bdonohue | 0:912c221cc186 | 267 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 268 | |
bdonohue | 0:912c221cc186 | 269 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 270 | //ATTACK |
bdonohue | 0:912c221cc186 | 271 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 272 | else if(state == 3){ |
bdonohue | 0:912c221cc186 | 273 | myled[1] = 1; |
bdonohue | 0:912c221cc186 | 274 | |
bdonohue | 0:912c221cc186 | 275 | if(fire){ |
bdonohue | 0:912c221cc186 | 276 | fire = 0; |
bdonohue | 0:912c221cc186 | 277 | if(player.getEnemyColor(x,y) == 0){ |
bdonohue | 0:912c221cc186 | 278 | fireX = x; |
bdonohue | 0:912c221cc186 | 279 | fireY = y; |
bdonohue | 0:912c221cc186 | 280 | pc.putc(x + '0'); |
bdonohue | 0:912c221cc186 | 281 | pc.putc(y + '0'); |
bdonohue | 0:912c221cc186 | 282 | pc.putc(nextHit + '0'); //hit or miss |
bdonohue | 0:912c221cc186 | 283 | pc.putc('0'); //endgame |
bdonohue | 0:912c221cc186 | 284 | |
bdonohue | 0:912c221cc186 | 285 | player.PrintBoard(); |
bdonohue | 0:912c221cc186 | 286 | addDefense(); |
bdonohue | 0:912c221cc186 | 287 | state = 4; |
bdonohue | 0:912c221cc186 | 288 | myled[1] = 0; |
bdonohue | 0:912c221cc186 | 289 | } |
bdonohue | 0:912c221cc186 | 290 | |
bdonohue | 0:912c221cc186 | 291 | } |
bdonohue | 0:912c221cc186 | 292 | } |
bdonohue | 0:912c221cc186 | 293 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 294 | |
bdonohue | 0:912c221cc186 | 295 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 296 | //WAIT |
bdonohue | 0:912c221cc186 | 297 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 298 | else if(state == 4){ |
bdonohue | 0:912c221cc186 | 299 | myled[2] = 1; |
bdonohue | 0:912c221cc186 | 300 | wait(1); |
bdonohue | 0:912c221cc186 | 301 | cmd[0] = pc.getc(); |
bdonohue | 0:912c221cc186 | 302 | cmd[1] = pc.getc(); |
bdonohue | 0:912c221cc186 | 303 | cmd[2] = pc.getc(); |
bdonohue | 0:912c221cc186 | 304 | cmd[3] = pc.getc(); |
bdonohue | 0:912c221cc186 | 305 | |
bdonohue | 0:912c221cc186 | 306 | int enemyX = cmd[0] - '0'; |
bdonohue | 0:912c221cc186 | 307 | int enemyY = cmd[1] - '0'; |
bdonohue | 0:912c221cc186 | 308 | int yourHit = cmd[2] - '0'; |
bdonohue | 0:912c221cc186 | 309 | int win = cmd[3] - '0'; |
bdonohue | 0:912c221cc186 | 310 | |
bdonohue | 0:912c221cc186 | 311 | state = 3; |
bdonohue | 0:912c221cc186 | 312 | if(win){ |
bdonohue | 0:912c221cc186 | 313 | state = 5; |
bdonohue | 0:912c221cc186 | 314 | winOrLose = 1; |
bdonohue | 0:912c221cc186 | 315 | } else { |
bdonohue | 0:912c221cc186 | 316 | //UPDATE ENEMY BOARD |
bdonohue | 0:912c221cc186 | 317 | player.Shot(fireX,fireY,yourHit); |
bdonohue | 0:912c221cc186 | 318 | //UPDATE YOUR BOARD |
bdonohue | 0:912c221cc186 | 319 | nextHit = player.EnemyShot(enemyX,enemyY); |
bdonohue | 0:912c221cc186 | 320 | if(nextHit == 1){ |
bdonohue | 0:912c221cc186 | 321 | enemyHits++; |
bdonohue | 0:912c221cc186 | 322 | if(enemyHits == 17){ |
bdonohue | 0:912c221cc186 | 323 | state = 5; |
bdonohue | 0:912c221cc186 | 324 | winOrLose = 0; |
bdonohue | 0:912c221cc186 | 325 | //SEND WIN CONDITION |
bdonohue | 0:912c221cc186 | 326 | pc.putc('0'); |
bdonohue | 0:912c221cc186 | 327 | pc.putc('0'); |
bdonohue | 0:912c221cc186 | 328 | pc.putc('0'); |
bdonohue | 0:912c221cc186 | 329 | pc.putc('1'); |
bdonohue | 0:912c221cc186 | 330 | } |
bdonohue | 0:912c221cc186 | 331 | } |
bdonohue | 0:912c221cc186 | 332 | wait_ms(3000); |
bdonohue | 0:912c221cc186 | 333 | } |
bdonohue | 0:912c221cc186 | 334 | myled[2] = 0; |
bdonohue | 0:912c221cc186 | 335 | addAttack(); |
bdonohue | 0:912c221cc186 | 336 | player.PrintEnemyBoard(); |
bdonohue | 0:912c221cc186 | 337 | } |
bdonohue | 0:912c221cc186 | 338 | |
bdonohue | 0:912c221cc186 | 339 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 340 | |
bdonohue | 0:912c221cc186 | 341 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 342 | //END STATE |
bdonohue | 0:912c221cc186 | 343 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 344 | else if(state == 5){ |
bdonohue | 0:912c221cc186 | 345 | //GAME OVER |
bdonohue | 0:912c221cc186 | 346 | myled[0] = 1; |
bdonohue | 0:912c221cc186 | 347 | myled[1] = 1; |
bdonohue | 0:912c221cc186 | 348 | myled[2] = 1; |
bdonohue | 0:912c221cc186 | 349 | myled[3] = 1; |
bdonohue | 0:912c221cc186 | 350 | uLCD.cls(); |
bdonohue | 0:912c221cc186 | 351 | if(winOrLose){ |
bdonohue | 0:912c221cc186 | 352 | addWin(); |
bdonohue | 0:912c221cc186 | 353 | } else { |
bdonohue | 0:912c221cc186 | 354 | addLose(); |
bdonohue | 0:912c221cc186 | 355 | } |
bdonohue | 0:912c221cc186 | 356 | } |
bdonohue | 0:912c221cc186 | 357 | ///////////////////////////////////////////////// |
bdonohue | 0:912c221cc186 | 358 | } |
bdonohue | 0:912c221cc186 | 359 | } |