Nasrun Hayeeyama
/
project_ShipDot
Nasrun
Fork of project_ShipDot by
main.cpp
- Committer:
- gunpuen2
- Date:
- 2016-12-03
- Revision:
- 9:102af5021c6c
- Parent:
- 8:a8d39b5d17ab
- Child:
- 10:b8d45f9b00bf
File content as of revision 9:102af5021c6c:
/******************************************************************************/ /* This Nicleo is state 0 */ /******************************************************************************/ #include "mbed.h" // Initail I/O. AnalogIn VRx(A2); AnalogIn VRy(A3); DigitalIn Button(D13); Serial bt(D8, D2); // Initial function. char posit2char(int posit); char check_joys(int jx,int jy); int char2posit(char cha); int IamWin(); int TellUrEnemy_uWin(); int check_joys_int(int jx,int jy); int check_correctPosition(int X,int Y); int getShip_position(int input_position,int ship_type); void atk_enemy(); void enemy_atk(); void check_ready(); void print_ship_destroyed(); void print_enemy_platform(); void print_ship_platform(); void print_atk_platform(); void mark_ship_destroyed(); void Select_Position(char M); void getATK_platform(int atkpos); void check_end(); // Setting int ifUrJirat = 1; // Initial variable. int check_ship_underATK = 0; int select_Position = 0; int ATKposition = 0; int enemy_ready = 0; int check_ship1 = 0; int check_ship2 = 0; int iWaitATK = 0; int state = 0; int ship_SelectType[8] = {4,3,2,2,1,1,1,1}; int enemy_platform[8][8] = {{0}}; int ship_platform[8][8] = {{0}}; //______________________________________________This is ship area. int atk_platform[8][8] = {{0}}; int ship_underATK[15] = {9}; int ship_destroyed[8] = {0}; int ship4[4] = {0}; int ship3[3] = {0}; int ship2[2][2] = {{0}}; int ship1[4] = {0}; int main(){ //-------------------------------------------------------------------Main Function. float Vx; float Vy; int SW,iVxVy,iSW,n; int iShipType = 0; unsigned long int uli = 0; char m; while(1) { if(iShipType<8){ Vx = VRx.read() * 1024; Vy = VRy.read() * 1024; SW = Button.read(); m = check_joys(Vx,Vy); n = check_joys_int(Vx,Vy); if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. iVxVy = 0; check_ready(); } if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. iSW = 0; check_ready(); } if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. Select_Position(m); check_ready(); printf("select_Position = %d\n",select_Position); iVxVy = 1; check_ready(); } if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. printf("Wait!\n"); check_ready(); if(getShip_position(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function getShip_position. printf("Please input correct position\n"); }else{ iShipType++; } check_ready(); iSW = 1; print_ship_platform(); check_ready(); } }else{ if(enemy_ready!=1){ printf("sent E\n"); while(uli<1000){ bt.printf("E"); uli++; } } printf("\n"); break; } } select_Position = 0; printf("%d",enemy_ready); if(enemy_ready == 1){state = 1;}else{state = 0;} while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play if(state == 0){ atk_enemy(); printf("This is enemy_platform.\n"); print_enemy_platform(); if(IamWin()){printf("You win!\n");}else{state = 1;} }else{ enemy_atk(); printf("This is atk_platform.\n"); print_atk_platform(); printf("This is ship_destroyed. : "); print_ship_destroyed(); if(TellUrEnemy_uWin()){ bt.putc('q'); printf("BOO!! You lost!"); break; }else{state = 0;} } } } /******************************************************************************/ /* */ /* */ /* Another function */ /* */ /* */ /******************************************************************************/ char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. if(ifUrJirat==1){ if(jx < 14.0){ return 'a';} else if(jx > 1010.0){return 'd';} else if(jy < 14.0){ return 'w';} else if(jy > 1010.0){return 's';} else{return NULL;} }else{ if(jx < 14.0){ return 'd';} else if(jx > 1010.0){return 'a';} else if(jy < 14.0){ return 's';} else if(jy > 1010.0){return 'w';} else{return NULL;} } } int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. if(jx>=450.0&&jx<=600.0){ if(jy>=450.0&&jy<=600.0){ return 1; }else{ return 0; } }else{ return 0; } } void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). switch(M){ case 'a': if(select_Position%10!=0){select_Position = select_Position - 1;} break; case 'd': if(select_Position%10!=7){select_Position = select_Position + 1;} break; case 'w': if(select_Position/10!=0){select_Position = select_Position - 10;} break; case 's': if(select_Position/10!=7){select_Position = select_Position + 10;} break; } } int getShip_position(int input_position,int ship_type){ //-----------------------Plot ship in ship area. int i,x,y = 0; int error = 0; y = input_position/10; x = input_position%10; for(i=0;i<ship_type;i++){ if(check_correctPosition(y,x) == 1){ if(ship_type == 4){ship4[i] = (y*10)+x;} else if(ship_type == 3){ship3[i] = (y*10)+x;} else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;} else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;} }else{ error = 1; break; } if(ship_type%2 == 0){ y++; }else{ x++; } } if(ship_type==2){ check_ship2++; } if(error == 1){ return 0; }else{ y = input_position/10; x = input_position%10; for(i=0;i<ship_type;i++){ ship_platform[y][x] = ship_type; if(ship_type%2 == 0){ y++; }else{ x++; } } return 1; } } int check_correctPosition(int X,int Y){ if(X<=7){ if(Y<=7){ if(ship_platform[X][Y] == 0){ return 1; }else{ return 0; } }else{ return 0; } }else{ return 0; } } void print_ship_platform(){ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ printf("| %d |",ship_platform[i][j]); } printf("\n"); } } void print_enemy_platform(){ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ printf("| %d |",enemy_platform[i][j]); } printf("\n"); } } void print_atk_platform(){ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ printf("| %d |",atk_platform[i][j]); } printf("\n"); } } void print_ship_destroyed(){ printf("{"); for(int i=0;i<8;i++){ printf("%d",ship_destroyed[i]); if(i!=14){printf(",");} } printf("}\n"); } void check_ready(){ if(bt.readable()){ enemy_ready = 1; } } char posit2char(int posit){ int as = posit+48; char ch = as; return ch ; } int char2posit(char cha){ int as = cha; as = as-48; return as; } void getATK_platform(int atkpos){ int i,j,x,y = 0; x = atkpos/10; y = atkpos%10; if(ship_platform[x][y]!=0 and atk_platform[x][y]==0){ atk_platform[x][y] = ship_platform[x][y]; bt.putc(posit2char(atk_platform[x][y])); ship_underATK[check_ship_underATK] = (x*10)+y; check_ship_underATK++; if(atk_platform[x][y]==4){ for(i=0;i<4;i++){ for(j=0;j<15;j++){ if(ship4[i]==ship_underATK[j]){ ship_destroyed[0]++; } } } }else if(atk_platform[x][y]==3){ for(i=0;i<3;i++){ for(j=0;j<15;j++){ if(ship3[i]==ship_underATK[j]){ ship_destroyed[1]++; } } } }else if(atk_platform[x][y]==2){ for(i=0;i<4;i++){ for(j=0;j<15;j++){ if(ship2[0][i]==ship_underATK[j]){ ship_destroyed[2]++; } if(ship2[1][i]==ship_underATK[j]){ ship_destroyed[3]++; } } } }else if(atk_platform[x][y]==1){ for(i=0;i<4;i++){ for(j=0;j<15;j++){ if(ship3[i]==ship_underATK[j]){ ship_destroyed[4+i]++; atk_platform[x][y] = 11; } } } } }else{ atk_platform[x][y] = 8; bt.putc(posit2char(atk_platform[x][y])); } } void mark_ship_destroyed(){ int i,j,k = 0; if(ship_destroyed[0]==4){ for(i=0;i<8;i++){ for(j=0;j<8;j++){ if(atk_platform[i][j]==4){ atk_platform[i][j] = 11; } } } }else if(ship_destroyed[1]==3){ for(i=0;i<8;i++){ for(j=0;j<8;j++){ if(atk_platform[i][j]==3){ atk_platform[i][j] = 11; } } } }else if(ship_destroyed[2]==2){ for(k=0;k<2;k++){ i = ship2[0][k]/10; j = ship2[0][k]%10; atk_platform[i][j] = 11; } }else if(ship_destroyed[3]==2){ for(k=0;k<2;k++){ i = ship2[1][k]/10; j = ship2[1][k]%10; atk_platform[i][j] = 11; } } } void atk_enemy(){ float Vx; float Vy; int SW,iVxVy,iSW,n = 0; int iPrintUturn = 0; char m; while(1){ if(iPrintUturn==0){ printf("Your turn.\n"); iPrintUturn = 1; } Vx = VRx.read() * 1024; Vy = VRy.read() * 1024; SW = Button.read(); m = check_joys(Vx,Vy); n = check_joys_int(Vx,Vy); if(n == 1){ iVxVy = 0; } if(SW == 1){ iSW = 0; if(bt.readable()){ enemy_platform[select_Position/10][select_Position%10] = char2posit(bt.getc()); break; } } if(m!=NULL && iVxVy==0){ Select_Position(m); printf("select_Position = %d\n",select_Position); bt.putc(posit2char(select_Position)); iVxVy = 1; } if(SW==0 && iSW==0){ bt.putc('B'); printf("You attacking at position %d\n",select_Position); iSW = 1; } } } void enemy_atk(){ int data_fromEnemy; printf("Wait for enemy attack.\n"); while(1){ if(bt.readable()){ data_fromEnemy = char2posit(bt.getc()); if(data_fromEnemy!=18){ select_Position = data_fromEnemy; printf("enemy select_Position = %d\n",select_Position); }else{ ATKposition = select_Position; break; } } } printf("Enemy attack at your area %d\n",ATKposition); getATK_platform(ATKposition); mark_ship_destroyed(); } int TellUrEnemy_uWin(){ int sub_end = 0; for(int i=0;i<15;i++){ if(ship_underATK[i]!=0){sub_end++;} } if(sub_end==15){return 1;}else{return 0;} } int IamWin(){ if(bt.readable()){if(bt.getc()=='q'){return 1;}else{return 0;}}else{return 0;} }