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