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

Committer:
domkay97
Date:
Sun Apr 23 16:36:37 2017 +0000
Revision:
9:54c620f7d736
Parent:
8:93f18f1c1241
Child:
10:e0e820636025
Updated Comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
domkay97 0:3097759acb02 1 #include "Operator.h"
domkay97 0:3097759acb02 2
domkay97 0:3097759acb02 3 Operator::Operator()
domkay97 0:3097759acb02 4 {
domkay97 8:93f18f1c1241 5 srand(time(NULL)); //seed random number so the sequence is not the same each run through.
domkay97 8:93f18f1c1241 6 _num_players=0;
domkay97 0:3097759acb02 7
domkay97 0:3097759acb02 8 }
domkay97 0:3097759acb02 9
domkay97 0:3097759acb02 10 Operator::~Operator()
domkay97 0:3097759acb02 11 {
domkay97 0:3097759acb02 12
domkay97 0:3097759acb02 13 }
domkay97 0:3097759acb02 14
domkay97 8:93f18f1c1241 15
domkay97 8:93f18f1c1241 16 bool Operator::test_player(int next_player, Controller &ctrl, Display &display, N5110 &lcd)
domkay97 8:93f18f1c1241 17 {
domkay97 8:93f18f1c1241 18
domkay97 8:93f18f1c1241 19 ctrl.ledsOFF();
domkay97 8:93f18f1c1241 20 int instruction_val = random_instruction(display, lcd); //sets instruction_val as the random instruction from display
domkay97 9:54c620f7d736 21 printf("OPP:test_player instruction_val = %d\n", instruction_val);
domkay97 8:93f18f1c1241 22 display.drawCircle(ctrl, lcd); //Draws circle, displays instruction and allows the circle to be drawn faster with time
domkay97 8:93f18f1c1241 23 int button_val = ctrl.check_for_buttons(); //sets button_val as the instruction performed by the user
domkay97 9:54c620f7d736 24 printf("OPP:test_player button_val = %d\n", button_val);
domkay97 8:93f18f1c1241 25
domkay97 8:93f18f1c1241 26
domkay97 8:93f18f1c1241 27 if (button_val == instruction_val) { //if the user performs the instruction correctly perform:
domkay97 8:93f18f1c1241 28
domkay97 8:93f18f1c1241 29 return true;
domkay97 8:93f18f1c1241 30 } else {
domkay97 8:93f18f1c1241 31 return false;
domkay97 8:93f18f1c1241 32 } //otherwise the user has performed the instruction incorrectly so perform:
domkay97 8:93f18f1c1241 33 }
domkay97 8:93f18f1c1241 34
domkay97 6:9882809b0694 35 int Operator::random_instruction(Display &display, N5110 &lcd)
domkay97 6:9882809b0694 36 {
domkay97 8:93f18f1c1241 37
domkay97 6:9882809b0694 38 int ran = rand() % 7 + 1;
domkay97 6:9882809b0694 39 printf("OPP ran = %d\n", ran);
domkay97 6:9882809b0694 40 display.display_instruction(lcd, ran);
domkay97 6:9882809b0694 41 return ran;
domkay97 6:9882809b0694 42 }
domkay97 5:84dfbfb17ef2 43
domkay97 8:93f18f1c1241 44 void Operator::Correct(int next_player, Controller &ctrl)
domkay97 6:9882809b0694 45 {
domkay97 8:93f18f1c1241 46 myplayers[next_player].score++; //increment score for player
domkay97 8:93f18f1c1241 47 score = myplayers[next_player].score;
domkay97 7:5d9b9d0bc6e7 48 double freq_change = score*20;
domkay97 6:9882809b0694 49 printf("OPP score = %d\n", score);
domkay97 7:5d9b9d0bc6e7 50 ctrl.led(1,1);
domkay97 7:5d9b9d0bc6e7 51 ctrl.led(2,1);
domkay97 7:5d9b9d0bc6e7 52 ctrl.led(3,1);
domkay97 8:93f18f1c1241 53 ctrl.sound(50.0 + freq_change,0.2); //update speed of reaction
domkay97 8:93f18f1c1241 54 }
domkay97 8:93f18f1c1241 55
domkay97 8:93f18f1c1241 56 void Operator::InCorrect(int next_player, Controller &ctrl)
domkay97 8:93f18f1c1241 57 {
domkay97 9:54c620f7d736 58 printf("OPP.Incorrect player = %d\n", next_player);
domkay97 8:93f18f1c1241 59 ctrl.sound(200,1);
domkay97 8:93f18f1c1241 60 myplayers[next_player].status = false; //set player to dead (false)
domkay97 8:93f18f1c1241 61 }
domkay97 8:93f18f1c1241 62
domkay97 8:93f18f1c1241 63 bool Operator::check_dead()
domkay97 8:93f18f1c1241 64 {
domkay97 9:54c620f7d736 65 printf("OPP.check_dead P1 %d\n", myplayers[0].status);
domkay97 9:54c620f7d736 66 printf("OPP.check_dead P2 %d\n", myplayers[1].status);
domkay97 9:54c620f7d736 67
domkay97 9:54c620f7d736 68 if (!myplayers[0].status && !myplayers[1].status) { // if both players false then both dead = true
domkay97 8:93f18f1c1241 69 return true;
domkay97 8:93f18f1c1241 70 } else {
domkay97 8:93f18f1c1241 71 return false;
domkay97 8:93f18f1c1241 72 }
domkay97 8:93f18f1c1241 73
domkay97 6:9882809b0694 74 }
domkay97 6:9882809b0694 75
domkay97 8:93f18f1c1241 76
domkay97 8:93f18f1c1241 77
domkay97 8:93f18f1c1241 78 int Operator::check_next_player(int next_player, N5110 &lcd, Controller &ctrl, Display display)
domkay97 8:93f18f1c1241 79 {
domkay97 8:93f18f1c1241 80
domkay97 8:93f18f1c1241 81 int mynext_player = next_player;
domkay97 9:54c620f7d736 82 printf("OPP.check_next_player(1) next_player %d\n", mynext_player);
domkay97 8:93f18f1c1241 83 if (_num_players > 1) {
domkay97 8:93f18f1c1241 84
domkay97 9:54c620f7d736 85 if ( (myplayers[next_player].score % 10 == 0) || (myplayers[next_player].status == false)) { //on 0, 10 etc swap player unless other player is dead
domkay97 8:93f18f1c1241 86 myplayers[next_player].wait = display.get_wait(); //save speed
domkay97 8:93f18f1c1241 87 lcd.clear();
domkay97 8:93f18f1c1241 88
domkay97 8:93f18f1c1241 89 if (next_player == 0 && myplayers[1].status) { //chagne to next player
domkay97 8:93f18f1c1241 90 mynext_player = 1;
domkay97 8:93f18f1c1241 91 }
domkay97 8:93f18f1c1241 92
domkay97 8:93f18f1c1241 93 if (next_player == 1 && myplayers[0].status) { //change to next player
domkay97 8:93f18f1c1241 94 mynext_player = 0;
domkay97 8:93f18f1c1241 95 }
domkay97 9:54c620f7d736 96 printf("OPP.check_next_player(2) next_player %d\n", mynext_player);
domkay97 9:54c620f7d736 97 display.put_wait(myplayers[mynext_player].wait); //put speed
domkay97 9:54c620f7d736 98 DisplayNextPlayer(ctrl,lcd,mynext_player); //Display next player
domkay97 8:93f18f1c1241 99 }
domkay97 8:93f18f1c1241 100
domkay97 8:93f18f1c1241 101 }
domkay97 8:93f18f1c1241 102 return mynext_player;
domkay97 8:93f18f1c1241 103 }
domkay97 8:93f18f1c1241 104
domkay97 9:54c620f7d736 105 void Operator::DisplayNextPlayer(Controller &ctrl, N5110 &lcd, int thenext_player)
domkay97 9:54c620f7d736 106 {
domkay97 9:54c620f7d736 107 printf("OPP.DisplayNextPlayer threnext_player %d\n", thenext_player);
domkay97 9:54c620f7d736 108 char buff[14];
domkay97 9:54c620f7d736 109 sprintf(buff,"Player %d ",thenext_player+1);
domkay97 9:54c620f7d736 110 lcd.printString(buff,20,2);
domkay97 9:54c620f7d736 111 lcd.refresh();
domkay97 9:54c620f7d736 112 lcd.printString("3",40,4);
domkay97 9:54c620f7d736 113 ctrl.sound(50,1);
domkay97 9:54c620f7d736 114 lcd.refresh();
domkay97 9:54c620f7d736 115 wait(1);
domkay97 9:54c620f7d736 116 lcd.printString("2",40,4);
domkay97 9:54c620f7d736 117 lcd.refresh();
domkay97 9:54c620f7d736 118 ctrl.sound(200,1);
domkay97 9:54c620f7d736 119 wait(1);
domkay97 9:54c620f7d736 120 lcd.printString("1",40,4);
domkay97 9:54c620f7d736 121 lcd.refresh();
domkay97 9:54c620f7d736 122 ctrl.sound(300,1);
domkay97 9:54c620f7d736 123 wait(1);
domkay97 9:54c620f7d736 124 lcd.printString("0",40,4);
domkay97 9:54c620f7d736 125 lcd.refresh();
domkay97 9:54c620f7d736 126 ctrl.sound(400,2);
domkay97 9:54c620f7d736 127 wait(2);
domkay97 9:54c620f7d736 128 }
domkay97 9:54c620f7d736 129
domkay97 9:54c620f7d736 130
domkay97 8:93f18f1c1241 131 void Operator::setup_players(int num_of_players)
domkay97 8:93f18f1c1241 132 {
domkay97 8:93f18f1c1241 133
domkay97 8:93f18f1c1241 134 myplayers[0].wait = 0.3;
domkay97 8:93f18f1c1241 135 myplayers[1].wait = 0.3;
domkay97 8:93f18f1c1241 136 myplayers[0].status = true;
domkay97 8:93f18f1c1241 137 myplayers[0].score = 0;
domkay97 8:93f18f1c1241 138 myplayers[1].score = 0;
domkay97 8:93f18f1c1241 139
domkay97 8:93f18f1c1241 140 if (num_of_players == 1) {
domkay97 8:93f18f1c1241 141 myplayers[1].status = false;
domkay97 8:93f18f1c1241 142 } else {
domkay97 8:93f18f1c1241 143 myplayers[1].status = true;
domkay97 8:93f18f1c1241 144 }
domkay97 8:93f18f1c1241 145 _num_players = num_of_players;
domkay97 8:93f18f1c1241 146 }
domkay97 8:93f18f1c1241 147
domkay97 8:93f18f1c1241 148
domkay97 8:93f18f1c1241 149
domkay97 7:5d9b9d0bc6e7 150 void Operator::Game_Over(Controller &ctrl, N5110 &lcd)
domkay97 6:9882809b0694 151 {
domkay97 8:93f18f1c1241 152 ctrl.init(); //reset flags
domkay97 8:93f18f1c1241 153
domkay97 7:5d9b9d0bc6e7 154 while(ctrl.check_event(Controller::BACK_PRESSED) == false) {
domkay97 8:93f18f1c1241 155 if (_num_players == 1) {
domkay97 9:54c620f7d736 156 Assessment(lcd, myplayers[0].score); //Display results for single player
domkay97 8:93f18f1c1241 157 printf("OPP num of Players = %d\n", _num_players);
domkay97 8:93f18f1c1241 158 } else {
domkay97 9:54c620f7d736 159 MultiResults(lcd); //display results for multiPlayer
domkay97 8:93f18f1c1241 160 }
domkay97 7:5d9b9d0bc6e7 161 ctrl.ledsOFF();
domkay97 7:5d9b9d0bc6e7 162 ctrl.led(4,1);
domkay97 7:5d9b9d0bc6e7 163 ctrl.led(5,1);
domkay97 7:5d9b9d0bc6e7 164 ctrl.led(6,1);
domkay97 6:9882809b0694 165 }
domkay97 8:93f18f1c1241 166
domkay97 8:93f18f1c1241 167 setup_players(1);
domkay97 3:7213eee4c093 168 score = 0;
domkay97 6:9882809b0694 169 }
domkay97 5:84dfbfb17ef2 170
domkay97 6:9882809b0694 171 void Operator::Assessment(N5110 &lcd, int score)
domkay97 6:9882809b0694 172 {
domkay97 6:9882809b0694 173 printf("OPP.ASSESSMENT score = %d\n", score);
domkay97 6:9882809b0694 174 lcd.printString("Game Over",16,0);
domkay97 6:9882809b0694 175 char buffer[14];
domkay97 6:9882809b0694 176 if (score < 10) {
domkay97 6:9882809b0694 177 sprintf(buffer,"0%d",score);
domkay97 6:9882809b0694 178 lcd.printString("Newbie",26,4);
domkay97 6:9882809b0694 179 } else if (score < 20) {
domkay97 6:9882809b0694 180 sprintf(buffer,"%2d",score);
domkay97 7:5d9b9d0bc6e7 181 lcd.printString("Amuteur",24,4);
domkay97 6:9882809b0694 182 } else if (score < 30) {
domkay97 6:9882809b0694 183 sprintf(buffer,"%2d",score);
domkay97 7:5d9b9d0bc6e7 184 lcd.printString("Expert",26,4);
domkay97 6:9882809b0694 185 } else if (score < 40) {
domkay97 6:9882809b0694 186 sprintf(buffer,"%2d",score);
domkay97 7:5d9b9d0bc6e7 187 lcd.printString("Professional",10,4);
domkay97 6:9882809b0694 188 } else if (score < 50) {
domkay97 6:9882809b0694 189 sprintf(buffer,"%2d",score);
domkay97 9:54c620f7d736 190 lcd.printString("World Class",14,4);
domkay97 6:9882809b0694 191 } else {
domkay97 9:54c620f7d736 192 sprintf(buffer,"%2d",score);
domkay97 9:54c620f7d736 193 lcd.printString("Legendary",16,4);
domkay97 9:54c620f7d736 194
domkay97 6:9882809b0694 195 }
domkay97 6:9882809b0694 196 lcd.printString(buffer,37,2);
domkay97 6:9882809b0694 197 lcd.drawCircle(42,20,10,FILL_TRANSPARENT);
domkay97 6:9882809b0694 198 lcd.refresh();
domkay97 8:93f18f1c1241 199 }
domkay97 8:93f18f1c1241 200
domkay97 8:93f18f1c1241 201
domkay97 8:93f18f1c1241 202 void Operator::MultiResults(N5110 &lcd)
domkay97 8:93f18f1c1241 203 {
domkay97 8:93f18f1c1241 204 printf("OPP.MULTIRESULTS\n");
domkay97 8:93f18f1c1241 205 lcd.clear();
domkay97 8:93f18f1c1241 206 lcd.printString("Game Over",16,0);
domkay97 8:93f18f1c1241 207 char buffer[14];
domkay97 8:93f18f1c1241 208 sprintf(buffer,"Player 1 - %d",myplayers[0].score);
domkay97 8:93f18f1c1241 209 lcd.printString(buffer,5,2);
domkay97 8:93f18f1c1241 210 sprintf(buffer,"Player 2 - %d",myplayers[1].score);
domkay97 8:93f18f1c1241 211 lcd.printString(buffer,5,3);
domkay97 9:54c620f7d736 212 int score1 = myplayers[0].score;
domkay97 9:54c620f7d736 213 int score2 = myplayers[1].score;
domkay97 8:93f18f1c1241 214
domkay97 8:93f18f1c1241 215
domkay97 8:93f18f1c1241 216 if (score1 > score2) {
domkay97 8:93f18f1c1241 217 lcd.printString("Player 1 Wins", 5,4);
domkay97 8:93f18f1c1241 218 } else {
domkay97 8:93f18f1c1241 219 if (score2 > score1) {
domkay97 8:93f18f1c1241 220 lcd.printString("Player 2 Wins", 5,4);
domkay97 8:93f18f1c1241 221 } else {
domkay97 8:93f18f1c1241 222 lcd.printString("Draw",5,4);
domkay97 8:93f18f1c1241 223 }
domkay97 8:93f18f1c1241 224 }
domkay97 8:93f18f1c1241 225
domkay97 8:93f18f1c1241 226 lcd.refresh();
domkay97 3:7213eee4c093 227 }