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