Nasrun

Dependencies:   mbed

Fork of project_ShipDot by Jirat Nakarit

Revision:
19:d78586719868
Parent:
18:ecef8382d41e
Child:
20:4b9be9a423a8
--- a/main.cpp	Sat Dec 03 15:48:37 2016 +0000
+++ b/main.cpp	Sat Dec 03 16:30:22 2016 +0000
@@ -43,6 +43,7 @@
 int check_ship2 = 0;
 int iShipType = 0;
 int iWaitATK = 0;
+int check_win = 0;
 int state = 0;
 int ship_SelectType[8] = {4,3,2,2,1,1,1,1};
 int enemy_platform[8][8] = {{0}};
@@ -59,7 +60,7 @@
     bt.baud(9600);
     float Vx;
     float Vy;
-    int SW,iVxVy,iSW,n,check_win = 0;
+    int SW,iVxVy,iSW,n = 0;
     int uli = 0;
     char m;
     
@@ -117,16 +118,19 @@
         while(1){if(bt.readable()){if(bt.getc()=='l'){break;}}}
         state = 0;
     }else{state = 1;}
-    wait(2);
+    wait(5);
     while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play
         if(state == 0){
             atk_enemy();
             printf("This is enemy_platform.\n");
             print_enemy_platform();
-            check_win = IamWin();
-            if(check_win==1){printf("You win!\n");break;}else{state = 1;}
+            state = 1;
         }else{
             enemy_atk();
+            if(check_win==1){
+                printf("BOO!! You Win!\n");
+                break;
+            }
             printf("This is atk_platform.\n");
             print_atk_platform();
             printf("This is ship_destroyed. : ");
@@ -136,11 +140,12 @@
                     bt.putc('q');
                     uli++;
                 }
-                printf("BOO!! You lost!");
+                printf("BOO!! You lost!\n");
                 break;
             }else{state = 0;}
         }
     }
+    while(1){printf("END\n");wait(1);}
 }
 
 /******************************************************************************/
@@ -418,24 +423,32 @@
 }
 void enemy_atk(){
     int data_fromEnemy;
+    char replica_data_fromEnemy;
     printf("Wait for enemy attack.\n");
     select_Position = 0;
     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);
+            if(replica_data_fromEnemy!='q'){
+                data_fromEnemy = char2posit(replica_data_fromEnemy);
+                if(data_fromEnemy!=18){
+                    select_Position = data_fromEnemy;
+                    printf("enemy select_Position = %d\n",select_Position);
+                }else{
+                    ATKposition = select_Position;
+                    break;
+                }
             }else{
-                ATKposition = select_Position;
+                check_win = 1;
                 break;
             }
         }
     }
-    printf("Enemy attack at your area %d\n",ATKposition);
-    get_atk_platform(ATKposition);
-    check_ship_underATK++;
-    mark_ship_destroyed();
+    if(check_win!=1){
+        printf("Enemy attack at your area %d\n",ATKposition);
+        get_atk_platform(ATKposition);
+        check_ship_underATK++;
+        mark_ship_destroyed();
+    }
 }
 int TellUrEnemy_uWin(){
     int sub_end = 0;