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.
Fork of project by
main.cpp
00001 /******************************************************************************/ 00002 /* This Nicleo is state 0 */ 00003 /******************************************************************************/ 00004 #include "mbed.h" 00005 #include "ColorMbed.h" 00006 00007 // Initail I/O. 00008 AnalogIn VRx(A0); 00009 AnalogIn VRy(A1); 00010 DigitalIn Button(PA_4); 00011 Serial bt(PA_15, PB_7); 00012 ColorMbed dotmatrix; 00013 00014 // Initial function. 00015 char posit2char(int posit); 00016 char check_joys(int jx,int jy); 00017 int char2posit(char cha); 00018 int IamWin(); 00019 int protect_ship(int head_position,int ship_type); 00020 int TellUrEnemy_uWin(); 00021 int check_joys_int(int jx,int jy); 00022 int check_correctPosition(int X,int Y); 00023 int get_ship_platform(int input_position,int ship_type); 00024 void atk_enemy(); 00025 void enemy_atk(); 00026 void check_ready(); 00027 void mark_ship_destroyed(); 00028 void Select_Position(char M); 00029 void get_atk_platform(int atkpos); 00030 void check_end(); 00031 //inessential 00032 void print_ship_destroyed(); 00033 void print_enemy_platform(); 00034 void print_ship_platform(); 00035 void print_atk_platform(); 00036 00037 // Setting 00038 int ifUrJirat = 0; 00039 // Initial variable. 00040 int check_ship_underATK = 0; 00041 int select_Position = 0; 00042 int ATKposition = 0; 00043 int enemy_ready = 0; 00044 int check_ship1 = 0; 00045 int check_ship2 = 0; 00046 int iShipType = 0; 00047 int iWaitATK = 0; 00048 int check_win = 0; 00049 int state = 0; 00050 int ship_SelectType[8] = {4,3,2,2,1,1,1,1}; 00051 int enemy_platform[8][8] = {{0}}; 00052 int ship_platform[8][8] = {{0}}; //______________________________________________This is ship area. 00053 int atk_platform[8][8] = {{0}}; 00054 int ship_underATK[15] = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9}; 00055 int ship_destroyed[8] = {0}; 00056 int ship4[4] = {0}; 00057 int ship3[3] = {0}; 00058 int ship2[2][2] = {{0}}; 00059 int ship1[4] = {0}; 00060 00061 //fuction nasrun 00062 int return_position[8][8] ; 00063 int for_hex = 0; 00064 int ch_post[8][8] = {{1,1,1,1,1,1,1,1}, 00065 {1,1,1,1,1,1,1,1}, 00066 {1,1,1,1,1,1,1,1}, 00067 {1,1,1,1,1,1,1,1}, 00068 {1,1,1,1,1,1,1,1}, 00069 {1,1,1,1,1,1,1,1}, 00070 {1,1,1,1,1,1,1,1}, 00071 {1,1,1,1,1,1,1,1}}; 00072 int return_hex[8]; 00073 00074 int display_battle[8][8][3] = {0}; 00075 00076 int _temppic[8] = {0x01,0x01,0x01,0x01,0,0,0,0}; 00077 00078 int temppic2[8] = {0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 00079 int temppic3[3][8] = {{0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00},{0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00},{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}; 00080 int temppic4[8] = {0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 00081 int forshift[8] = {0}; 00082 int pic[8] = {0}; 00083 00084 int i,j; 00085 int check_boat3=0; 00086 int check_hex=0; 00087 00088 00089 void hex_to_array(int* ar){ 00090 int i; 00091 int j; 00092 int temp; 00093 for(i = 0;i<8;i++){ 00094 temp = ar[i]; 00095 for(j = 0;j<8;j++){ 00096 if(0x80 & temp){ 00097 return_position[i][j] = 1; 00098 } 00099 else{ 00100 return_position[i][j] = 0; 00101 } 00102 temp = temp << 1; 00103 } 00104 } 00105 } 00106 00107 void ch_same_position(int ar1[8][8],int ar2[8][8]){ 00108 int i; 00109 int j; 00110 for(i = 0;i<8;i++){ 00111 for(j = 0;j<8;j++){ 00112 if(ar1[i][j] == 1 && ar2[i][j] == 1){ 00113 ch_post[i][j] = 1; 00114 } 00115 else{ 00116 ch_post[i][j] = 0; 00117 } 00118 } 00119 } 00120 00121 } 00122 00123 void discard_same_position(int ar1[8][8],int ar2[8][8]){ 00124 int i; 00125 int j; 00126 for(i = 0;i<8;i++){ 00127 for(j = 0;j<8;j++){ 00128 if(ar1[i][j] == ch_post[i][j] && ar2[i][j] == ch_post[i][j] ){ 00129 ar1[i][j] = 0; 00130 ar2[i][j] = 0; 00131 } 00132 } 00133 } 00134 } 00135 00136 00137 int _power(int number , int power){ 00138 int i = 0; 00139 int number1 = number; 00140 if(power == 0){return 1;} 00141 for(i = 1 ; i <= power ; i++ ){ 00142 if(power == 1){return number;} 00143 if(i == 1){number = number * 1;} 00144 else{number1 = number1 * number;} 00145 } 00146 return number1; 00147 } 00148 00149 00150 void array_to_hex(int ar1[8][8]){ 00151 int prompt; 00152 int i; 00153 int j; 00154 for(i = 0;i<8;i++){ 00155 for(j = 0;j<8;j++){ 00156 prompt = _power(2,7-j); 00157 if(ar1[i][j] == 1){ 00158 for_hex = for_hex + prompt; 00159 } 00160 else{ 00161 for_hex = for_hex + 0; 00162 } 00163 } 00164 return_hex[i] = for_hex; 00165 prompt = 0; 00166 for_hex = 0; 00167 } 00168 } 00169 00170 void place_ship3(char dir){ 00171 int post1[8][8] = {0}; 00172 int post2[8][8] = {0}; 00173 int hex_post1[8] = {0}; 00174 int hex_post2[8] = {0}; 00175 int hex_post_cut[8] = {0}; 00176 int get_same_position[8][8]; 00177 int wow=0; 00178 00179 if(dir == 'w'&& temppic2[0] == 0){ 00180 printf("w\n"); 00181 for(j=0;j<8;j++){ 00182 if(j == 7){ 00183 forshift[7] = 0; 00184 } 00185 else{ 00186 forshift[j] = temppic2[j+1]; 00187 } 00188 } 00189 for(j=0;j<8;j++){ 00190 temppic2[j] = forshift[j]; 00191 _temppic[j] = temppic2[j]; 00192 } 00193 // begin to compare and combine two frame 00194 hex_to_array(_temppic); 00195 for(i = 0;i<8;i++){ 00196 for(j = 0;j<8;j++){ 00197 post1[i][j] = return_position[i][j]; 00198 } 00199 } 00200 hex_to_array(pic); 00201 for(i = 0;i<8;i++){ 00202 for(j = 0;j<8;j++){ 00203 post2[i][j] = return_position[i][j]; 00204 } 00205 } 00206 00207 // begin to check boypeacemaker 00208 ch_same_position(post1,post2); 00209 for(i=0;i<8;i++){ 00210 for(j=0;j<8;j++){ 00211 get_same_position[i][j] = ch_post[i][j]; 00212 if(get_same_position[i][j] == 1){ 00213 wow = 1; 00214 } 00215 } 00216 } 00217 if(wow == 0){ 00218 for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} 00219 } 00220 else{ 00221 discard_same_position(post1,post2); 00222 array_to_hex(post1); 00223 for(i = 0;i<8;i++){ 00224 hex_post1[i] = return_hex[i]; 00225 } 00226 array_to_hex(post2); 00227 for(i = 0;i<8;i++){ 00228 hex_post2[i] = return_hex[i]; 00229 } 00230 array_to_hex(get_same_position); 00231 for(i = 0;i<8;i++){ 00232 hex_post_cut[i] = return_hex[i]; 00233 } 00234 for(i = 0;i<8;i++){ 00235 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00236 } 00237 } 00238 } 00239 if(dir == 'a'){ 00240 printf("a\n"); 00241 00242 for(i=0;i<8;i++){ 00243 if(temppic2[i] == 0xe0){ 00244 check_boat3 = 1; 00245 } 00246 } 00247 if(check_boat3 == 0){ 00248 for(j=0;j<8;j++){ 00249 if(temppic2[j] != 0){ 00250 temppic2[j] = temppic2[j]*2; 00251 } 00252 else{ 00253 temppic2[j] = 0; 00254 } 00255 _temppic[j] = temppic2[j]; 00256 } 00257 // begin to compare and combine two frame 00258 hex_to_array(_temppic); 00259 for(i = 0;i<8;i++){ 00260 for(j = 0;j<8;j++){ 00261 post1[i][j] = return_position[i][j]; 00262 } 00263 } 00264 hex_to_array(pic); 00265 for(i = 0;i<8;i++){ 00266 for(j = 0;j<8;j++){ 00267 post2[i][j] = return_position[i][j]; 00268 } 00269 } 00270 00271 // begin to check boypeacemaker 00272 ch_same_position(post1,post2); 00273 for(i=0;i<8;i++){ 00274 for(j=0;j<8;j++){ 00275 get_same_position[i][j] = ch_post[i][j]; 00276 if(get_same_position[i][j] == 1){ 00277 wow = 1; 00278 } 00279 } 00280 } 00281 if(wow == 0){ 00282 for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} 00283 } 00284 else{ 00285 discard_same_position(post1,post2); 00286 array_to_hex(post1); 00287 for(i = 0;i<8;i++){ 00288 hex_post1[i] = return_hex[i]; 00289 } 00290 array_to_hex(post2); 00291 for(i = 0;i<8;i++){ 00292 hex_post2[i] = return_hex[i]; 00293 } 00294 array_to_hex(get_same_position); 00295 for(i = 0;i<8;i++){ 00296 hex_post_cut[i] = return_hex[i]; 00297 } 00298 for(i = 0;i<8;i++){ 00299 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00300 } 00301 } 00302 } 00303 check_boat3 = 0; 00304 } 00305 if(dir == 'd'){ 00306 printf("d\n"); 00307 for(i=0;i<8;i++){ 00308 if(temppic2[i] == 0x07){ 00309 check_boat3 = 1; 00310 } 00311 } 00312 if(check_boat3 == 0){ 00313 for(j=0;j<8;j++){ 00314 if(temppic2[j] != 0){ 00315 temppic2[j] = temppic2[j]/2; 00316 } 00317 else{ 00318 temppic2[j] = 0; 00319 } 00320 _temppic[j] = temppic2[j]; 00321 } 00322 // begin to compare and combine two frame 00323 hex_to_array(_temppic); 00324 for(i = 0;i<8;i++){ 00325 for(j = 0;j<8;j++){ 00326 post1[i][j] = return_position[i][j]; 00327 } 00328 } 00329 hex_to_array(pic); 00330 for(i = 0;i<8;i++){ 00331 for(j = 0;j<8;j++){ 00332 post2[i][j] = return_position[i][j]; 00333 } 00334 } 00335 00336 // begin to check boypeacemaker 00337 ch_same_position(post1,post2); 00338 for(i=0;i<8;i++){ 00339 for(j=0;j<8;j++){ 00340 get_same_position[i][j] = ch_post[i][j]; 00341 if(get_same_position[i][j] == 1){ 00342 wow = 1; 00343 } 00344 } 00345 } 00346 if(wow == 0){ 00347 for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} 00348 } 00349 else{ 00350 discard_same_position(post1,post2); 00351 array_to_hex(post1); 00352 for(i = 0;i<8;i++){ 00353 hex_post1[i] = return_hex[i]; 00354 } 00355 array_to_hex(post2); 00356 for(i = 0;i<8;i++){ 00357 hex_post2[i] = return_hex[i]; 00358 } 00359 array_to_hex(get_same_position); 00360 for(i = 0;i<8;i++){ 00361 hex_post_cut[i] = return_hex[i]; 00362 } 00363 for(i = 0;i<8;i++){ 00364 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00365 } 00366 } 00367 } 00368 check_boat3 = 0; 00369 } 00370 if(dir == 's' && temppic2[7] == 0){ 00371 printf("s\n"); 00372 for(j=0;j<8;j++){ 00373 if(j == 0){ 00374 forshift[0] = 0; 00375 } 00376 else{ 00377 forshift[j] = temppic2[j-1]; 00378 } 00379 } 00380 for(j=0;j<8;j++){ 00381 temppic2[j] = forshift[j]; 00382 _temppic[j] = temppic2[j]; 00383 } 00384 // begin to compare and combine two frame 00385 hex_to_array(_temppic); 00386 for(i = 0;i<8;i++){ 00387 for(j = 0;j<8;j++){ 00388 post1[i][j] = return_position[i][j]; 00389 } 00390 } 00391 hex_to_array(pic); 00392 for(i = 0;i<8;i++){ 00393 for(j = 0;j<8;j++){ 00394 post2[i][j] = return_position[i][j]; 00395 } 00396 } 00397 00398 // begin to check boypeacemaker 00399 ch_same_position(post1,post2); 00400 for(i=0;i<8;i++){ 00401 for(j=0;j<8;j++){ 00402 get_same_position[i][j] = ch_post[i][j]; 00403 if(get_same_position[i][j] == 1){ 00404 wow = 1; 00405 } 00406 } 00407 } 00408 if(wow == 0){ 00409 for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} 00410 } 00411 else{ 00412 discard_same_position(post1,post2); 00413 array_to_hex(post1); 00414 for(i = 0;i<8;i++){ 00415 hex_post1[i] = return_hex[i]; 00416 } 00417 array_to_hex(post2); 00418 for(i = 0;i<8;i++){ 00419 hex_post2[i] = return_hex[i]; 00420 } 00421 array_to_hex(get_same_position); 00422 for(i = 0;i<8;i++){ 00423 hex_post_cut[i] = return_hex[i]; 00424 } 00425 for(i = 0;i<8;i++){ 00426 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00427 } 00428 } 00429 00430 } 00431 } 00432 // ******************************************************************** ship 2 channal ****************************************************************** 00433 void place_ship2_4(int ship,char dir){ 00434 00435 int post1[8][8] = {0}; 00436 int post2[8][8] = {0}; 00437 int hex_post1[8] = {0}; 00438 int hex_post2[8] = {0}; 00439 int hex_post_cut[8] = {0}; 00440 int get_same_position[8][8]; 00441 int wow=0; 00442 00443 if(dir == 'w'&& temppic3[ship][0] == 0){ 00444 printf("w\n"); 00445 for(j=0;j<8;j++){ 00446 if(j == 7){ 00447 forshift[7] = 0; 00448 } 00449 else{ 00450 forshift[j] = temppic3[ship][j+1]; 00451 } 00452 } 00453 for(j=0;j<8;j++){ 00454 temppic3[ship][j] = forshift[j]; 00455 _temppic[j] = temppic3[ship][j]; 00456 } 00457 // begin to compare and combine two frame 00458 hex_to_array(_temppic); 00459 for(i = 0;i<8;i++){ 00460 for(j = 0;j<8;j++){ 00461 post1[i][j] = return_position[i][j]; 00462 } 00463 } 00464 hex_to_array(pic); 00465 for(i = 0;i<8;i++){ 00466 for(j = 0;j<8;j++){ 00467 post2[i][j] = return_position[i][j]; 00468 } 00469 } 00470 00471 // begin to check boypeacemaker 00472 ch_same_position(post1,post2); 00473 for(i=0;i<8;i++){ 00474 for(j=0;j<8;j++){ 00475 get_same_position[i][j] = ch_post[i][j]; 00476 if(get_same_position[i][j] == 1){ 00477 wow = 1; 00478 } 00479 } 00480 } 00481 if(wow == 0){ 00482 for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} 00483 } 00484 else{ 00485 discard_same_position(post1,post2); 00486 array_to_hex(post1); 00487 for(i = 0;i<8;i++){ 00488 hex_post1[i] = return_hex[i]; 00489 } 00490 array_to_hex(post2); 00491 for(i = 0;i<8;i++){ 00492 hex_post2[i] = return_hex[i]; 00493 } 00494 array_to_hex(get_same_position); 00495 for(i = 0;i<8;i++){ 00496 hex_post_cut[i] = return_hex[i]; 00497 } 00498 for(i = 0;i<8;i++){ 00499 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00500 } 00501 } 00502 00503 } 00504 if(dir == 'a' && temppic3[ship][3] != 0x80 && temppic3[ship][4] != 0x80 ){ 00505 printf("a\n"); 00506 for(j=0;j<8;j++){ 00507 if(temppic3[j] != 0){ 00508 temppic3[ship][j] = temppic3[ship][j]*2; 00509 } 00510 else{ 00511 temppic3[ship][j] = 0; 00512 } 00513 _temppic[j] = temppic3[ship][j]; 00514 } 00515 // begin to compare and combine two frame 00516 hex_to_array(_temppic); 00517 for(i = 0;i<8;i++){ 00518 for(j = 0;j<8;j++){ 00519 post1[i][j] = return_position[i][j]; 00520 } 00521 } 00522 hex_to_array(pic); 00523 for(i = 0;i<8;i++){ 00524 for(j = 0;j<8;j++){ 00525 post2[i][j] = return_position[i][j]; 00526 } 00527 } 00528 00529 // begin to check boypeacemaker 00530 ch_same_position(post1,post2); 00531 for(i=0;i<8;i++){ 00532 for(j=0;j<8;j++){ 00533 get_same_position[i][j] = ch_post[i][j]; 00534 if(get_same_position[i][j] == 1){ 00535 wow = 1; 00536 } 00537 } 00538 } 00539 if(wow == 0){ 00540 for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} 00541 } 00542 else{ 00543 discard_same_position(post1,post2); 00544 array_to_hex(post1); 00545 for(i = 0;i<8;i++){ 00546 hex_post1[i] = return_hex[i]; 00547 } 00548 array_to_hex(post2); 00549 for(i = 0;i<8;i++){ 00550 hex_post2[i] = return_hex[i]; 00551 } 00552 array_to_hex(get_same_position); 00553 for(i = 0;i<8;i++){ 00554 hex_post_cut[i] = return_hex[i]; 00555 } 00556 for(i = 0;i<8;i++){ 00557 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00558 } 00559 } 00560 00561 } 00562 if(dir == 'd' && temppic3[ship][3] != 0x01 && temppic3[ship][4] != 0x01 ){ 00563 printf("d\n"); 00564 for(j=0;j<8;j++){ 00565 if(temppic3[ship][j] != 0){ 00566 temppic3[ship][j] = temppic3[ship][j]/2; 00567 } 00568 else{ 00569 temppic3[ship][j] = 0; 00570 } 00571 _temppic[j] = temppic3[ship][j]; 00572 } 00573 // begin to compare and combine two frame 00574 hex_to_array(_temppic); 00575 for(i = 0;i<8;i++){ 00576 for(j = 0;j<8;j++){ 00577 post1[i][j] = return_position[i][j]; 00578 } 00579 } 00580 hex_to_array(pic); 00581 for(i = 0;i<8;i++){ 00582 for(j = 0;j<8;j++){ 00583 post2[i][j] = return_position[i][j]; 00584 } 00585 } 00586 00587 // begin to check boypeacemaker 00588 ch_same_position(post1,post2); 00589 for(i=0;i<8;i++){ 00590 for(j=0;j<8;j++){ 00591 get_same_position[i][j] = ch_post[i][j]; 00592 if(get_same_position[i][j] == 1){ 00593 wow = 1; 00594 } 00595 } 00596 } 00597 if(wow == 0){ 00598 for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} 00599 } 00600 else{ 00601 discard_same_position(post1,post2); 00602 array_to_hex(post1); 00603 for(i = 0;i<8;i++){ 00604 hex_post1[i] = return_hex[i]; 00605 } 00606 array_to_hex(post2); 00607 for(i = 0;i<8;i++){ 00608 hex_post2[i] = return_hex[i]; 00609 } 00610 array_to_hex(get_same_position); 00611 for(i = 0;i<8;i++){ 00612 hex_post_cut[i] = return_hex[i]; 00613 } 00614 for(i = 0;i<8;i++){ 00615 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00616 } 00617 } 00618 00619 } 00620 if(dir == 's' && temppic3[ship][7] == 0){ 00621 printf("s\n"); 00622 for(j=0;j<8;j++){ 00623 if(j == 0){ 00624 forshift[0] = 0; 00625 } 00626 else{ 00627 forshift[j] = temppic3[ship][j-1]; 00628 } 00629 } 00630 for(j=0;j<8;j++){ 00631 temppic3[ship][j] = forshift[j]; 00632 _temppic[j] = temppic3[ship][j]; 00633 } 00634 // begin to compare and combine two frame 00635 hex_to_array(_temppic); 00636 for(i = 0;i<8;i++){ 00637 for(j = 0;j<8;j++){ 00638 post1[i][j] = return_position[i][j]; 00639 } 00640 } 00641 hex_to_array(pic); 00642 for(i = 0;i<8;i++){ 00643 for(j = 0;j<8;j++){ 00644 post2[i][j] = return_position[i][j]; 00645 } 00646 } 00647 00648 // begin to check boypeacemaker 00649 ch_same_position(post1,post2); 00650 for(i=0;i<8;i++){ 00651 for(j=0;j<8;j++){ 00652 get_same_position[i][j] = ch_post[i][j]; 00653 if(get_same_position[i][j] == 1){ 00654 wow = 1; 00655 } 00656 } 00657 } 00658 if(wow == 0){ 00659 for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} 00660 } 00661 else{ 00662 discard_same_position(post1,post2); 00663 array_to_hex(post1); 00664 for(i = 0;i<8;i++){ 00665 hex_post1[i] = return_hex[i]; 00666 } 00667 array_to_hex(post2); 00668 for(i = 0;i<8;i++){ 00669 hex_post2[i] = return_hex[i]; 00670 } 00671 array_to_hex(get_same_position); 00672 for(i = 0;i<8;i++){ 00673 hex_post_cut[i] = return_hex[i]; 00674 } 00675 for(i = 0;i<8;i++){ 00676 _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; 00677 } 00678 } 00679 00680 } 00681 } 00682 00683 00684 // function Test! 00685 00686 int main(){ //-------------------------------------------------------------------Main Function. 00687 bt.baud(9600); 00688 float Vx; 00689 float Vy; 00690 int SW,iVxVy,iSW,n = 0; 00691 int uli = 0; 00692 char m; 00693 int rgb[3] = {10,0,0}; 00694 int state_place_ship = 0; 00695 dotmatrix.init(); 00696 00697 while(1) { 00698 dotmatrix.display_pic(_temppic,rgb); 00699 if(iShipType<8){ 00700 Vx = VRx.read() * 1024; 00701 Vy = VRy.read() * 1024; 00702 SW = Button.read(); 00703 m = check_joys(Vx,Vy); 00704 n = check_joys_int(Vx,Vy); 00705 if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. 00706 iVxVy = 0; 00707 check_ready(); 00708 } 00709 if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. 00710 iSW = 0; 00711 check_ready(); 00712 } 00713 if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. 00714 Select_Position(m); 00715 if(state_place_ship == 0){ 00716 place_ship2_4(0,m); 00717 } 00718 else if(state_place_ship == 1){ 00719 place_ship3(m); 00720 } 00721 else if(state_place_ship == 2){ 00722 place_ship2_4(1,m); 00723 } 00724 else if(state_place_ship == 3 || state_place_ship == 4){ 00725 place_ship2_4(2,m); 00726 } 00727 printf("select_Position = %d\n",select_Position); 00728 check_ready(); 00729 iVxVy = 1; 00730 check_ready(); 00731 } 00732 if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. 00733 printf("Wait!\n"); 00734 00735 check_ready(); 00736 if(get_ship_platform(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function get_ship_platform. 00737 printf("Please input correct position\n"); 00738 }else{ 00739 iShipType++; 00740 if(state_place_ship == 0){ 00741 for(j=0;j<8;j++){ 00742 pic[j] = temppic3[0][j]+pic[j]; 00743 } 00744 } 00745 else if(state_place_ship == 1){ 00746 for(j=0;j<8;j++){ 00747 pic[j] = temppic2[j]+pic[j]; 00748 } 00749 } 00750 else if(state_place_ship == 2 || state_place_ship == 3){ 00751 for(j=0;j<8;j++){ 00752 pic[j] = temppic2[j]+pic[j]; 00753 } 00754 } 00755 00756 else if(state_place_ship == 4 || state_place_ship == 5 || state_place_ship == 6 || state_place_ship == 7){ 00757 for(j=0;j<8;j++){ 00758 pic[j] = temppic2[j]+pic[j]; 00759 } 00760 } 00761 state_place_ship = state_place_ship + 1; 00762 } 00763 check_ready(); 00764 iSW = 1; 00765 print_ship_platform(); 00766 check_ready(); 00767 } 00768 }else{ 00769 if(enemy_ready!=1){ 00770 printf("sent E\n"); 00771 while(uli<50){ 00772 bt.printf("E"); 00773 uli++; 00774 } 00775 }else{ 00776 bt.putc('l'); 00777 } 00778 printf("\n"); 00779 break; 00780 } 00781 } 00782 if(enemy_ready == 0){ 00783 printf("Wait for enemy\n"); 00784 while(1){if(bt.readable()){if(bt.getc()=='l'){break;}}} 00785 state = 0; 00786 }else{state = 1;} 00787 wait(5); 00788 while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play 00789 if(state == 0){ 00790 atk_enemy(); 00791 printf("This is enemy_platform.\n"); 00792 print_enemy_platform(); 00793 state = 1; 00794 }else{ 00795 enemy_atk(); 00796 if(check_win==1){ 00797 printf("BOO!! You Win!\n"); 00798 break; 00799 } 00800 printf("This is atk_platform.\n"); 00801 print_atk_platform(); 00802 printf("This is ship_destroyed. : "); 00803 print_ship_destroyed(); 00804 if(TellUrEnemy_uWin()==1){ 00805 while(uli<100){ 00806 bt.putc('q'); 00807 uli++; 00808 } 00809 printf("BOO!! You lost!\n"); 00810 break; 00811 }else{state = 0;} 00812 } 00813 } 00814 while(1){printf("END\n");wait(1);} 00815 } 00816 00817 /******************************************************************************/ 00818 /* */ 00819 /* */ 00820 /* Another function */ 00821 /* */ 00822 /* */ 00823 /******************************************************************************/ 00824 00825 char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. 00826 00827 if(ifUrJirat==1){ 00828 if(jx < 14.0){ return 'a';} 00829 else if(jx > 1010.0){return 'd';} 00830 else if(jy < 14.0){ return 'w';} 00831 else if(jy > 1010.0){return 's';} 00832 else{return NULL;} 00833 }else{ 00834 if(jx < 14.0){ return 'd';} 00835 else if(jx > 1010.0){return 'a';} 00836 else if(jy < 14.0){ return 's';} 00837 else if(jy > 1010.0){return 'w';} 00838 else{return NULL;} 00839 } 00840 } 00841 int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. 00842 if(jx>=450.0&&jx<=600.0){ 00843 if(jy>=450.0&&jy<=600.0){ 00844 return 1; 00845 }else{ 00846 return 0; 00847 } 00848 }else{ 00849 return 0; 00850 } 00851 } 00852 void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). 00853 int protect_ships; 00854 protect_ships = protect_ship(select_Position,ship_SelectType[iShipType]); 00855 switch(M){ 00856 case 'a': 00857 if(select_Position%10!=0){ 00858 select_Position = select_Position - 1; 00859 } 00860 break; 00861 case 'd': 00862 if(select_Position%10!=7){ 00863 if(protect_ships==1 or protect_ships==0){ 00864 select_Position = select_Position + 1; 00865 }//else{ 00866 //printf("Not allow position.\n"); 00867 //} 00868 } 00869 break; 00870 case 'w': 00871 if(select_Position/10!=0){ 00872 select_Position = select_Position - 10; 00873 } 00874 break; 00875 case 's': 00876 if(select_Position/10!=7){ 00877 if(protect_ships==1 or protect_ships==2){ 00878 select_Position = select_Position + 10; 00879 }//else{ 00880 //printf("Not allow position.\n"); 00881 //} 00882 } 00883 break; 00884 } 00885 } 00886 int protect_ship(int head_position,int ship_type){ 00887 int x,y = 0; 00888 y = head_position/10; 00889 x = head_position%10; 00890 if(ship_type==4){if((y+4)>7){return 0;}else{return 1;}} 00891 else if(ship_type==3){if((x+3)>7){return 2;}else{return 1;}} 00892 else if(ship_type==2){if((y+2)>7){return 0;}else{return 1;}} 00893 else{return 1;} 00894 } 00895 int get_ship_platform(int input_position,int ship_type){ //-----------------------Plot ship in ship area. 00896 int i,x,y = 0; 00897 int error = 0; 00898 00899 y = input_position/10; 00900 x = input_position%10; 00901 00902 for(i=0;i<ship_type;i++){ 00903 if(check_correctPosition(y,x) == 1){ 00904 if(ship_type == 4){ship4[i] = (y*10)+x;} 00905 else if(ship_type == 3){ship3[i] = (y*10)+x;} 00906 else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;} 00907 else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;} 00908 }else{error = 1;break;} 00909 if(ship_type%2 == 0){y++;}else{x++;} 00910 } 00911 if(ship_type==2){check_ship2++;} 00912 if(error == 1){return 0;}else{ 00913 y = input_position/10; 00914 x = input_position%10; 00915 for(i=0;i<ship_type;i++){ 00916 ship_platform[y][x] = ship_type; 00917 if(ship_type%2 == 0){y++;}else{x++;} 00918 } 00919 return 1; 00920 } 00921 } 00922 int check_correctPosition(int X,int Y){ 00923 if(X<=7){ 00924 if(Y<=7){ 00925 if(ship_platform[X][Y] == 0){return 1;}else{return 0;} 00926 }else{return 0;} 00927 }else{return 0;} 00928 } 00929 void print_ship_platform(){ 00930 for(int i=0;i<8;i++){ 00931 for(int j=0;j<8;j++){ 00932 printf("| %d |",ship_platform[i][j]); 00933 } 00934 printf("\n"); 00935 } 00936 } 00937 void print_enemy_platform(){ 00938 for(int i=0;i<8;i++){ 00939 for(int j=0;j<8;j++){ 00940 printf("| %d |",enemy_platform[i][j]); 00941 } 00942 printf("\n"); 00943 } 00944 } 00945 void print_atk_platform(){ 00946 for(int i=0;i<8;i++){ 00947 for(int j=0;j<8;j++){ 00948 printf("| %d |",atk_platform[i][j]); 00949 } 00950 printf("\n"); 00951 } 00952 } 00953 void print_ship_destroyed(){ 00954 printf("{"); 00955 for(int i=0;i<8;i++){ 00956 printf("%d",ship_destroyed[i]); 00957 if(i!=14){printf(",");} 00958 } 00959 printf("}\n"); 00960 } 00961 void check_ready(){ 00962 if(bt.readable()){enemy_ready = 1;} 00963 } 00964 char posit2char(int posit){ 00965 int as = posit+48; 00966 char ch = as; 00967 return ch ; 00968 } 00969 int char2posit(char cha){ 00970 int as = cha; 00971 as = as-48; 00972 return as; 00973 } 00974 void get_atk_platform(int atkpos){ 00975 int i,j,x,y = 0; 00976 00977 x = atkpos/10; 00978 y = atkpos%10; 00979 00980 if(ship_platform[x][y]!=0 and atk_platform[x][y]==0){ 00981 atk_platform[x][y] = ship_platform[x][y]; 00982 bt.putc(posit2char(atk_platform[x][y])); 00983 ship_underATK[check_ship_underATK] = (x*10)+y; 00984 if(atk_platform[x][y]==4){ 00985 for(i=0;i<4;i++){ 00986 for(j=0;j<15;j++){ 00987 if(ship4[i]==ship_underATK[j]){ship_destroyed[0]++;} 00988 } 00989 } 00990 }else if(atk_platform[x][y]==3){ 00991 for(i=0;i<3;i++){ 00992 for(j=0;j<15;j++){ 00993 if(ship3[i]==ship_underATK[j]){ship_destroyed[1]++;} 00994 } 00995 } 00996 }else if(atk_platform[x][y]==2){ 00997 for(i=0;i<4;i++){ 00998 for(j=0;j<15;j++){ 00999 if(ship2[0][i]==ship_underATK[j]){ship_destroyed[2]++;} 01000 if(ship2[1][i]==ship_underATK[j]){ship_destroyed[3]++;} 01001 } 01002 } 01003 }else if(atk_platform[x][y]==1){ 01004 for(i=0;i<4;i++){ 01005 for(j=0;j<15;j++){ 01006 if(ship3[i]==ship_underATK[j]){ 01007 ship_destroyed[4+i]++; 01008 atk_platform[x][y] = 11; 01009 } 01010 } 01011 } 01012 } 01013 }else{ 01014 atk_platform[x][y] = 8; 01015 bt.putc(posit2char(atk_platform[x][y])); 01016 } 01017 } 01018 void mark_ship_destroyed(){ 01019 int i,j,k = 0; 01020 01021 if(ship_destroyed[0]==10){ 01022 for(i=0;i<8;i++){ 01023 for(j=0;j<8;j++){ 01024 if(atk_platform[i][j]==4){atk_platform[i][j] = 11;} 01025 } 01026 } 01027 } 01028 if(ship_destroyed[1]==6){ 01029 for(i=0;i<8;i++){ 01030 for(j=0;j<8;j++){ 01031 if(atk_platform[i][j]==3){atk_platform[i][j] = 11;} 01032 } 01033 } 01034 } 01035 if(ship_destroyed[2]==3){ 01036 for(k=0;k<2;k++){ 01037 i = ship2[0][k]/10; 01038 j = ship2[0][k]%10; 01039 atk_platform[i][j] = 11; 01040 } 01041 } 01042 if(ship_destroyed[3]==3){ 01043 for(k=0;k<2;k++){ 01044 i = ship2[1][k]/10; 01045 j = ship2[1][k]%10; 01046 atk_platform[i][j] = 11; 01047 } 01048 } 01049 } 01050 void atk_enemy(){ 01051 float Vx; 01052 float Vy; 01053 int SW,iVxVy,iSW,n = 0; 01054 int iPrintUturn = 0; 01055 char m; 01056 01057 select_Position = 0; 01058 while(1){ 01059 if(iPrintUturn==0){ 01060 printf("Your turn.\n"); 01061 iPrintUturn = 1; 01062 } 01063 Vx = VRx.read() * 1024; 01064 Vy = VRy.read() * 1024; 01065 SW = Button.read(); 01066 m = check_joys(Vx,Vy); 01067 n = check_joys_int(Vx,Vy); 01068 if(n == 1){ iVxVy = 0;} 01069 if(SW == 1){ 01070 iSW = 0; 01071 if(bt.readable()){ 01072 enemy_platform[select_Position/10][select_Position%10] = char2posit(bt.getc()); 01073 break; 01074 } 01075 } 01076 if(m!=NULL && iVxVy==0){ 01077 Select_Position(m); 01078 printf("select_Position = %d\n",select_Position); 01079 bt.putc(posit2char(select_Position)); 01080 iVxVy = 1; 01081 } 01082 if(SW==0 && iSW==0){ 01083 bt.putc('B'); 01084 printf("You attacking at position %d\n",select_Position); 01085 iSW = 1; 01086 } 01087 } 01088 } 01089 void enemy_atk(){ 01090 int data_fromEnemy; 01091 char replica_data_fromEnemy; 01092 printf("Wait for enemy attack.\n"); 01093 select_Position = 0; 01094 while(1){ 01095 if(bt.readable()){ 01096 replica_data_fromEnemy = bt.getc(); 01097 if(replica_data_fromEnemy!='q' and replica_data_fromEnemy!=8){ 01098 data_fromEnemy = char2posit(replica_data_fromEnemy); 01099 if(data_fromEnemy!=18){ 01100 select_Position = data_fromEnemy; 01101 printf("enemy select_Position = %d\n",select_Position); 01102 }else{ 01103 ATKposition = select_Position; 01104 break; 01105 } 01106 } 01107 if(replica_data_fromEnemy=='q'){ 01108 check_win = 1; 01109 break; 01110 } 01111 } 01112 } 01113 if(check_win!=1){ 01114 printf("Enemy attack at your area %d\n",ATKposition); 01115 get_atk_platform(ATKposition); 01116 check_ship_underATK++; 01117 mark_ship_destroyed(); 01118 } 01119 } 01120 int TellUrEnemy_uWin(){ 01121 int sub_end = 0; 01122 01123 for(int i=0;i<15;i++){ 01124 if(ship_underATK[i]!=9){sub_end++;} 01125 } 01126 if(sub_end==15){return 1;}else{return 0;} 01127 } 01128 int IamWin(){ 01129 char chare; 01130 if(bt.readable()){ 01131 chare = bt.getc(); 01132 if(chare=='q'){return 1;}else{return 0;} 01133 }else{return 0;} 01134 }
Generated on Tue Jul 12 2022 17:51:10 by
1.7.2
