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