Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:6157c77da3d5
- Parent:
- 0:a8e057156263
- Child:
- 2:d93832fe8deb
--- a/main.cpp Fri Dec 02 07:00:27 2016 +0000
+++ b/main.cpp Fri Dec 02 08:16:18 2016 +0000
@@ -1,5 +1,5 @@
/******************************************************************************/
-/* This Nicleo is stat 0! */
+/* This Nicleo is state 0 */
/******************************************************************************/
#include "mbed.h"
@@ -17,16 +17,21 @@
void check_ready();
void print_metrix();
void Select_Position(char M);
+void getATK_position(int atkpos);
+char posit2char(int posit);
+int char2posit(char cha);
// Initial variable.
int select_Position = 0;
int ATKposition = 0;
+int enemy_ready = 0;
+int iPrintUturn = 0;
int iWaitATK = 0;
-int U_ready = 0;
int state = 0;
int ship_notdestroy[8] = {4,3,2,2,1,1,1,1};
-int ship_destroyed[8] = {0};
-int ship_flor[8][8] = {{0}}; //__________________________________________________This is ship area.
+int ship_platform[8][8] = {{0}}; //__________________________________________________This is ship area.
+int atk_platform[8][8] = {{0}};
+int ship_destroyed[15] = {0};
int check_ship1 = 0;
int check_ship2 = 0;
int ship4[4] = {0};
@@ -74,18 +79,20 @@
print_metrix();
}
}else{
- if(U_ready!=1){bt.putc('E');}
+ if(enemy_ready!=1){bt.putc('E');}
printf("\n");
break;
}
}
select_Position = 0;
-
+ if(enemy_ready == 1){state = 1;}else{state = 0;} //__________________________Set state
while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play
- if(U_ready == 1){state = 0;}else{state = 1;}
- if(state == 1){
- printf("Your turn.\n");
+ if(state == 0){
+ if(iPrintUturn==0){
+ printf("Your turn.\n");
+ iPrintUturn = 1;
+ }
Vx = VRx.read() * 1024;
Vy = VRy.read() * 1024;
SW = Button.read();
@@ -103,19 +110,24 @@
iVxVy = 1;
}
if(SW==0 && iSW==0){
- printf("send\n");
- bt.printf("%d",select_Position);
- state = 0;
+ bt.putc(posit2char(select_Position));
+ printf("You attacking at position %d\n",select_Position);
+ state = 1;
iSW = 1;
}
}else{
+ iPrintUturn = 0;
while(1){
- printf("");
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);
+ state = 0;
break;
}
}
@@ -136,16 +148,6 @@
-
-
-
-
-
-
-
-
-
-
char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right.
if(jx < 24.0){
return 'a';
@@ -221,7 +223,7 @@
y = input_position/10;
x = input_position%10;
for(i=0;i<ship_type;i++){
- ship_flor[y][x] = ship_type;
+ ship_platform[y][x] = ship_type;
if(ship_type%2 == 0){
y++;
}else{
@@ -234,11 +236,13 @@
int check_correctPosition(int X,int Y){
if(X<=7){
if(Y<=7){
- if(ship_flor[X][Y] == 0){
+ if(ship_platform[X][Y] == 0){
return 1;
}else{
return 0;
}
+ }else{
+ return 0;
}
}else{
return 0;
@@ -247,14 +251,38 @@
void print_metrix(){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
- printf("| %d |",ship_flor[i][j]);
+ printf("| %d |",ship_platform[i][j]);
}
printf("\n");
}
}
void check_ready(){
if(bt.readable()){
- U_ready = 1;
+ 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_position(int atkpos){
+ int i,x,y = 0;
+
+ y = atkpos/10;
+ x = atkpos%10;
+
+ if(ship_platform[x][y]!=0){
+ atk_platform[x][y] = ship_platform[x][y];
+ ship_destroyed[] = (y*10)+x;
+ }else{
+ atk_platform[x][y] = 8;
}
}
/*