Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Cricket/Cricket.cpp@13:924891519a95, 2019-04-23 (annotated)
- Committer:
- shahidsajid
- Date:
- Tue Apr 23 14:35:39 2019 +0000
- Revision:
- 13:924891519a95
- Parent:
- 12:954da4f4e565
- Child:
- 14:122eaa3b7a50
Created a method check_fielder() to check for collisions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shahidsajid | 10:6c6e09023942 | 1 | #include "Cricket.h" |
shahidsajid | 10:6c6e09023942 | 2 | |
shahidsajid | 10:6c6e09023942 | 3 | |
shahidsajid | 10:6c6e09023942 | 4 | Cricket::Cricket() |
shahidsajid | 10:6c6e09023942 | 5 | { |
shahidsajid | 10:6c6e09023942 | 6 | |
shahidsajid | 10:6c6e09023942 | 7 | } |
shahidsajid | 10:6c6e09023942 | 8 | |
shahidsajid | 10:6c6e09023942 | 9 | Cricket::~Cricket() |
shahidsajid | 10:6c6e09023942 | 10 | { |
shahidsajid | 10:6c6e09023942 | 11 | |
shahidsajid | 10:6c6e09023942 | 12 | } |
shahidsajid | 10:6c6e09023942 | 13 | |
shahidsajid | 10:6c6e09023942 | 14 | void Cricket::init() |
shahidsajid | 10:6c6e09023942 | 15 | { |
shahidsajid | 10:6c6e09023942 | 16 | bat.init(4,5); |
shahidsajid | 10:6c6e09023942 | 17 | ball.init(2,3); |
shahidsajid | 12:954da4f4e565 | 18 | //float f; |
shahidsajid | 10:6c6e09023942 | 19 | new_round=0; |
shahidsajid | 12:954da4f4e565 | 20 | direction_set=0; |
shahidsajid | 12:954da4f4e565 | 21 | fieldersCount=0; |
shahidsajid | 12:954da4f4e565 | 22 | init_field_counter=0; |
shahidsajid | 12:954da4f4e565 | 23 | init_positions(); |
shahidsajid | 12:954da4f4e565 | 24 | fielder_no=-1; |
shahidsajid | 12:954da4f4e565 | 25 | check_bowled=0; |
shahidsajid | 13:924891519a95 | 26 | outfield_fielder=-1; |
shahidsajid | 13:924891519a95 | 27 | } |
shahidsajid | 13:924891519a95 | 28 | void Cricket::reset(){ |
shahidsajid | 13:924891519a95 | 29 | ball.reset(); |
shahidsajid | 13:924891519a95 | 30 | new_round=0; |
shahidsajid | 13:924891519a95 | 31 | direction_set=0; |
shahidsajid | 13:924891519a95 | 32 | fieldersCount=0; |
shahidsajid | 13:924891519a95 | 33 | init_field_counter=0; |
shahidsajid | 13:924891519a95 | 34 | init_positions(); |
shahidsajid | 13:924891519a95 | 35 | fielder_no=-1; |
shahidsajid | 13:924891519a95 | 36 | check_bowled=0; |
shahidsajid | 13:924891519a95 | 37 | |
shahidsajid | 10:6c6e09023942 | 38 | } |
shahidsajid | 11:f481ec642cc5 | 39 | void Cricket::game(N5110 &lcd,Gamepad &pad){ |
shahidsajid | 13:924891519a95 | 40 | if (new_round==0){ |
shahidsajid | 10:6c6e09023942 | 41 | //ball.reset(); |
shahidsajid | 10:6c6e09023942 | 42 | set_field(lcd); |
shahidsajid | 11:f481ec642cc5 | 43 | play_game(lcd,pad); |
shahidsajid | 13:924891519a95 | 44 | } |
shahidsajid | 13:924891519a95 | 45 | else |
shahidsajid | 13:924891519a95 | 46 | { |
shahidsajid | 13:924891519a95 | 47 | play_game(lcd,pad); |
shahidsajid | 13:924891519a95 | 48 | } |
shahidsajid | 12:954da4f4e565 | 49 | |
shahidsajid | 10:6c6e09023942 | 50 | |
shahidsajid | 10:6c6e09023942 | 51 | } |
shahidsajid | 12:954da4f4e565 | 52 | void Cricket::init_positions(){ |
shahidsajid | 12:954da4f4e565 | 53 | set_init_positions(42,0,N,1); |
shahidsajid | 12:954da4f4e565 | 54 | set_init_positions(42,48,S,2); |
shahidsajid | 12:954da4f4e565 | 55 | set_init_positions(84,25,E,3); |
shahidsajid | 12:954da4f4e565 | 56 | set_init_positions(0,25,W,4); |
shahidsajid | 12:954da4f4e565 | 57 | set_init_positions(0,44,SW,5); |
shahidsajid | 12:954da4f4e565 | 58 | set_init_positions(0,2,NW,6); |
shahidsajid | 12:954da4f4e565 | 59 | set_init_positions(84,2,NE,7); |
shahidsajid | 12:954da4f4e565 | 60 | set_init_positions(84,44,SE,8); |
shahidsajid | 12:954da4f4e565 | 61 | } |
shahidsajid | 12:954da4f4e565 | 62 | void Cricket::set_init_positions(int x,int y, Direction direction,int no){ |
shahidsajid | 12:954da4f4e565 | 63 | positions[init_field_counter].x=x; |
shahidsajid | 12:954da4f4e565 | 64 | positions[init_field_counter].y=y; |
shahidsajid | 12:954da4f4e565 | 65 | positions[init_field_counter].dir=direction; |
shahidsajid | 12:954da4f4e565 | 66 | positions[init_field_counter].no=no; |
shahidsajid | 12:954da4f4e565 | 67 | init_field_counter++; |
shahidsajid | 12:954da4f4e565 | 68 | } |
shahidsajid | 11:f481ec642cc5 | 69 | void Cricket::play_game(N5110 &lcd,Gamepad &pad){ |
shahidsajid | 11:f481ec642cc5 | 70 | int check_hit=bat.get_hitBall(); |
shahidsajid | 13:924891519a95 | 71 | int loft_check=bat.get_loft_ball(); |
shahidsajid | 12:954da4f4e565 | 72 | Direction dir=pad.get_direction(); |
shahidsajid | 12:954da4f4e565 | 73 | if (check_bowled!=1){ |
shahidsajid | 12:954da4f4e565 | 74 | check_bowled=ball.ball_start(); |
shahidsajid | 12:954da4f4e565 | 75 | } |
shahidsajid | 12:954da4f4e565 | 76 | else{ |
shahidsajid | 13:924891519a95 | 77 | update_game(check_hit, loft_check, dir); |
shahidsajid | 12:954da4f4e565 | 78 | } |
shahidsajid | 11:f481ec642cc5 | 79 | } |
shahidsajid | 13:924891519a95 | 80 | void Cricket::update_game(int checkHit,int loft_check, Direction dir){ |
shahidsajid | 12:954da4f4e565 | 81 | Vector2D pos=ball.get_pos(); |
shahidsajid | 13:924891519a95 | 82 | set_ball_direction(dir); |
shahidsajid | 13:924891519a95 | 83 | int fielder_check=check_fielder(ball_direction); |
shahidsajid | 13:924891519a95 | 84 | if (checkHit ==1 && ballHit!=1){ |
shahidsajid | 13:924891519a95 | 85 | ballHit=1; |
shahidsajid | 13:924891519a95 | 86 | } |
shahidsajid | 13:924891519a95 | 87 | if (fielder_check!=-1){ |
shahidsajid | 13:924891519a95 | 88 | if(direction_set==1 && ballHit==1){ |
shahidsajid | 13:924891519a95 | 89 | ball.update_ball_y(field[fielder_check].y); |
shahidsajid | 13:924891519a95 | 90 | ball.update_ball_x(field[fielder_check].x); |
shahidsajid | 13:924891519a95 | 91 | } |
shahidsajid | 13:924891519a95 | 92 | } |
shahidsajid | 13:924891519a95 | 93 | if (fielder_check==-1){ |
shahidsajid | 13:924891519a95 | 94 | if(direction_set==1 && ballHit==1){ |
shahidsajid | 13:924891519a95 | 95 | ball.update_ball_y(positions[fielder_no].y); |
shahidsajid | 13:924891519a95 | 96 | ball.update_ball_x(positions[fielder_no].x); |
shahidsajid | 13:924891519a95 | 97 | } |
shahidsajid | 13:924891519a95 | 98 | } |
shahidsajid | 13:924891519a95 | 99 | } |
shahidsajid | 13:924891519a95 | 100 | |
shahidsajid | 13:924891519a95 | 101 | void Cricket::draw(N5110 &lcd){ |
shahidsajid | 13:924891519a95 | 102 | lcd.drawCircle((WIDTH/2),HEIGHT/2,23,FILL_TRANSPARENT); |
shahidsajid | 13:924891519a95 | 103 | lcd.drawRect(37,11,12,30,FILL_TRANSPARENT); |
shahidsajid | 13:924891519a95 | 104 | ball.draw(lcd); |
shahidsajid | 13:924891519a95 | 105 | draw_field(lcd); |
shahidsajid | 13:924891519a95 | 106 | bat.draw(lcd); |
shahidsajid | 13:924891519a95 | 107 | } |
shahidsajid | 13:924891519a95 | 108 | void Cricket::set_ball_direction(Direction dir){ |
shahidsajid | 13:924891519a95 | 109 | if (direction_set!=1){ |
shahidsajid | 12:954da4f4e565 | 110 | for (int i=0;i<init_field_counter;i++){ |
shahidsajid | 12:954da4f4e565 | 111 | if(dir==positions[i].dir){ |
shahidsajid | 12:954da4f4e565 | 112 | printf("%i \n",positions[i].no); |
shahidsajid | 12:954da4f4e565 | 113 | ball_direction=dir; |
shahidsajid | 12:954da4f4e565 | 114 | fielder_no=i; |
shahidsajid | 12:954da4f4e565 | 115 | direction_set=1; |
shahidsajid | 12:954da4f4e565 | 116 | break; |
shahidsajid | 12:954da4f4e565 | 117 | } |
shahidsajid | 12:954da4f4e565 | 118 | } |
shahidsajid | 12:954da4f4e565 | 119 | } |
shahidsajid | 13:924891519a95 | 120 | } |
shahidsajid | 13:924891519a95 | 121 | int Cricket::check_fielder(Direction dir){ |
shahidsajid | 13:924891519a95 | 122 | Vector2D p; |
shahidsajid | 13:924891519a95 | 123 | for (int i=0;i<7;i++){ |
shahidsajid | 13:924891519a95 | 124 | if(dir == field[i].dir){ |
shahidsajid | 13:924891519a95 | 125 | return i; |
shahidsajid | 13:924891519a95 | 126 | } |
shahidsajid | 13:924891519a95 | 127 | } |
shahidsajid | 13:924891519a95 | 128 | return -1; |
shahidsajid | 10:6c6e09023942 | 129 | } |
shahidsajid | 10:6c6e09023942 | 130 | void Cricket::draw_field(N5110 &lcd){ |
shahidsajid | 13:924891519a95 | 131 | lcd.drawCircle(field[0].x,field[0].y,2,FILL_TRANSPARENT); |
shahidsajid | 13:924891519a95 | 132 | lcd.drawCircle(field[1].x,field[1].y,2,FILL_TRANSPARENT); |
shahidsajid | 13:924891519a95 | 133 | lcd.drawCircle(field[2].x,field[2].y,2,FILL_TRANSPARENT); |
shahidsajid | 13:924891519a95 | 134 | lcd.drawCircle(field[3].x,field[3].y,2,FILL_TRANSPARENT); |
shahidsajid | 10:6c6e09023942 | 135 | } |
shahidsajid | 10:6c6e09023942 | 136 | void Cricket::set_field(N5110 &lcd){ |
shahidsajid | 10:6c6e09023942 | 137 | int d=0; |
shahidsajid | 10:6c6e09023942 | 138 | int pos=0; |
shahidsajid | 10:6c6e09023942 | 139 | int i=0; |
shahidsajid | 10:6c6e09023942 | 140 | int continueCount=0; |
shahidsajid | 10:6c6e09023942 | 141 | srand(time(NULL)); |
shahidsajid | 10:6c6e09023942 | 142 | while (fieldersCount!=5){ |
shahidsajid | 10:6c6e09023942 | 143 | d = 1+ rand() % 6; |
shahidsajid | 10:6c6e09023942 | 144 | pos=rand()%2; |
shahidsajid | 10:6c6e09023942 | 145 | for (int j=0;j<10;j++){ |
shahidsajid | 10:6c6e09023942 | 146 | if (fieldNumbers[j]==d){ |
shahidsajid | 10:6c6e09023942 | 147 | continueCount=1; |
shahidsajid | 10:6c6e09023942 | 148 | } |
shahidsajid | 10:6c6e09023942 | 149 | } |
shahidsajid | 10:6c6e09023942 | 150 | if (continueCount==1){ |
shahidsajid | 10:6c6e09023942 | 151 | continueCount=0; |
shahidsajid | 10:6c6e09023942 | 152 | continue; |
shahidsajid | 10:6c6e09023942 | 153 | } |
shahidsajid | 10:6c6e09023942 | 154 | if (d==1){ //NW |
shahidsajid | 10:6c6e09023942 | 155 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 156 | field[i].dir=NW; |
shahidsajid | 10:6c6e09023942 | 157 | field[i].x=30; |
shahidsajid | 10:6c6e09023942 | 158 | field[i].y=9; |
shahidsajid | 10:6c6e09023942 | 159 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 160 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 161 | i++; |
shahidsajid | 10:6c6e09023942 | 162 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 163 | } |
shahidsajid | 10:6c6e09023942 | 164 | else{ |
shahidsajid | 10:6c6e09023942 | 165 | field[i].dir=NW; |
shahidsajid | 10:6c6e09023942 | 166 | field[i].x=25; |
shahidsajid | 10:6c6e09023942 | 167 | field[i].y=2; |
shahidsajid | 10:6c6e09023942 | 168 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 169 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 170 | i++; |
shahidsajid | 10:6c6e09023942 | 171 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 172 | } |
shahidsajid | 10:6c6e09023942 | 173 | } |
shahidsajid | 10:6c6e09023942 | 174 | |
shahidsajid | 10:6c6e09023942 | 175 | |
shahidsajid | 10:6c6e09023942 | 176 | if (d==2){ //NE |
shahidsajid | 10:6c6e09023942 | 177 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 178 | field[i].dir=NE; |
shahidsajid | 10:6c6e09023942 | 179 | field[i].x=55; |
shahidsajid | 10:6c6e09023942 | 180 | field[i].y=9; |
shahidsajid | 10:6c6e09023942 | 181 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 182 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 183 | i++; |
shahidsajid | 10:6c6e09023942 | 184 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 185 | } |
shahidsajid | 10:6c6e09023942 | 186 | else{ |
shahidsajid | 10:6c6e09023942 | 187 | field[i].dir=NE; |
shahidsajid | 10:6c6e09023942 | 188 | field[i].x=65; |
shahidsajid | 10:6c6e09023942 | 189 | field[i].y=2; |
shahidsajid | 10:6c6e09023942 | 190 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 191 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 192 | i++; |
shahidsajid | 10:6c6e09023942 | 193 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 194 | } |
shahidsajid | 10:6c6e09023942 | 195 | } |
shahidsajid | 10:6c6e09023942 | 196 | |
shahidsajid | 10:6c6e09023942 | 197 | |
shahidsajid | 10:6c6e09023942 | 198 | if (d==3){ //SW |
shahidsajid | 10:6c6e09023942 | 199 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 200 | field[i].dir=SW; |
shahidsajid | 10:6c6e09023942 | 201 | field[i].x=33; |
shahidsajid | 10:6c6e09023942 | 202 | field[i].y=40; |
shahidsajid | 10:6c6e09023942 | 203 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 204 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 205 | i++; |
shahidsajid | 10:6c6e09023942 | 206 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 207 | } |
shahidsajid | 10:6c6e09023942 | 208 | |
shahidsajid | 10:6c6e09023942 | 209 | else{ |
shahidsajid | 10:6c6e09023942 | 210 | field[i].dir=SW; |
shahidsajid | 10:6c6e09023942 | 211 | field[i].x=25; |
shahidsajid | 10:6c6e09023942 | 212 | field[i].y=44; |
shahidsajid | 10:6c6e09023942 | 213 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 214 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 215 | i++; |
shahidsajid | 10:6c6e09023942 | 216 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 217 | } |
shahidsajid | 10:6c6e09023942 | 218 | } |
shahidsajid | 10:6c6e09023942 | 219 | |
shahidsajid | 10:6c6e09023942 | 220 | if (d==4){ //SE |
shahidsajid | 10:6c6e09023942 | 221 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 222 | field[i].dir=SE; |
shahidsajid | 10:6c6e09023942 | 223 | field[i].x=55; |
shahidsajid | 10:6c6e09023942 | 224 | field[i].y=40; |
shahidsajid | 10:6c6e09023942 | 225 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 226 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 227 | i++; |
shahidsajid | 10:6c6e09023942 | 228 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 229 | } |
shahidsajid | 10:6c6e09023942 | 230 | else{ |
shahidsajid | 10:6c6e09023942 | 231 | field[i].dir=SE; |
shahidsajid | 10:6c6e09023942 | 232 | field[i].x=65; |
shahidsajid | 10:6c6e09023942 | 233 | field[i].y=44; |
shahidsajid | 10:6c6e09023942 | 234 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 235 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 236 | i++; |
shahidsajid | 10:6c6e09023942 | 237 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 238 | } |
shahidsajid | 10:6c6e09023942 | 239 | } |
shahidsajid | 10:6c6e09023942 | 240 | |
shahidsajid | 10:6c6e09023942 | 241 | if (d==5){ //W |
shahidsajid | 10:6c6e09023942 | 242 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 243 | field[i].dir=W; |
shahidsajid | 10:6c6e09023942 | 244 | field[i].x=3; |
shahidsajid | 10:6c6e09023942 | 245 | field[i].y=25; |
shahidsajid | 10:6c6e09023942 | 246 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 247 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 248 | i++; |
shahidsajid | 10:6c6e09023942 | 249 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 250 | } |
shahidsajid | 10:6c6e09023942 | 251 | else{ |
shahidsajid | 10:6c6e09023942 | 252 | field[i].dir=W; |
shahidsajid | 10:6c6e09023942 | 253 | field[i].x=25; |
shahidsajid | 10:6c6e09023942 | 254 | field[i].y=25; |
shahidsajid | 10:6c6e09023942 | 255 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 256 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 257 | i++; |
shahidsajid | 10:6c6e09023942 | 258 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 259 | } |
shahidsajid | 13:924891519a95 | 260 | } |
shahidsajid | 10:6c6e09023942 | 261 | if (d==6){ //W |
shahidsajid | 10:6c6e09023942 | 262 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 263 | field[i].dir=E; |
shahidsajid | 10:6c6e09023942 | 264 | field[i].x=80; |
shahidsajid | 12:954da4f4e565 | 265 | field[i].y=25; |
shahidsajid | 10:6c6e09023942 | 266 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 267 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 268 | i++; |
shahidsajid | 10:6c6e09023942 | 269 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 270 | } |
shahidsajid | 10:6c6e09023942 | 271 | else{ |
shahidsajid | 10:6c6e09023942 | 272 | field[i].dir=E; |
shahidsajid | 10:6c6e09023942 | 273 | field[i].x=50; |
shahidsajid | 12:954da4f4e565 | 274 | field[i].y=25; |
shahidsajid | 10:6c6e09023942 | 275 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 276 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 277 | i++; |
shahidsajid | 10:6c6e09023942 | 278 | fieldersCount++; |
shahidsajid | 13:924891519a95 | 279 | } |
shahidsajid | 10:6c6e09023942 | 280 | } |
shahidsajid | 10:6c6e09023942 | 281 | } |
shahidsajid | 10:6c6e09023942 | 282 | |
shahidsajid | 10:6c6e09023942 | 283 | |
shahidsajid | 10:6c6e09023942 | 284 | } |