Battle ship game.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
gunpuen2
Date:
Sun Dec 04 05:04:03 2016 +0000
Parent:
21:d76aa19984be
Commit message:
Fix all (12:03 || 4/12/2559)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d76aa19984be -r 965d84d98784 main.cpp
--- a/main.cpp	Sat Dec 03 17:45:57 2016 +0000
+++ b/main.cpp	Sun Dec 04 05:04:03 2016 +0000
@@ -4,10 +4,10 @@
 #include "mbed.h"
  
 // Initail I/O.
-AnalogIn VRx(A2);
-AnalogIn VRy(A3);
-DigitalIn Button(D13);
-Serial bt(D8, D2);
+AnalogIn VRx(A0);
+AnalogIn VRy(A1);
+DigitalIn Button(PA_4);
+Serial bt(PA_15, PB_7);
 
 // Initial function.
 char posit2char(int posit);
@@ -16,6 +16,7 @@
 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 get_ship_platform(int input_position,int ship_type);
@@ -23,7 +24,7 @@
 void enemy_atk();
 void check_ready();
 void mark_ship_destroyed();
-void Select_Position(char M);
+void Select_Position(char M,int SetOrPlay);
 void get_atk_platform(int atkpos);
 void check_end();
 //inessential
@@ -80,7 +81,7 @@
                 check_ready();
             }
             if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch.
-                Select_Position(m);
+                Select_Position(m,1);
                 printf("select_Position = %d\n",select_Position);
                 check_ready();
                 iVxVy = 1;
@@ -126,6 +127,7 @@
             print_enemy_platform();
             state = 1;
         }else{
+            wait(1);
             enemy_atk();
             if(check_win==1){
                 printf("BOO!! You Win!\n");
@@ -183,7 +185,7 @@
         return 0;
     }
 }
-void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick).
+void Select_Position(char M,int SetOrPlay){ //-------------------------------------------------Change position of cursor (Joystick).
     int protect_ships;
     protect_ships = protect_ship(select_Position,ship_SelectType[iShipType]);
     switch(M){
@@ -196,9 +198,10 @@
             if(select_Position%10!=7){
                 if(protect_ships==1 or protect_ships==0){
                     select_Position = select_Position + 1;
-                }//else{
-                    //printf("Not allow position.\n");
-                //}
+                }
+                if(SetOrPlay==0){
+                    select_Position = select_Position + 1;
+                }
             }
             break;
         case 'w':
@@ -210,9 +213,10 @@
             if(select_Position/10!=7){
                 if(protect_ships==1 or protect_ships==2){
                     select_Position = select_Position + 10;
-                }//else{
-                    //printf("Not allow position.\n");
-                //}
+                }
+                if(SetOrPlay==0){
+                    select_Position = select_Position + 10;
+                }
             }
             break;
     }
@@ -345,8 +349,12 @@
             }
         }
     }else{
-        atk_platform[x][y] = 8;
-        bt.putc(posit2char(atk_platform[x][y]));
+        if(atk_platform[x][y]!=0){
+            printf("You repeatedly attacked.\n");
+        }else{
+            atk_platform[x][y] = 8;
+            bt.putc(posit2char(atk_platform[x][y]));
+        }
     }
 }
 void mark_ship_destroyed(){
@@ -408,7 +416,7 @@
             }
         }
         if(m!=NULL && iVxVy==0){
-            Select_Position(m);
+            Select_Position(m,1);
             printf("select_Position = %d\n",select_Position);
             bt.putc(posit2char(select_Position));
             iVxVy = 1;