ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Revision:
31:eefa1d23a843
Parent:
30:43aace0fdbdf
Child:
32:1bc731f03a30
--- a/Cricket/Cricket.cpp	Wed May 08 01:20:06 2019 +0000
+++ b/Cricket/Cricket.cpp	Wed May 08 12:13:28 2019 +0000
@@ -1,37 +1,44 @@
 #include "Cricket.h"
 
+#define BALL_LIIMT 10
 
+//Constructor for the Cricket Object
 Cricket::Cricket()
 {
 
 }
-
+//Deconstructor for the Cricket Object
 Cricket::~Cricket()
 {
 
 }
-
-void Cricket::init()
+/*Initialises the Ball Object
+  Initialises all the game objects
+  sets the cricekt game variabes to intitial values
+*/
+void Cricket::init(int ball_size,int bat_width,int bat_height)
 {
    _ux.init();
-   _bat.init(4,5);
-   _ball.init(2,3);
+   _bat.init(bat_width,bat_height);
+   _ball.init(ball_size);
    _scoreboard.init();
    _new_round=1;
    direction_set=0;
    _new_game=1;
    _fielders_count=0;
    _init_field_counter=0;
-   init_positions();
+   init_positions();  //sets the positions for ball to be hit
    _set_hit=0;
    _set_loft=0;
    _position_no=-1;
    _check_bowled=0;
-   _ball_limit=3;
+   _ball_limit=BALL_LIIMT;
    _check_hit=0;
    _loft_check=0;
    _check_update=-1;
 }
+
+//Resets the game variables for each round
 void Cricket::round_reset(){
    _ball.reset();
    _bat.reset();
@@ -48,50 +55,56 @@
    _check_bowled=0;
    _check_update=-1;
 }
+//resets the ux method and the game variables for start of new game
 void Cricket::game_reset(){
     //pad.leds_off();
     _ux.reset();
     round_reset();
     _new_game=1;
-    _scoreboard.reset();
-    _ball.reset_ball_count();
+    _scoreboard.reset(); //sets score to 0
+    _ball.reset_ball_count(); //sets ball count to 0
 }
+
+//Calls the first menu when game is booted up
+
 void Cricket::intro(N5110 &lcd){
     _ux.first_menu(lcd);
 }
+/* Method checks the state of the game (start of new game or round) 
+   and calls play_game() accordingly. */
 void Cricket::game(N5110 &lcd,Gamepad &pad){
-     if (_new_game==1){
-         printf("TEST 1\n");
+     if (_new_game==1){  //_new_game acts as a flag to start a new game
          _scoreboard.generate_target();
-         _ux.second_menu(lcd);
+         _ux.second_menu(lcd);  //calls the second menu giving user option to start game or read options 
          _new_game=0;
      }
      if (_new_round==1){
-        if (game_status(lcd)==false){
-            game_reset();
-            _ux.second_menu(lcd);
+        if (game_status(lcd)==false){ //game_status returns false if the game is over
+            game_reset(); //resets the game
         }
-        else{
-          check_ball_count(lcd);
+        else{  
           pad.leds_off();
-          round_reset();
-          set_field(lcd);
-          _ball.increment_ball_count();
-          play_game(lcd,pad);
+          round_reset();  //resets the round variables
+          set_field(lcd); //randomly sets a new field for each round
+          _ball.increment_ball_count(); 
+          play_game(lcd,pad); 
           _new_round=0;
         }
     }
-    else
+    else{
         play_game(lcd,pad);
+    }
 }
+/*Method to check current status of the game
+  returns false is game is over and true if not */
 bool Cricket::game_status(N5110 &lcd){
-    if (_scoreboard.compare_target()==true){
-             printf("COMING FROM HERE");
+    if (_scoreboard.compare_target()==true){   //checks if the player has reached the target score
+             //printf("TARGET BLOCK"); //used for debugging
              _ux.victory_menu(lcd);
              game_reset();
              return false;
-           //}
         }
+        //checks if player has balls played has reached the limit
         else if(check_ball_count(lcd)==true){
            _ux.game_over_menu(lcd,1);
            game_reset();
@@ -101,8 +114,10 @@
             return true;
         }
 }
+/*Boolean function that checks if the current ball count has reached ball limit
+ returns true if ball limit has been reached */
 bool Cricket::check_ball_count(N5110 &lcd){
-    int count=_ball.get_ball_count();
+    int count=_ball.get_ball_count(); //gets ball_count from ball object
     if (count==_ball_limit){
         _new_round=1;
         _ball.reset_ball_count();
@@ -113,6 +128,8 @@
         return false;
     }
 }
+/*Method that checks if the target score has been accomplished
+  and if true prints the Game Won screen */
 void Cricket::check_victory(N5110 &lcd){
     bool _victory_check=_scoreboard.compare_target();
         if (_victory_check==true){
@@ -121,6 +138,10 @@
             _ball.reset_ball_count();
         }
 }
+
+/*Initialises te positions for the ball to be hit
+  The ball can be hit in 7 positions in the game 
+  according to the joystick orientation */
 void Cricket::init_positions(){
     set_init_positions(42,15,N,1);
     set_init_positions(84,30,E,2);
@@ -130,7 +151,7 @@
     set_init_positions(65,2,NE,6);
     set_init_positions(60,44,SE,7);
 }
-
+/*Method to set the ball hit positions*/
 void Cricket::set_init_positions(int x,int y, Direction direction,int no){
     positions[_init_field_counter].x=x;
     positions[_init_field_counter].y=y;
@@ -138,51 +159,70 @@
     positions[_init_field_counter].no=no;
     _init_field_counter++;
 }
+/*Method takes user input and sets the flags for the game accordingly */
 void Cricket::play_game(N5110 &lcd,Gamepad &pad){
-    _check_hit=_bat.get_hitBall(_ux);
+    _check_hit=_bat.get_hit_ball(_ux); //checks if the ball has been hit
     if (_check_hit==1){
         _set_hit=1;
     }
-    _loft_check=_bat.get_loft_ball(_ux);
+    _loft_check=_bat.get_loft_ball(_ux); //checks if the ball hit is lofted
     if (_loft_check==1){
         _set_loft=1;
     }
-    Direction dir=pad.get_direction();
-    if (_check_bowled!=1){
+    Direction dir=pad.get_direction(); //sets ball direction 
+    if (_check_bowled!=1){ //checks if the ball has been bowled
         _check_bowled=_ball.ball_start(pad);
     }
     else{
-        update_game(_set_hit, _set_loft, dir,pad,lcd);
+        update_game(_set_hit, _set_loft, dir,pad,lcd); //updates the game
     }
 }
 void Cricket::update_game(int checkHit,int _loft_check, Direction dir,Gamepad &pad,N5110 &lcd){
-  set_ball_direction(dir);
-  int fielder_check=check_fielder(ball_direction);
-  if (checkHit!=1)
-      batsman_out(2,pad,lcd);
-  if(direction_set==1 && checkHit==1){
-  if (fielder_check!=-1){
+  set_ball_direction(dir); //sets the direction for ball to be hit
+  int fielder_check=check_fielder(ball_direction); //checks if there is a fielder in direction of ball
+  
+  if (checkHit!=1){ //checks if ball has been hit
+      batsman_out(2,pad,lcd); //if false, batsman is out and game over
+  }    
+  if(direction_set==1 && checkHit==1){ //if direction is set and ball is hit game continues
+  // checks if there is a fielder present in the direction of ball hit
+  if (fielder_check!=-1){ 
+     /*updates the position of the ball according the to direction and placement of the fielder 
+       returns 1 once ball has reached fielder */
      int _check_update=_ball.update_ball(field[fielder_check].x,field[fielder_check].y);
-     printf("Location x %i y %i\n",field[fielder_check].x,field[fielder_check].y);
-     if (_loft_check==1){
-         if (_check_update==1)
-           batsman_out(3,pad,lcd);
+     
+     //printf("Location x %i y %i\n",field[fielder_check].x,field[fielder_check].y);
+     
+     if (_loft_check==1){ //checks if hit is lofted
+         if (_check_update==1) 
+           batsman_out(3,pad,lcd); //lofted hit to a fielder is out and game over
      }
+     //if batsman is not out, scoreboard is updating according to the runs scored
      else
          update_scoreboard(_check_update,field[fielder_check].position+1,pad);
+        /*position+1 is used for runs argument  because a fielder inside the circle has position 0 and prodices 1 run
+          whereas a fielder outside the circle has position number 1 and produces 2 runs. */
   }
+  //If there is no fielder present
   if (fielder_check==-1){
         _check_update=_ball.update_ball(positions[_position_no].x,positions[_position_no].y);
-        printf("location x %i y %i\n  update %i\n",positions[_position_no].x,positions[_position_no].y,_check_update);
+        //printf("location x %i y %i\n  update %i\n",positions[_position_no].x,positions[_position_no].y,_check_update);
         if (_loft_check==1)
-         update_scoreboard(_check_update,6,pad);
+         update_scoreboard(_check_update,6,pad); //lofted shot in the gap is 6 runs
         else
-            update_scoreboard(_check_update,4,pad);
+            update_scoreboard(_check_update,4,pad); //normal shot in the gap is 4 runs
     }
 }
 }
-void Cricket::update_scoreboard(int checkUpdate, int runs,Gamepad &pad){
-    if (checkUpdate==1){
+
+/* Method to update the score of the game
+   Takes runs and check_update as arguments */
+void Cricket::update_scoreboard(int check_update, int runs,Gamepad &pad){
+    //runs can only be updated after the ball has reached the destination co-ordinates
+    if (check_update==1){ 
+       /* if ball is hit straight there are no runs given
+          This was done because hitting straight would form an easy way
+          of scoring runs every round and would not make the game challenging */
         if (ball_direction==N){
             _scoreboard.update_score(0);
             pad.led(3,1.0);
@@ -191,59 +231,66 @@
         }
         else{
             _scoreboard.update_score(runs);
-            pad.led(3,1.0);
+            pad.led(3,1.0);  //LED flashes to inform user that runs has been updated and get ready for new round
             wait(0.5);
             _new_round=1;
         }
     }
     pad.leds_off();
 }
-
+/*Method when batsman is out and game is over
+  Method resets game and prints a screen to the LCD to inform user that game is over
+*/
 void Cricket::batsman_out(int option,Gamepad &pad, N5110 &lcd){
     _scoreboard.reset();
-    pad.led(1,1.0);
-    //pad.tone(750.0,0.5);
+    pad.led(1,1.0); //red LED indicates that the game is over
+    pad.tone(750.0,0.5); //tone indicates that game is over
     _ball.reset_ball_count();
     _ux.game_over_menu(lcd,option);
     game_reset();
     _new_game=1;
     wait(2);
     pad.leds_off();
-     _ux.second_menu(lcd);
 }
+//Draws all the components of the Cricket to the LCD
 void Cricket::draw(N5110 &lcd){
     lcd.drawCircle((WIDTH/2),HEIGHT/2,23,FILL_TRANSPARENT);
     lcd.drawRect(37,11,12,30,FILL_TRANSPARENT);
+    //calls draw method of game objects
     _ball.draw(lcd);
     draw_field(lcd);
     _bat.draw(lcd);
     _scoreboard.draw(lcd);
 }
+//Method to set the ball direction
 void Cricket::set_ball_direction(Direction dir){
     if (direction_set!=1){
         for (int i=0;i<_init_field_counter+1;i++){
             if(dir==positions[i].dir){
                 ball_direction=dir;
                 printf(" BALL DIRECTION %i x %i y %i \n",positions[i].no,positions[i].x,positions[i].y);
-                _position_no=i;
-                direction_set=1;
+                _position_no=i; //index of direction in array is stoed in the variable
+                direction_set=1; //toggles direction_set flag
                 break;
             }
         }
     }
 }
+/*Method that checks if a fielder is present in the direction of the ball*/
 int Cricket::check_fielder(Direction dir){
-    Vector2D p;
     if (dir==N){
         return -1;
     }
+    /*loops through fielder array and checks if fielder is present in the direction*/
     for (int i=0;i<8;i++){
       if(dir == field[i].dir){
          return i;
      }
    }
+   //returns -1 if no fielder is present
    return -1;
   }
+/*Draws the fielders by printing each fielder in the array to the LCD*/
 void Cricket::draw_field(N5110 &lcd){
     lcd.drawCircle(field[0].x,field[0].y,2,FILL_TRANSPARENT);
     lcd.drawCircle(field[1].x,field[1].y,2,FILL_TRANSPARENT);
@@ -251,6 +298,9 @@
     lcd.drawCircle(field[3].x,field[3].y,2,FILL_TRANSPARENT);
     lcd.drawCircle(field[4].x,field[4].y,2,FILL_TRANSPARENT);
 }
+/*Method to set the fielders for the game 
+ Loops through a for loop 6 times, generating a random number each time
+ and sets a fielder based on the randon number*/
 void Cricket::set_field(N5110 &lcd){
     int d=0;
     int pos=0;
@@ -290,7 +340,6 @@
             }
         }
 
-
         if (d==2){ //NE
             if (pos==1){
             field[i].dir=NE;
@@ -311,8 +360,6 @@
                   _fielders_count++;
             }
         }
-
-
         if (d==3){ //SW
             if (pos==1){
                 field[i].dir=SW;
@@ -334,7 +381,6 @@
               _fielders_count++;
             }
         }
-
         if (d==4){ //SE
             if (pos==1){
               field[i].dir=SE;
@@ -398,18 +444,4 @@
         }
   }
 }
-  void Cricket::info_screen(N5110 &lcd,int target){
-    lcd.clear();
-    lcd.printString("YOU ARE GIVEN",2,1);
-    lcd.printString("10 BALLS ",3,2);
-    lcd.refresh();
-    wait(0.5);
-    char buffer[5];
-    int length=sprintf(buffer,"SCORE %i RUNS",target);
-    lcd.printString(buffer,2,4);
-    lcd.refresh();
-    wait(0.5);
-    lcd.printString("GOOD LUCK!!!",0,5);
-    lcd.refresh();
-    wait(3);
-  }
+ 
\ No newline at end of file