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