Jirat Nakarit / Mbed 2 deprecated project

Dependencies:   mbed

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  
00006 // Initail I/O.
00007 AnalogIn VRx(A0);
00008 AnalogIn VRy(A1);
00009 DigitalIn Button(PA_4);
00010 Serial bt(PA_15, PB_7);
00011 
00012 // Initial function.
00013 char posit2char(int posit);
00014 char check_joys(int jx,int jy);
00015 int char2posit(char cha);
00016 int IamWin();
00017 int protect_ship(int head_position,int ship_type);
00018 int TellUrEnemy_uWin();
00019 
00020 int check_joys_int(int jx,int jy);
00021 int check_correctPosition(int X,int Y);
00022 int get_ship_platform(int input_position,int ship_type);
00023 void atk_enemy();
00024 void enemy_atk();
00025 void check_ready();
00026 void mark_ship_destroyed();
00027 void Select_Position(char M,int SetOrPlay);
00028 void get_atk_platform(int atkpos);
00029 void check_end();
00030 //inessential
00031 void print_ship_destroyed();
00032 void print_enemy_platform();
00033 void print_ship_platform();
00034 void print_atk_platform();
00035 
00036 // Setting
00037 int ifUrJirat = 1;
00038 // Initial variable.
00039 int check_ship_underATK = 0;
00040 int select_Position = 0;
00041 int ATKposition = 0;
00042 int enemy_ready = 0;
00043 int check_ship1 = 0;
00044 int check_ship2 = 0;
00045 int iShipType = 0;
00046 int iWaitATK = 0;
00047 int check_win = 0;
00048 int state = 0;
00049 int ship_SelectType[8] = {4,3,2,2,1,1,1,1};
00050 int enemy_platform[8][8] = {{0}};
00051 int ship_platform[8][8] = {{0}}; //______________________________________________This is ship area.
00052 int atk_platform[8][8] = {{0}};
00053 int ship_underATK[15] = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9};
00054 int ship_destroyed[8] = {0};
00055 int ship4[4] = {0};
00056 int ship3[3] = {0};
00057 int ship2[2][2] = {{0}};
00058 int ship1[4] = {0};
00059 
00060 int main(){ //-------------------------------------------------------------------Main Function.
00061     bt.baud(9600);
00062     float Vx;
00063     float Vy;
00064     int SW,iVxVy,iSW,n = 0;
00065     int uli = 0;
00066     char m;
00067     
00068     while(1) {
00069         if(iShipType<8){
00070             Vx = VRx.read() * 1024;
00071             Vy = VRy.read() * 1024;
00072             SW = Button.read();
00073             m = check_joys(Vx,Vy);
00074             n = check_joys_int(Vx,Vy);
00075             if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time.
00076                 iVxVy = 0;
00077                 check_ready();
00078             }
00079             if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time.
00080                 iSW = 0;
00081                 check_ready();
00082             }
00083             if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch.
00084                 Select_Position(m,1);
00085                 printf("select_Position = %d\n",select_Position);
00086                 check_ready();
00087                 iVxVy = 1;
00088                 check_ready();
00089             }
00090             if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human.
00091                 printf("Wait!\n");
00092                 check_ready();
00093                 if(get_ship_platform(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function get_ship_platform.
00094                     printf("Please input correct position\n");
00095                 }else{
00096                     iShipType++;
00097                 }
00098                 check_ready();
00099                 iSW = 1;
00100                 print_ship_platform();
00101                 check_ready();
00102             }
00103         }else{
00104             if(enemy_ready!=1){
00105                 printf("sent E\n");
00106                 while(uli<50){
00107                     bt.printf("E");
00108                     uli++;
00109                 }
00110             }else{
00111                 bt.putc('l');
00112             }
00113             printf("\n");
00114             break;
00115         }
00116     }
00117     if(enemy_ready == 0){
00118         printf("Wait for enemy\n");
00119         while(1){if(bt.readable()){if(bt.getc()=='l'){break;}}}
00120         state = 0;
00121     }else{state = 1;}
00122     wait(5);
00123     while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play
00124         if(state == 0){
00125             atk_enemy();
00126             printf("This is enemy_platform.\n");
00127             print_enemy_platform();
00128             state = 1;
00129         }else{
00130             wait(1);
00131             enemy_atk();
00132             if(check_win==1){
00133                 printf("BOO!! You Win!\n");
00134                 break;
00135             }
00136             printf("This is atk_platform.\n");
00137             print_atk_platform();
00138             printf("This is ship_destroyed. : ");
00139             print_ship_destroyed();
00140             if(TellUrEnemy_uWin()==1){
00141                 while(uli<100){
00142                     bt.putc('q');
00143                     uli++;
00144                 }
00145                 printf("BOO!! You lost!\n");
00146                 break;
00147             }else{state = 0;}
00148         }
00149     }
00150     while(1){printf("END\n");wait(1);}
00151 }
00152 
00153 /******************************************************************************/
00154 /*                                                                            */
00155 /*                                                                            */
00156 /*                              Another function                              */
00157 /*                                                                            */
00158 /*                                                                            */
00159 /******************************************************************************/
00160 
00161 char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right.
00162     
00163     if(ifUrJirat==1){
00164         if(jx < 14.0){       return 'a';}
00165         else if(jx > 1010.0){return 'd';}
00166         else if(jy < 14.0){  return 'w';}
00167         else if(jy > 1010.0){return 's';}
00168         else{return NULL;}
00169     }else{
00170         if(jx < 14.0){       return 'd';}
00171         else if(jx > 1010.0){return 'a';}
00172         else if(jy < 14.0){  return 's';}
00173         else if(jy > 1010.0){return 'w';}
00174         else{return NULL;}
00175     }
00176 }
00177 int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre.
00178     if(jx>=450.0&&jx<=600.0){
00179         if(jy>=450.0&&jy<=600.0){
00180             return 1;
00181         }else{
00182             return 0;
00183         }
00184     }else{
00185         return 0;
00186     }
00187 }
00188 void Select_Position(char M,int SetOrPlay){ //-------------------------------------------------Change position of cursor (Joystick).
00189     int protect_ships;
00190     protect_ships = protect_ship(select_Position,ship_SelectType[iShipType]);
00191     switch(M){
00192         case 'a':
00193             if(select_Position%10!=0){
00194                 select_Position = select_Position - 1;
00195             }
00196             break;
00197         case 'd':
00198             if(select_Position%10!=7){
00199                 if(protect_ships==1 or protect_ships==0){
00200                     select_Position = select_Position + 1;
00201                 }
00202                 if(SetOrPlay==0){
00203                     select_Position = select_Position + 1;
00204                 }
00205             }
00206             break;
00207         case 'w':
00208             if(select_Position/10!=0){
00209                 select_Position = select_Position - 10;
00210             }
00211             break;
00212         case 's':
00213             if(select_Position/10!=7){
00214                 if(protect_ships==1 or protect_ships==2){
00215                     select_Position = select_Position + 10;
00216                 }
00217                 if(SetOrPlay==0){
00218                     select_Position = select_Position + 10;
00219                 }
00220             }
00221             break;
00222     }
00223 }
00224 int protect_ship(int head_position,int ship_type){
00225     int x,y = 0;
00226     y = head_position/10;
00227     x = head_position%10;
00228     if(ship_type==4){if((y+4)>7){return 0;}else{return 1;}}
00229     else if(ship_type==3){if((x+3)>7){return 2;}else{return 1;}}
00230     else if(ship_type==2){if((y+2)>7){return 0;}else{return 1;}}
00231     else{return 1;}
00232 }
00233 int get_ship_platform(int input_position,int ship_type){ //-----------------------Plot ship in ship area.
00234     int i,x,y = 0;
00235     int error = 0;
00236 
00237     y = input_position/10;
00238     x = input_position%10;
00239 
00240     for(i=0;i<ship_type;i++){
00241         if(check_correctPosition(y,x) == 1){
00242             if(ship_type == 4){ship4[i] = (y*10)+x;}
00243             else if(ship_type == 3){ship3[i] = (y*10)+x;}
00244             else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;}
00245             else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;}
00246         }else{error = 1;break;}
00247         if(ship_type%2 == 0){y++;}else{x++;}
00248     }
00249     if(ship_type==2){check_ship2++;}
00250     if(error == 1){return 0;}else{
00251         y = input_position/10;
00252         x = input_position%10;
00253         for(i=0;i<ship_type;i++){
00254             ship_platform[y][x] = ship_type;
00255             if(ship_type%2 == 0){y++;}else{x++;}
00256         }
00257         return 1;
00258     }
00259 }
00260 int check_correctPosition(int X,int Y){
00261     if(X<=7){
00262         if(Y<=7){
00263             if(ship_platform[X][Y] == 0){return 1;}else{return 0;}
00264         }else{return 0;}
00265     }else{return 0;}
00266 }
00267 void print_ship_platform(){
00268     for(int i=0;i<8;i++){
00269         for(int j=0;j<8;j++){
00270             printf("| %d |",ship_platform[i][j]);
00271         }
00272         printf("\n");
00273     }
00274 }
00275 void print_enemy_platform(){
00276     for(int i=0;i<8;i++){
00277         for(int j=0;j<8;j++){
00278             printf("| %d |",enemy_platform[i][j]);
00279         }
00280         printf("\n");
00281     }
00282 }
00283 void print_atk_platform(){
00284     for(int i=0;i<8;i++){
00285         for(int j=0;j<8;j++){
00286             printf("| %d |",atk_platform[i][j]);
00287         }
00288         printf("\n");
00289     }
00290 }
00291 void print_ship_destroyed(){
00292     printf("{");
00293     for(int i=0;i<8;i++){
00294         printf("%d",ship_destroyed[i]);
00295         if(i!=14){printf(",");}
00296     }
00297     printf("}\n");
00298 }
00299 void check_ready(){
00300     if(bt.readable()){enemy_ready = 1;}
00301 }
00302 char posit2char(int posit){
00303     int as = posit+48;
00304     char ch = as;
00305     return ch ;
00306 }
00307 int char2posit(char cha){
00308     int as = cha;
00309     as = as-48;
00310     return as;
00311 }
00312 void get_atk_platform(int atkpos){
00313     int i,j,x,y = 0;
00314 
00315     x = atkpos/10;
00316     y = atkpos%10;
00317     
00318     if(ship_platform[x][y]!=0 and atk_platform[x][y]==0){
00319         atk_platform[x][y] = ship_platform[x][y];
00320         bt.putc(posit2char(atk_platform[x][y]));
00321         ship_underATK[check_ship_underATK] = (x*10)+y;
00322         if(atk_platform[x][y]==4){
00323             for(i=0;i<4;i++){
00324                 for(j=0;j<15;j++){
00325                     if(ship4[i]==ship_underATK[j]){ship_destroyed[0]++;}
00326                 }
00327             }
00328         }else if(atk_platform[x][y]==3){
00329             for(i=0;i<3;i++){
00330                 for(j=0;j<15;j++){
00331                     if(ship3[i]==ship_underATK[j]){ship_destroyed[1]++;}
00332                 }
00333             }
00334         }else if(atk_platform[x][y]==2){
00335             for(i=0;i<4;i++){
00336                 for(j=0;j<15;j++){
00337                     if(ship2[0][i]==ship_underATK[j]){ship_destroyed[2]++;}
00338                     if(ship2[1][i]==ship_underATK[j]){ship_destroyed[3]++;}
00339                 }
00340             }
00341         }else if(atk_platform[x][y]==1){
00342             for(i=0;i<4;i++){
00343                 for(j=0;j<15;j++){
00344                     if(ship3[i]==ship_underATK[j]){
00345                         ship_destroyed[4+i]++;
00346                         atk_platform[x][y] = 11;
00347                     }
00348                 }
00349             }
00350         }
00351     }else{
00352         if(atk_platform[x][y]!=0){
00353             printf("You repeatedly attacked.\n");
00354         }else{
00355             atk_platform[x][y] = 8;
00356             bt.putc(posit2char(atk_platform[x][y]));
00357         }
00358     }
00359 }
00360 void mark_ship_destroyed(){
00361     int i,j,k = 0;
00362     
00363     if(ship_destroyed[0]==10){
00364         for(i=0;i<8;i++){
00365             for(j=0;j<8;j++){
00366                 if(atk_platform[i][j]==4){atk_platform[i][j] = 11;}
00367             }
00368         }
00369     }
00370     if(ship_destroyed[1]==6){
00371         for(i=0;i<8;i++){
00372             for(j=0;j<8;j++){
00373                 if(atk_platform[i][j]==3){atk_platform[i][j] = 11;}
00374             }
00375         }
00376     }
00377     if(ship_destroyed[2]==3){
00378         for(k=0;k<2;k++){
00379             i = ship2[0][k]/10;
00380             j = ship2[0][k]%10;
00381             atk_platform[i][j] = 11;
00382         }
00383     }
00384     if(ship_destroyed[3]==3){
00385         for(k=0;k<2;k++){
00386             i = ship2[1][k]/10;
00387             j = ship2[1][k]%10;
00388             atk_platform[i][j] = 11;
00389         }
00390     }
00391 }
00392 void atk_enemy(){
00393     float Vx;
00394     float Vy;
00395     int SW,iVxVy,iSW,n = 0;
00396     int iPrintUturn = 0;
00397     char m;
00398     
00399     select_Position = 0;
00400     while(1){
00401         if(iPrintUturn==0){
00402             printf("Your turn.\n");
00403             iPrintUturn = 1;
00404         }
00405         Vx = VRx.read() * 1024;
00406         Vy = VRy.read() * 1024;
00407         SW = Button.read();
00408         m = check_joys(Vx,Vy);
00409         n = check_joys_int(Vx,Vy);
00410         if(n == 1){ iVxVy = 0;}
00411         if(SW == 1){
00412             iSW = 0;
00413             if(bt.readable()){
00414                 enemy_platform[select_Position/10][select_Position%10] = char2posit(bt.getc());
00415                 break;
00416             }
00417         }
00418         if(m!=NULL && iVxVy==0){
00419             Select_Position(m,1);
00420             printf("select_Position = %d\n",select_Position);
00421             bt.putc(posit2char(select_Position));
00422             iVxVy = 1;
00423         }
00424         if(SW==0 && iSW==0){
00425             bt.putc('B');
00426             printf("You attacking at position %d\n",select_Position);
00427             iSW = 1;
00428         }
00429     }
00430 }
00431 void enemy_atk(){
00432     int data_fromEnemy;
00433     char replica_data_fromEnemy;
00434     printf("Wait for enemy attack.\n");
00435     select_Position = 0;
00436     while(1){
00437         if(bt.readable()){
00438             replica_data_fromEnemy = bt.getc();
00439             if(replica_data_fromEnemy!='q' and replica_data_fromEnemy!=8){
00440                 data_fromEnemy = char2posit(replica_data_fromEnemy);
00441                 if(data_fromEnemy!=18){
00442                     select_Position = data_fromEnemy;
00443                     printf("enemy select_Position = %d\n",select_Position);
00444                 }else{
00445                     ATKposition = select_Position;
00446                     break;
00447                 }
00448             }
00449             if(replica_data_fromEnemy=='q'){
00450                 check_win = 1;
00451                 break;
00452             }
00453         }
00454     }
00455     if(check_win!=1){
00456         printf("Enemy attack at your area %d\n",ATKposition);
00457         get_atk_platform(ATKposition);
00458         check_ship_underATK++;
00459         mark_ship_destroyed();
00460     }
00461 }
00462 int TellUrEnemy_uWin(){
00463     int sub_end = 0;
00464     
00465     for(int i=0;i<15;i++){
00466         if(ship_underATK[i]!=9){sub_end++;}
00467     }
00468     if(sub_end==15){return 1;}else{return 0;}
00469 }
00470 int IamWin(){
00471     char chare;
00472     if(bt.readable()){
00473         chare = bt.getc();
00474         if(chare=='q'){return 1;}else{return 0;}
00475     }else{return 0;}
00476 }