Nasrun

Dependencies:   mbed

Fork of project_ShipDot by Jirat Nakarit

Revision:
15:5902cf545449
Parent:
14:8abed4026326
Child:
16:3fac68a7fd7c
--- a/main.cpp	Sat Dec 03 09:48:38 2016 +0000
+++ b/main.cpp	Sat Dec 03 11:58:15 2016 +0000
@@ -14,10 +14,11 @@
 char check_joys(int jx,int jy);
 int char2posit(char cha);
 int IamWin();
+int protect_ship(int head_position,int ship_type);
 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);
+int get_ship_platform(int input_position,int ship_type);
 void atk_enemy();
 void enemy_atk();
 void check_ready();
@@ -27,7 +28,7 @@
 void print_atk_platform();
 void mark_ship_destroyed();
 void Select_Position(char M);
-void getATK_platform(int atkpos);
+void get_atk_platform(int atkpos);
 void check_end();
 
 // Setting
@@ -53,6 +54,7 @@
 int ship1[4] = {0};
 
 int main(){ //-------------------------------------------------------------------Main Function.
+    bt.baud(9600);
     float Vx;
     float Vy;
     int SW,iVxVy,iSW,n;
@@ -76,16 +78,18 @@
                 check_ready();
             }
             if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch.
+            if(protect_ship(select_Position,ship_SelectType[iShipType]!=0)){
                 Select_Position(m);
+                printf("select_Position = %d\n",select_Position);
+            }else{printf("Not allow position.\n");}
                 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.
+                if(get_ship_platform(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function get_ship_platform.
                     printf("Please input correct position\n");
                 }else{
                     iShipType++;
@@ -114,6 +118,7 @@
         while(1){if(bt.readable()){if(bt.getc()=='l'){break;}}}
         state = 0;
     }else{state = 1;}
+    wait(2);
     while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play
         if(state == 0){
             atk_enemy();
@@ -189,7 +194,19 @@
             break;
     }
 }
-int getShip_position(int input_position,int ship_type){ //-----------------------Plot ship in ship area.
+int protect_ship(int head_position,int ship_type){
+    int x,y = 0;
+
+    y = head_position/10;
+    x = head_position%10;
+    
+    if(ship_type==4){if((y+3)>7){return 0;}else{return 1;}}
+    else if(ship_type==3){if((x+2)>7){return 0;}else{return 1;}}
+    else if(ship_type==2){if((y+1)>7){return 0;}else{return 1;}}
+    else{return 1;}
+    
+}
+int get_ship_platform(int input_position,int ship_type){ //-----------------------Plot ship in ship area.
     int i,x,y = 0;
     int error = 0;
 
@@ -294,7 +311,7 @@
     as = as-48;
     return as;
 }
-void getATK_platform(int atkpos){
+void get_atk_platform(int atkpos){
     int i,j,x,y = 0;
 
     x = atkpos/10;
@@ -440,7 +457,7 @@
         }
     }
     printf("Enemy attack at your area %d\n",ATKposition);
-    getATK_platform(ATKposition);
+    get_atk_platform(ATKposition);
     check_ship_underATK++;
     mark_ship_destroyed();
 }