ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Committer:
shahidsajid
Date:
Thu May 09 10:31:34 2019 +0000
Revision:
35:dc957021982c
Parent:
34:d9099874bbc3
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shahidsajid 10:6c6e09023942 1 #include "Cricket.h"
shahidsajid 10:6c6e09023942 2
shahidsajid 31:eefa1d23a843 3 #define BALL_LIIMT 10
shahidsajid 10:6c6e09023942 4
shahidsajid 31:eefa1d23a843 5 //Constructor for the Cricket Object
shahidsajid 10:6c6e09023942 6 Cricket::Cricket()
shahidsajid 10:6c6e09023942 7 {
shahidsajid 10:6c6e09023942 8
shahidsajid 10:6c6e09023942 9 }
shahidsajid 31:eefa1d23a843 10 //Deconstructor for the Cricket Object
shahidsajid 10:6c6e09023942 11 Cricket::~Cricket()
shahidsajid 10:6c6e09023942 12 {
shahidsajid 10:6c6e09023942 13
shahidsajid 10:6c6e09023942 14 }
shahidsajid 31:eefa1d23a843 15 /*Initialises the Ball Object
shahidsajid 31:eefa1d23a843 16 Initialises all the game objects
shahidsajid 31:eefa1d23a843 17 sets the cricekt game variabes to intitial values
shahidsajid 31:eefa1d23a843 18 */
shahidsajid 31:eefa1d23a843 19 void Cricket::init(int ball_size,int bat_width,int bat_height)
shahidsajid 10:6c6e09023942 20 {
shahidsajid 30:43aace0fdbdf 21 _ux.init();
shahidsajid 31:eefa1d23a843 22 _bat.init(bat_width,bat_height);
shahidsajid 31:eefa1d23a843 23 _ball.init(ball_size);
shahidsajid 30:43aace0fdbdf 24 _scoreboard.init();
shahidsajid 26:6427f09cf8d3 25 _new_round=1;
shahidsajid 30:43aace0fdbdf 26 direction_set=0;
shahidsajid 26:6427f09cf8d3 27 _new_game=1;
shahidsajid 26:6427f09cf8d3 28 _fielders_count=0;
shahidsajid 26:6427f09cf8d3 29 _init_field_counter=0;
shahidsajid 31:eefa1d23a843 30 init_positions(); //sets the positions for ball to be hit
shahidsajid 26:6427f09cf8d3 31 _set_hit=0;
shahidsajid 26:6427f09cf8d3 32 _set_loft=0;
shahidsajid 20:9d21599fe350 33 _position_no=-1;
shahidsajid 26:6427f09cf8d3 34 _check_bowled=0;
shahidsajid 31:eefa1d23a843 35 _ball_limit=BALL_LIIMT;
shahidsajid 26:6427f09cf8d3 36 _check_hit=0;
shahidsajid 26:6427f09cf8d3 37 _loft_check=0;
shahidsajid 26:6427f09cf8d3 38 _check_update=-1;
shahidsajid 13:924891519a95 39 }
shahidsajid 31:eefa1d23a843 40
shahidsajid 31:eefa1d23a843 41 //Resets the game variables for each round
shahidsajid 19:e1ded5acb64a 42 void Cricket::round_reset(){
shahidsajid 30:43aace0fdbdf 43 _ball.reset();
shahidsajid 30:43aace0fdbdf 44 _bat.reset();
shahidsajid 26:6427f09cf8d3 45 _new_round=0;
shahidsajid 30:43aace0fdbdf 46 direction_set=0;
shahidsajid 26:6427f09cf8d3 47 _fielders_count=0;
shahidsajid 26:6427f09cf8d3 48 _loft_check=0;
shahidsajid 26:6427f09cf8d3 49 _check_hit=0;
shahidsajid 26:6427f09cf8d3 50 _set_hit=0;
shahidsajid 30:43aace0fdbdf 51 _set_loft=
shahidsajid 26:6427f09cf8d3 52 _init_field_counter=0;
shahidsajid 13:924891519a95 53 init_positions();
shahidsajid 20:9d21599fe350 54 _position_no=-1;
shahidsajid 26:6427f09cf8d3 55 _check_bowled=0;
shahidsajid 26:6427f09cf8d3 56 _check_update=-1;
shahidsajid 19:e1ded5acb64a 57 }
shahidsajid 35:dc957021982c 58 //resets the ux method and the game variables for start of new game.
shahidsajid 19:e1ded5acb64a 59 void Cricket::game_reset(){
shahidsajid 24:23fd6b451db7 60 //pad.leds_off();
shahidsajid 30:43aace0fdbdf 61 _ux.reset();
shahidsajid 19:e1ded5acb64a 62 round_reset();
shahidsajid 26:6427f09cf8d3 63 _new_game=1;
shahidsajid 31:eefa1d23a843 64 _scoreboard.reset(); //sets score to 0
shahidsajid 31:eefa1d23a843 65 _ball.reset_ball_count(); //sets ball count to 0
shahidsajid 10:6c6e09023942 66 }
shahidsajid 31:eefa1d23a843 67
shahidsajid 31:eefa1d23a843 68 //Calls the first menu when game is booted up
shahidsajid 31:eefa1d23a843 69
shahidsajid 24:23fd6b451db7 70 void Cricket::intro(N5110 &lcd){
shahidsajid 30:43aace0fdbdf 71 _ux.first_menu(lcd);
shahidsajid 24:23fd6b451db7 72 }
shahidsajid 31:eefa1d23a843 73 /* Method checks the state of the game (start of new game or round)
shahidsajid 31:eefa1d23a843 74 and calls play_game() accordingly. */
shahidsajid 11:f481ec642cc5 75 void Cricket::game(N5110 &lcd,Gamepad &pad){
shahidsajid 31:eefa1d23a843 76 if (_new_game==1){ //_new_game acts as a flag to start a new game
shahidsajid 30:43aace0fdbdf 77 _scoreboard.generate_target();
shahidsajid 31:eefa1d23a843 78 _ux.second_menu(lcd); //calls the second menu giving user option to start game or read options
shahidsajid 30:43aace0fdbdf 79 _new_game=0;
shahidsajid 18:a260ce8db9e7 80 }
shahidsajid 30:43aace0fdbdf 81 if (_new_round==1){
shahidsajid 31:eefa1d23a843 82 if (game_status(lcd)==false){ //game_status returns false if the game is over
shahidsajid 31:eefa1d23a843 83 game_reset(); //resets the game
shahidsajid 25:ead0e7bd1569 84 }
shahidsajid 31:eefa1d23a843 85 else{
shahidsajid 19:e1ded5acb64a 86 pad.leds_off();
shahidsajid 31:eefa1d23a843 87 round_reset(); //resets the round variables
shahidsajid 31:eefa1d23a843 88 set_field(lcd); //randomly sets a new field for each round
shahidsajid 31:eefa1d23a843 89 _ball.increment_ball_count();
shahidsajid 31:eefa1d23a843 90 play_game(lcd,pad);
shahidsajid 26:6427f09cf8d3 91 _new_round=0;
shahidsajid 19:e1ded5acb64a 92 }
shahidsajid 13:924891519a95 93 }
shahidsajid 31:eefa1d23a843 94 else{
shahidsajid 13:924891519a95 95 play_game(lcd,pad);
shahidsajid 31:eefa1d23a843 96 }
shahidsajid 10:6c6e09023942 97 }
shahidsajid 31:eefa1d23a843 98 /*Method to check current status of the game
shahidsajid 31:eefa1d23a843 99 returns false is game is over and true if not */
shahidsajid 24:23fd6b451db7 100 bool Cricket::game_status(N5110 &lcd){
shahidsajid 31:eefa1d23a843 101 if (_scoreboard.compare_target()==true){ //checks if the player has reached the target score
shahidsajid 31:eefa1d23a843 102 //printf("TARGET BLOCK"); //used for debugging
shahidsajid 30:43aace0fdbdf 103 _ux.victory_menu(lcd);
shahidsajid 24:23fd6b451db7 104 game_reset();
shahidsajid 24:23fd6b451db7 105 return false;
shahidsajid 24:23fd6b451db7 106 }
shahidsajid 31:eefa1d23a843 107 //checks if player has balls played has reached the limit
shahidsajid 24:23fd6b451db7 108 else if(check_ball_count(lcd)==true){
shahidsajid 30:43aace0fdbdf 109 _ux.game_over_menu(lcd,1);
shahidsajid 24:23fd6b451db7 110 game_reset();
shahidsajid 24:23fd6b451db7 111 return false;
shahidsajid 24:23fd6b451db7 112 }
shahidsajid 24:23fd6b451db7 113 else{
shahidsajid 24:23fd6b451db7 114 return true;
shahidsajid 24:23fd6b451db7 115 }
shahidsajid 24:23fd6b451db7 116 }
shahidsajid 31:eefa1d23a843 117 /*Boolean function that checks if the current ball count has reached ball limit
shahidsajid 31:eefa1d23a843 118 returns true if ball limit has been reached */
shahidsajid 19:e1ded5acb64a 119 bool Cricket::check_ball_count(N5110 &lcd){
shahidsajid 31:eefa1d23a843 120 int count=_ball.get_ball_count(); //gets ball_count from ball object
shahidsajid 18:a260ce8db9e7 121 if (count==_ball_limit){
shahidsajid 26:6427f09cf8d3 122 _new_round=1;
shahidsajid 30:43aace0fdbdf 123 _ball.reset_ball_count();
shahidsajid 26:6427f09cf8d3 124 _new_game=1;
shahidsajid 19:e1ded5acb64a 125 return true;
shahidsajid 19:e1ded5acb64a 126 }
shahidsajid 19:e1ded5acb64a 127 else{
shahidsajid 19:e1ded5acb64a 128 return false;
shahidsajid 18:a260ce8db9e7 129 }
shahidsajid 18:a260ce8db9e7 130 }
shahidsajid 31:eefa1d23a843 131 /*Method that checks if the target score has been accomplished
shahidsajid 31:eefa1d23a843 132 and if true prints the Game Won screen */
shahidsajid 18:a260ce8db9e7 133 void Cricket::check_victory(N5110 &lcd){
shahidsajid 30:43aace0fdbdf 134 bool _victory_check=_scoreboard.compare_target();
shahidsajid 18:a260ce8db9e7 135 if (_victory_check==true){
shahidsajid 30:43aace0fdbdf 136 _ux.victory_menu(lcd);
shahidsajid 26:6427f09cf8d3 137 _new_game=1;
shahidsajid 30:43aace0fdbdf 138 _ball.reset_ball_count();
shahidsajid 18:a260ce8db9e7 139 }
shahidsajid 18:a260ce8db9e7 140 }
shahidsajid 31:eefa1d23a843 141
shahidsajid 31:eefa1d23a843 142 /*Initialises te positions for the ball to be hit
shahidsajid 31:eefa1d23a843 143 The ball can be hit in 7 positions in the game
shahidsajid 31:eefa1d23a843 144 according to the joystick orientation */
shahidsajid 12:954da4f4e565 145 void Cricket::init_positions(){
shahidsajid 20:9d21599fe350 146 set_init_positions(42,15,N,1);
shahidsajid 15:81a3aaf52647 147 set_init_positions(84,30,E,2);
shahidsajid 15:81a3aaf52647 148 set_init_positions(0,30,W,3);
shahidsajid 15:81a3aaf52647 149 set_init_positions(2,44,SW,4);
shahidsajid 15:81a3aaf52647 150 set_init_positions(0,2,NW,5);
shahidsajid 20:9d21599fe350 151 set_init_positions(65,2,NE,6);
shahidsajid 15:81a3aaf52647 152 set_init_positions(60,44,SE,7);
shahidsajid 12:954da4f4e565 153 }
shahidsajid 31:eefa1d23a843 154 /*Method to set the ball hit positions*/
shahidsajid 12:954da4f4e565 155 void Cricket::set_init_positions(int x,int y, Direction direction,int no){
shahidsajid 26:6427f09cf8d3 156 positions[_init_field_counter].x=x;
shahidsajid 26:6427f09cf8d3 157 positions[_init_field_counter].y=y;
shahidsajid 26:6427f09cf8d3 158 positions[_init_field_counter].dir=direction;
shahidsajid 26:6427f09cf8d3 159 positions[_init_field_counter].no=no;
shahidsajid 26:6427f09cf8d3 160 _init_field_counter++;
shahidsajid 12:954da4f4e565 161 }
shahidsajid 31:eefa1d23a843 162 /*Method takes user input and sets the flags for the game accordingly */
shahidsajid 11:f481ec642cc5 163 void Cricket::play_game(N5110 &lcd,Gamepad &pad){
shahidsajid 31:eefa1d23a843 164 _check_hit=_bat.get_hit_ball(_ux); //checks if the ball has been hit
shahidsajid 26:6427f09cf8d3 165 if (_check_hit==1){
shahidsajid 26:6427f09cf8d3 166 _set_hit=1;
shahidsajid 21:a0904159e183 167 }
shahidsajid 31:eefa1d23a843 168 _loft_check=_bat.get_loft_ball(_ux); //checks if the ball hit is lofted
shahidsajid 26:6427f09cf8d3 169 if (_loft_check==1){
shahidsajid 26:6427f09cf8d3 170 _set_loft=1;
shahidsajid 24:23fd6b451db7 171 }
shahidsajid 31:eefa1d23a843 172 Direction dir=pad.get_direction(); //sets ball direction
shahidsajid 31:eefa1d23a843 173 if (_check_bowled!=1){ //checks if the ball has been bowled
shahidsajid 30:43aace0fdbdf 174 _check_bowled=_ball.ball_start(pad);
shahidsajid 12:954da4f4e565 175 }
shahidsajid 12:954da4f4e565 176 else{
shahidsajid 31:eefa1d23a843 177 update_game(_set_hit, _set_loft, dir,pad,lcd); //updates the game
shahidsajid 12:954da4f4e565 178 }
shahidsajid 11:f481ec642cc5 179 }
shahidsajid 30:43aace0fdbdf 180 void Cricket::update_game(int checkHit,int _loft_check, Direction dir,Gamepad &pad,N5110 &lcd){
shahidsajid 31:eefa1d23a843 181 set_ball_direction(dir); //sets the direction for ball to be hit
shahidsajid 31:eefa1d23a843 182 int fielder_check=check_fielder(ball_direction); //checks if there is a fielder in direction of ball
shahidsajid 31:eefa1d23a843 183
shahidsajid 31:eefa1d23a843 184 if (checkHit!=1){ //checks if ball has been hit
shahidsajid 31:eefa1d23a843 185 batsman_out(2,pad,lcd); //if false, batsman is out and game over
shahidsajid 31:eefa1d23a843 186 }
shahidsajid 31:eefa1d23a843 187 if(direction_set==1 && checkHit==1){ //if direction is set and ball is hit game continues
shahidsajid 31:eefa1d23a843 188 // checks if there is a fielder present in the direction of ball hit
shahidsajid 31:eefa1d23a843 189 if (fielder_check!=-1){
shahidsajid 34:d9099874bbc3 190 int _check_update=_ball.update_ball(field[fielder_check].x,field[fielder_check].y); //checks for position of ball
shahidsajid 31:eefa1d23a843 191
shahidsajid 31:eefa1d23a843 192 //printf("Location x %i y %i\n",field[fielder_check].x,field[fielder_check].y);
shahidsajid 31:eefa1d23a843 193
shahidsajid 31:eefa1d23a843 194 if (_loft_check==1){ //checks if hit is lofted
shahidsajid 31:eefa1d23a843 195 if (_check_update==1)
shahidsajid 31:eefa1d23a843 196 batsman_out(3,pad,lcd); //lofted hit to a fielder is out and game over
shahidsajid 13:924891519a95 197 }
shahidsajid 31:eefa1d23a843 198 //if batsman is not out, scoreboard is updating according to the runs scored
shahidsajid 15:81a3aaf52647 199 else
shahidsajid 30:43aace0fdbdf 200 update_scoreboard(_check_update,field[fielder_check].position+1,pad);
shahidsajid 31:eefa1d23a843 201 /*position+1 is used for runs argument because a fielder inside the circle has position 0 and prodices 1 run
shahidsajid 31:eefa1d23a843 202 whereas a fielder outside the circle has position number 1 and produces 2 runs. */
shahidsajid 13:924891519a95 203 }
shahidsajid 31:eefa1d23a843 204 //If there is no fielder present
shahidsajid 13:924891519a95 205 if (fielder_check==-1){
shahidsajid 30:43aace0fdbdf 206 _check_update=_ball.update_ball(positions[_position_no].x,positions[_position_no].y);
shahidsajid 31:eefa1d23a843 207 //printf("location x %i y %i\n update %i\n",positions[_position_no].x,positions[_position_no].y,_check_update);
shahidsajid 30:43aace0fdbdf 208 if (_loft_check==1)
shahidsajid 31:eefa1d23a843 209 update_scoreboard(_check_update,6,pad); //lofted shot in the gap is 6 runs
shahidsajid 15:81a3aaf52647 210 else
shahidsajid 31:eefa1d23a843 211 update_scoreboard(_check_update,4,pad); //normal shot in the gap is 4 runs
shahidsajid 13:924891519a95 212 }
shahidsajid 15:81a3aaf52647 213 }
shahidsajid 30:43aace0fdbdf 214 }
shahidsajid 31:eefa1d23a843 215
shahidsajid 31:eefa1d23a843 216 /* Method to update the score of the game
shahidsajid 31:eefa1d23a843 217 Takes runs and check_update as arguments */
shahidsajid 31:eefa1d23a843 218 void Cricket::update_scoreboard(int check_update, int runs,Gamepad &pad){
shahidsajid 31:eefa1d23a843 219 //runs can only be updated after the ball has reached the destination co-ordinates
shahidsajid 31:eefa1d23a843 220 if (check_update==1){
shahidsajid 31:eefa1d23a843 221 /* if ball is hit straight there are no runs given
shahidsajid 31:eefa1d23a843 222 This was done because hitting straight would form an easy way
shahidsajid 31:eefa1d23a843 223 of scoring runs every round and would not make the game challenging */
shahidsajid 30:43aace0fdbdf 224 if (ball_direction==N){
shahidsajid 30:43aace0fdbdf 225 _scoreboard.update_score(0);
shahidsajid 20:9d21599fe350 226 pad.led(3,1.0);
shahidsajid 20:9d21599fe350 227 wait(0.5);
shahidsajid 26:6427f09cf8d3 228 _new_round=1;
shahidsajid 20:9d21599fe350 229 }
shahidsajid 20:9d21599fe350 230 else{
shahidsajid 30:43aace0fdbdf 231 _scoreboard.update_score(runs);
shahidsajid 31:eefa1d23a843 232 pad.led(3,1.0); //LED flashes to inform user that runs has been updated and get ready for new round
shahidsajid 20:9d21599fe350 233 wait(0.5);
shahidsajid 26:6427f09cf8d3 234 _new_round=1;
shahidsajid 30:43aace0fdbdf 235 }
shahidsajid 14:122eaa3b7a50 236 }
shahidsajid 24:23fd6b451db7 237 pad.leds_off();
shahidsajid 13:924891519a95 238 }
shahidsajid 31:eefa1d23a843 239 /*Method when batsman is out and game is over
shahidsajid 31:eefa1d23a843 240 Method resets game and prints a screen to the LCD to inform user that game is over
shahidsajid 31:eefa1d23a843 241 */
shahidsajid 19:e1ded5acb64a 242 void Cricket::batsman_out(int option,Gamepad &pad, N5110 &lcd){
shahidsajid 30:43aace0fdbdf 243 _scoreboard.reset();
shahidsajid 31:eefa1d23a843 244 pad.led(1,1.0); //red LED indicates that the game is over
shahidsajid 31:eefa1d23a843 245 pad.tone(750.0,0.5); //tone indicates that game is over
shahidsajid 30:43aace0fdbdf 246 _ball.reset_ball_count();
shahidsajid 30:43aace0fdbdf 247 _ux.game_over_menu(lcd,option);
shahidsajid 24:23fd6b451db7 248 game_reset();
shahidsajid 26:6427f09cf8d3 249 _new_game=1;
shahidsajid 15:81a3aaf52647 250 wait(2);
shahidsajid 24:23fd6b451db7 251 pad.leds_off();
shahidsajid 30:43aace0fdbdf 252 }
shahidsajid 31:eefa1d23a843 253 //Draws all the components of the Cricket to the LCD
shahidsajid 13:924891519a95 254 void Cricket::draw(N5110 &lcd){
shahidsajid 13:924891519a95 255 lcd.drawCircle((WIDTH/2),HEIGHT/2,23,FILL_TRANSPARENT);
shahidsajid 13:924891519a95 256 lcd.drawRect(37,11,12,30,FILL_TRANSPARENT);
shahidsajid 31:eefa1d23a843 257 //calls draw method of game objects
shahidsajid 30:43aace0fdbdf 258 _ball.draw(lcd);
shahidsajid 13:924891519a95 259 draw_field(lcd);
shahidsajid 30:43aace0fdbdf 260 _bat.draw(lcd);
shahidsajid 30:43aace0fdbdf 261 _scoreboard.draw(lcd);
shahidsajid 13:924891519a95 262 }
shahidsajid 31:eefa1d23a843 263 //Method to set the ball direction
shahidsajid 13:924891519a95 264 void Cricket::set_ball_direction(Direction dir){
shahidsajid 30:43aace0fdbdf 265 if (direction_set!=1){
shahidsajid 26:6427f09cf8d3 266 for (int i=0;i<_init_field_counter+1;i++){
shahidsajid 20:9d21599fe350 267 if(dir==positions[i].dir){
shahidsajid 30:43aace0fdbdf 268 ball_direction=dir;
shahidsajid 20:9d21599fe350 269 printf(" BALL DIRECTION %i x %i y %i \n",positions[i].no,positions[i].x,positions[i].y);
shahidsajid 31:eefa1d23a843 270 _position_no=i; //index of direction in array is stoed in the variable
shahidsajid 31:eefa1d23a843 271 direction_set=1; //toggles direction_set flag
shahidsajid 30:43aace0fdbdf 272 break;
shahidsajid 20:9d21599fe350 273 }
shahidsajid 20:9d21599fe350 274 }
shahidsajid 12:954da4f4e565 275 }
shahidsajid 12:954da4f4e565 276 }
shahidsajid 31:eefa1d23a843 277 /*Method that checks if a fielder is present in the direction of the ball*/
shahidsajid 13:924891519a95 278 int Cricket::check_fielder(Direction dir){
shahidsajid 20:9d21599fe350 279 if (dir==N){
shahidsajid 20:9d21599fe350 280 return -1;
shahidsajid 20:9d21599fe350 281 }
shahidsajid 31:eefa1d23a843 282 /*loops through fielder array and checks if fielder is present in the direction*/
shahidsajid 15:81a3aaf52647 283 for (int i=0;i<8;i++){
shahidsajid 13:924891519a95 284 if(dir == field[i].dir){
shahidsajid 13:924891519a95 285 return i;
shahidsajid 13:924891519a95 286 }
shahidsajid 13:924891519a95 287 }
shahidsajid 31:eefa1d23a843 288 //returns -1 if no fielder is present
shahidsajid 13:924891519a95 289 return -1;
shahidsajid 10:6c6e09023942 290 }
shahidsajid 31:eefa1d23a843 291 /*Draws the fielders by printing each fielder in the array to the LCD*/
shahidsajid 10:6c6e09023942 292 void Cricket::draw_field(N5110 &lcd){
shahidsajid 13:924891519a95 293 lcd.drawCircle(field[0].x,field[0].y,2,FILL_TRANSPARENT);
shahidsajid 13:924891519a95 294 lcd.drawCircle(field[1].x,field[1].y,2,FILL_TRANSPARENT);
shahidsajid 13:924891519a95 295 lcd.drawCircle(field[2].x,field[2].y,2,FILL_TRANSPARENT);
shahidsajid 13:924891519a95 296 lcd.drawCircle(field[3].x,field[3].y,2,FILL_TRANSPARENT);
shahidsajid 14:122eaa3b7a50 297 lcd.drawCircle(field[4].x,field[4].y,2,FILL_TRANSPARENT);
shahidsajid 10:6c6e09023942 298 }
shahidsajid 31:eefa1d23a843 299 /*Method to set the fielders for the game
shahidsajid 31:eefa1d23a843 300 Loops through a for loop 6 times, generating a random number each time
shahidsajid 31:eefa1d23a843 301 and sets a fielder based on the randon number*/
shahidsajid 10:6c6e09023942 302 void Cricket::set_field(N5110 &lcd){
shahidsajid 10:6c6e09023942 303 int d=0;
shahidsajid 10:6c6e09023942 304 int pos=0;
shahidsajid 10:6c6e09023942 305 int i=0;
shahidsajid 10:6c6e09023942 306 int continueCount=0;
shahidsajid 10:6c6e09023942 307 srand(time(NULL));
shahidsajid 26:6427f09cf8d3 308 while (_fielders_count!=5){
shahidsajid 10:6c6e09023942 309 d = 1+ rand() % 6;
shahidsajid 30:43aace0fdbdf 310 pos=rand()%2;
shahidsajid 10:6c6e09023942 311 for (int j=0;j<10;j++){
shahidsajid 32:1bc731f03a30 312 //checks if the number generated has already been used to set a fielder
shahidsajid 30:43aace0fdbdf 313 if (fieldNumbers[j]==d){
shahidsajid 10:6c6e09023942 314 continueCount=1;
shahidsajid 10:6c6e09023942 315 }
shahidsajid 10:6c6e09023942 316 }
shahidsajid 10:6c6e09023942 317 if (continueCount==1){
shahidsajid 10:6c6e09023942 318 continueCount=0;
shahidsajid 10:6c6e09023942 319 continue;
shahidsajid 10:6c6e09023942 320 }
shahidsajid 32:1bc731f03a30 321 //sets fielders based on random number generated
shahidsajid 10:6c6e09023942 322 if (d==1){ //NW
shahidsajid 10:6c6e09023942 323 if (pos==1){
shahidsajid 10:6c6e09023942 324 field[i].dir=NW;
shahidsajid 10:6c6e09023942 325 field[i].x=30;
shahidsajid 10:6c6e09023942 326 field[i].y=9;
shahidsajid 10:6c6e09023942 327 field[i].position=1;
shahidsajid 30:43aace0fdbdf 328 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 329 i++;
shahidsajid 26:6427f09cf8d3 330 _fielders_count++;
shahidsajid 10:6c6e09023942 331 }
shahidsajid 10:6c6e09023942 332 else{
shahidsajid 10:6c6e09023942 333 field[i].dir=NW;
shahidsajid 10:6c6e09023942 334 field[i].x=25;
shahidsajid 10:6c6e09023942 335 field[i].y=2;
shahidsajid 10:6c6e09023942 336 field[i].position=0;
shahidsajid 30:43aace0fdbdf 337 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 338 i++;
shahidsajid 26:6427f09cf8d3 339 _fielders_count++;
shahidsajid 10:6c6e09023942 340 }
shahidsajid 10:6c6e09023942 341 }
shahidsajid 30:43aace0fdbdf 342
shahidsajid 10:6c6e09023942 343 if (d==2){ //NE
shahidsajid 10:6c6e09023942 344 if (pos==1){
shahidsajid 10:6c6e09023942 345 field[i].dir=NE;
shahidsajid 10:6c6e09023942 346 field[i].x=55;
shahidsajid 10:6c6e09023942 347 field[i].y=9;
shahidsajid 10:6c6e09023942 348 field[i].position=1;
shahidsajid 30:43aace0fdbdf 349 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 350 i++;
shahidsajid 26:6427f09cf8d3 351 _fielders_count++;
shahidsajid 10:6c6e09023942 352 }
shahidsajid 10:6c6e09023942 353 else{
shahidsajid 10:6c6e09023942 354 field[i].dir=NE;
shahidsajid 10:6c6e09023942 355 field[i].x=65;
shahidsajid 10:6c6e09023942 356 field[i].y=2;
shahidsajid 10:6c6e09023942 357 field[i].position=0;
shahidsajid 30:43aace0fdbdf 358 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 359 i++;
shahidsajid 26:6427f09cf8d3 360 _fielders_count++;
shahidsajid 10:6c6e09023942 361 }
shahidsajid 10:6c6e09023942 362 }
shahidsajid 10:6c6e09023942 363 if (d==3){ //SW
shahidsajid 10:6c6e09023942 364 if (pos==1){
shahidsajid 10:6c6e09023942 365 field[i].dir=SW;
shahidsajid 10:6c6e09023942 366 field[i].x=33;
shahidsajid 10:6c6e09023942 367 field[i].y=40;
shahidsajid 10:6c6e09023942 368 field[i].position=1;
shahidsajid 30:43aace0fdbdf 369 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 370 i++;
shahidsajid 26:6427f09cf8d3 371 _fielders_count++;
shahidsajid 10:6c6e09023942 372 }
shahidsajid 30:43aace0fdbdf 373
shahidsajid 10:6c6e09023942 374 else{
shahidsajid 10:6c6e09023942 375 field[i].dir=SW;
shahidsajid 10:6c6e09023942 376 field[i].x=25;
shahidsajid 10:6c6e09023942 377 field[i].y=44;
shahidsajid 10:6c6e09023942 378 field[i].position=0;
shahidsajid 30:43aace0fdbdf 379 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 380 i++;
shahidsajid 26:6427f09cf8d3 381 _fielders_count++;
shahidsajid 10:6c6e09023942 382 }
shahidsajid 10:6c6e09023942 383 }
shahidsajid 10:6c6e09023942 384 if (d==4){ //SE
shahidsajid 10:6c6e09023942 385 if (pos==1){
shahidsajid 10:6c6e09023942 386 field[i].dir=SE;
shahidsajid 10:6c6e09023942 387 field[i].x=55;
shahidsajid 10:6c6e09023942 388 field[i].y=40;
shahidsajid 10:6c6e09023942 389 field[i].position=1;
shahidsajid 30:43aace0fdbdf 390 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 391 i++;
shahidsajid 26:6427f09cf8d3 392 _fielders_count++;
shahidsajid 10:6c6e09023942 393 }
shahidsajid 10:6c6e09023942 394 else{
shahidsajid 10:6c6e09023942 395 field[i].dir=SE;
shahidsajid 10:6c6e09023942 396 field[i].x=65;
shahidsajid 10:6c6e09023942 397 field[i].y=44;
shahidsajid 10:6c6e09023942 398 field[i].position=0;
shahidsajid 30:43aace0fdbdf 399 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 400 i++;
shahidsajid 26:6427f09cf8d3 401 _fielders_count++;
shahidsajid 10:6c6e09023942 402 }
shahidsajid 10:6c6e09023942 403 }
shahidsajid 30:43aace0fdbdf 404
shahidsajid 10:6c6e09023942 405 if (d==5){ //W
shahidsajid 10:6c6e09023942 406 if (pos==1){
shahidsajid 10:6c6e09023942 407 field[i].dir=W;
shahidsajid 10:6c6e09023942 408 field[i].x=3;
shahidsajid 15:81a3aaf52647 409 field[i].y=30;
shahidsajid 10:6c6e09023942 410 field[i].position=1;
shahidsajid 30:43aace0fdbdf 411 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 412 i++;
shahidsajid 26:6427f09cf8d3 413 _fielders_count++;
shahidsajid 10:6c6e09023942 414 }
shahidsajid 10:6c6e09023942 415 else{
shahidsajid 10:6c6e09023942 416 field[i].dir=W;
shahidsajid 10:6c6e09023942 417 field[i].x=25;
shahidsajid 15:81a3aaf52647 418 field[i].y=30;
shahidsajid 10:6c6e09023942 419 field[i].position=0;
shahidsajid 30:43aace0fdbdf 420 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 421 i++;
shahidsajid 26:6427f09cf8d3 422 _fielders_count++;
shahidsajid 10:6c6e09023942 423 }
shahidsajid 30:43aace0fdbdf 424 }
shahidsajid 10:6c6e09023942 425 if (d==6){ //W
shahidsajid 10:6c6e09023942 426 if (pos==1){
shahidsajid 10:6c6e09023942 427 field[i].dir=E;
shahidsajid 10:6c6e09023942 428 field[i].x=80;
shahidsajid 15:81a3aaf52647 429 field[i].y=30;
shahidsajid 10:6c6e09023942 430 field[i].position=1;
shahidsajid 30:43aace0fdbdf 431 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 432 i++;
shahidsajid 26:6427f09cf8d3 433 _fielders_count++;
shahidsajid 10:6c6e09023942 434 }
shahidsajid 10:6c6e09023942 435 else{
shahidsajid 10:6c6e09023942 436 field[i].dir=E;
shahidsajid 15:81a3aaf52647 437 field[i].x=60;
shahidsajid 15:81a3aaf52647 438 field[i].y=30;
shahidsajid 15:81a3aaf52647 439 field[i].position=0;
shahidsajid 30:43aace0fdbdf 440 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 441 i++;
shahidsajid 26:6427f09cf8d3 442 _fielders_count++;
shahidsajid 30:43aace0fdbdf 443 }
shahidsajid 10:6c6e09023942 444 }
shahidsajid 10:6c6e09023942 445 }
shahidsajid 24:23fd6b451db7 446 }
shahidsajid 31:eefa1d23a843 447