Nasrun Hayeeyama
/
project_ShipDot
Nasrun
Fork of project_ShipDot by
main.cpp@15:5902cf545449, 2016-12-03 (annotated)
- Committer:
- gunpuen2
- Date:
- Sat Dec 03 11:58:15 2016 +0000
- Revision:
- 15:5902cf545449
- Parent:
- 14:8abed4026326
- Child:
- 16:3fac68a7fd7c
Add function for Nasarun function (18:58 || 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 | 7:e35ba3ddb9af | 25 | void print_ship_destroyed(); |
gunpuen2 | 6:7f99124272d9 | 26 | void print_enemy_platform(); |
gunpuen2 | 6:7f99124272d9 | 27 | void print_ship_platform(); |
gunpuen2 | 6:7f99124272d9 | 28 | void print_atk_platform(); |
gunpuen2 | 4:8a86bacddcac | 29 | void mark_ship_destroyed(); |
gunpuen2 | 0:a8e057156263 | 30 | void Select_Position(char M); |
gunpuen2 | 15:5902cf545449 | 31 | void get_atk_platform(int atkpos); |
gunpuen2 | 7:e35ba3ddb9af | 32 | void check_end(); |
gunpuen2 | 0:a8e057156263 | 33 | |
gunpuen2 | 7:e35ba3ddb9af | 34 | // Setting |
gunpuen2 | 7:e35ba3ddb9af | 35 | int ifUrJirat = 1; |
gunpuen2 | 0:a8e057156263 | 36 | // Initial variable. |
gunpuen2 | 2:d93832fe8deb | 37 | int check_ship_underATK = 0; |
gunpuen2 | 0:a8e057156263 | 38 | int select_Position = 0; |
gunpuen2 | 0:a8e057156263 | 39 | int ATKposition = 0; |
gunpuen2 | 1:6157c77da3d5 | 40 | int enemy_ready = 0; |
gunpuen2 | 2:d93832fe8deb | 41 | int check_ship1 = 0; |
gunpuen2 | 2:d93832fe8deb | 42 | int check_ship2 = 0; |
gunpuen2 | 0:a8e057156263 | 43 | int iWaitATK = 0; |
gunpuen2 | 0:a8e057156263 | 44 | int state = 0; |
gunpuen2 | 2:d93832fe8deb | 45 | int ship_SelectType[8] = {4,3,2,2,1,1,1,1}; |
gunpuen2 | 6:7f99124272d9 | 46 | int enemy_platform[8][8] = {{0}}; |
gunpuen2 | 4:8a86bacddcac | 47 | int ship_platform[8][8] = {{0}}; //______________________________________________This is ship area. |
gunpuen2 | 1:6157c77da3d5 | 48 | int atk_platform[8][8] = {{0}}; |
gunpuen2 | 10:b8d45f9b00bf | 49 | int ship_underATK[15] = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9}; |
gunpuen2 | 2:d93832fe8deb | 50 | int ship_destroyed[8] = {0}; |
gunpuen2 | 0:a8e057156263 | 51 | int ship4[4] = {0}; |
gunpuen2 | 0:a8e057156263 | 52 | int ship3[3] = {0}; |
gunpuen2 | 0:a8e057156263 | 53 | int ship2[2][2] = {{0}}; |
gunpuen2 | 0:a8e057156263 | 54 | int ship1[4] = {0}; |
gunpuen2 | 0:a8e057156263 | 55 | |
gunpuen2 | 0:a8e057156263 | 56 | int main(){ //-------------------------------------------------------------------Main Function. |
gunpuen2 | 15:5902cf545449 | 57 | bt.baud(9600); |
gunpuen2 | 0:a8e057156263 | 58 | float Vx; |
gunpuen2 | 0:a8e057156263 | 59 | float Vy; |
gunpuen2 | 0:a8e057156263 | 60 | int SW,iVxVy,iSW,n; |
gunpuen2 | 0:a8e057156263 | 61 | int iShipType = 0; |
gunpuen2 | 3:baff75bbb78e | 62 | unsigned long int uli = 0; |
gunpuen2 | 0:a8e057156263 | 63 | char m; |
gunpuen2 | 14:8abed4026326 | 64 | |
gunpuen2 | 0:a8e057156263 | 65 | while(1) { |
gunpuen2 | 0:a8e057156263 | 66 | if(iShipType<8){ |
gunpuen2 | 0:a8e057156263 | 67 | Vx = VRx.read() * 1024; |
gunpuen2 | 0:a8e057156263 | 68 | Vy = VRy.read() * 1024; |
gunpuen2 | 0:a8e057156263 | 69 | SW = Button.read(); |
gunpuen2 | 0:a8e057156263 | 70 | m = check_joys(Vx,Vy); |
gunpuen2 | 0:a8e057156263 | 71 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 0:a8e057156263 | 72 | if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. |
gunpuen2 | 0:a8e057156263 | 73 | iVxVy = 0; |
gunpuen2 | 0:a8e057156263 | 74 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 75 | } |
gunpuen2 | 0:a8e057156263 | 76 | if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. |
gunpuen2 | 0:a8e057156263 | 77 | iSW = 0; |
gunpuen2 | 0:a8e057156263 | 78 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 79 | } |
gunpuen2 | 0:a8e057156263 | 80 | if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. |
gunpuen2 | 15:5902cf545449 | 81 | if(protect_ship(select_Position,ship_SelectType[iShipType]!=0)){ |
gunpuen2 | 0:a8e057156263 | 82 | Select_Position(m); |
gunpuen2 | 15:5902cf545449 | 83 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 15:5902cf545449 | 84 | }else{printf("Not allow position.\n");} |
gunpuen2 | 0:a8e057156263 | 85 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 86 | iVxVy = 1; |
gunpuen2 | 3:baff75bbb78e | 87 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 88 | } |
gunpuen2 | 0:a8e057156263 | 89 | if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. |
gunpuen2 | 0:a8e057156263 | 90 | printf("Wait!\n"); |
gunpuen2 | 0:a8e057156263 | 91 | check_ready(); |
gunpuen2 | 15:5902cf545449 | 92 | if(get_ship_platform(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function get_ship_platform. |
gunpuen2 | 0:a8e057156263 | 93 | printf("Please input correct position\n"); |
gunpuen2 | 0:a8e057156263 | 94 | }else{ |
gunpuen2 | 0:a8e057156263 | 95 | iShipType++; |
gunpuen2 | 0:a8e057156263 | 96 | } |
gunpuen2 | 0:a8e057156263 | 97 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 98 | iSW = 1; |
gunpuen2 | 6:7f99124272d9 | 99 | print_ship_platform(); |
gunpuen2 | 3:baff75bbb78e | 100 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 101 | } |
gunpuen2 | 0:a8e057156263 | 102 | }else{ |
gunpuen2 | 3:baff75bbb78e | 103 | if(enemy_ready!=1){ |
gunpuen2 | 3:baff75bbb78e | 104 | printf("sent E\n"); |
gunpuen2 | 12:287c6a9560ca | 105 | while(uli<50){ |
gunpuen2 | 4:8a86bacddcac | 106 | bt.printf("E"); |
gunpuen2 | 3:baff75bbb78e | 107 | uli++; |
gunpuen2 | 3:baff75bbb78e | 108 | } |
gunpuen2 | 12:287c6a9560ca | 109 | }else{ |
gunpuen2 | 12:287c6a9560ca | 110 | bt.putc('l'); |
gunpuen2 | 3:baff75bbb78e | 111 | } |
gunpuen2 | 0:a8e057156263 | 112 | printf("\n"); |
gunpuen2 | 0:a8e057156263 | 113 | break; |
gunpuen2 | 0:a8e057156263 | 114 | } |
gunpuen2 | 0:a8e057156263 | 115 | } |
gunpuen2 | 12:287c6a9560ca | 116 | if(enemy_ready == 0){ |
gunpuen2 | 12:287c6a9560ca | 117 | printf("Wait for enemy\n"); |
gunpuen2 | 12:287c6a9560ca | 118 | while(1){if(bt.readable()){if(bt.getc()=='l'){break;}}} |
gunpuen2 | 12:287c6a9560ca | 119 | state = 0; |
gunpuen2 | 12:287c6a9560ca | 120 | }else{state = 1;} |
gunpuen2 | 15:5902cf545449 | 121 | wait(2); |
gunpuen2 | 12:287c6a9560ca | 122 | while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play |
gunpuen2 | 1:6157c77da3d5 | 123 | if(state == 0){ |
gunpuen2 | 6:7f99124272d9 | 124 | atk_enemy(); |
gunpuen2 | 6:7f99124272d9 | 125 | printf("This is enemy_platform.\n"); |
gunpuen2 | 6:7f99124272d9 | 126 | print_enemy_platform(); |
gunpuen2 | 14:8abed4026326 | 127 | if(IamWin()==1){printf("You win!\n");}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 | 0:a8e057156263 | 182 | switch(M){ |
gunpuen2 | 0:a8e057156263 | 183 | case 'a': |
gunpuen2 | 0:a8e057156263 | 184 | if(select_Position%10!=0){select_Position = select_Position - 1;} |
gunpuen2 | 0:a8e057156263 | 185 | break; |
gunpuen2 | 0:a8e057156263 | 186 | case 'd': |
gunpuen2 | 0:a8e057156263 | 187 | if(select_Position%10!=7){select_Position = select_Position + 1;} |
gunpuen2 | 0:a8e057156263 | 188 | break; |
gunpuen2 | 0:a8e057156263 | 189 | case 'w': |
gunpuen2 | 0:a8e057156263 | 190 | if(select_Position/10!=0){select_Position = select_Position - 10;} |
gunpuen2 | 0:a8e057156263 | 191 | break; |
gunpuen2 | 0:a8e057156263 | 192 | case 's': |
gunpuen2 | 0:a8e057156263 | 193 | if(select_Position/10!=7){select_Position = select_Position + 10;} |
gunpuen2 | 0:a8e057156263 | 194 | break; |
gunpuen2 | 0:a8e057156263 | 195 | } |
gunpuen2 | 0:a8e057156263 | 196 | } |
gunpuen2 | 15:5902cf545449 | 197 | int protect_ship(int head_position,int ship_type){ |
gunpuen2 | 15:5902cf545449 | 198 | int x,y = 0; |
gunpuen2 | 15:5902cf545449 | 199 | |
gunpuen2 | 15:5902cf545449 | 200 | y = head_position/10; |
gunpuen2 | 15:5902cf545449 | 201 | x = head_position%10; |
gunpuen2 | 15:5902cf545449 | 202 | |
gunpuen2 | 15:5902cf545449 | 203 | if(ship_type==4){if((y+3)>7){return 0;}else{return 1;}} |
gunpuen2 | 15:5902cf545449 | 204 | else if(ship_type==3){if((x+2)>7){return 0;}else{return 1;}} |
gunpuen2 | 15:5902cf545449 | 205 | else if(ship_type==2){if((y+1)>7){return 0;}else{return 1;}} |
gunpuen2 | 15:5902cf545449 | 206 | else{return 1;} |
gunpuen2 | 15:5902cf545449 | 207 | |
gunpuen2 | 15:5902cf545449 | 208 | } |
gunpuen2 | 15:5902cf545449 | 209 | int get_ship_platform(int input_position,int ship_type){ //-----------------------Plot ship in ship area. |
gunpuen2 | 0:a8e057156263 | 210 | int i,x,y = 0; |
gunpuen2 | 0:a8e057156263 | 211 | int error = 0; |
gunpuen2 | 0:a8e057156263 | 212 | |
gunpuen2 | 0:a8e057156263 | 213 | y = input_position/10; |
gunpuen2 | 0:a8e057156263 | 214 | x = input_position%10; |
gunpuen2 | 0:a8e057156263 | 215 | |
gunpuen2 | 0:a8e057156263 | 216 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 0:a8e057156263 | 217 | if(check_correctPosition(y,x) == 1){ |
gunpuen2 | 0:a8e057156263 | 218 | if(ship_type == 4){ship4[i] = (y*10)+x;} |
gunpuen2 | 0:a8e057156263 | 219 | else if(ship_type == 3){ship3[i] = (y*10)+x;} |
gunpuen2 | 0:a8e057156263 | 220 | else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;} |
gunpuen2 | 0:a8e057156263 | 221 | else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;} |
gunpuen2 | 0:a8e057156263 | 222 | }else{ |
gunpuen2 | 0:a8e057156263 | 223 | error = 1; |
gunpuen2 | 0:a8e057156263 | 224 | break; |
gunpuen2 | 0:a8e057156263 | 225 | } |
gunpuen2 | 0:a8e057156263 | 226 | if(ship_type%2 == 0){ |
gunpuen2 | 0:a8e057156263 | 227 | y++; |
gunpuen2 | 0:a8e057156263 | 228 | }else{ |
gunpuen2 | 0:a8e057156263 | 229 | x++; |
gunpuen2 | 0:a8e057156263 | 230 | } |
gunpuen2 | 0:a8e057156263 | 231 | } |
gunpuen2 | 0:a8e057156263 | 232 | if(ship_type==2){ |
gunpuen2 | 0:a8e057156263 | 233 | check_ship2++; |
gunpuen2 | 0:a8e057156263 | 234 | } |
gunpuen2 | 0:a8e057156263 | 235 | if(error == 1){ |
gunpuen2 | 0:a8e057156263 | 236 | return 0; |
gunpuen2 | 0:a8e057156263 | 237 | }else{ |
gunpuen2 | 0:a8e057156263 | 238 | y = input_position/10; |
gunpuen2 | 0:a8e057156263 | 239 | x = input_position%10; |
gunpuen2 | 0:a8e057156263 | 240 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 1:6157c77da3d5 | 241 | ship_platform[y][x] = ship_type; |
gunpuen2 | 0:a8e057156263 | 242 | if(ship_type%2 == 0){ |
gunpuen2 | 0:a8e057156263 | 243 | y++; |
gunpuen2 | 0:a8e057156263 | 244 | }else{ |
gunpuen2 | 0:a8e057156263 | 245 | x++; |
gunpuen2 | 0:a8e057156263 | 246 | } |
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 | 1:6157c77da3d5 | 254 | if(ship_platform[X][Y] == 0){ |
gunpuen2 | 0:a8e057156263 | 255 | return 1; |
gunpuen2 | 0:a8e057156263 | 256 | }else{ |
gunpuen2 | 0:a8e057156263 | 257 | return 0; |
gunpuen2 | 0:a8e057156263 | 258 | } |
gunpuen2 | 1:6157c77da3d5 | 259 | }else{ |
gunpuen2 | 1:6157c77da3d5 | 260 | return 0; |
gunpuen2 | 0:a8e057156263 | 261 | } |
gunpuen2 | 0:a8e057156263 | 262 | }else{ |
gunpuen2 | 0:a8e057156263 | 263 | return 0; |
gunpuen2 | 0:a8e057156263 | 264 | } |
gunpuen2 | 0:a8e057156263 | 265 | } |
gunpuen2 | 6:7f99124272d9 | 266 | void print_ship_platform(){ |
gunpuen2 | 0:a8e057156263 | 267 | for(int i=0;i<8;i++){ |
gunpuen2 | 0:a8e057156263 | 268 | for(int j=0;j<8;j++){ |
gunpuen2 | 1:6157c77da3d5 | 269 | printf("| %d |",ship_platform[i][j]); |
gunpuen2 | 0:a8e057156263 | 270 | } |
gunpuen2 | 0:a8e057156263 | 271 | printf("\n"); |
gunpuen2 | 0:a8e057156263 | 272 | } |
gunpuen2 | 0:a8e057156263 | 273 | } |
gunpuen2 | 6:7f99124272d9 | 274 | void print_enemy_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 |",enemy_platform[i][j]); |
gunpuen2 | 6:7f99124272d9 | 278 | } |
gunpuen2 | 6:7f99124272d9 | 279 | printf("\n"); |
gunpuen2 | 6:7f99124272d9 | 280 | } |
gunpuen2 | 6:7f99124272d9 | 281 | } |
gunpuen2 | 6:7f99124272d9 | 282 | void print_atk_platform(){ |
gunpuen2 | 6:7f99124272d9 | 283 | for(int i=0;i<8;i++){ |
gunpuen2 | 6:7f99124272d9 | 284 | for(int j=0;j<8;j++){ |
gunpuen2 | 6:7f99124272d9 | 285 | printf("| %d |",atk_platform[i][j]); |
gunpuen2 | 6:7f99124272d9 | 286 | } |
gunpuen2 | 6:7f99124272d9 | 287 | printf("\n"); |
gunpuen2 | 6:7f99124272d9 | 288 | } |
gunpuen2 | 6:7f99124272d9 | 289 | } |
gunpuen2 | 7:e35ba3ddb9af | 290 | void print_ship_destroyed(){ |
gunpuen2 | 8:a8d39b5d17ab | 291 | printf("{"); |
gunpuen2 | 7:e35ba3ddb9af | 292 | for(int i=0;i<8;i++){ |
gunpuen2 | 7:e35ba3ddb9af | 293 | printf("%d",ship_destroyed[i]); |
gunpuen2 | 7:e35ba3ddb9af | 294 | if(i!=14){printf(",");} |
gunpuen2 | 7:e35ba3ddb9af | 295 | } |
gunpuen2 | 7:e35ba3ddb9af | 296 | printf("}\n"); |
gunpuen2 | 7:e35ba3ddb9af | 297 | } |
gunpuen2 | 0:a8e057156263 | 298 | void check_ready(){ |
gunpuen2 | 0:a8e057156263 | 299 | if(bt.readable()){ |
gunpuen2 | 1:6157c77da3d5 | 300 | enemy_ready = 1; |
gunpuen2 | 1:6157c77da3d5 | 301 | } |
gunpuen2 | 1:6157c77da3d5 | 302 | } |
gunpuen2 | 1:6157c77da3d5 | 303 | char posit2char(int posit){ |
gunpuen2 | 1:6157c77da3d5 | 304 | int as = posit+48; |
gunpuen2 | 1:6157c77da3d5 | 305 | char ch = as; |
gunpuen2 | 1:6157c77da3d5 | 306 | return ch ; |
gunpuen2 | 1:6157c77da3d5 | 307 | } |
gunpuen2 | 1:6157c77da3d5 | 308 | |
gunpuen2 | 1:6157c77da3d5 | 309 | int char2posit(char cha){ |
gunpuen2 | 1:6157c77da3d5 | 310 | int as = cha; |
gunpuen2 | 1:6157c77da3d5 | 311 | as = as-48; |
gunpuen2 | 1:6157c77da3d5 | 312 | return as; |
gunpuen2 | 1:6157c77da3d5 | 313 | } |
gunpuen2 | 15:5902cf545449 | 314 | void get_atk_platform(int atkpos){ |
gunpuen2 | 4:8a86bacddcac | 315 | int i,j,x,y = 0; |
gunpuen2 | 1:6157c77da3d5 | 316 | |
gunpuen2 | 7:e35ba3ddb9af | 317 | x = atkpos/10; |
gunpuen2 | 7:e35ba3ddb9af | 318 | y = atkpos%10; |
gunpuen2 | 1:6157c77da3d5 | 319 | |
gunpuen2 | 2:d93832fe8deb | 320 | if(ship_platform[x][y]!=0 and atk_platform[x][y]==0){ |
gunpuen2 | 1:6157c77da3d5 | 321 | atk_platform[x][y] = ship_platform[x][y]; |
gunpuen2 | 6:7f99124272d9 | 322 | bt.putc(posit2char(atk_platform[x][y])); |
gunpuen2 | 9:102af5021c6c | 323 | ship_underATK[check_ship_underATK] = (x*10)+y; |
gunpuen2 | 2:d93832fe8deb | 324 | if(atk_platform[x][y]==4){ |
gunpuen2 | 2:d93832fe8deb | 325 | for(i=0;i<4;i++){ |
gunpuen2 | 2:d93832fe8deb | 326 | for(j=0;j<15;j++){ |
gunpuen2 | 2:d93832fe8deb | 327 | if(ship4[i]==ship_underATK[j]){ |
gunpuen2 | 2:d93832fe8deb | 328 | ship_destroyed[0]++; |
gunpuen2 | 2:d93832fe8deb | 329 | } |
gunpuen2 | 2:d93832fe8deb | 330 | } |
gunpuen2 | 2:d93832fe8deb | 331 | } |
gunpuen2 | 2:d93832fe8deb | 332 | }else if(atk_platform[x][y]==3){ |
gunpuen2 | 2:d93832fe8deb | 333 | for(i=0;i<3;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[1]++; |
gunpuen2 | 2:d93832fe8deb | 337 | } |
gunpuen2 | 2:d93832fe8deb | 338 | } |
gunpuen2 | 2:d93832fe8deb | 339 | } |
gunpuen2 | 2:d93832fe8deb | 340 | }else if(atk_platform[x][y]==2){ |
gunpuen2 | 2:d93832fe8deb | 341 | for(i=0;i<4;i++){ |
gunpuen2 | 2:d93832fe8deb | 342 | for(j=0;j<15;j++){ |
gunpuen2 | 2:d93832fe8deb | 343 | if(ship2[0][i]==ship_underATK[j]){ |
gunpuen2 | 2:d93832fe8deb | 344 | ship_destroyed[2]++; |
gunpuen2 | 2:d93832fe8deb | 345 | } |
gunpuen2 | 2:d93832fe8deb | 346 | if(ship2[1][i]==ship_underATK[j]){ |
gunpuen2 | 2:d93832fe8deb | 347 | ship_destroyed[3]++; |
gunpuen2 | 2:d93832fe8deb | 348 | } |
gunpuen2 | 2:d93832fe8deb | 349 | } |
gunpuen2 | 2:d93832fe8deb | 350 | } |
gunpuen2 | 2:d93832fe8deb | 351 | }else if(atk_platform[x][y]==1){ |
gunpuen2 | 2:d93832fe8deb | 352 | for(i=0;i<4;i++){ |
gunpuen2 | 2:d93832fe8deb | 353 | for(j=0;j<15;j++){ |
gunpuen2 | 2:d93832fe8deb | 354 | if(ship3[i]==ship_underATK[j]){ |
gunpuen2 | 2:d93832fe8deb | 355 | ship_destroyed[4+i]++; |
gunpuen2 | 4:8a86bacddcac | 356 | atk_platform[x][y] = 11; |
gunpuen2 | 2:d93832fe8deb | 357 | } |
gunpuen2 | 2:d93832fe8deb | 358 | } |
gunpuen2 | 2:d93832fe8deb | 359 | } |
gunpuen2 | 2:d93832fe8deb | 360 | } |
gunpuen2 | 1:6157c77da3d5 | 361 | }else{ |
gunpuen2 | 1:6157c77da3d5 | 362 | atk_platform[x][y] = 8; |
gunpuen2 | 6:7f99124272d9 | 363 | bt.putc(posit2char(atk_platform[x][y])); |
gunpuen2 | 0:a8e057156263 | 364 | } |
gunpuen2 | 0:a8e057156263 | 365 | } |
gunpuen2 | 4:8a86bacddcac | 366 | void mark_ship_destroyed(){ |
gunpuen2 | 4:8a86bacddcac | 367 | int i,j,k = 0; |
gunpuen2 | 0:a8e057156263 | 368 | |
gunpuen2 | 11:305164107874 | 369 | if(ship_destroyed[0]==10){ |
gunpuen2 | 4:8a86bacddcac | 370 | for(i=0;i<8;i++){ |
gunpuen2 | 4:8a86bacddcac | 371 | for(j=0;j<8;j++){ |
gunpuen2 | 4:8a86bacddcac | 372 | if(atk_platform[i][j]==4){ |
gunpuen2 | 4:8a86bacddcac | 373 | atk_platform[i][j] = 11; |
gunpuen2 | 4:8a86bacddcac | 374 | } |
gunpuen2 | 4:8a86bacddcac | 375 | } |
gunpuen2 | 4:8a86bacddcac | 376 | } |
gunpuen2 | 13:eee9af3b4308 | 377 | } |
gunpuen2 | 13:eee9af3b4308 | 378 | if(ship_destroyed[1]==6){ |
gunpuen2 | 4:8a86bacddcac | 379 | for(i=0;i<8;i++){ |
gunpuen2 | 4:8a86bacddcac | 380 | for(j=0;j<8;j++){ |
gunpuen2 | 4:8a86bacddcac | 381 | if(atk_platform[i][j]==3){ |
gunpuen2 | 4:8a86bacddcac | 382 | atk_platform[i][j] = 11; |
gunpuen2 | 4:8a86bacddcac | 383 | } |
gunpuen2 | 4:8a86bacddcac | 384 | } |
gunpuen2 | 4:8a86bacddcac | 385 | } |
gunpuen2 | 13:eee9af3b4308 | 386 | } |
gunpuen2 | 13:eee9af3b4308 | 387 | if(ship_destroyed[2]==3){ |
gunpuen2 | 4:8a86bacddcac | 388 | for(k=0;k<2;k++){ |
gunpuen2 | 4:8a86bacddcac | 389 | i = ship2[0][k]/10; |
gunpuen2 | 4:8a86bacddcac | 390 | j = ship2[0][k]%10; |
gunpuen2 | 4:8a86bacddcac | 391 | atk_platform[i][j] = 11; |
gunpuen2 | 4:8a86bacddcac | 392 | } |
gunpuen2 | 13:eee9af3b4308 | 393 | } |
gunpuen2 | 13:eee9af3b4308 | 394 | if(ship_destroyed[3]==3){ |
gunpuen2 | 4:8a86bacddcac | 395 | for(k=0;k<2;k++){ |
gunpuen2 | 4:8a86bacddcac | 396 | i = ship2[1][k]/10; |
gunpuen2 | 4:8a86bacddcac | 397 | j = ship2[1][k]%10; |
gunpuen2 | 4:8a86bacddcac | 398 | atk_platform[i][j] = 11; |
gunpuen2 | 4:8a86bacddcac | 399 | } |
gunpuen2 | 4:8a86bacddcac | 400 | } |
gunpuen2 | 6:7f99124272d9 | 401 | } |
gunpuen2 | 6:7f99124272d9 | 402 | void atk_enemy(){ |
gunpuen2 | 6:7f99124272d9 | 403 | float Vx; |
gunpuen2 | 6:7f99124272d9 | 404 | float Vy; |
gunpuen2 | 7:e35ba3ddb9af | 405 | int SW,iVxVy,iSW,n = 0; |
gunpuen2 | 7:e35ba3ddb9af | 406 | int iPrintUturn = 0; |
gunpuen2 | 6:7f99124272d9 | 407 | char m; |
gunpuen2 | 6:7f99124272d9 | 408 | |
gunpuen2 | 14:8abed4026326 | 409 | select_Position = 0; |
gunpuen2 | 6:7f99124272d9 | 410 | while(1){ |
gunpuen2 | 6:7f99124272d9 | 411 | if(iPrintUturn==0){ |
gunpuen2 | 6:7f99124272d9 | 412 | printf("Your turn.\n"); |
gunpuen2 | 6:7f99124272d9 | 413 | iPrintUturn = 1; |
gunpuen2 | 6:7f99124272d9 | 414 | } |
gunpuen2 | 6:7f99124272d9 | 415 | Vx = VRx.read() * 1024; |
gunpuen2 | 6:7f99124272d9 | 416 | Vy = VRy.read() * 1024; |
gunpuen2 | 6:7f99124272d9 | 417 | SW = Button.read(); |
gunpuen2 | 6:7f99124272d9 | 418 | m = check_joys(Vx,Vy); |
gunpuen2 | 6:7f99124272d9 | 419 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 6:7f99124272d9 | 420 | if(n == 1){ |
gunpuen2 | 6:7f99124272d9 | 421 | iVxVy = 0; |
gunpuen2 | 6:7f99124272d9 | 422 | } |
gunpuen2 | 6:7f99124272d9 | 423 | if(SW == 1){ |
gunpuen2 | 6:7f99124272d9 | 424 | iSW = 0; |
gunpuen2 | 6:7f99124272d9 | 425 | if(bt.readable()){ |
gunpuen2 | 6:7f99124272d9 | 426 | enemy_platform[select_Position/10][select_Position%10] = char2posit(bt.getc()); |
gunpuen2 | 6:7f99124272d9 | 427 | break; |
gunpuen2 | 6:7f99124272d9 | 428 | } |
gunpuen2 | 6:7f99124272d9 | 429 | } |
gunpuen2 | 6:7f99124272d9 | 430 | if(m!=NULL && iVxVy==0){ |
gunpuen2 | 6:7f99124272d9 | 431 | Select_Position(m); |
gunpuen2 | 6:7f99124272d9 | 432 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 6:7f99124272d9 | 433 | bt.putc(posit2char(select_Position)); |
gunpuen2 | 6:7f99124272d9 | 434 | iVxVy = 1; |
gunpuen2 | 6:7f99124272d9 | 435 | } |
gunpuen2 | 6:7f99124272d9 | 436 | if(SW==0 && iSW==0){ |
gunpuen2 | 6:7f99124272d9 | 437 | bt.putc('B'); |
gunpuen2 | 6:7f99124272d9 | 438 | printf("You attacking at position %d\n",select_Position); |
gunpuen2 | 6:7f99124272d9 | 439 | iSW = 1; |
gunpuen2 | 6:7f99124272d9 | 440 | } |
gunpuen2 | 6:7f99124272d9 | 441 | } |
gunpuen2 | 6:7f99124272d9 | 442 | } |
gunpuen2 | 6:7f99124272d9 | 443 | void enemy_atk(){ |
gunpuen2 | 6:7f99124272d9 | 444 | int data_fromEnemy; |
gunpuen2 | 6:7f99124272d9 | 445 | printf("Wait for enemy attack.\n"); |
gunpuen2 | 13:eee9af3b4308 | 446 | select_Position = 0; |
gunpuen2 | 6:7f99124272d9 | 447 | while(1){ |
gunpuen2 | 6:7f99124272d9 | 448 | if(bt.readable()){ |
gunpuen2 | 6:7f99124272d9 | 449 | data_fromEnemy = char2posit(bt.getc()); |
gunpuen2 | 6:7f99124272d9 | 450 | if(data_fromEnemy!=18){ |
gunpuen2 | 6:7f99124272d9 | 451 | select_Position = data_fromEnemy; |
gunpuen2 | 7:e35ba3ddb9af | 452 | printf("enemy select_Position = %d\n",select_Position); |
gunpuen2 | 6:7f99124272d9 | 453 | }else{ |
gunpuen2 | 6:7f99124272d9 | 454 | ATKposition = select_Position; |
gunpuen2 | 6:7f99124272d9 | 455 | break; |
gunpuen2 | 6:7f99124272d9 | 456 | } |
gunpuen2 | 6:7f99124272d9 | 457 | } |
gunpuen2 | 6:7f99124272d9 | 458 | } |
gunpuen2 | 6:7f99124272d9 | 459 | printf("Enemy attack at your area %d\n",ATKposition); |
gunpuen2 | 15:5902cf545449 | 460 | get_atk_platform(ATKposition); |
gunpuen2 | 10:b8d45f9b00bf | 461 | check_ship_underATK++; |
gunpuen2 | 6:7f99124272d9 | 462 | mark_ship_destroyed(); |
gunpuen2 | 7:e35ba3ddb9af | 463 | } |
gunpuen2 | 7:e35ba3ddb9af | 464 | int TellUrEnemy_uWin(){ |
gunpuen2 | 7:e35ba3ddb9af | 465 | int sub_end = 0; |
gunpuen2 | 7:e35ba3ddb9af | 466 | |
gunpuen2 | 7:e35ba3ddb9af | 467 | for(int i=0;i<15;i++){ |
gunpuen2 | 11:305164107874 | 468 | if(ship_underATK[i]!=9){sub_end++;} |
gunpuen2 | 7:e35ba3ddb9af | 469 | } |
gunpuen2 | 8:a8d39b5d17ab | 470 | if(sub_end==15){return 1;}else{return 0;} |
gunpuen2 | 7:e35ba3ddb9af | 471 | } |
gunpuen2 | 7:e35ba3ddb9af | 472 | int IamWin(){ |
gunpuen2 | 8:a8d39b5d17ab | 473 | if(bt.readable()){if(bt.getc()=='q'){return 1;}else{return 0;}}else{return 0;} |
gunpuen2 | 4:8a86bacddcac | 474 | } |