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:
Tue May 02 18:05:38 2017 +0000
Revision:
13:1684970587ce
Parent:
12:bdaf4e1d615e
Changed some method names, added more 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 10:e0e820636025 21 //printf("OPP:test_player instruction_val = %d\n", instruction_val);
domkay97 13:1684970587ce 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 10:e0e820636025 24 //printf("OPP:test_player button_val = %d\n", button_val);
domkay97 8:93f18f1c1241 25
domkay97 8:93f18f1c1241 26
domkay97 13:1684970587ce 27 if (button_val == instruction_val) { //if the user performs the instruction correctly return true
domkay97 8:93f18f1c1241 28
domkay97 8:93f18f1c1241 29 return true;
domkay97 13:1684970587ce 30 } else { //otherwise the user has performed the instruction incorrectly return false
domkay97 8:93f18f1c1241 31 return false;
domkay97 13:1684970587ce 32 }
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 13:1684970587ce 38 int ran = rand() % 7 + 1; //generates random numbers between 0 and 8.
domkay97 10:e0e820636025 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 13:1684970587ce 44 void Operator::correct(int current_player, Controller &ctrl)
domkay97 6:9882809b0694 45 {
domkay97 13:1684970587ce 46 myplayers[current_player]._score++; //increment score for player
domkay97 13:1684970587ce 47 _score = myplayers[current_player]._score;
domkay97 11:48190450b7e5 48 double _freqChange = _score*20;
domkay97 10:e0e820636025 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 11:48190450b7e5 53 ctrl.sound(50.0 + _freqChange,0.2); //update speed of reaction
domkay97 8:93f18f1c1241 54 }
domkay97 8:93f18f1c1241 55
domkay97 13:1684970587ce 56 void Operator::inCorrect(int current_player, Controller &ctrl)
domkay97 8:93f18f1c1241 57 {
domkay97 10:e0e820636025 58 //printf("OPP.Incorrect player = %d\n", next_player);
domkay97 8:93f18f1c1241 59 ctrl.sound(200,1);
domkay97 13:1684970587ce 60 myplayers[current_player].status = false; //set player to dead (false)
domkay97 8:93f18f1c1241 61 }
domkay97 8:93f18f1c1241 62
domkay97 13:1684970587ce 63 bool Operator::both_dead()
domkay97 8:93f18f1c1241 64 {
domkay97 13:1684970587ce 65 //printf("OPP.both_dead P1 %d\n", myplayers[0].status);
domkay97 13:1684970587ce 66 //printf("OPP.both_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 13:1684970587ce 78 int Operator::check_next_player(int current_player, N5110 &lcd, Controller &ctrl, Display display)
domkay97 8:93f18f1c1241 79 {
domkay97 8:93f18f1c1241 80
domkay97 13:1684970587ce 81 int mynext_player = current_player;
domkay97 13:1684970587ce 82 //printf("OPP.check_next_player(1) current_player %d\n", mynext_player);
domkay97 8:93f18f1c1241 83 if (_num_players > 1) {
domkay97 8:93f18f1c1241 84
domkay97 13:1684970587ce 85 if ( (myplayers[current_player]._score % 10 == 0) || (!myplayers[current_player].status)) { //on 0, and multiples of 10 swap player unless other player is dead
domkay97 13:1684970587ce 86 myplayers[current_player].wait = display.get_wait(); //save speed
domkay97 13:1684970587ce 87
domkay97 13:1684970587ce 88 if (current_player == 0 && myplayers[1].status) { //change to next player
domkay97 8:93f18f1c1241 89 mynext_player = 1;
domkay97 8:93f18f1c1241 90 }
domkay97 8:93f18f1c1241 91
domkay97 13:1684970587ce 92 if (current_player == 1 && myplayers[0].status) { //change to next player
domkay97 8:93f18f1c1241 93 mynext_player = 0;
domkay97 8:93f18f1c1241 94 }
domkay97 10:e0e820636025 95 //printf("OPP.check_next_player(2) next_player %d\n", mynext_player);
domkay97 9:54c620f7d736 96 display.put_wait(myplayers[mynext_player].wait); //put speed
domkay97 11:48190450b7e5 97 _displayNextPlayer(ctrl,lcd,mynext_player); //Display next player
domkay97 8:93f18f1c1241 98 }
domkay97 8:93f18f1c1241 99
domkay97 8:93f18f1c1241 100 }
domkay97 8:93f18f1c1241 101 return mynext_player;
domkay97 8:93f18f1c1241 102 }
domkay97 8:93f18f1c1241 103
domkay97 11:48190450b7e5 104 void Operator::_displayNextPlayer(Controller &ctrl, N5110 &lcd, int thenext_player)
domkay97 13:1684970587ce 105 {
domkay97 13:1684970587ce 106 //Screen telling user to swap players
domkay97 12:bdaf4e1d615e 107 printf("OPP.DisplayNextPlayer threnext_player %d\n", thenext_player);
domkay97 9:54c620f7d736 108 char buff[14];
domkay97 13:1684970587ce 109 lcd.clear();
domkay97 9:54c620f7d736 110 sprintf(buff,"Player %d ",thenext_player+1);
domkay97 9:54c620f7d736 111 lcd.printString(buff,20,2);
domkay97 9:54c620f7d736 112 lcd.refresh();
domkay97 9:54c620f7d736 113 lcd.printString("3",40,4);
domkay97 9:54c620f7d736 114 ctrl.sound(50,1);
domkay97 9:54c620f7d736 115 lcd.refresh();
domkay97 9:54c620f7d736 116 wait(1);
domkay97 9:54c620f7d736 117 lcd.printString("2",40,4);
domkay97 9:54c620f7d736 118 lcd.refresh();
domkay97 9:54c620f7d736 119 ctrl.sound(200,1);
domkay97 9:54c620f7d736 120 wait(1);
domkay97 9:54c620f7d736 121 lcd.printString("1",40,4);
domkay97 9:54c620f7d736 122 lcd.refresh();
domkay97 9:54c620f7d736 123 ctrl.sound(300,1);
domkay97 9:54c620f7d736 124 wait(1);
domkay97 9:54c620f7d736 125 lcd.printString("0",40,4);
domkay97 9:54c620f7d736 126 lcd.refresh();
domkay97 9:54c620f7d736 127 ctrl.sound(400,2);
domkay97 9:54c620f7d736 128 wait(2);
domkay97 9:54c620f7d736 129 }
domkay97 9:54c620f7d736 130
domkay97 9:54c620f7d736 131
domkay97 8:93f18f1c1241 132 void Operator::setup_players(int num_of_players)
domkay97 8:93f18f1c1241 133 {
domkay97 8:93f18f1c1241 134
domkay97 8:93f18f1c1241 135 myplayers[0].wait = 0.3;
domkay97 8:93f18f1c1241 136 myplayers[0].status = true;
domkay97 11:48190450b7e5 137 myplayers[0]._score = 0;
domkay97 12:bdaf4e1d615e 138 myplayers[1]._score = 0;
domkay97 12:bdaf4e1d615e 139 myplayers[1].wait = 0.3;
domkay97 8:93f18f1c1241 140
domkay97 8:93f18f1c1241 141 if (num_of_players == 1) {
domkay97 8:93f18f1c1241 142 myplayers[1].status = false;
domkay97 8:93f18f1c1241 143 } else {
domkay97 8:93f18f1c1241 144 myplayers[1].status = true;
domkay97 8:93f18f1c1241 145 }
domkay97 8:93f18f1c1241 146 _num_players = num_of_players;
domkay97 8:93f18f1c1241 147 }
domkay97 8:93f18f1c1241 148
domkay97 8:93f18f1c1241 149
domkay97 8:93f18f1c1241 150
domkay97 11:48190450b7e5 151 void Operator::gameOver(Controller &ctrl, N5110 &lcd)
domkay97 6:9882809b0694 152 {
domkay97 8:93f18f1c1241 153 ctrl.init(); //reset flags
domkay97 8:93f18f1c1241 154
domkay97 7:5d9b9d0bc6e7 155 while(ctrl.check_event(Controller::BACK_PRESSED) == false) {
domkay97 8:93f18f1c1241 156 if (_num_players == 1) {
domkay97 11:48190450b7e5 157 _assessment(lcd, myplayers[0]._score); //Display results for single player
domkay97 10:e0e820636025 158 //printf("OPP num of Players = %d\n", _num_players);
domkay97 8:93f18f1c1241 159 } else {
domkay97 13:1684970587ce 160 _multiResults(lcd); //display results for multiplayer
domkay97 8:93f18f1c1241 161 }
domkay97 7:5d9b9d0bc6e7 162 ctrl.ledsOFF();
domkay97 7:5d9b9d0bc6e7 163 ctrl.led(4,1);
domkay97 7:5d9b9d0bc6e7 164 ctrl.led(5,1);
domkay97 7:5d9b9d0bc6e7 165 ctrl.led(6,1);
domkay97 6:9882809b0694 166 }
domkay97 8:93f18f1c1241 167
domkay97 8:93f18f1c1241 168 setup_players(1);
domkay97 11:48190450b7e5 169 _score = 0;
domkay97 6:9882809b0694 170 }
domkay97 5:84dfbfb17ef2 171
domkay97 12:bdaf4e1d615e 172 void Operator::_assessment(N5110 &lcd, int _score)
domkay97 13:1684970587ce 173 {
domkay97 13:1684970587ce 174 //Awarding the user a level based on score achieved
domkay97 11:48190450b7e5 175 //printf("OPP.ASSESSMENT score = %d\n", _score);
domkay97 6:9882809b0694 176 lcd.printString("Game Over",16,0);
domkay97 6:9882809b0694 177 char buffer[14];
domkay97 11:48190450b7e5 178 if (_score < 10) {
domkay97 11:48190450b7e5 179 sprintf(buffer,"0%d",_score);
domkay97 6:9882809b0694 180 lcd.printString("Newbie",26,4);
domkay97 11:48190450b7e5 181 } else if (_score < 20) {
domkay97 11:48190450b7e5 182 sprintf(buffer,"%2d",_score);
domkay97 7:5d9b9d0bc6e7 183 lcd.printString("Amuteur",24,4);
domkay97 11:48190450b7e5 184 } else if (_score < 30) {
domkay97 11:48190450b7e5 185 sprintf(buffer,"%2d",_score);
domkay97 7:5d9b9d0bc6e7 186 lcd.printString("Expert",26,4);
domkay97 11:48190450b7e5 187 } else if (_score < 40) {
domkay97 11:48190450b7e5 188 sprintf(buffer,"%2d",_score);
domkay97 7:5d9b9d0bc6e7 189 lcd.printString("Professional",10,4);
domkay97 11:48190450b7e5 190 } else if (_score < 50) {
domkay97 11:48190450b7e5 191 sprintf(buffer,"%2d",_score);
domkay97 9:54c620f7d736 192 lcd.printString("World Class",14,4);
domkay97 6:9882809b0694 193 } else {
domkay97 11:48190450b7e5 194 sprintf(buffer,"%2d",_score);
domkay97 9:54c620f7d736 195 lcd.printString("Legendary",16,4);
domkay97 9:54c620f7d736 196
domkay97 6:9882809b0694 197 }
domkay97 6:9882809b0694 198 lcd.printString(buffer,37,2);
domkay97 13:1684970587ce 199 lcd.drawCircle(42,20,10,FILL_TRANSPARENT);
domkay97 6:9882809b0694 200 lcd.refresh();
domkay97 8:93f18f1c1241 201 }
domkay97 8:93f18f1c1241 202
domkay97 8:93f18f1c1241 203
domkay97 11:48190450b7e5 204 void Operator::_multiResults(N5110 &lcd)
domkay97 13:1684970587ce 205 {
domkay97 13:1684970587ce 206 //Displaying scores
domkay97 10:e0e820636025 207 //printf("OPP.MULTIRESULTS\n");
domkay97 8:93f18f1c1241 208 lcd.clear();
domkay97 8:93f18f1c1241 209 lcd.printString("Game Over",16,0);
domkay97 8:93f18f1c1241 210 char buffer[14];
domkay97 11:48190450b7e5 211 sprintf(buffer,"Player 1 - %d",myplayers[0]._score);
domkay97 8:93f18f1c1241 212 lcd.printString(buffer,5,2);
domkay97 11:48190450b7e5 213 sprintf(buffer,"Player 2 - %d",myplayers[1]._score);
domkay97 8:93f18f1c1241 214 lcd.printString(buffer,5,3);
domkay97 11:48190450b7e5 215 int score1 = myplayers[0]._score;
domkay97 11:48190450b7e5 216 int score2 = myplayers[1]._score;
domkay97 8:93f18f1c1241 217
domkay97 13:1684970587ce 218 //Identifying the winner
domkay97 13:1684970587ce 219 if (score1 > score2) {
domkay97 8:93f18f1c1241 220 lcd.printString("Player 1 Wins", 5,4);
domkay97 8:93f18f1c1241 221 } else {
domkay97 8:93f18f1c1241 222 if (score2 > score1) {
domkay97 8:93f18f1c1241 223 lcd.printString("Player 2 Wins", 5,4);
domkay97 8:93f18f1c1241 224 } else {
domkay97 8:93f18f1c1241 225 lcd.printString("Draw",5,4);
domkay97 8:93f18f1c1241 226 }
domkay97 8:93f18f1c1241 227 }
domkay97 8:93f18f1c1241 228
domkay97 8:93f18f1c1241 229 lcd.refresh();
domkay97 3:7213eee4c093 230 }