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