Jirat

Dependencies:   mbed

Fork of project by Nasrun Hayeeyama

Committer:
gunpuen2
Date:
Sat Dec 03 12:04:22 2016 +0000
Revision:
16:3fac68a7fd7c
Parent:
15:5902cf545449
Child:
17:aeb7cbfb8640
Fix condition (19:04 || 3/12/2559)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gunpuen2 0:a8e057156263 1 /******************************************************************************/
gunpuen2 1:6157c77da3d5 2 /* This Nicleo is state 0 */
gunpuen2 0:a8e057156263 3 /******************************************************************************/
gunpuen2 0:a8e057156263 4 #include "mbed.h"
gunpuen2 0:a8e057156263 5
gunpuen2 0:a8e057156263 6 // Initail I/O.
gunpuen2 0:a8e057156263 7 AnalogIn VRx(A2);
gunpuen2 0:a8e057156263 8 AnalogIn VRy(A3);
gunpuen2 0:a8e057156263 9 DigitalIn Button(D13);
gunpuen2 5:41b42d8e0af1 10 Serial bt(D8, D2);
gunpuen2 0:a8e057156263 11
gunpuen2 0:a8e057156263 12 // Initial function.
gunpuen2 4:8a86bacddcac 13 char posit2char(int posit);
gunpuen2 0:a8e057156263 14 char check_joys(int jx,int jy);
gunpuen2 4:8a86bacddcac 15 int char2posit(char cha);
gunpuen2 7:e35ba3ddb9af 16 int IamWin();
gunpuen2 15:5902cf545449 17 int protect_ship(int head_position,int ship_type);
gunpuen2 7:e35ba3ddb9af 18 int TellUrEnemy_uWin();
gunpuen2 0:a8e057156263 19 int check_joys_int(int jx,int jy);
gunpuen2 0:a8e057156263 20 int check_correctPosition(int X,int Y);
gunpuen2 15:5902cf545449 21 int get_ship_platform(int input_position,int ship_type);
gunpuen2 6:7f99124272d9 22 void atk_enemy();
gunpuen2 6:7f99124272d9 23 void enemy_atk();
gunpuen2 0:a8e057156263 24 void check_ready();
gunpuen2 16:3fac68a7fd7c 25 void mark_ship_destroyed();
gunpuen2 16:3fac68a7fd7c 26 void Select_Position(char M);
gunpuen2 16:3fac68a7fd7c 27 void get_atk_platform(int atkpos);
gunpuen2 16:3fac68a7fd7c 28 void check_end();
gunpuen2 16:3fac68a7fd7c 29 //inessential
gunpuen2 7:e35ba3ddb9af 30 void print_ship_destroyed();
gunpuen2 6:7f99124272d9 31 void print_enemy_platform();
gunpuen2 6:7f99124272d9 32 void print_ship_platform();
gunpuen2 6:7f99124272d9 33 void print_atk_platform();
gunpuen2 0:a8e057156263 34
gunpuen2 7:e35ba3ddb9af 35 // Setting
gunpuen2 7:e35ba3ddb9af 36 int ifUrJirat = 1;
gunpuen2 0:a8e057156263 37 // Initial variable.
gunpuen2 2:d93832fe8deb 38 int check_ship_underATK = 0;
gunpuen2 0:a8e057156263 39 int select_Position = 0;
gunpuen2 0:a8e057156263 40 int ATKposition = 0;
gunpuen2 1:6157c77da3d5 41 int enemy_ready = 0;
gunpuen2 2:d93832fe8deb 42 int check_ship1 = 0;
gunpuen2 2:d93832fe8deb 43 int check_ship2 = 0;
gunpuen2 0:a8e057156263 44 int iWaitATK = 0;
gunpuen2 0:a8e057156263 45 int state = 0;
gunpuen2 2:d93832fe8deb 46 int ship_SelectType[8] = {4,3,2,2,1,1,1,1};
gunpuen2 6:7f99124272d9 47 int enemy_platform[8][8] = {{0}};
gunpuen2 4:8a86bacddcac 48 int ship_platform[8][8] = {{0}}; //______________________________________________This is ship area.
gunpuen2 1:6157c77da3d5 49 int atk_platform[8][8] = {{0}};
gunpuen2 10:b8d45f9b00bf 50 int ship_underATK[15] = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9};
gunpuen2 2:d93832fe8deb 51 int ship_destroyed[8] = {0};
gunpuen2 0:a8e057156263 52 int ship4[4] = {0};
gunpuen2 0:a8e057156263 53 int ship3[3] = {0};
gunpuen2 0:a8e057156263 54 int ship2[2][2] = {{0}};
gunpuen2 0:a8e057156263 55 int ship1[4] = {0};
gunpuen2 0:a8e057156263 56
gunpuen2 0:a8e057156263 57 int main(){ //-------------------------------------------------------------------Main Function.
gunpuen2 15:5902cf545449 58 bt.baud(9600);
gunpuen2 0:a8e057156263 59 float Vx;
gunpuen2 0:a8e057156263 60 float Vy;
gunpuen2 0:a8e057156263 61 int SW,iVxVy,iSW,n;
gunpuen2 0:a8e057156263 62 int iShipType = 0;
gunpuen2 3:baff75bbb78e 63 unsigned long int uli = 0;
gunpuen2 0:a8e057156263 64 char m;
gunpuen2 14:8abed4026326 65
gunpuen2 0:a8e057156263 66 while(1) {
gunpuen2 0:a8e057156263 67 if(iShipType<8){
gunpuen2 0:a8e057156263 68 Vx = VRx.read() * 1024;
gunpuen2 0:a8e057156263 69 Vy = VRy.read() * 1024;
gunpuen2 0:a8e057156263 70 SW = Button.read();
gunpuen2 0:a8e057156263 71 m = check_joys(Vx,Vy);
gunpuen2 0:a8e057156263 72 n = check_joys_int(Vx,Vy);
gunpuen2 0:a8e057156263 73 if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time.
gunpuen2 0:a8e057156263 74 iVxVy = 0;
gunpuen2 0:a8e057156263 75 check_ready();
gunpuen2 0:a8e057156263 76 }
gunpuen2 0:a8e057156263 77 if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time.
gunpuen2 0:a8e057156263 78 iSW = 0;
gunpuen2 0:a8e057156263 79 check_ready();
gunpuen2 0:a8e057156263 80 }
gunpuen2 0:a8e057156263 81 if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch.
gunpuen2 15:5902cf545449 82 if(protect_ship(select_Position,ship_SelectType[iShipType]!=0)){
gunpuen2 0:a8e057156263 83 Select_Position(m);
gunpuen2 15:5902cf545449 84 printf("select_Position = %d\n",select_Position);
gunpuen2 15:5902cf545449 85 }else{printf("Not allow position.\n");}
gunpuen2 0:a8e057156263 86 check_ready();
gunpuen2 0:a8e057156263 87 iVxVy = 1;
gunpuen2 3:baff75bbb78e 88 check_ready();
gunpuen2 0:a8e057156263 89 }
gunpuen2 0:a8e057156263 90 if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human.
gunpuen2 0:a8e057156263 91 printf("Wait!\n");
gunpuen2 0:a8e057156263 92 check_ready();
gunpuen2 15:5902cf545449 93 if(get_ship_platform(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function get_ship_platform.
gunpuen2 0:a8e057156263 94 printf("Please input correct position\n");
gunpuen2 0:a8e057156263 95 }else{
gunpuen2 0:a8e057156263 96 iShipType++;
gunpuen2 0:a8e057156263 97 }
gunpuen2 0:a8e057156263 98 check_ready();
gunpuen2 0:a8e057156263 99 iSW = 1;
gunpuen2 6:7f99124272d9 100 print_ship_platform();
gunpuen2 3:baff75bbb78e 101 check_ready();
gunpuen2 0:a8e057156263 102 }
gunpuen2 0:a8e057156263 103 }else{
gunpuen2 3:baff75bbb78e 104 if(enemy_ready!=1){
gunpuen2 3:baff75bbb78e 105 printf("sent E\n");
gunpuen2 12:287c6a9560ca 106 while(uli<50){
gunpuen2 4:8a86bacddcac 107 bt.printf("E");
gunpuen2 3:baff75bbb78e 108 uli++;
gunpuen2 3:baff75bbb78e 109 }
gunpuen2 12:287c6a9560ca 110 }else{
gunpuen2 12:287c6a9560ca 111 bt.putc('l');
gunpuen2 3:baff75bbb78e 112 }
gunpuen2 0:a8e057156263 113 printf("\n");
gunpuen2 0:a8e057156263 114 break;
gunpuen2 0:a8e057156263 115 }
gunpuen2 0:a8e057156263 116 }
gunpuen2 12:287c6a9560ca 117 if(enemy_ready == 0){
gunpuen2 12:287c6a9560ca 118 printf("Wait for enemy\n");
gunpuen2 12:287c6a9560ca 119 while(1){if(bt.readable()){if(bt.getc()=='l'){break;}}}
gunpuen2 12:287c6a9560ca 120 state = 0;
gunpuen2 12:287c6a9560ca 121 }else{state = 1;}
gunpuen2 15:5902cf545449 122 wait(2);
gunpuen2 12:287c6a9560ca 123 while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play
gunpuen2 1:6157c77da3d5 124 if(state == 0){
gunpuen2 6:7f99124272d9 125 atk_enemy();
gunpuen2 6:7f99124272d9 126 printf("This is enemy_platform.\n");
gunpuen2 6:7f99124272d9 127 print_enemy_platform();
gunpuen2 14:8abed4026326 128 if(IamWin()==1){printf("You win!\n");}else{state = 1;}
gunpuen2 0:a8e057156263 129 }else{
gunpuen2 6:7f99124272d9 130 enemy_atk();
gunpuen2 6:7f99124272d9 131 printf("This is atk_platform.\n");
gunpuen2 6:7f99124272d9 132 print_atk_platform();
gunpuen2 7:e35ba3ddb9af 133 printf("This is ship_destroyed. : ");
gunpuen2 7:e35ba3ddb9af 134 print_ship_destroyed();
gunpuen2 14:8abed4026326 135 if(TellUrEnemy_uWin()==1){
gunpuen2 14:8abed4026326 136 while(uli<100){
gunpuen2 14:8abed4026326 137 bt.putc('q');
gunpuen2 14:8abed4026326 138 uli++;
gunpuen2 14:8abed4026326 139 }
gunpuen2 7:e35ba3ddb9af 140 printf("BOO!! You lost!");
gunpuen2 7:e35ba3ddb9af 141 break;
gunpuen2 7:e35ba3ddb9af 142 }else{state = 0;}
gunpuen2 0:a8e057156263 143 }
gunpuen2 0:a8e057156263 144 }
gunpuen2 0:a8e057156263 145 }
gunpuen2 0:a8e057156263 146
gunpuen2 4:8a86bacddcac 147 /******************************************************************************/
gunpuen2 4:8a86bacddcac 148 /* */
gunpuen2 4:8a86bacddcac 149 /* */
gunpuen2 4:8a86bacddcac 150 /* Another function */
gunpuen2 4:8a86bacddcac 151 /* */
gunpuen2 4:8a86bacddcac 152 /* */
gunpuen2 4:8a86bacddcac 153 /******************************************************************************/
gunpuen2 0:a8e057156263 154
gunpuen2 0:a8e057156263 155 char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right.
gunpuen2 8:a8d39b5d17ab 156
gunpuen2 7:e35ba3ddb9af 157 if(ifUrJirat==1){
gunpuen2 7:e35ba3ddb9af 158 if(jx < 14.0){ return 'a';}
gunpuen2 7:e35ba3ddb9af 159 else if(jx > 1010.0){return 'd';}
gunpuen2 7:e35ba3ddb9af 160 else if(jy < 14.0){ return 'w';}
gunpuen2 7:e35ba3ddb9af 161 else if(jy > 1010.0){return 's';}
gunpuen2 7:e35ba3ddb9af 162 else{return NULL;}
gunpuen2 8:a8d39b5d17ab 163 }else{
gunpuen2 9:102af5021c6c 164 if(jx < 14.0){ return 'd';}
gunpuen2 9:102af5021c6c 165 else if(jx > 1010.0){return 'a';}
gunpuen2 9:102af5021c6c 166 else if(jy < 14.0){ return 's';}
gunpuen2 9:102af5021c6c 167 else if(jy > 1010.0){return 'w';}
gunpuen2 7:e35ba3ddb9af 168 else{return NULL;}
gunpuen2 0:a8e057156263 169 }
gunpuen2 0:a8e057156263 170 }
gunpuen2 0:a8e057156263 171 int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre.
gunpuen2 0:a8e057156263 172 if(jx>=450.0&&jx<=600.0){
gunpuen2 0:a8e057156263 173 if(jy>=450.0&&jy<=600.0){
gunpuen2 0:a8e057156263 174 return 1;
gunpuen2 0:a8e057156263 175 }else{
gunpuen2 0:a8e057156263 176 return 0;
gunpuen2 0:a8e057156263 177 }
gunpuen2 0:a8e057156263 178 }else{
gunpuen2 0:a8e057156263 179 return 0;
gunpuen2 0:a8e057156263 180 }
gunpuen2 0:a8e057156263 181 }
gunpuen2 0:a8e057156263 182 void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick).
gunpuen2 0:a8e057156263 183 switch(M){
gunpuen2 0:a8e057156263 184 case 'a':
gunpuen2 0:a8e057156263 185 if(select_Position%10!=0){select_Position = select_Position - 1;}
gunpuen2 0:a8e057156263 186 break;
gunpuen2 0:a8e057156263 187 case 'd':
gunpuen2 0:a8e057156263 188 if(select_Position%10!=7){select_Position = select_Position + 1;}
gunpuen2 0:a8e057156263 189 break;
gunpuen2 0:a8e057156263 190 case 'w':
gunpuen2 0:a8e057156263 191 if(select_Position/10!=0){select_Position = select_Position - 10;}
gunpuen2 0:a8e057156263 192 break;
gunpuen2 0:a8e057156263 193 case 's':
gunpuen2 0:a8e057156263 194 if(select_Position/10!=7){select_Position = select_Position + 10;}
gunpuen2 0:a8e057156263 195 break;
gunpuen2 0:a8e057156263 196 }
gunpuen2 0:a8e057156263 197 }
gunpuen2 15:5902cf545449 198 int protect_ship(int head_position,int ship_type){
gunpuen2 15:5902cf545449 199 int x,y = 0;
gunpuen2 15:5902cf545449 200
gunpuen2 15:5902cf545449 201 y = head_position/10;
gunpuen2 15:5902cf545449 202 x = head_position%10;
gunpuen2 15:5902cf545449 203
gunpuen2 15:5902cf545449 204 if(ship_type==4){if((y+3)>7){return 0;}else{return 1;}}
gunpuen2 15:5902cf545449 205 else if(ship_type==3){if((x+2)>7){return 0;}else{return 1;}}
gunpuen2 15:5902cf545449 206 else if(ship_type==2){if((y+1)>7){return 0;}else{return 1;}}
gunpuen2 15:5902cf545449 207 else{return 1;}
gunpuen2 15:5902cf545449 208
gunpuen2 15:5902cf545449 209 }
gunpuen2 15:5902cf545449 210 int get_ship_platform(int input_position,int ship_type){ //-----------------------Plot ship in ship area.
gunpuen2 0:a8e057156263 211 int i,x,y = 0;
gunpuen2 0:a8e057156263 212 int error = 0;
gunpuen2 0:a8e057156263 213
gunpuen2 0:a8e057156263 214 y = input_position/10;
gunpuen2 0:a8e057156263 215 x = input_position%10;
gunpuen2 0:a8e057156263 216
gunpuen2 0:a8e057156263 217 for(i=0;i<ship_type;i++){
gunpuen2 0:a8e057156263 218 if(check_correctPosition(y,x) == 1){
gunpuen2 0:a8e057156263 219 if(ship_type == 4){ship4[i] = (y*10)+x;}
gunpuen2 0:a8e057156263 220 else if(ship_type == 3){ship3[i] = (y*10)+x;}
gunpuen2 0:a8e057156263 221 else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;}
gunpuen2 0:a8e057156263 222 else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;}
gunpuen2 16:3fac68a7fd7c 223 }else{error = 1;break;}
gunpuen2 16:3fac68a7fd7c 224 if(ship_type%2 == 0){y++;}else{x++;}
gunpuen2 0:a8e057156263 225 }
gunpuen2 16:3fac68a7fd7c 226 if(ship_type==2){check_ship2++;}
gunpuen2 16:3fac68a7fd7c 227 if(error == 1){return 0;}else{
gunpuen2 0:a8e057156263 228 y = input_position/10;
gunpuen2 0:a8e057156263 229 x = input_position%10;
gunpuen2 0:a8e057156263 230 for(i=0;i<ship_type;i++){
gunpuen2 1:6157c77da3d5 231 ship_platform[y][x] = ship_type;
gunpuen2 16:3fac68a7fd7c 232 if(ship_type%2 == 0){y++;}else{x++;}
gunpuen2 0:a8e057156263 233 }
gunpuen2 0:a8e057156263 234 return 1;
gunpuen2 0:a8e057156263 235 }
gunpuen2 0:a8e057156263 236 }
gunpuen2 0:a8e057156263 237 int check_correctPosition(int X,int Y){
gunpuen2 0:a8e057156263 238 if(X<=7){
gunpuen2 0:a8e057156263 239 if(Y<=7){
gunpuen2 16:3fac68a7fd7c 240 if(ship_platform[X][Y] == 0){return 1;}else{return 0;}
gunpuen2 16:3fac68a7fd7c 241 }else{return 0;}
gunpuen2 16:3fac68a7fd7c 242 }else{return 0;}
gunpuen2 0:a8e057156263 243 }
gunpuen2 6:7f99124272d9 244 void print_ship_platform(){
gunpuen2 0:a8e057156263 245 for(int i=0;i<8;i++){
gunpuen2 0:a8e057156263 246 for(int j=0;j<8;j++){
gunpuen2 1:6157c77da3d5 247 printf("| %d |",ship_platform[i][j]);
gunpuen2 0:a8e057156263 248 }
gunpuen2 0:a8e057156263 249 printf("\n");
gunpuen2 0:a8e057156263 250 }
gunpuen2 0:a8e057156263 251 }
gunpuen2 6:7f99124272d9 252 void print_enemy_platform(){
gunpuen2 6:7f99124272d9 253 for(int i=0;i<8;i++){
gunpuen2 6:7f99124272d9 254 for(int j=0;j<8;j++){
gunpuen2 6:7f99124272d9 255 printf("| %d |",enemy_platform[i][j]);
gunpuen2 6:7f99124272d9 256 }
gunpuen2 6:7f99124272d9 257 printf("\n");
gunpuen2 6:7f99124272d9 258 }
gunpuen2 6:7f99124272d9 259 }
gunpuen2 6:7f99124272d9 260 void print_atk_platform(){
gunpuen2 6:7f99124272d9 261 for(int i=0;i<8;i++){
gunpuen2 6:7f99124272d9 262 for(int j=0;j<8;j++){
gunpuen2 6:7f99124272d9 263 printf("| %d |",atk_platform[i][j]);
gunpuen2 6:7f99124272d9 264 }
gunpuen2 6:7f99124272d9 265 printf("\n");
gunpuen2 6:7f99124272d9 266 }
gunpuen2 6:7f99124272d9 267 }
gunpuen2 7:e35ba3ddb9af 268 void print_ship_destroyed(){
gunpuen2 8:a8d39b5d17ab 269 printf("{");
gunpuen2 7:e35ba3ddb9af 270 for(int i=0;i<8;i++){
gunpuen2 7:e35ba3ddb9af 271 printf("%d",ship_destroyed[i]);
gunpuen2 7:e35ba3ddb9af 272 if(i!=14){printf(",");}
gunpuen2 7:e35ba3ddb9af 273 }
gunpuen2 7:e35ba3ddb9af 274 printf("}\n");
gunpuen2 7:e35ba3ddb9af 275 }
gunpuen2 0:a8e057156263 276 void check_ready(){
gunpuen2 16:3fac68a7fd7c 277 if(bt.readable()){enemy_ready = 1;}
gunpuen2 1:6157c77da3d5 278 }
gunpuen2 1:6157c77da3d5 279 char posit2char(int posit){
gunpuen2 1:6157c77da3d5 280 int as = posit+48;
gunpuen2 1:6157c77da3d5 281 char ch = as;
gunpuen2 1:6157c77da3d5 282 return ch ;
gunpuen2 1:6157c77da3d5 283 }
gunpuen2 1:6157c77da3d5 284
gunpuen2 1:6157c77da3d5 285 int char2posit(char cha){
gunpuen2 1:6157c77da3d5 286 int as = cha;
gunpuen2 1:6157c77da3d5 287 as = as-48;
gunpuen2 1:6157c77da3d5 288 return as;
gunpuen2 1:6157c77da3d5 289 }
gunpuen2 15:5902cf545449 290 void get_atk_platform(int atkpos){
gunpuen2 4:8a86bacddcac 291 int i,j,x,y = 0;
gunpuen2 1:6157c77da3d5 292
gunpuen2 7:e35ba3ddb9af 293 x = atkpos/10;
gunpuen2 7:e35ba3ddb9af 294 y = atkpos%10;
gunpuen2 1:6157c77da3d5 295
gunpuen2 2:d93832fe8deb 296 if(ship_platform[x][y]!=0 and atk_platform[x][y]==0){
gunpuen2 1:6157c77da3d5 297 atk_platform[x][y] = ship_platform[x][y];
gunpuen2 6:7f99124272d9 298 bt.putc(posit2char(atk_platform[x][y]));
gunpuen2 9:102af5021c6c 299 ship_underATK[check_ship_underATK] = (x*10)+y;
gunpuen2 2:d93832fe8deb 300 if(atk_platform[x][y]==4){
gunpuen2 2:d93832fe8deb 301 for(i=0;i<4;i++){
gunpuen2 2:d93832fe8deb 302 for(j=0;j<15;j++){
gunpuen2 16:3fac68a7fd7c 303 if(ship4[i]==ship_underATK[j]){ship_destroyed[0]++;}
gunpuen2 2:d93832fe8deb 304 }
gunpuen2 2:d93832fe8deb 305 }
gunpuen2 2:d93832fe8deb 306 }else if(atk_platform[x][y]==3){
gunpuen2 2:d93832fe8deb 307 for(i=0;i<3;i++){
gunpuen2 2:d93832fe8deb 308 for(j=0;j<15;j++){
gunpuen2 16:3fac68a7fd7c 309 if(ship3[i]==ship_underATK[j]){ship_destroyed[1]++;}
gunpuen2 2:d93832fe8deb 310 }
gunpuen2 2:d93832fe8deb 311 }
gunpuen2 2:d93832fe8deb 312 }else if(atk_platform[x][y]==2){
gunpuen2 2:d93832fe8deb 313 for(i=0;i<4;i++){
gunpuen2 2:d93832fe8deb 314 for(j=0;j<15;j++){
gunpuen2 16:3fac68a7fd7c 315 if(ship2[0][i]==ship_underATK[j]){ship_destroyed[2]++;}
gunpuen2 16:3fac68a7fd7c 316 if(ship2[1][i]==ship_underATK[j]){ship_destroyed[3]++;}
gunpuen2 2:d93832fe8deb 317 }
gunpuen2 2:d93832fe8deb 318 }
gunpuen2 2:d93832fe8deb 319 }else if(atk_platform[x][y]==1){
gunpuen2 2:d93832fe8deb 320 for(i=0;i<4;i++){
gunpuen2 2:d93832fe8deb 321 for(j=0;j<15;j++){
gunpuen2 2:d93832fe8deb 322 if(ship3[i]==ship_underATK[j]){
gunpuen2 2:d93832fe8deb 323 ship_destroyed[4+i]++;
gunpuen2 4:8a86bacddcac 324 atk_platform[x][y] = 11;
gunpuen2 2:d93832fe8deb 325 }
gunpuen2 2:d93832fe8deb 326 }
gunpuen2 2:d93832fe8deb 327 }
gunpuen2 2:d93832fe8deb 328 }
gunpuen2 1:6157c77da3d5 329 }else{
gunpuen2 1:6157c77da3d5 330 atk_platform[x][y] = 8;
gunpuen2 6:7f99124272d9 331 bt.putc(posit2char(atk_platform[x][y]));
gunpuen2 0:a8e057156263 332 }
gunpuen2 0:a8e057156263 333 }
gunpuen2 4:8a86bacddcac 334 void mark_ship_destroyed(){
gunpuen2 4:8a86bacddcac 335 int i,j,k = 0;
gunpuen2 0:a8e057156263 336
gunpuen2 11:305164107874 337 if(ship_destroyed[0]==10){
gunpuen2 4:8a86bacddcac 338 for(i=0;i<8;i++){
gunpuen2 4:8a86bacddcac 339 for(j=0;j<8;j++){
gunpuen2 16:3fac68a7fd7c 340 if(atk_platform[i][j]==4){atk_platform[i][j] = 11;}
gunpuen2 4:8a86bacddcac 341 }
gunpuen2 4:8a86bacddcac 342 }
gunpuen2 13:eee9af3b4308 343 }
gunpuen2 13:eee9af3b4308 344 if(ship_destroyed[1]==6){
gunpuen2 4:8a86bacddcac 345 for(i=0;i<8;i++){
gunpuen2 4:8a86bacddcac 346 for(j=0;j<8;j++){
gunpuen2 16:3fac68a7fd7c 347 if(atk_platform[i][j]==3){atk_platform[i][j] = 11;}
gunpuen2 4:8a86bacddcac 348 }
gunpuen2 4:8a86bacddcac 349 }
gunpuen2 13:eee9af3b4308 350 }
gunpuen2 13:eee9af3b4308 351 if(ship_destroyed[2]==3){
gunpuen2 4:8a86bacddcac 352 for(k=0;k<2;k++){
gunpuen2 4:8a86bacddcac 353 i = ship2[0][k]/10;
gunpuen2 4:8a86bacddcac 354 j = ship2[0][k]%10;
gunpuen2 4:8a86bacddcac 355 atk_platform[i][j] = 11;
gunpuen2 4:8a86bacddcac 356 }
gunpuen2 13:eee9af3b4308 357 }
gunpuen2 13:eee9af3b4308 358 if(ship_destroyed[3]==3){
gunpuen2 4:8a86bacddcac 359 for(k=0;k<2;k++){
gunpuen2 4:8a86bacddcac 360 i = ship2[1][k]/10;
gunpuen2 4:8a86bacddcac 361 j = ship2[1][k]%10;
gunpuen2 4:8a86bacddcac 362 atk_platform[i][j] = 11;
gunpuen2 4:8a86bacddcac 363 }
gunpuen2 4:8a86bacddcac 364 }
gunpuen2 6:7f99124272d9 365 }
gunpuen2 6:7f99124272d9 366 void atk_enemy(){
gunpuen2 6:7f99124272d9 367 float Vx;
gunpuen2 6:7f99124272d9 368 float Vy;
gunpuen2 7:e35ba3ddb9af 369 int SW,iVxVy,iSW,n = 0;
gunpuen2 7:e35ba3ddb9af 370 int iPrintUturn = 0;
gunpuen2 6:7f99124272d9 371 char m;
gunpuen2 6:7f99124272d9 372
gunpuen2 14:8abed4026326 373 select_Position = 0;
gunpuen2 6:7f99124272d9 374 while(1){
gunpuen2 6:7f99124272d9 375 if(iPrintUturn==0){
gunpuen2 6:7f99124272d9 376 printf("Your turn.\n");
gunpuen2 6:7f99124272d9 377 iPrintUturn = 1;
gunpuen2 6:7f99124272d9 378 }
gunpuen2 6:7f99124272d9 379 Vx = VRx.read() * 1024;
gunpuen2 6:7f99124272d9 380 Vy = VRy.read() * 1024;
gunpuen2 6:7f99124272d9 381 SW = Button.read();
gunpuen2 6:7f99124272d9 382 m = check_joys(Vx,Vy);
gunpuen2 6:7f99124272d9 383 n = check_joys_int(Vx,Vy);
gunpuen2 16:3fac68a7fd7c 384 if(n == 1){ iVxVy = 0;}
gunpuen2 6:7f99124272d9 385 if(SW == 1){
gunpuen2 6:7f99124272d9 386 iSW = 0;
gunpuen2 6:7f99124272d9 387 if(bt.readable()){
gunpuen2 6:7f99124272d9 388 enemy_platform[select_Position/10][select_Position%10] = char2posit(bt.getc());
gunpuen2 6:7f99124272d9 389 break;
gunpuen2 6:7f99124272d9 390 }
gunpuen2 6:7f99124272d9 391 }
gunpuen2 6:7f99124272d9 392 if(m!=NULL && iVxVy==0){
gunpuen2 6:7f99124272d9 393 Select_Position(m);
gunpuen2 6:7f99124272d9 394 printf("select_Position = %d\n",select_Position);
gunpuen2 6:7f99124272d9 395 bt.putc(posit2char(select_Position));
gunpuen2 6:7f99124272d9 396 iVxVy = 1;
gunpuen2 6:7f99124272d9 397 }
gunpuen2 6:7f99124272d9 398 if(SW==0 && iSW==0){
gunpuen2 6:7f99124272d9 399 bt.putc('B');
gunpuen2 6:7f99124272d9 400 printf("You attacking at position %d\n",select_Position);
gunpuen2 6:7f99124272d9 401 iSW = 1;
gunpuen2 6:7f99124272d9 402 }
gunpuen2 6:7f99124272d9 403 }
gunpuen2 6:7f99124272d9 404 }
gunpuen2 6:7f99124272d9 405 void enemy_atk(){
gunpuen2 6:7f99124272d9 406 int data_fromEnemy;
gunpuen2 6:7f99124272d9 407 printf("Wait for enemy attack.\n");
gunpuen2 13:eee9af3b4308 408 select_Position = 0;
gunpuen2 6:7f99124272d9 409 while(1){
gunpuen2 6:7f99124272d9 410 if(bt.readable()){
gunpuen2 6:7f99124272d9 411 data_fromEnemy = char2posit(bt.getc());
gunpuen2 6:7f99124272d9 412 if(data_fromEnemy!=18){
gunpuen2 6:7f99124272d9 413 select_Position = data_fromEnemy;
gunpuen2 7:e35ba3ddb9af 414 printf("enemy select_Position = %d\n",select_Position);
gunpuen2 6:7f99124272d9 415 }else{
gunpuen2 6:7f99124272d9 416 ATKposition = select_Position;
gunpuen2 6:7f99124272d9 417 break;
gunpuen2 6:7f99124272d9 418 }
gunpuen2 6:7f99124272d9 419 }
gunpuen2 6:7f99124272d9 420 }
gunpuen2 6:7f99124272d9 421 printf("Enemy attack at your area %d\n",ATKposition);
gunpuen2 15:5902cf545449 422 get_atk_platform(ATKposition);
gunpuen2 10:b8d45f9b00bf 423 check_ship_underATK++;
gunpuen2 6:7f99124272d9 424 mark_ship_destroyed();
gunpuen2 7:e35ba3ddb9af 425 }
gunpuen2 7:e35ba3ddb9af 426 int TellUrEnemy_uWin(){
gunpuen2 7:e35ba3ddb9af 427 int sub_end = 0;
gunpuen2 7:e35ba3ddb9af 428
gunpuen2 7:e35ba3ddb9af 429 for(int i=0;i<15;i++){
gunpuen2 11:305164107874 430 if(ship_underATK[i]!=9){sub_end++;}
gunpuen2 7:e35ba3ddb9af 431 }
gunpuen2 8:a8d39b5d17ab 432 if(sub_end==15){return 1;}else{return 0;}
gunpuen2 7:e35ba3ddb9af 433 }
gunpuen2 7:e35ba3ddb9af 434 int IamWin(){
gunpuen2 8:a8d39b5d17ab 435 if(bt.readable()){if(bt.getc()=='q'){return 1;}else{return 0;}}else{return 0;}
gunpuen2 4:8a86bacddcac 436 }