Nasrun Hayeeyama / Mbed 2 deprecated project_ShipDot

Dependencies:   mbed

Fork of project_ShipDot by Jirat Nakarit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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