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