This class is the engine of the program. It encapsulates all the methods to do with managing scores, commands and player states(dead/alive).

Files at this revision

API Documentation at this revision

Comitter:
domkay97
Date:
Tue May 02 18:05:38 2017 +0000
Parent:
12:bdaf4e1d615e
Commit message:
Changed some method names, added more comments

Changed in this revision

Operator.cpp Show annotated file Show diff for this revision Revisions of this file
Operator.h Show annotated file Show diff for this revision Revisions of this file
--- a/Operator.cpp	Tue May 02 14:02:32 2017 +0000
+++ b/Operator.cpp	Tue May 02 18:05:38 2017 +0000
@@ -19,32 +19,32 @@
     ctrl.ledsOFF();
     int instruction_val = random_instruction(display, lcd);     //sets instruction_val as the random instruction from display
     //printf("OPP:test_player instruction_val = %d\n", instruction_val);
-    display.drawCircle(ctrl, lcd);                               //Draws circle, displays instruction and allows the circle to be drawn faster with time
+    display.drawCircle(ctrl, lcd);                                    //Draws circle, displays instruction and allows the circle to be drawn faster with time
     int button_val = ctrl.check_for_buttons();                       //sets button_val as the instruction performed by the user
     //printf("OPP:test_player button_val = %d\n", button_val);
 
 
-    if (button_val == instruction_val) {                        //if the user performs the instruction correctly perform:
+    if (button_val == instruction_val) {         //if the user performs the instruction correctly return true
 
         return true;
-    } else {
+    } else {                                    //otherwise the user has performed the instruction incorrectly return false
         return false;
-    }                                                   //otherwise the user has performed the instruction incorrectly so perform:
+    }                                              
 }
 
 int Operator::random_instruction(Display &display, N5110 &lcd)
 {
 
-    int ran = rand() % 7 + 1;
+    int ran = rand() % 7 + 1;                   //generates random numbers between 0 and 8.
     //printf("OPP ran = %d\n", ran);
     display.display_instruction(lcd, ran);
     return ran;
 }
 
-void Operator::correct(int next_player, Controller &ctrl)
+void Operator::correct(int current_player, Controller &ctrl)
 {
-    myplayers[next_player]._score++;                                                //increment score for player
-    _score = myplayers[next_player]._score;
+    myplayers[current_player]._score++;                                                //increment score for player
+    _score = myplayers[current_player]._score;
     double _freqChange = _score*20;
     //printf("OPP score = %d\n", score);
     ctrl.led(1,1);
@@ -53,17 +53,17 @@
     ctrl.sound(50.0 + _freqChange,0.2);                                             //update speed of reaction
 }
 
-void Operator::inCorrect(int next_player, Controller &ctrl)
+void Operator::inCorrect(int current_player, Controller &ctrl)
 {
     //printf("OPP.Incorrect player = %d\n", next_player);
     ctrl.sound(200,1);
-    myplayers[next_player].status = false;                                          //set player to dead (false)
+    myplayers[current_player].status = false;                                          //set player to dead (false)
 }
 
-bool Operator::check_dead()
+bool Operator::both_dead()
 {
-    //printf("OPP.check_dead P1 %d\n", myplayers[0].status);
-    //printf("OPP.check_dead P2 %d\n", myplayers[1].status);
+    //printf("OPP.both_dead P1 %d\n", myplayers[0].status);
+    //printf("OPP.both_dead P2 %d\n", myplayers[1].status);
     
     if (!myplayers[0].status && !myplayers[1].status) {                         // if both players false then both dead = true
         return true;
@@ -75,22 +75,21 @@
 
 
 
-int Operator::check_next_player(int next_player, N5110 &lcd, Controller &ctrl, Display display)
+int Operator::check_next_player(int current_player, N5110 &lcd, Controller &ctrl, Display display)
 {
 
-    int mynext_player = next_player;
-    //printf("OPP.check_next_player(1) next_player %d\n", mynext_player);
+    int mynext_player = current_player;
+    //printf("OPP.check_next_player(1) current_player %d\n", mynext_player);
     if (_num_players > 1) {
 
-        if ( (myplayers[next_player]._score % 10 == 0) || (myplayers[next_player].status == false)) {  //on 0, 10, 20... etc swap player unless other player is dead
-            myplayers[next_player].wait = display.get_wait();      //save speed
-            lcd.clear();
-
-            if (next_player == 0 && myplayers[1].status) {                             //change to next player
+        if ( (myplayers[current_player]._score % 10 == 0) || (!myplayers[current_player].status)) {  //on 0, and multiples of 10 swap player unless other player is dead
+            myplayers[current_player].wait = display.get_wait();      //save speed
+            
+            if (current_player == 0 && myplayers[1].status) {                             //change to next player
                 mynext_player = 1;
             }
 
-            if (next_player == 1 && myplayers[0].status) {                             //change to next player
+            if (current_player == 1 && myplayers[0].status) {                             //change to next player
                 mynext_player = 0;
             }
             //printf("OPP.check_next_player(2) next_player %d\n", mynext_player);
@@ -103,9 +102,11 @@
 }
 
 void Operator::_displayNextPlayer(Controller &ctrl, N5110 &lcd, int thenext_player)
-{
+{ 
+//Screen telling user to swap players
     printf("OPP.DisplayNextPlayer threnext_player %d\n", thenext_player); 
     char buff[14];
+    lcd.clear();
     sprintf(buff,"Player %d ",thenext_player+1);
     lcd.printString(buff,20,2);
     lcd.refresh();
@@ -156,7 +157,7 @@
             _assessment(lcd, myplayers[0]._score);                //Display results for single player
             //printf("OPP num of Players = %d\n", _num_players);
         } else {
-            _multiResults(lcd);                                 //display results for multiPlayer
+            _multiResults(lcd);                                 //display results for multiplayer
         }
         ctrl.ledsOFF();
         ctrl.led(4,1);
@@ -169,7 +170,8 @@
 }
 
 void Operator::_assessment(N5110 &lcd, int _score) 
-{
+{ 
+//Awarding the user a level based on score achieved
     //printf("OPP.ASSESSMENT score = %d\n", _score);
     lcd.printString("Game Over",16,0);
     char buffer[14];
@@ -194,13 +196,14 @@
         
     }
     lcd.printString(buffer,37,2);
-    lcd.drawCircle(42,20,10,FILL_TRANSPARENT);
+    lcd.drawCircle(42,20,10,FILL_TRANSPARENT); 
     lcd.refresh();
 }
 
 
 void Operator::_multiResults(N5110 &lcd)
-{
+{ 
+//Displaying scores
     //printf("OPP.MULTIRESULTS\n");
     lcd.clear();
     lcd.printString("Game Over",16,0);
@@ -212,8 +215,8 @@
     int score1 = myplayers[0]._score;   
     int score2 = myplayers[1]._score;   
 
-
-    if (score1 > score2) {
+//Identifying the winner
+    if (score1 > score2) { 
         lcd.printString("Player 1 Wins", 5,4);
     } else {
         if (score2 > score1) {
--- a/Operator.h	Tue May 02 14:02:32 2017 +0000
+++ b/Operator.h	Tue May 02 18:05:38 2017 +0000
@@ -21,7 +21,7 @@
 };
 
 /** Operator Class
-@brief This class encapsulates all the algorithms required for managing scores, commands and player states(dead/alive). 
+@brief This class is the engine of the program. It encapsulates all the methods to do with managing scores, commands and player states(dead/alive). 
 
 @author Dominic Kay
 
@@ -59,17 +59,17 @@
     *  @brief Method to check if both players are dead 
     *  @return true/false
     */
-    bool check_dead();
+    bool both_dead();
 
 /** Method performed when the user performs the RIGHT instruction on time, score is increased, lighting and sound performed.
     *  @param the player, Controller
     */
-    void correct(int next_player, Controller &ctrl);  
+    void correct(int current_player, Controller &ctrl);  
 
 /** Method performed when the user performs the WRONG instruction on time 
     *  @param the player, Controller
     */
-    void inCorrect(int next_player, Controller &ctrl);  
+    void inCorrect(int current_player, Controller &ctrl);  
 
 /** Method to set up array of Player strut 
     *  @param the number of players to set up
@@ -82,7 +82,7 @@
     void gameOver(Controller &ctrl, N5110 &lcd);
     
 /** Method performed to check which user it is and to swap after every ten actions*/
-    int check_next_player(int next_player, N5110 &lcd, Controller &ctrl, Display display);
+    int check_next_player(int current_player, N5110 &lcd, Controller &ctrl, Display display);
 
 private:  
     /** This method displays and assesses score and display a performace rating for user to see.