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

Revision:
8:93f18f1c1241
Parent:
7:5d9b9d0bc6e7
Child:
9:54c620f7d736
--- a/Operator.cpp	Sun Apr 16 19:37:55 2017 +0000
+++ b/Operator.cpp	Fri Apr 21 10:55:23 2017 +0000
@@ -2,6 +2,8 @@
 
 Operator::Operator()
 {
+    srand(time(NULL)); //seed random number so the sequence is not the same each run through.
+    _num_players=0;
 
 }
 
@@ -10,35 +12,155 @@
 
 }
 
+
+bool Operator::test_player(int next_player, Controller &ctrl, Display &display, N5110 &lcd)
+{
+
+    ctrl.ledsOFF();
+    int instruction_val = random_instruction(display, lcd);     //sets instruction_val as the random instruction from display
+    printf("MAIN instruction_val = %d\n", instruction_val);
+    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("MAIN button_val = %d\n", button_val);
+
+
+    if (button_val == instruction_val) {                        //if the user performs the instruction correctly perform:
+
+        return true;
+    } else {
+        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;
     printf("OPP ran = %d\n", ran);
     display.display_instruction(lcd, ran);
     return ran;
 }
 
-void Operator::Correct(Controller &ctrl)
+void Operator::Correct(int next_player, Controller &ctrl)
 {
-    score++; 
+    myplayers[next_player].score++;                                                //increment score for player
+    score = myplayers[next_player].score;
     double freq_change = score*20;
     printf("OPP score = %d\n", score);
     ctrl.led(1,1);
     ctrl.led(2,1);
     ctrl.led(3,1);
-    ctrl.sound(50.0 + freq_change,0.2);
+    ctrl.sound(50.0 + freq_change,0.2);                                             //update speed of reaction
+}
+
+void Operator::InCorrect(int next_player, Controller &ctrl)
+{
+    ctrl.sound(200,1);
+    myplayers[next_player].status = false;                                          //set player to dead (false)
+}
+
+bool Operator::check_dead()
+{
+
+    if (!myplayers[0].status && !myplayers[1].status) {
+        return true;
+    } else {
+        return false;
+    }
+
 }
 
+
+
+int Operator::check_next_player(int next_player, N5110 &lcd, Controller &ctrl, Display display)
+{
+
+    int mynext_player = next_player;
+
+    if (_num_players > 1) {
+
+        if ( (myplayers[next_player].score % 10 == 0) || (myplayers[next_player].status == false)) {
+            myplayers[next_player].wait = display.get_wait();      //save speed
+            lcd.clear();
+
+            if (next_player == 0 && myplayers[1].status) {                             //chagne to next player
+                mynext_player = 1;
+            }
+
+            if (next_player == 1 && myplayers[0].status) {                             //change to next player
+                mynext_player = 0;
+            }
+            
+      //      if (mynext_player != next_player) {
+            char buff[14];
+            display.put_wait(myplayers[mynext_player].wait);  //put speed
+            sprintf(buff,"Player %d ",mynext_player+1);
+            lcd.printString(buff,3,2);
+
+            lcd.refresh();
+            lcd.printString("3",20,4);
+            ctrl.sound(50,1);
+            lcd.refresh();
+            wait(1);
+            lcd.printString("2",20,4);
+            lcd.refresh();
+            ctrl.sound(200,1);
+            wait(1);
+            lcd.printString("1",20,4);
+            lcd.refresh();
+            ctrl.sound(300,1);
+            wait(1);
+            lcd.printString("0",20,4);
+            lcd.refresh();
+            ctrl.sound(400,2);
+            wait(2);
+    //        }
+        }
+
+
+
+    }
+    return mynext_player;
+}
+
+void Operator::setup_players(int num_of_players)
+{
+
+
+    myplayers[0].wait = 0.3;
+    myplayers[1].wait = 0.3;
+    myplayers[0].status = true;
+    myplayers[0].score = 0;
+    myplayers[1].score = 0;
+
+    if (num_of_players == 1) {
+        myplayers[1].status = false;
+    } else {
+        myplayers[1].status = true;
+    }
+    _num_players = num_of_players;
+}
+
+
+
 void Operator::Game_Over(Controller &ctrl, N5110 &lcd)
 {
-    ctrl.sound(200,1);
+    ctrl.init();                                            //reset flags
+
     while(ctrl.check_event(Controller::BACK_PRESSED) == false) {
-        Assessment(lcd, score);
+        if (_num_players == 1) {
+            Assessment(lcd, myplayers[0].score);
+            printf("OPP num of Players = %d\n", _num_players);
+        } else {
+            MultiResults(lcd);
+        }
         ctrl.ledsOFF();
         ctrl.led(4,1);
         ctrl.led(5,1);
         ctrl.led(6,1);
     }
+
+    setup_players(1);
     score = 0;
 }
 
@@ -72,4 +194,32 @@
     lcd.printString(buffer,37,2);
     lcd.drawCircle(42,20,10,FILL_TRANSPARENT);
     lcd.refresh();
+}
+
+
+void Operator::MultiResults(N5110 &lcd)
+{
+    printf("OPP.MULTIRESULTS\n");
+    lcd.clear();
+    lcd.printString("Game Over",16,0);
+    char buffer[14];
+    sprintf(buffer,"Player 1 - %d",myplayers[0].score);
+    lcd.printString(buffer,5,2);
+    sprintf(buffer,"Player 2 - %d",myplayers[1].score);
+    lcd.printString(buffer,5,3);
+    int score1 = myplayers[0].score;
+    int score2 = myplayers[1].score;
+
+
+    if (score1 > score2) {
+        lcd.printString("Player 1 Wins", 5,4);
+    } else {
+        if (score2 > score1) {
+            lcd.printString("Player 2 Wins", 5,4);
+        } else {
+            lcd.printString("Draw",5,4);
+        }
+    }
+
+    lcd.refresh();
 }
\ No newline at end of file