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.
Dependencies: SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem
main.cpp
00001 /********************************** 00002 * Author: Clifton Thomas 00003 * Date: 3/28/13 00004 * Institution: Georgia Tech 00005 * 00006 * Title: MAIN 00007 * Class: ECE2035 00008 * Assignment: Project 2 00009 * Student: Yingyan Samantha Wang 00010 **********************************/ 00011 00012 //includes 00013 #include <string.h> 00014 #include <cstdarg> 00015 #include "mbed.h" 00016 #include "rtos.h" 00017 #include "TextLCD.h" 00018 #include "SDFileSystem.h" 00019 #include "CommModule.h" 00020 #include "wave_player.h" 00021 #include "FATFileSystem.h" 00022 00023 00024 //defines 00025 #define BAUDRATE 9600 00026 #define BUFFSIZE 100 00027 #define BOMB_WAVFILE "/sd/wavfiles/bomb.wav" 00028 #define ACK "status-ack" 00029 #define STATUS_KEY "status" 00030 #define HIT_KEY "hit" 00031 #define MISS_KEY "miss" 00032 #define SANK_KEY "sank" 00033 00034 00035 //function prototypes 00036 void lowerCase(char *src); 00037 void startGame(void); 00038 void waitForAck(void); 00039 int bomb(int rowIndex, int colIndex); 00040 int** init(int** mark); 00041 int* find(int** mark); 00042 int col_ship(int row, int col, int **mark); 00043 int row_ship(int row, int col, int **mark); 00044 //void playBombSound(void); 00045 void print(const char *format, ...); 00046 00047 00048 //declare functions (assembly subroutines) 00049 extern "C" void setup_sequence(void); 00050 extern "C" void seg_driver_initialize(void); 00051 extern "C" void seg_driver(int value); 00052 00053 00054 //initialize hardware 00055 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs 00056 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7 00057 AnalogOut DACout(p18); 00058 wave_player waver(&DACout); 00059 00060 00061 //communication device 00062 commSerial serDevice(USBTX, USBRX, BAUDRATE); //tx, rx 00063 00064 int times; 00065 00066 //main 00067 int main() { 00068 //initialize and clear 7-Segment Display (assembly subroutine) 00069 setup_sequence(); 00070 seg_driver_initialize(); 00071 int seg_times; 00072 for (seg_times=0;seg_times<10;seg_times++){ 00073 seg_driver(seg_times); 00074 lcd.cls(); 00075 lcd.printf("%d",seg_times); 00076 wait(0.5); 00077 } 00078 //check for wav file 00079 lcd.cls(); 00080 lcd.printf("Locating WAV file..."); 00081 FILE *test_file; 00082 /*while(1) { 00083 test_file=fopen(BOMB_WAVFILE,"r"); 00084 if(test_file != NULL) {break;} 00085 wait(0.5); 00086 } 00087 fclose(test_file);*/ 00088 00089 //notification 00090 lcd.cls(); 00091 lcd.printf("Battleship"); 00092 wait(1); 00093 00094 //loop 00095 while(1) { 00096 //synchronize front end display 00097 startGame(); 00098 seg_driver(0); 00099 /**** BEGIN - your code goes here ****/ 00100 //temp variables 00101 int i,j,k,end_row,end_col,ret,tempret; 00102 int **mark; 00103 int *ndx; 00104 //allocate a 10 by 10 array to keep track of tiles that are 00105 //either checked,unchecked or no need to check 00106 mark = (int **)malloc(10 * sizeof(int *)); 00107 if(mark == NULL){ 00108 fprintf(stderr, "out of memory\n"); 00109 return; 00110 } 00111 for(i = 0; i < 10; i++){ 00112 mark[i] = (int *)malloc(10 * sizeof(int)); 00113 if(mark[i] == NULL){ 00114 fprintf(stderr, "out of memory\n"); 00115 return; 00116 } 00117 } 00118 mark=init(mark); 00119 i=0; 00120 j=0; 00121 k=0; 00122 times=0; 00123 //debug statements 00124 print("testing"); 00125 print("string: %s\nstring2: %s", "hello", "world"); 00126 print("int: %d", 8); 00127 00128 while (k!=5){ 00129 ret=bomb(i,j); 00130 //mark tiles already checked 00131 mark[i][j]=1; 00132 //tiles no need to check 00133 if (i+1<10){ 00134 mark[i+1][j]=2; 00135 //mark[i+2][j]=2; 00136 } 00137 if (j+1<10){ 00138 mark[i][j+1]=2; 00139 //mark[i][j+2]=2; 00140 } 00141 //when a boat is hit 00142 if (ret==1){ 00143 //row ship 00144 mark[i][j+1]=1; 00145 tempret=bomb(i,j+1); 00146 if (tempret==1){ 00147 end_col=row_ship(i,j+1,mark); 00148 if(i<9){ 00149 mark[i+1][j]=1; 00150 mark[i+1][j+1]=1; 00151 } 00152 if (end_col+1<10){ 00153 mark[i][end_col+1]=1; 00154 } 00155 } 00156 //vertical ship 00157 else if (tempret!=2){ 00158 if (bomb(i,j-1)==0||j==0){ 00159 end_row=col_ship(i,j,mark); 00160 if(end_row+1<10){ 00161 mark[end_row+1][j]=1; 00162 } 00163 } 00164 } 00165 k++; 00166 seg_driver(k); 00167 00168 } 00169 ndx=find(mark); 00170 i=ndx[0]; 00171 j=ndx[1]; 00172 } 00173 00174 free(mark); 00175 00176 //have fun... 00177 00178 /**** END - your code stops here ****/ 00179 } 00180 } 00181 00182 //init mask; 00183 int** init(int **mark){ 00184 int i,j; 00185 for (i=0;i<10;i++){ 00186 for (j=0;j<10;j++){ 00187 mark[i][j]=0; 00188 } 00189 } 00190 return mark; 00191 } 00192 00193 //find the first unchecked tile 00194 int* find(int **mark){ 00195 int i,j,a; 00196 int *ndx; 00197 ndx = (int *)malloc(2 * sizeof(int )); 00198 a=0; 00199 for (i=0;i<10;i++){ 00200 for (j=0;j<10;j++){ 00201 if (mark[i][j]==0){ 00202 a=1; 00203 break; 00204 } 00205 } 00206 if (a==1){ 00207 break; 00208 } 00209 } 00210 ndx[0]=i; 00211 ndx[1]=j; 00212 return ndx; 00213 } 00214 00215 //bomb the entire vertical ship 00216 int col_ship(int row,int col, int **mark){ 00217 int backtrack=0; 00218 int tempret=1; 00219 int temprow; 00220 temprow=row; 00221 while (tempret!=2){ 00222 if(tempret==1 && row<9 && backtrack==0){ 00223 row++; 00224 } 00225 else if ((tempret==0||row==9) && backtrack==0){ 00226 row=temprow-1; 00227 backtrack=1; 00228 } 00229 else if (backtrack==1){ 00230 row--; 00231 } 00232 tempret=bomb(row,col); 00233 mark[row][col]=1; 00234 if (col+1<10){ 00235 mark[row][col+1]=1; 00236 } 00237 if (col-1>=0){ 00238 mark[row][col-1]=1; 00239 } 00240 //lcd.cls(); 00241 //lcd.printf("Ret: %d", tempret); 00242 } 00243 return row; 00244 } 00245 00246 //bomb the entire horizontal ship 00247 int row_ship(int row,int col,int **mark){ 00248 int backtrack=0; 00249 int tempret=1; 00250 int tempcol; 00251 tempcol=col; 00252 while (tempret!=2){ 00253 //continue to bomb the horizontal ship 00254 if (tempret==1 && col<9 && backtrack==0){ 00255 col++; 00256 } 00257 //bomb the first tile of the ship 00258 else if ((tempret==0||col==9) && backtrack==0){ 00259 col=tempcol-2; 00260 backtrack=1; 00261 } 00262 else if (backtrack==1){ 00263 col--; 00264 } 00265 tempret=bomb(row,col); 00266 mark[row][col]=1; 00267 //the tiles below the ship are no need to check 00268 if(row+1<10){ 00269 mark[row+1][col]=2; 00270 } 00271 //lcd.cls(); 00272 //lcd.printf("Ret: %d", tempret); 00273 } 00274 return col; 00275 } 00276 00277 00278 //fcn to get acknowledgement from serial peripheral 00279 void waitForAck(void) { 00280 //get acknowlegement 00281 char buffer[BUFFSIZE]; 00282 while(1) { 00283 serDevice.receiveData(buffer); 00284 lowerCase(buffer); 00285 if(strcmp(ACK, buffer) == 0) { 00286 break; 00287 } 00288 memset(&buffer[0],0,strlen(buffer)); 00289 } 00290 return; 00291 } 00292 00293 //fcn to initialize the frontend display 00294 void startGame(void) { 00295 //temp variables 00296 char buffer[BUFFSIZE]; 00297 //construct message 00298 sprintf(buffer, "start"); 00299 00300 //send message 00301 serDevice.sendData(buffer); 00302 00303 //wait for acknowledgement 00304 waitForAck(); 00305 } 00306 00307 //fcn to bomb a given coordinate 00308 int bomb(int rowIndex, int colIndex) { 00309 //temp variables 00310 char buffer[BUFFSIZE]; 00311 //check if coordinate is valid 00312 times++; 00313 lcd.cls(); 00314 lcd.printf("Bomb Used: %d", times); 00315 if((rowIndex >= 0) && (rowIndex <= 9) && (colIndex >= 0) && (colIndex <= 9)) { 00316 //construct message 00317 sprintf(buffer, "bomb-%d-%d", rowIndex, colIndex); 00318 //send message 00319 serDevice.sendData(buffer); 00320 00321 //wait for status response 00322 while(1) { 00323 //temp variables 00324 memset(&buffer[0],0,strlen(buffer)); 00325 char *ptr = NULL; 00326 00327 //receive status response 00328 serDevice.receiveData(buffer); 00329 00330 //parse string to extract status key 00331 ptr = strstr(buffer, STATUS_KEY); 00332 if(ptr == NULL) {continue;} 00333 00334 //if status key found, parse string to extract status message 00335 ptr+=(strlen(STATUS_KEY)+1); 00336 if(strcmp(ptr, HIT_KEY) == 0) { 00337 //playBombSound(); 00338 return(1); 00339 } 00340 else if(strcmp(ptr, MISS_KEY) == 0) { 00341 return(0); 00342 } 00343 else if(strcmp(ptr, SANK_KEY) == 0) { 00344 //playBombSound(); 00345 return(2); 00346 } 00347 else { 00348 return(-1); 00349 } 00350 } 00351 } 00352 return(-1); 00353 } 00354 00355 //fcn to play bomb noise 00356 /*void playBombSound(void) { 00357 //open wav file 00358 FILE *wave_file; 00359 wave_file=fopen(BOMB_WAVFILE,"r"); 00360 00361 //play wav file 00362 waver.play(wave_file); 00363 00364 //close wav file 00365 fclose(wave_file); 00366 }*/ 00367 00368 //fcn to print to console 00369 void print(const char *format, ...) { 00370 //temp variables 00371 char buffer[BUFFSIZE]; 00372 char temp[BUFFSIZE-6]; 00373 00374 //construct message part 1 00375 sprintf(buffer, "print-"); 00376 00377 //construct message part 2 00378 va_list arguments; 00379 va_start(arguments, format); 00380 vsnprintf(temp, BUFFSIZE-7, format, arguments); 00381 va_end(arguments); 00382 00383 //concatenate parts 00384 strcat(buffer, temp); 00385 00386 //send message 00387 serDevice.sendData(buffer); 00388 00389 //wait for acknowledgement 00390 waitForAck(); 00391 } 00392 00393 //fcn to convert string to lowercase 00394 void lowerCase(char *src) { 00395 int i=0;; 00396 while(src[i] != '\0') { 00397 if((src[i] > 64) && (src[i] < 91)) { 00398 src[i]+=32; 00399 } 00400 i++; 00401 } 00402 return; 00403 }
Generated on Sun Jul 17 2022 15:18:53 by
1.7.2