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