Nasrun

Dependencies:   mbed

Fork of project_ShipDot by Jirat Nakarit

Revision:
6:7f99124272d9
Parent:
5:41b42d8e0af1
Child:
7:e35ba3ddb9af
--- a/main.cpp	Sat Dec 03 03:56:04 2016 +0000
+++ b/main.cpp	Sat Dec 03 05:14:38 2016 +0000
@@ -16,8 +16,12 @@
 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_metrix();
+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);
@@ -33,6 +37,7 @@
 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};
@@ -81,7 +86,7 @@
                 }
                 check_ready();
                 iSW = 1;
-                print_metrix();
+                print_ship_platform();
                 check_ready();
             }
         }else{
@@ -101,50 +106,13 @@
     if(enemy_ready == 1){state = 1;}else{state = 0;} //__________________________Set state
     while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play
         if(state == 0){
-            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(m!=NULL && iVxVy==0){
-                Select_Position(m);
-                printf("select_Position = %d\n",select_Position);
-                iVxVy = 1;
-            }
-            if(SW==0 && iSW==0){
-                bt.putc(posit2char(select_Position));
-                printf("You attacking at position %d\n",select_Position);
-                state = 1;
-                iSW = 1;
-            }
+            atk_enemy();
+            printf("This is enemy_platform.\n");
+            print_enemy_platform();
         }else{
-            iPrintUturn = 0;
-            while(1){
-                if(bt.readable()){
-                    // Wait for resive position of enemy that atk you.
-                    ATKposition = char2posit(bt.getc());
-                    iWaitATK = 1;
-                }else{
-                    printf("Wait for enemy attack\n");
-                }
-                if(iWaitATK==1){
-                    printf("Enemy attack at your area %d\n",ATKposition);
-                    getATK_platform(ATKposition);
-                    mark_ship_destroyed();
-                    state = 0;
-                    break;
-                }
-            }
+            enemy_atk();
+            printf("This is atk_platform.\n");
+            print_atk_platform();
         }
         // wait for check that who is winner.
     }
@@ -258,7 +226,7 @@
         return 0;
     }
 }
-void print_metrix(){
+void print_ship_platform(){
     for(int i=0;i<8;i++){
         for(int j=0;j<8;j++){
             printf("| %d |",ship_platform[i][j]);
@@ -266,6 +234,22 @@
         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 check_ready(){
     if(bt.readable()){
         enemy_ready = 1;
@@ -290,6 +274,7 @@
     
     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] = (y*10)+x;
         check_ship_underATK++;
         if(atk_platform[x][y]==4){
@@ -331,6 +316,7 @@
         }
     }else{
         atk_platform[x][y] = 8;
+        bt.putc(posit2char(atk_platform[x][y]));
     }
 }
 void mark_ship_destroyed(){
@@ -365,4 +351,61 @@
             atk_platform[i][j] = 11;
         }
     }
+}
+void atk_enemy(){
+    float Vx;
+    float Vy;
+    int SW,iVxVy,iSW,n;
+    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;
+            }else{
+                ATKposition = select_Position;
+                break;
+            }
+        }
+    }
+    printf("Enemy attack at your area %d\n",ATKposition);
+    getATK_platform(ATKposition);
+    mark_ship_destroyed();
 }
\ No newline at end of file