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