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