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
00001 //Headers 00002 #include "globals.h" 00003 #include "Player.h" 00004 #include "PinDetect.h" 00005 #include "letters.h" 00006 #include "bluetoothFunc.h" 00007 00008 //Local functions 00009 void buttonHit (void); 00010 void bluetoothResponse(); 00011 void moveCursor(); 00012 void callback(); 00013 00014 BusOut myled(LED1,LED2,LED3,LED4); 00015 00016 //I/O 00017 Serial blue(p28,p27); 00018 uLCD_4DGL uLCD(p9,p10,p11); 00019 PinDetect buttonLeft(p15); 00020 PinDetect buttonUp(p14); 00021 PinDetect buttonRight(p16); 00022 PinDetect buttonDown(p13); 00023 AnalogIn potentiometer(p20); 00024 Serial pc(USBTX, USBRX); 00025 00026 //Class initialization 00027 Player player; 00028 Mutex serialLock; 00029 00030 //VARIABLES 00031 int state; 00032 int fire, dir; 00033 int winOrLose; 00034 int enemyHits; 00035 00036 //bluetooth variables 00037 int num; 00038 int upOrDown; 00039 int updateMove; 00040 00041 //move cursor variables 00042 int x, y, prevX, prevY; 00043 00044 //ship placement variables 00045 int newX, newY; 00046 int ships[5]; 00047 int shipPointer; 00048 int shipLength; 00049 int prevXship[5]; 00050 int prevYship[5]; 00051 int prevShipColor[5]; 00052 int prevShipLength; 00053 int layout; 00054 00055 //server variables 00056 char cmd[4]; 00057 int clearBackground; 00058 int fireX, fireY; 00059 int nextHit; 00060 00061 int main() { 00062 //Variable setups 00063 state = 1; 00064 fire = dir = 0; 00065 enemyHits = 0; 00066 x = y = prevX = prevY = 0; 00067 num = upOrDown = updateMove = 0; 00068 ships[0] = 5; 00069 ships[1] = 4; 00070 ships[2] = 3; 00071 ships[3] = 3; 00072 ships[4] = 2; 00073 newX = newY = shipPointer = 0; 00074 shipLength = ships[shipPointer]; 00075 for(int i = 0; i < 5; i++){ 00076 prevXship[i] = 0; 00077 prevYship[i] = 0; 00078 prevShipColor[i] = 0; 00079 } 00080 prevShipLength = 0; 00081 layout = -1; 00082 clearBackground = 0; 00083 00084 nextHit = 2; 00085 //Screen setup 00086 uLCD.set_font_size(8,8); 00087 uLCD.color(0xFFFFFF); 00088 uLCD.background_color(0x0000FF); 00089 uLCD.cls(); 00090 addText(); 00091 player.PrintBoard(); 00092 while(pc.readable()) { 00093 pc.getc(); 00094 } 00095 //Main loop 00096 while(1){ 00097 00098 ///////////////////////////////////////////////// 00099 //bluetooth function 00100 ///////////////////////////////////////////////// 00101 if(bluetoothFunc(num,upOrDown)){ 00102 if(num >= 1 && num <= 4 && upOrDown){ 00103 layout = num; 00104 if(num == 1){ 00105 fire = 1; 00106 } else if(num == 2){ 00107 updateMove = 1; 00108 if(dir){ 00109 dir = 0; 00110 } else { 00111 dir = 1; 00112 } 00113 } 00114 } else if (num >= 5 && num <= 8 && upOrDown){ 00115 updateMove = 1; 00116 if(num == 7){ 00117 x = prevX - 1; 00118 if(x < 0){ 00119 x = 0; 00120 } 00121 } else if(num == 8){ 00122 x = prevX + 1; 00123 if(x > 9){ 00124 x = 9; 00125 } 00126 } else if(num == 5){ 00127 y = prevY - 1; 00128 if(y < 0){ 00129 y = 0; 00130 } 00131 } else if(num == 6){ 00132 y = prevY + 1; 00133 if(y > 9){ 00134 y = 9; 00135 } 00136 } 00137 uLCD.rectangle(26+prevX*10,26+prevY*10,36+prevX*10,36+prevY*10,WHITE); 00138 uLCD.rectangle(26+x*10,26+y*10,36+x*10,36+y*10,RED); 00139 prevX = x; 00140 prevY = y; 00141 } 00142 } 00143 00144 ///////////////////////////////////////////////// 00145 //STATE MACHINE 00146 ///////////////////////////////////////////////// 00147 00148 ///////////////////////////////////////////////// 00149 //PLACE SHIPS 00150 ///////////////////////////////////////////////// 00151 if(state == 1){ 00152 /* 00153 if(player.presetShips()){ 00154 state = 2; 00155 } 00156 */ 00157 //Clear the previous ships 00158 if(updateMove && clearBackground){ 00159 clearBackground = 0; 00160 for(int i = 0; i < shipLength; i++){ 00161 newX = prevXship[i]; 00162 newY = prevYship[i]; 00163 if(prevShipColor[i]){ 00164 if(newX < 10 && newY < 10){ 00165 uLCD.filled_circle(31+newX*10,31+newY*10,4,0xFFFFFF); 00166 } 00167 } else { 00168 if(newX < 10 && newY < 10){ 00169 uLCD.filled_circle(31+newX*10,31+newY*10,4,0x0000FF); 00170 } 00171 } 00172 } 00173 } 00174 00175 if(updateMove){ 00176 updateMove = 0; 00177 clearBackground = 1; 00178 //Print the ship 00179 if(dir){ 00180 for(int i = x; i < x + shipLength; i++){ 00181 if(i < 10){ 00182 uLCD.filled_circle(31+i*10,31+y*10,4,0x00FF00); 00183 } 00184 } 00185 for(int i = 0; i < shipLength; i++){ 00186 prevXship[i] = x + i; 00187 prevYship[i] = y; 00188 prevShipColor[i] = player.getColor(x+i,y); 00189 } 00190 } else { 00191 for(int i = y; i < y + shipLength; i++){ 00192 if(i < 10){ 00193 uLCD.filled_circle(31+x*10,31+i*10,4,0x00FF00); 00194 } 00195 } 00196 for(int i = 0; i < shipLength; i++){ 00197 prevXship[i] = x; 00198 prevYship[i] = y + i; 00199 prevShipColor[i] = player.getColor(x,y+i); 00200 } 00201 } 00202 } 00203 00204 //Player presses the fire button 00205 if(fire){ 00206 fire = 0; 00207 bool canPlace = true; 00208 //Check if the boat can be placed 00209 for(int i = 0; i < shipLength; i++){ 00210 if(player.getColor(prevXship[i],prevYship[i]) != 0){ 00211 canPlace = false; 00212 break; 00213 } 00214 if(prevXship[i] > 10 || prevYship[i] > 10){ 00215 canPlace = false; 00216 break; 00217 } 00218 } 00219 //Place the boat 00220 if(canPlace){ 00221 for(int i = 0; i < shipLength; i++){ 00222 player.PlaceShips(prevXship[i],prevYship[i]); 00223 } 00224 shipPointer++; 00225 clearBackground = 0; 00226 shipLength = ships[shipPointer]; 00227 player.PrintBoard(); 00228 } 00229 } 00230 00231 //Check if all the boats were placed 00232 if(shipPointer > 4){ 00233 state = 2; 00234 } 00235 } 00236 ///////////////////////////////////////////////// 00237 00238 ///////////////////////////////////////////////// 00239 //DETERMINE WHICH PLAYER GOES FIRST 00240 ///////////////////////////////////////////////// 00241 else if(state == 2){ 00242 myled[0] = 1; 00243 myled[1] = 1; 00244 myled[2] = 1; 00245 myled[3] = 1; 00246 00247 fire = 0; 00248 00249 for (int i = 0; i < 4; i++) { 00250 cmd[i] = pc.getc(); 00251 } 00252 00253 if(cmd[0] == 'a'){ 00254 player.PrintEnemyBoard(); 00255 addAttack(); 00256 state = 3; 00257 } else { 00258 player.PrintBoard(); 00259 addDefense(); 00260 state = 4; 00261 } 00262 myled[0] = 0; 00263 myled[1] = 0; 00264 myled[2] = 0; 00265 myled[3] = 0; 00266 } 00267 ///////////////////////////////////////////////// 00268 00269 ///////////////////////////////////////////////// 00270 //ATTACK 00271 ///////////////////////////////////////////////// 00272 else if(state == 3){ 00273 myled[1] = 1; 00274 00275 if(fire){ 00276 fire = 0; 00277 if(player.getEnemyColor(x,y) == 0){ 00278 fireX = x; 00279 fireY = y; 00280 pc.putc(x + '0'); 00281 pc.putc(y + '0'); 00282 pc.putc(nextHit + '0'); //hit or miss 00283 pc.putc('0'); //endgame 00284 00285 player.PrintBoard(); 00286 addDefense(); 00287 state = 4; 00288 myled[1] = 0; 00289 } 00290 00291 } 00292 } 00293 ///////////////////////////////////////////////// 00294 00295 ///////////////////////////////////////////////// 00296 //WAIT 00297 ///////////////////////////////////////////////// 00298 else if(state == 4){ 00299 myled[2] = 1; 00300 wait(1); 00301 cmd[0] = pc.getc(); 00302 cmd[1] = pc.getc(); 00303 cmd[2] = pc.getc(); 00304 cmd[3] = pc.getc(); 00305 00306 int enemyX = cmd[0] - '0'; 00307 int enemyY = cmd[1] - '0'; 00308 int yourHit = cmd[2] - '0'; 00309 int win = cmd[3] - '0'; 00310 00311 state = 3; 00312 if(win){ 00313 state = 5; 00314 winOrLose = 1; 00315 } else { 00316 //UPDATE ENEMY BOARD 00317 player.Shot(fireX,fireY,yourHit); 00318 //UPDATE YOUR BOARD 00319 nextHit = player.EnemyShot(enemyX,enemyY); 00320 if(nextHit == 1){ 00321 enemyHits++; 00322 if(enemyHits == 17){ 00323 state = 5; 00324 winOrLose = 0; 00325 //SEND WIN CONDITION 00326 pc.putc('0'); 00327 pc.putc('0'); 00328 pc.putc('0'); 00329 pc.putc('1'); 00330 } 00331 } 00332 wait_ms(3000); 00333 } 00334 myled[2] = 0; 00335 addAttack(); 00336 player.PrintEnemyBoard(); 00337 } 00338 00339 ///////////////////////////////////////////////// 00340 00341 ///////////////////////////////////////////////// 00342 //END STATE 00343 ///////////////////////////////////////////////// 00344 else if(state == 5){ 00345 //GAME OVER 00346 myled[0] = 1; 00347 myled[1] = 1; 00348 myled[2] = 1; 00349 myled[3] = 1; 00350 uLCD.cls(); 00351 if(winOrLose){ 00352 addWin(); 00353 } else { 00354 addLose(); 00355 } 00356 } 00357 ///////////////////////////////////////////////// 00358 } 00359 }
Generated on Thu Jul 14 2022 20:08:03 by
1.7.2