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