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