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