Nasrun Hayeeyama
/
project_ShipDot
Nasrun
Fork of project_ShipDot by
main.cpp@19:d78586719868, 2016-12-03 (annotated)
- Committer:
- gunpuen2
- Date:
- Sat Dec 03 16:30:22 2016 +0000
- Revision:
- 19:d78586719868
- Parent:
- 18:ecef8382d41e
- Child:
- 20:4b9be9a423a8
In my opinion I think this is last edit version T__T (Joe&Puen) (23:30 || 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 | 19:d78586719868 | 46 | int check_win = 0; |
gunpuen2 | 0:a8e057156263 | 47 | int state = 0; |
gunpuen2 | 2:d93832fe8deb | 48 | int ship_SelectType[8] = {4,3,2,2,1,1,1,1}; |
gunpuen2 | 6:7f99124272d9 | 49 | int enemy_platform[8][8] = {{0}}; |
gunpuen2 | 4:8a86bacddcac | 50 | int ship_platform[8][8] = {{0}}; //______________________________________________This is ship area. |
gunpuen2 | 1:6157c77da3d5 | 51 | int atk_platform[8][8] = {{0}}; |
gunpuen2 | 10:b8d45f9b00bf | 52 | int ship_underATK[15] = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9}; |
gunpuen2 | 2:d93832fe8deb | 53 | int ship_destroyed[8] = {0}; |
gunpuen2 | 0:a8e057156263 | 54 | int ship4[4] = {0}; |
gunpuen2 | 0:a8e057156263 | 55 | int ship3[3] = {0}; |
gunpuen2 | 0:a8e057156263 | 56 | int ship2[2][2] = {{0}}; |
gunpuen2 | 0:a8e057156263 | 57 | int ship1[4] = {0}; |
gunpuen2 | 0:a8e057156263 | 58 | |
gunpuen2 | 0:a8e057156263 | 59 | int main(){ //-------------------------------------------------------------------Main Function. |
gunpuen2 | 15:5902cf545449 | 60 | bt.baud(9600); |
gunpuen2 | 0:a8e057156263 | 61 | float Vx; |
gunpuen2 | 0:a8e057156263 | 62 | float Vy; |
gunpuen2 | 19:d78586719868 | 63 | int SW,iVxVy,iSW,n = 0; |
gunpuen2 | 18:ecef8382d41e | 64 | int uli = 0; |
gunpuen2 | 0:a8e057156263 | 65 | char m; |
gunpuen2 | 14:8abed4026326 | 66 | |
gunpuen2 | 0:a8e057156263 | 67 | while(1) { |
gunpuen2 | 0:a8e057156263 | 68 | if(iShipType<8){ |
gunpuen2 | 0:a8e057156263 | 69 | Vx = VRx.read() * 1024; |
gunpuen2 | 0:a8e057156263 | 70 | Vy = VRy.read() * 1024; |
gunpuen2 | 0:a8e057156263 | 71 | SW = Button.read(); |
gunpuen2 | 0:a8e057156263 | 72 | m = check_joys(Vx,Vy); |
gunpuen2 | 0:a8e057156263 | 73 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 0:a8e057156263 | 74 | if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. |
gunpuen2 | 0:a8e057156263 | 75 | iVxVy = 0; |
gunpuen2 | 0:a8e057156263 | 76 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 77 | } |
gunpuen2 | 0:a8e057156263 | 78 | if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. |
gunpuen2 | 0:a8e057156263 | 79 | iSW = 0; |
gunpuen2 | 0:a8e057156263 | 80 | check_ready(); |
gunpuen2 | 0:a8e057156263 | 81 | } |
gunpuen2 | 0:a8e057156263 | 82 | if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. |
gunpuen2 | 0:a8e057156263 | 83 | Select_Position(m); |
gunpuen2 | 15:5902cf545449 | 84 | printf("select_Position = %d\n",select_Position); |
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 | 19:d78586719868 | 121 | wait(5); |
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 | 19:d78586719868 | 127 | state = 1; |
gunpuen2 | 0:a8e057156263 | 128 | }else{ |
gunpuen2 | 6:7f99124272d9 | 129 | enemy_atk(); |
gunpuen2 | 19:d78586719868 | 130 | if(check_win==1){ |
gunpuen2 | 19:d78586719868 | 131 | printf("BOO!! You Win!\n"); |
gunpuen2 | 19:d78586719868 | 132 | break; |
gunpuen2 | 19:d78586719868 | 133 | } |
gunpuen2 | 6:7f99124272d9 | 134 | printf("This is atk_platform.\n"); |
gunpuen2 | 6:7f99124272d9 | 135 | print_atk_platform(); |
gunpuen2 | 7:e35ba3ddb9af | 136 | printf("This is ship_destroyed. : "); |
gunpuen2 | 7:e35ba3ddb9af | 137 | print_ship_destroyed(); |
gunpuen2 | 14:8abed4026326 | 138 | if(TellUrEnemy_uWin()==1){ |
gunpuen2 | 14:8abed4026326 | 139 | while(uli<100){ |
gunpuen2 | 14:8abed4026326 | 140 | bt.putc('q'); |
gunpuen2 | 14:8abed4026326 | 141 | uli++; |
gunpuen2 | 14:8abed4026326 | 142 | } |
gunpuen2 | 19:d78586719868 | 143 | printf("BOO!! You lost!\n"); |
gunpuen2 | 7:e35ba3ddb9af | 144 | break; |
gunpuen2 | 7:e35ba3ddb9af | 145 | }else{state = 0;} |
gunpuen2 | 0:a8e057156263 | 146 | } |
gunpuen2 | 0:a8e057156263 | 147 | } |
gunpuen2 | 19:d78586719868 | 148 | while(1){printf("END\n");wait(1);} |
gunpuen2 | 0:a8e057156263 | 149 | } |
gunpuen2 | 0:a8e057156263 | 150 | |
gunpuen2 | 4:8a86bacddcac | 151 | /******************************************************************************/ |
gunpuen2 | 4:8a86bacddcac | 152 | /* */ |
gunpuen2 | 4:8a86bacddcac | 153 | /* */ |
gunpuen2 | 4:8a86bacddcac | 154 | /* Another function */ |
gunpuen2 | 4:8a86bacddcac | 155 | /* */ |
gunpuen2 | 4:8a86bacddcac | 156 | /* */ |
gunpuen2 | 4:8a86bacddcac | 157 | /******************************************************************************/ |
gunpuen2 | 0:a8e057156263 | 158 | |
gunpuen2 | 0:a8e057156263 | 159 | char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. |
gunpuen2 | 8:a8d39b5d17ab | 160 | |
gunpuen2 | 7:e35ba3ddb9af | 161 | if(ifUrJirat==1){ |
gunpuen2 | 7:e35ba3ddb9af | 162 | if(jx < 14.0){ return 'a';} |
gunpuen2 | 7:e35ba3ddb9af | 163 | else if(jx > 1010.0){return 'd';} |
gunpuen2 | 7:e35ba3ddb9af | 164 | else if(jy < 14.0){ return 'w';} |
gunpuen2 | 7:e35ba3ddb9af | 165 | else if(jy > 1010.0){return 's';} |
gunpuen2 | 7:e35ba3ddb9af | 166 | else{return NULL;} |
gunpuen2 | 8:a8d39b5d17ab | 167 | }else{ |
gunpuen2 | 9:102af5021c6c | 168 | if(jx < 14.0){ return 'd';} |
gunpuen2 | 9:102af5021c6c | 169 | else if(jx > 1010.0){return 'a';} |
gunpuen2 | 9:102af5021c6c | 170 | else if(jy < 14.0){ return 's';} |
gunpuen2 | 9:102af5021c6c | 171 | else if(jy > 1010.0){return 'w';} |
gunpuen2 | 7:e35ba3ddb9af | 172 | else{return NULL;} |
gunpuen2 | 0:a8e057156263 | 173 | } |
gunpuen2 | 0:a8e057156263 | 174 | } |
gunpuen2 | 0:a8e057156263 | 175 | int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. |
gunpuen2 | 0:a8e057156263 | 176 | if(jx>=450.0&&jx<=600.0){ |
gunpuen2 | 0:a8e057156263 | 177 | if(jy>=450.0&&jy<=600.0){ |
gunpuen2 | 0:a8e057156263 | 178 | return 1; |
gunpuen2 | 0:a8e057156263 | 179 | }else{ |
gunpuen2 | 0:a8e057156263 | 180 | return 0; |
gunpuen2 | 0:a8e057156263 | 181 | } |
gunpuen2 | 0:a8e057156263 | 182 | }else{ |
gunpuen2 | 0:a8e057156263 | 183 | return 0; |
gunpuen2 | 0:a8e057156263 | 184 | } |
gunpuen2 | 0:a8e057156263 | 185 | } |
gunpuen2 | 0:a8e057156263 | 186 | void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). |
gunpuen2 | 17:aeb7cbfb8640 | 187 | int protect_ships; |
gunpuen2 | 17:aeb7cbfb8640 | 188 | protect_ships = protect_ship(select_Position,ship_SelectType[iShipType]); |
gunpuen2 | 0:a8e057156263 | 189 | switch(M){ |
gunpuen2 | 0:a8e057156263 | 190 | case 'a': |
gunpuen2 | 17:aeb7cbfb8640 | 191 | if(select_Position%10!=0){ |
gunpuen2 | 17:aeb7cbfb8640 | 192 | select_Position = select_Position - 1; |
gunpuen2 | 17:aeb7cbfb8640 | 193 | } |
gunpuen2 | 0:a8e057156263 | 194 | break; |
gunpuen2 | 0:a8e057156263 | 195 | case 'd': |
gunpuen2 | 17:aeb7cbfb8640 | 196 | if(select_Position%10!=7){ |
gunpuen2 | 17:aeb7cbfb8640 | 197 | if(protect_ships==1 or protect_ships==0){ |
gunpuen2 | 17:aeb7cbfb8640 | 198 | select_Position = select_Position + 1; |
gunpuen2 | 17:aeb7cbfb8640 | 199 | }//else{ |
gunpuen2 | 17:aeb7cbfb8640 | 200 | //printf("Not allow position.\n"); |
gunpuen2 | 17:aeb7cbfb8640 | 201 | //} |
gunpuen2 | 17:aeb7cbfb8640 | 202 | } |
gunpuen2 | 0:a8e057156263 | 203 | break; |
gunpuen2 | 0:a8e057156263 | 204 | case 'w': |
gunpuen2 | 17:aeb7cbfb8640 | 205 | if(select_Position/10!=0){ |
gunpuen2 | 17:aeb7cbfb8640 | 206 | select_Position = select_Position - 10; |
gunpuen2 | 17:aeb7cbfb8640 | 207 | } |
gunpuen2 | 0:a8e057156263 | 208 | break; |
gunpuen2 | 0:a8e057156263 | 209 | case 's': |
gunpuen2 | 17:aeb7cbfb8640 | 210 | if(select_Position/10!=7){ |
gunpuen2 | 17:aeb7cbfb8640 | 211 | if(protect_ships==1 or protect_ships==2){ |
gunpuen2 | 17:aeb7cbfb8640 | 212 | select_Position = select_Position + 10; |
gunpuen2 | 17:aeb7cbfb8640 | 213 | }//else{ |
gunpuen2 | 17:aeb7cbfb8640 | 214 | //printf("Not allow position.\n"); |
gunpuen2 | 17:aeb7cbfb8640 | 215 | //} |
gunpuen2 | 17:aeb7cbfb8640 | 216 | } |
gunpuen2 | 0:a8e057156263 | 217 | break; |
gunpuen2 | 0:a8e057156263 | 218 | } |
gunpuen2 | 0:a8e057156263 | 219 | } |
gunpuen2 | 15:5902cf545449 | 220 | int protect_ship(int head_position,int ship_type){ |
gunpuen2 | 15:5902cf545449 | 221 | int x,y = 0; |
gunpuen2 | 15:5902cf545449 | 222 | y = head_position/10; |
gunpuen2 | 15:5902cf545449 | 223 | x = head_position%10; |
gunpuen2 | 17:aeb7cbfb8640 | 224 | if(ship_type==4){if((y+4)>7){return 0;}else{return 1;}} |
gunpuen2 | 17:aeb7cbfb8640 | 225 | else if(ship_type==3){if((x+3)>7){return 2;}else{return 1;}} |
gunpuen2 | 17:aeb7cbfb8640 | 226 | else if(ship_type==2){if((y+2)>7){return 0;}else{return 1;}} |
gunpuen2 | 15:5902cf545449 | 227 | else{return 1;} |
gunpuen2 | 15:5902cf545449 | 228 | } |
gunpuen2 | 15:5902cf545449 | 229 | int get_ship_platform(int input_position,int ship_type){ //-----------------------Plot ship in ship area. |
gunpuen2 | 0:a8e057156263 | 230 | int i,x,y = 0; |
gunpuen2 | 0:a8e057156263 | 231 | int error = 0; |
gunpuen2 | 0:a8e057156263 | 232 | |
gunpuen2 | 0:a8e057156263 | 233 | y = input_position/10; |
gunpuen2 | 0:a8e057156263 | 234 | x = input_position%10; |
gunpuen2 | 0:a8e057156263 | 235 | |
gunpuen2 | 0:a8e057156263 | 236 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 0:a8e057156263 | 237 | if(check_correctPosition(y,x) == 1){ |
gunpuen2 | 0:a8e057156263 | 238 | if(ship_type == 4){ship4[i] = (y*10)+x;} |
gunpuen2 | 0:a8e057156263 | 239 | else if(ship_type == 3){ship3[i] = (y*10)+x;} |
gunpuen2 | 0:a8e057156263 | 240 | else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;} |
gunpuen2 | 0:a8e057156263 | 241 | else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;} |
gunpuen2 | 16:3fac68a7fd7c | 242 | }else{error = 1;break;} |
gunpuen2 | 16:3fac68a7fd7c | 243 | if(ship_type%2 == 0){y++;}else{x++;} |
gunpuen2 | 0:a8e057156263 | 244 | } |
gunpuen2 | 16:3fac68a7fd7c | 245 | if(ship_type==2){check_ship2++;} |
gunpuen2 | 16:3fac68a7fd7c | 246 | if(error == 1){return 0;}else{ |
gunpuen2 | 0:a8e057156263 | 247 | y = input_position/10; |
gunpuen2 | 0:a8e057156263 | 248 | x = input_position%10; |
gunpuen2 | 0:a8e057156263 | 249 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 1:6157c77da3d5 | 250 | ship_platform[y][x] = ship_type; |
gunpuen2 | 16:3fac68a7fd7c | 251 | if(ship_type%2 == 0){y++;}else{x++;} |
gunpuen2 | 0:a8e057156263 | 252 | } |
gunpuen2 | 0:a8e057156263 | 253 | return 1; |
gunpuen2 | 0:a8e057156263 | 254 | } |
gunpuen2 | 0:a8e057156263 | 255 | } |
gunpuen2 | 0:a8e057156263 | 256 | int check_correctPosition(int X,int Y){ |
gunpuen2 | 0:a8e057156263 | 257 | if(X<=7){ |
gunpuen2 | 0:a8e057156263 | 258 | if(Y<=7){ |
gunpuen2 | 16:3fac68a7fd7c | 259 | if(ship_platform[X][Y] == 0){return 1;}else{return 0;} |
gunpuen2 | 16:3fac68a7fd7c | 260 | }else{return 0;} |
gunpuen2 | 16:3fac68a7fd7c | 261 | }else{return 0;} |
gunpuen2 | 0:a8e057156263 | 262 | } |
gunpuen2 | 6:7f99124272d9 | 263 | void print_ship_platform(){ |
gunpuen2 | 0:a8e057156263 | 264 | for(int i=0;i<8;i++){ |
gunpuen2 | 0:a8e057156263 | 265 | for(int j=0;j<8;j++){ |
gunpuen2 | 1:6157c77da3d5 | 266 | printf("| %d |",ship_platform[i][j]); |
gunpuen2 | 0:a8e057156263 | 267 | } |
gunpuen2 | 0:a8e057156263 | 268 | printf("\n"); |
gunpuen2 | 0:a8e057156263 | 269 | } |
gunpuen2 | 0:a8e057156263 | 270 | } |
gunpuen2 | 6:7f99124272d9 | 271 | void print_enemy_platform(){ |
gunpuen2 | 6:7f99124272d9 | 272 | for(int i=0;i<8;i++){ |
gunpuen2 | 6:7f99124272d9 | 273 | for(int j=0;j<8;j++){ |
gunpuen2 | 6:7f99124272d9 | 274 | printf("| %d |",enemy_platform[i][j]); |
gunpuen2 | 6:7f99124272d9 | 275 | } |
gunpuen2 | 6:7f99124272d9 | 276 | printf("\n"); |
gunpuen2 | 6:7f99124272d9 | 277 | } |
gunpuen2 | 6:7f99124272d9 | 278 | } |
gunpuen2 | 6:7f99124272d9 | 279 | void print_atk_platform(){ |
gunpuen2 | 6:7f99124272d9 | 280 | for(int i=0;i<8;i++){ |
gunpuen2 | 6:7f99124272d9 | 281 | for(int j=0;j<8;j++){ |
gunpuen2 | 6:7f99124272d9 | 282 | printf("| %d |",atk_platform[i][j]); |
gunpuen2 | 6:7f99124272d9 | 283 | } |
gunpuen2 | 6:7f99124272d9 | 284 | printf("\n"); |
gunpuen2 | 6:7f99124272d9 | 285 | } |
gunpuen2 | 6:7f99124272d9 | 286 | } |
gunpuen2 | 7:e35ba3ddb9af | 287 | void print_ship_destroyed(){ |
gunpuen2 | 8:a8d39b5d17ab | 288 | printf("{"); |
gunpuen2 | 7:e35ba3ddb9af | 289 | for(int i=0;i<8;i++){ |
gunpuen2 | 7:e35ba3ddb9af | 290 | printf("%d",ship_destroyed[i]); |
gunpuen2 | 7:e35ba3ddb9af | 291 | if(i!=14){printf(",");} |
gunpuen2 | 7:e35ba3ddb9af | 292 | } |
gunpuen2 | 7:e35ba3ddb9af | 293 | printf("}\n"); |
gunpuen2 | 7:e35ba3ddb9af | 294 | } |
gunpuen2 | 0:a8e057156263 | 295 | void check_ready(){ |
gunpuen2 | 16:3fac68a7fd7c | 296 | if(bt.readable()){enemy_ready = 1;} |
gunpuen2 | 1:6157c77da3d5 | 297 | } |
gunpuen2 | 1:6157c77da3d5 | 298 | char posit2char(int posit){ |
gunpuen2 | 1:6157c77da3d5 | 299 | int as = posit+48; |
gunpuen2 | 1:6157c77da3d5 | 300 | char ch = as; |
gunpuen2 | 1:6157c77da3d5 | 301 | return ch ; |
gunpuen2 | 1:6157c77da3d5 | 302 | } |
gunpuen2 | 1:6157c77da3d5 | 303 | |
gunpuen2 | 1:6157c77da3d5 | 304 | int char2posit(char cha){ |
gunpuen2 | 1:6157c77da3d5 | 305 | int as = cha; |
gunpuen2 | 1:6157c77da3d5 | 306 | as = as-48; |
gunpuen2 | 1:6157c77da3d5 | 307 | return as; |
gunpuen2 | 1:6157c77da3d5 | 308 | } |
gunpuen2 | 15:5902cf545449 | 309 | void get_atk_platform(int atkpos){ |
gunpuen2 | 4:8a86bacddcac | 310 | int i,j,x,y = 0; |
gunpuen2 | 1:6157c77da3d5 | 311 | |
gunpuen2 | 7:e35ba3ddb9af | 312 | x = atkpos/10; |
gunpuen2 | 7:e35ba3ddb9af | 313 | y = atkpos%10; |
gunpuen2 | 1:6157c77da3d5 | 314 | |
gunpuen2 | 2:d93832fe8deb | 315 | if(ship_platform[x][y]!=0 and atk_platform[x][y]==0){ |
gunpuen2 | 1:6157c77da3d5 | 316 | atk_platform[x][y] = ship_platform[x][y]; |
gunpuen2 | 6:7f99124272d9 | 317 | bt.putc(posit2char(atk_platform[x][y])); |
gunpuen2 | 9:102af5021c6c | 318 | ship_underATK[check_ship_underATK] = (x*10)+y; |
gunpuen2 | 2:d93832fe8deb | 319 | if(atk_platform[x][y]==4){ |
gunpuen2 | 2:d93832fe8deb | 320 | for(i=0;i<4;i++){ |
gunpuen2 | 2:d93832fe8deb | 321 | for(j=0;j<15;j++){ |
gunpuen2 | 16:3fac68a7fd7c | 322 | if(ship4[i]==ship_underATK[j]){ship_destroyed[0]++;} |
gunpuen2 | 2:d93832fe8deb | 323 | } |
gunpuen2 | 2:d93832fe8deb | 324 | } |
gunpuen2 | 2:d93832fe8deb | 325 | }else if(atk_platform[x][y]==3){ |
gunpuen2 | 2:d93832fe8deb | 326 | for(i=0;i<3;i++){ |
gunpuen2 | 2:d93832fe8deb | 327 | for(j=0;j<15;j++){ |
gunpuen2 | 16:3fac68a7fd7c | 328 | if(ship3[i]==ship_underATK[j]){ship_destroyed[1]++;} |
gunpuen2 | 2:d93832fe8deb | 329 | } |
gunpuen2 | 2:d93832fe8deb | 330 | } |
gunpuen2 | 2:d93832fe8deb | 331 | }else if(atk_platform[x][y]==2){ |
gunpuen2 | 2:d93832fe8deb | 332 | for(i=0;i<4;i++){ |
gunpuen2 | 2:d93832fe8deb | 333 | for(j=0;j<15;j++){ |
gunpuen2 | 16:3fac68a7fd7c | 334 | if(ship2[0][i]==ship_underATK[j]){ship_destroyed[2]++;} |
gunpuen2 | 16:3fac68a7fd7c | 335 | if(ship2[1][i]==ship_underATK[j]){ship_destroyed[3]++;} |
gunpuen2 | 2:d93832fe8deb | 336 | } |
gunpuen2 | 2:d93832fe8deb | 337 | } |
gunpuen2 | 2:d93832fe8deb | 338 | }else if(atk_platform[x][y]==1){ |
gunpuen2 | 2:d93832fe8deb | 339 | for(i=0;i<4;i++){ |
gunpuen2 | 2:d93832fe8deb | 340 | for(j=0;j<15;j++){ |
gunpuen2 | 2:d93832fe8deb | 341 | if(ship3[i]==ship_underATK[j]){ |
gunpuen2 | 2:d93832fe8deb | 342 | ship_destroyed[4+i]++; |
gunpuen2 | 4:8a86bacddcac | 343 | atk_platform[x][y] = 11; |
gunpuen2 | 2:d93832fe8deb | 344 | } |
gunpuen2 | 2:d93832fe8deb | 345 | } |
gunpuen2 | 2:d93832fe8deb | 346 | } |
gunpuen2 | 2:d93832fe8deb | 347 | } |
gunpuen2 | 1:6157c77da3d5 | 348 | }else{ |
gunpuen2 | 1:6157c77da3d5 | 349 | atk_platform[x][y] = 8; |
gunpuen2 | 6:7f99124272d9 | 350 | bt.putc(posit2char(atk_platform[x][y])); |
gunpuen2 | 0:a8e057156263 | 351 | } |
gunpuen2 | 0:a8e057156263 | 352 | } |
gunpuen2 | 4:8a86bacddcac | 353 | void mark_ship_destroyed(){ |
gunpuen2 | 4:8a86bacddcac | 354 | int i,j,k = 0; |
gunpuen2 | 0:a8e057156263 | 355 | |
gunpuen2 | 11:305164107874 | 356 | if(ship_destroyed[0]==10){ |
gunpuen2 | 4:8a86bacddcac | 357 | for(i=0;i<8;i++){ |
gunpuen2 | 4:8a86bacddcac | 358 | for(j=0;j<8;j++){ |
gunpuen2 | 16:3fac68a7fd7c | 359 | if(atk_platform[i][j]==4){atk_platform[i][j] = 11;} |
gunpuen2 | 4:8a86bacddcac | 360 | } |
gunpuen2 | 4:8a86bacddcac | 361 | } |
gunpuen2 | 13:eee9af3b4308 | 362 | } |
gunpuen2 | 13:eee9af3b4308 | 363 | if(ship_destroyed[1]==6){ |
gunpuen2 | 4:8a86bacddcac | 364 | for(i=0;i<8;i++){ |
gunpuen2 | 4:8a86bacddcac | 365 | for(j=0;j<8;j++){ |
gunpuen2 | 16:3fac68a7fd7c | 366 | if(atk_platform[i][j]==3){atk_platform[i][j] = 11;} |
gunpuen2 | 4:8a86bacddcac | 367 | } |
gunpuen2 | 4:8a86bacddcac | 368 | } |
gunpuen2 | 13:eee9af3b4308 | 369 | } |
gunpuen2 | 13:eee9af3b4308 | 370 | if(ship_destroyed[2]==3){ |
gunpuen2 | 4:8a86bacddcac | 371 | for(k=0;k<2;k++){ |
gunpuen2 | 4:8a86bacddcac | 372 | i = ship2[0][k]/10; |
gunpuen2 | 4:8a86bacddcac | 373 | j = ship2[0][k]%10; |
gunpuen2 | 4:8a86bacddcac | 374 | atk_platform[i][j] = 11; |
gunpuen2 | 4:8a86bacddcac | 375 | } |
gunpuen2 | 13:eee9af3b4308 | 376 | } |
gunpuen2 | 13:eee9af3b4308 | 377 | if(ship_destroyed[3]==3){ |
gunpuen2 | 4:8a86bacddcac | 378 | for(k=0;k<2;k++){ |
gunpuen2 | 4:8a86bacddcac | 379 | i = ship2[1][k]/10; |
gunpuen2 | 4:8a86bacddcac | 380 | j = ship2[1][k]%10; |
gunpuen2 | 4:8a86bacddcac | 381 | atk_platform[i][j] = 11; |
gunpuen2 | 4:8a86bacddcac | 382 | } |
gunpuen2 | 4:8a86bacddcac | 383 | } |
gunpuen2 | 6:7f99124272d9 | 384 | } |
gunpuen2 | 6:7f99124272d9 | 385 | void atk_enemy(){ |
gunpuen2 | 6:7f99124272d9 | 386 | float Vx; |
gunpuen2 | 6:7f99124272d9 | 387 | float Vy; |
gunpuen2 | 7:e35ba3ddb9af | 388 | int SW,iVxVy,iSW,n = 0; |
gunpuen2 | 7:e35ba3ddb9af | 389 | int iPrintUturn = 0; |
gunpuen2 | 6:7f99124272d9 | 390 | char m; |
gunpuen2 | 6:7f99124272d9 | 391 | |
gunpuen2 | 14:8abed4026326 | 392 | select_Position = 0; |
gunpuen2 | 6:7f99124272d9 | 393 | while(1){ |
gunpuen2 | 6:7f99124272d9 | 394 | if(iPrintUturn==0){ |
gunpuen2 | 6:7f99124272d9 | 395 | printf("Your turn.\n"); |
gunpuen2 | 6:7f99124272d9 | 396 | iPrintUturn = 1; |
gunpuen2 | 6:7f99124272d9 | 397 | } |
gunpuen2 | 6:7f99124272d9 | 398 | Vx = VRx.read() * 1024; |
gunpuen2 | 6:7f99124272d9 | 399 | Vy = VRy.read() * 1024; |
gunpuen2 | 6:7f99124272d9 | 400 | SW = Button.read(); |
gunpuen2 | 6:7f99124272d9 | 401 | m = check_joys(Vx,Vy); |
gunpuen2 | 6:7f99124272d9 | 402 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 16:3fac68a7fd7c | 403 | if(n == 1){ iVxVy = 0;} |
gunpuen2 | 6:7f99124272d9 | 404 | if(SW == 1){ |
gunpuen2 | 6:7f99124272d9 | 405 | iSW = 0; |
gunpuen2 | 6:7f99124272d9 | 406 | if(bt.readable()){ |
gunpuen2 | 6:7f99124272d9 | 407 | enemy_platform[select_Position/10][select_Position%10] = char2posit(bt.getc()); |
gunpuen2 | 6:7f99124272d9 | 408 | break; |
gunpuen2 | 6:7f99124272d9 | 409 | } |
gunpuen2 | 6:7f99124272d9 | 410 | } |
gunpuen2 | 6:7f99124272d9 | 411 | if(m!=NULL && iVxVy==0){ |
gunpuen2 | 6:7f99124272d9 | 412 | Select_Position(m); |
gunpuen2 | 6:7f99124272d9 | 413 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 6:7f99124272d9 | 414 | bt.putc(posit2char(select_Position)); |
gunpuen2 | 6:7f99124272d9 | 415 | iVxVy = 1; |
gunpuen2 | 6:7f99124272d9 | 416 | } |
gunpuen2 | 6:7f99124272d9 | 417 | if(SW==0 && iSW==0){ |
gunpuen2 | 6:7f99124272d9 | 418 | bt.putc('B'); |
gunpuen2 | 6:7f99124272d9 | 419 | printf("You attacking at position %d\n",select_Position); |
gunpuen2 | 6:7f99124272d9 | 420 | iSW = 1; |
gunpuen2 | 6:7f99124272d9 | 421 | } |
gunpuen2 | 6:7f99124272d9 | 422 | } |
gunpuen2 | 6:7f99124272d9 | 423 | } |
gunpuen2 | 6:7f99124272d9 | 424 | void enemy_atk(){ |
gunpuen2 | 6:7f99124272d9 | 425 | int data_fromEnemy; |
gunpuen2 | 19:d78586719868 | 426 | char replica_data_fromEnemy; |
gunpuen2 | 6:7f99124272d9 | 427 | printf("Wait for enemy attack.\n"); |
gunpuen2 | 13:eee9af3b4308 | 428 | select_Position = 0; |
gunpuen2 | 6:7f99124272d9 | 429 | while(1){ |
gunpuen2 | 6:7f99124272d9 | 430 | if(bt.readable()){ |
gunpuen2 | 19:d78586719868 | 431 | if(replica_data_fromEnemy!='q'){ |
gunpuen2 | 19:d78586719868 | 432 | data_fromEnemy = char2posit(replica_data_fromEnemy); |
gunpuen2 | 19:d78586719868 | 433 | if(data_fromEnemy!=18){ |
gunpuen2 | 19:d78586719868 | 434 | select_Position = data_fromEnemy; |
gunpuen2 | 19:d78586719868 | 435 | printf("enemy select_Position = %d\n",select_Position); |
gunpuen2 | 19:d78586719868 | 436 | }else{ |
gunpuen2 | 19:d78586719868 | 437 | ATKposition = select_Position; |
gunpuen2 | 19:d78586719868 | 438 | break; |
gunpuen2 | 19:d78586719868 | 439 | } |
gunpuen2 | 6:7f99124272d9 | 440 | }else{ |
gunpuen2 | 19:d78586719868 | 441 | check_win = 1; |
gunpuen2 | 6:7f99124272d9 | 442 | break; |
gunpuen2 | 6:7f99124272d9 | 443 | } |
gunpuen2 | 6:7f99124272d9 | 444 | } |
gunpuen2 | 6:7f99124272d9 | 445 | } |
gunpuen2 | 19:d78586719868 | 446 | if(check_win!=1){ |
gunpuen2 | 19:d78586719868 | 447 | printf("Enemy attack at your area %d\n",ATKposition); |
gunpuen2 | 19:d78586719868 | 448 | get_atk_platform(ATKposition); |
gunpuen2 | 19:d78586719868 | 449 | check_ship_underATK++; |
gunpuen2 | 19:d78586719868 | 450 | mark_ship_destroyed(); |
gunpuen2 | 19:d78586719868 | 451 | } |
gunpuen2 | 7:e35ba3ddb9af | 452 | } |
gunpuen2 | 7:e35ba3ddb9af | 453 | int TellUrEnemy_uWin(){ |
gunpuen2 | 7:e35ba3ddb9af | 454 | int sub_end = 0; |
gunpuen2 | 7:e35ba3ddb9af | 455 | |
gunpuen2 | 7:e35ba3ddb9af | 456 | for(int i=0;i<15;i++){ |
gunpuen2 | 11:305164107874 | 457 | if(ship_underATK[i]!=9){sub_end++;} |
gunpuen2 | 7:e35ba3ddb9af | 458 | } |
gunpuen2 | 8:a8d39b5d17ab | 459 | if(sub_end==15){return 1;}else{return 0;} |
gunpuen2 | 7:e35ba3ddb9af | 460 | } |
gunpuen2 | 7:e35ba3ddb9af | 461 | int IamWin(){ |
gunpuen2 | 17:aeb7cbfb8640 | 462 | char chare; |
gunpuen2 | 17:aeb7cbfb8640 | 463 | if(bt.readable()){ |
gunpuen2 | 18:ecef8382d41e | 464 | chare = bt.getc(); |
gunpuen2 | 17:aeb7cbfb8640 | 465 | if(chare=='q'){return 1;}else{return 0;} |
gunpuen2 | 17:aeb7cbfb8640 | 466 | }else{return 0;} |
gunpuen2 | 4:8a86bacddcac | 467 | } |