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.h@32:1bc731f03a30, 2019-05-08 (annotated)
- Committer:
- shahidsajid
- Date:
- Wed May 08 13:15:45 2019 +0000
- Revision:
- 32:1bc731f03a30
- Parent:
- 31:eefa1d23a843
- Child:
- 33:9d34ef219fff
Completed Final Documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shahidsajid | 10:6c6e09023942 | 1 | #ifndef CRICKET_H |
shahidsajid | 10:6c6e09023942 | 2 | #define CRICKET_H |
shahidsajid | 10:6c6e09023942 | 3 | |
shahidsajid | 10:6c6e09023942 | 4 | #include "mbed.h" |
shahidsajid | 10:6c6e09023942 | 5 | #include "N5110.h" |
shahidsajid | 10:6c6e09023942 | 6 | #include "Gamepad.h" |
shahidsajid | 10:6c6e09023942 | 7 | #include "Ball.h" |
shahidsajid | 14:122eaa3b7a50 | 8 | #include "Scoreboard.h" |
shahidsajid | 21:a0904159e183 | 9 | #include "UX.h" |
shahidsajid | 10:6c6e09023942 | 10 | |
shahidsajid | 10:6c6e09023942 | 11 | /** Ball Class |
shahidsajid | 32:1bc731f03a30 | 12 | * @author Shahid Zubin Sajid el17szs |
shahidsajid | 32:1bc731f03a30 | 13 | * @brief Controls the bat in the Hero Cricket Game |
shahidsajid | 32:1bc731f03a30 | 14 | * @date May 2019 |
shahidsajid | 10:6c6e09023942 | 15 | */ |
shahidsajid | 10:6c6e09023942 | 16 | class Cricket |
shahidsajid | 10:6c6e09023942 | 17 | { |
shahidsajid | 10:6c6e09023942 | 18 | |
shahidsajid | 10:6c6e09023942 | 19 | public: |
shahidsajid | 32:1bc731f03a30 | 20 | /** Constructor */ |
shahidsajid | 10:6c6e09023942 | 21 | Cricket(); |
shahidsajid | 32:1bc731f03a30 | 22 | /** Deconstructor */ |
shahidsajid | 10:6c6e09023942 | 23 | ~Cricket(); |
shahidsajid | 30:43aace0fdbdf | 24 | /** |
shahidsajid | 28:d0b0a64a832d | 25 | * @brief Initlialises the Cricket class Object |
shahidsajid | 28:d0b0a64a832d | 26 | * @details Intialises the bat variables to the starting values |
shahidsajid | 28:d0b0a64a832d | 27 | * @details Initialises the other class objects used in the game |
shahidsajid | 28:d0b0a64a832d | 28 | */ |
shahidsajid | 31:eefa1d23a843 | 29 | void init(int ball_size, int bat_width,int bat_height); |
shahidsajid | 30:43aace0fdbdf | 30 | /** |
shahidsajid | 28:d0b0a64a832d | 31 | * @brief Draws the fielders, the outfield circle and the pitch onto the LCD |
shahidsajid | 30:43aace0fdbdf | 32 | * @param &lcd @details reference pointer for the LCD |
shahidsajid | 28:d0b0a64a832d | 33 | */ |
shahidsajid | 10:6c6e09023942 | 34 | void draw(N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 35 | /** |
shahidsajid | 30:43aace0fdbdf | 36 | *@brief sets the positions for the 5 fielders in the game |
shahidsajid | 30:43aace0fdbdf | 37 | *@details randomly generates 5 fielders and sets the fielders co-ordinates and fielder number |
shahidsajid | 30:43aace0fdbdf | 38 | */ |
shahidsajid | 10:6c6e09023942 | 39 | void set_field(N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 40 | /** |
shahidsajid | 28:d0b0a64a832d | 41 | * @brief Method to set the cor-dinates,direction and position for batsman to hit the ball |
shahidsajid | 28:d0b0a64a832d | 42 | * @param x @details Integer value for the x-cordinate |
shahidsajid | 28:d0b0a64a832d | 43 | * @param y @details Integer value for the y-cordinate |
shahidsajid | 28:d0b0a64a832d | 44 | * @param direction @details Direction variable to set the position direction |
shahidsajid | 28:d0b0a64a832d | 45 | * @param no @details integer to store the position number |
shahidsajid | 28:d0b0a64a832d | 46 | */ |
shahidsajid | 13:924891519a95 | 47 | void set_init_positions(int x,int y, Direction direction,int no); |
shahidsajid | 30:43aace0fdbdf | 48 | /** |
shahidsajid | 28:d0b0a64a832d | 49 | * @brief Sets the ball direction and sets _direction_set to 1 |
shahidsajid | 28:d0b0a64a832d | 50 | * @details loops through the positions available and sets the direction to the arguement if found |
shahidsajid | 28:d0b0a64a832d | 51 | * @param dir @details Direction variable recieved as recived from the joystick during gameplay |
shahidsajid | 28:d0b0a64a832d | 52 | */ |
shahidsajid | 30:43aace0fdbdf | 53 | void set_ball_direction(Direction dir); |
shahidsajid | 30:43aace0fdbdf | 54 | /** |
shahidsajid | 28:d0b0a64a832d | 55 | * @brief Draws the 5 outfield filders to the LCD |
shahidsajid | 28:d0b0a64a832d | 56 | * @details Loops through the fielders array and prints the fielders acc. to their cordinates |
shahidsajid | 28:d0b0a64a832d | 57 | * param &lcd @details reference pointer for the LCD |
shahidsajid | 28:d0b0a64a832d | 58 | */ |
shahidsajid | 10:6c6e09023942 | 59 | void draw_field(N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 60 | /** |
shahidsajid | 28:d0b0a64a832d | 61 | * @brief Method to set the cor-dinates,direction and position for batsman to hit the ball |
shahidsajid | 28:d0b0a64a832d | 62 | * @param x @details Integer value for the x-cordinate |
shahidsajid | 28:d0b0a64a832d | 63 | * @param y @details Integer value for the y-cordinate |
shahidsajid | 28:d0b0a64a832d | 64 | * @param direction @details Direction variable to set the position direction |
shahidsajid | 28:d0b0a64a832d | 65 | * @param no @details integer to store the position number |
shahidsajid | 28:d0b0a64a832d | 66 | */ |
shahidsajid | 19:e1ded5acb64a | 67 | void update_game(int checkHit,int loft_check, Direction dir,Gamepad &pad,N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 68 | /** |
shahidsajid | 30:43aace0fdbdf | 69 | * @brief Method checks if ball is hit, if ball is lofted and gets the ball direction |
shahidsajid | 30:43aace0fdbdf | 70 | * @param &lcd @details reference pointer for the LCD |
shahidsajid | 30:43aace0fdbdf | 71 | * @param &pad @details Takes a reference pointer to a Gamepad object |
shahidsajid | 30:43aace0fdbdf | 72 | */ |
shahidsajid | 11:f481ec642cc5 | 73 | void play_game(N5110 &lcd,Gamepad &pad); |
shahidsajid | 30:43aace0fdbdf | 74 | /** |
shahidsajid | 28:d0b0a64a832d | 75 | * @brief Updates the score by adding runs scored during the round to score |
shahidsajid | 28:d0b0a64a832d | 76 | * @param checkUpdate @details integer value which validates that the game has been updated |
shahidsajid | 28:d0b0a64a832d | 77 | * @param runs @details Integer value which stores the runs scored during the round |
shahidsajid | 28:d0b0a64a832d | 78 | * @param &pad @details Takes a reference pointer to a Gamepad object |
shahidsajid | 28:d0b0a64a832d | 79 | */ |
shahidsajid | 31:eefa1d23a843 | 80 | void update_scoreboard(int check_update, int runs,Gamepad &pad); |
shahidsajid | 28:d0b0a64a832d | 81 | /** |
shahidsajid | 28:d0b0a64a832d | 82 | * @brief method when a batsman is out, screen updates and game resets. |
shahidsajid | 28:d0b0a64a832d | 83 | * @param option @details integer value which identifies the way batsman is out |
shahidsajid | 28:d0b0a64a832d | 84 | * @param &pad @details Takes a reference pointer to a Gamepad object |
shahidsajid | 30:43aace0fdbdf | 85 | * @param &lcd @details Reference pointer for the LCD |
shahidsajid | 28:d0b0a64a832d | 86 | */ |
shahidsajid | 19:e1ded5acb64a | 87 | void batsman_out(int option,Gamepad &pad, N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 88 | /** |
shahidsajid | 28:d0b0a64a832d | 89 | * @brief method which compares score and target, if true game is won and game resets |
shahidsajid | 30:43aace0fdbdf | 90 | * @param &lcd @details Reference pointer for the LCD |
shahidsajid | 28:d0b0a64a832d | 91 | */ |
shahidsajid | 18:a260ce8db9e7 | 92 | void check_victory(N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 93 | /** |
shahidsajid | 29:f7a2d2a755ec | 94 | * @brief Method called after each round, checks if it is a new round or new game |
shahidsajid | 29:f7a2d2a755ec | 95 | * @param runs @details Integer value which stores the runs scored during the round |
shahidsajid | 29:f7a2d2a755ec | 96 | * @param &pad @details Takes a reference pointer to a Gamepad object |
shahidsajid | 29:f7a2d2a755ec | 97 | */ |
shahidsajid | 19:e1ded5acb64a | 98 | void game(N5110 &lcd,Gamepad &pad); |
shahidsajid | 30:43aace0fdbdf | 99 | /** |
shahidsajid | 29:f7a2d2a755ec | 100 | * @brief Prints the introduction screen when the game is first loaded up |
shahidsajid | 29:f7a2d2a755ec | 101 | * @param &lcd @details reference pointer for the LCD |
shahidsajid | 29:f7a2d2a755ec | 102 | */ |
shahidsajid | 24:23fd6b451db7 | 103 | void intro(N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 104 | /** |
shahidsajid | 29:f7a2d2a755ec | 105 | * @brief Method which checks if the game is won, lost and updates the game status |
shahidsajid | 29:f7a2d2a755ec | 106 | * @param &lcd @details reference pointer for the LCD |
shahidsajid | 29:f7a2d2a755ec | 107 | */ |
shahidsajid | 24:23fd6b451db7 | 108 | bool game_status(N5110 &lcd); |
shahidsajid | 30:43aace0fdbdf | 109 | /** |
shahidsajid | 29:f7a2d2a755ec | 110 | * @brief Sets the positions for where the ball can be hit |
shahidsajid | 30:43aace0fdbdf | 111 | * @details stores the positions in a position array |
shahidsajid | 30:43aace0fdbdf | 112 | */ |
shahidsajid | 12:954da4f4e565 | 113 | void init_positions(); |
shahidsajid | 30:43aace0fdbdf | 114 | /** |
shahidsajid | 30:43aace0fdbdf | 115 | * @brief Resets the game variables for each round |
shahidsajid | 30:43aace0fdbdf | 116 | */ |
shahidsajid | 19:e1ded5acb64a | 117 | void round_reset(); |
shahidsajid | 29:f7a2d2a755ec | 118 | /** |
shahidsajid | 29:f7a2d2a755ec | 119 | * @brief Resets the variables for the start of each game |
shahidsajid | 30:43aace0fdbdf | 120 | */ |
shahidsajid | 19:e1ded5acb64a | 121 | void game_reset(); |
shahidsajid | 30:43aace0fdbdf | 122 | /** |
shahidsajid | 29:f7a2d2a755ec | 123 | * @brief Method which checks if a fielder is present in the direciton of ball hit |
shahidsajid | 30:43aace0fdbdf | 124 | * @param dir @details Direction variable which stores the direction of the ball |
shahidsajid | 29:f7a2d2a755ec | 125 | * @returns an integer value fielder no if a fielder is present, -1 if no fielder |
shahidsajid | 30:43aace0fdbdf | 126 | */ |
shahidsajid | 13:924891519a95 | 127 | int check_fielder(Direction dir); |
shahidsajid | 30:43aace0fdbdf | 128 | /** |
shahidsajid | 29:f7a2d2a755ec | 129 | * @brief Method which checks if a fielder is present in the direciton of ball hit |
shahidsajid | 29:f7a2d2a755ec | 130 | * @returns a boolean value,true if ball count is equal to ball limit and false if it's < ball limit |
shahidsajid | 30:43aace0fdbdf | 131 | */ |
shahidsajid | 19:e1ded5acb64a | 132 | bool check_ball_count(N5110 &lcd); |
shahidsajid | 10:6c6e09023942 | 133 | private: |
shahidsajid | 29:f7a2d2a755ec | 134 | //Ball object variable |
shahidsajid | 30:43aace0fdbdf | 135 | Ball _ball; |
shahidsajid | 29:f7a2d2a755ec | 136 | //Bat object variable |
shahidsajid | 30:43aace0fdbdf | 137 | Bat _bat; |
shahidsajid | 29:f7a2d2a755ec | 138 | //Scoreboard object variable |
shahidsajid | 30:43aace0fdbdf | 139 | Scoreboard _scoreboard; |
shahidsajid | 29:f7a2d2a755ec | 140 | //UX object variable |
shahidsajid | 30:43aace0fdbdf | 141 | UX _ux; |
shahidsajid | 30:43aace0fdbdf | 142 | //Fielder Positions struct which holds the x & y cordinates and fielder no. |
shahidsajid | 12:954da4f4e565 | 143 | struct fielder_positions{ |
shahidsajid | 12:954da4f4e565 | 144 | Direction dir; |
shahidsajid | 12:954da4f4e565 | 145 | int x; |
shahidsajid | 12:954da4f4e565 | 146 | int y; |
shahidsajid | 12:954da4f4e565 | 147 | int no; |
shahidsajid | 12:954da4f4e565 | 148 | }; |
shahidsajid | 30:43aace0fdbdf | 149 | |
shahidsajid | 29:f7a2d2a755ec | 150 | //Array which stores the positions for the ball to be hit |
shahidsajid | 12:954da4f4e565 | 151 | fielder_positions positions[7]; |
shahidsajid | 30:43aace0fdbdf | 152 | |
shahidsajid | 29:f7a2d2a755ec | 153 | /*Struct created for fielders that are in the outfield |
shahidsajid | 30:43aace0fdbdf | 154 | stores the fielder cordinates on the LCD and the position no. |
shahidsajid | 29:f7a2d2a755ec | 155 | */ |
shahidsajid | 10:6c6e09023942 | 156 | struct Fielder{ |
shahidsajid | 10:6c6e09023942 | 157 | Direction dir; |
shahidsajid | 10:6c6e09023942 | 158 | int x; |
shahidsajid | 10:6c6e09023942 | 159 | int y; |
shahidsajid | 10:6c6e09023942 | 160 | int position; |
shahidsajid | 10:6c6e09023942 | 161 | }; |
shahidsajid | 32:1bc731f03a30 | 162 | /**Fielder array that stores the no. of fielders and it's characteristics*/ |
shahidsajid | 10:6c6e09023942 | 163 | Fielder field[5]; |
shahidsajid | 32:1bc731f03a30 | 164 | /**integer variable that checks if direction of ball has been set during gameplay*/ |
shahidsajid | 30:43aace0fdbdf | 165 | int direction_set; |
shahidsajid | 30:43aace0fdbdf | 166 | Vector2D ball_position; |
shahidsajid | 32:1bc731f03a30 | 167 | /**Direction object to store the ball direction*/ |
shahidsajid | 30:43aace0fdbdf | 168 | Direction ball_direction; |
shahidsajid | 32:1bc731f03a30 | 169 | /**integer variable which checks if the ball has been bowled |
shahidsajid | 30:43aace0fdbdf | 170 | it is used as a flag */ |
shahidsajid | 26:6427f09cf8d3 | 171 | int _check_bowled; |
shahidsajid | 32:1bc731f03a30 | 172 | /**Integer variable sued as counter positions initialised in the field**/ |
shahidsajid | 26:6427f09cf8d3 | 173 | int _init_field_counter; |
shahidsajid | 32:1bc731f03a30 | 174 | /**Integer value used as a counter for the no. of fielders*/ |
shahidsajid | 26:6427f09cf8d3 | 175 | int _fielders_count; |
shahidsajid | 32:1bc731f03a30 | 176 | /**Integer variable used as a flag to indicate start of new round*/ |
shahidsajid | 26:6427f09cf8d3 | 177 | int _new_round; |
shahidsajid | 32:1bc731f03a30 | 178 | /**Integer array to store no. of fielders during set_field()*/ |
shahidsajid | 30:43aace0fdbdf | 179 | int fieldNumbers[10]; |
shahidsajid | 32:1bc731f03a30 | 180 | /**Integer variable used as an array index for the position in the outfield where |
shahidsajid | 29:f7a2d2a755ec | 181 | the ball has been hit*/ |
shahidsajid | 20:9d21599fe350 | 182 | int _position_no; |
shahidsajid | 32:1bc731f03a30 | 183 | /**Integer variable that stores no. of balls that are to be played during each game*/ |
shahidsajid | 26:6427f09cf8d3 | 184 | int _ball_limit; |
shahidsajid | 29:f7a2d2a755ec | 185 | //Integer variable used as a flag to indicate start of new game |
shahidsajid | 26:6427f09cf8d3 | 186 | int _new_game; |
shahidsajid | 32:1bc731f03a30 | 187 | /**Integer variable used as a flag to indicate that ball has reached destination fielder*/ |
shahidsajid | 26:6427f09cf8d3 | 188 | int _check_update; |
shahidsajid | 32:1bc731f03a30 | 189 | /**Integer variable used to check if the ball has been hit*/ |
shahidsajid | 26:6427f09cf8d3 | 190 | int _check_hit; |
shahidsajid | 32:1bc731f03a30 | 191 | /**Integer variable used as a flag to indicate that ball has been hit*/ |
shahidsajid | 26:6427f09cf8d3 | 192 | int _set_hit; |
shahidsajid | 32:1bc731f03a30 | 193 | /**Integer variable used as a flag to indicate that the hit is lofted*/ |
shahidsajid | 26:6427f09cf8d3 | 194 | int _set_loft; |
shahidsajid | 32:1bc731f03a30 | 195 | /**Integer variable used to check if the hit is lofted*/ |
shahidsajid | 30:43aace0fdbdf | 196 | int _loft_check; |
shahidsajid | 10:6c6e09023942 | 197 | }; |
shahidsajid | 10:6c6e09023942 | 198 | #endif |