This class is the engine of the program. It encapsulates all the methods to do with managing scores, commands and player states(dead/alive).
Operator.cpp@8:93f18f1c1241, 2017-04-21 (annotated)
- Committer:
- domkay97
- Date:
- Fri Apr 21 10:55:23 2017 +0000
- Revision:
- 8:93f18f1c1241
- Parent:
- 7:5d9b9d0bc6e7
- Child:
- 9:54c620f7d736
Added multiplayer features and seeded my random number variable to make it 'more random'
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:93f18f1c1241 | 21 | printf("MAIN 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 | 8:93f18f1c1241 | 24 | printf("MAIN 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 | 8:93f18f1c1241 | 58 | ctrl.sound(200,1); |
domkay97 | 8:93f18f1c1241 | 59 | myplayers[next_player].status = false; //set player to dead (false) |
domkay97 | 8:93f18f1c1241 | 60 | } |
domkay97 | 8:93f18f1c1241 | 61 | |
domkay97 | 8:93f18f1c1241 | 62 | bool Operator::check_dead() |
domkay97 | 8:93f18f1c1241 | 63 | { |
domkay97 | 8:93f18f1c1241 | 64 | |
domkay97 | 8:93f18f1c1241 | 65 | if (!myplayers[0].status && !myplayers[1].status) { |
domkay97 | 8:93f18f1c1241 | 66 | return true; |
domkay97 | 8:93f18f1c1241 | 67 | } else { |
domkay97 | 8:93f18f1c1241 | 68 | return false; |
domkay97 | 8:93f18f1c1241 | 69 | } |
domkay97 | 8:93f18f1c1241 | 70 | |
domkay97 | 6:9882809b0694 | 71 | } |
domkay97 | 6:9882809b0694 | 72 | |
domkay97 | 8:93f18f1c1241 | 73 | |
domkay97 | 8:93f18f1c1241 | 74 | |
domkay97 | 8:93f18f1c1241 | 75 | int Operator::check_next_player(int next_player, N5110 &lcd, Controller &ctrl, Display display) |
domkay97 | 8:93f18f1c1241 | 76 | { |
domkay97 | 8:93f18f1c1241 | 77 | |
domkay97 | 8:93f18f1c1241 | 78 | int mynext_player = next_player; |
domkay97 | 8:93f18f1c1241 | 79 | |
domkay97 | 8:93f18f1c1241 | 80 | if (_num_players > 1) { |
domkay97 | 8:93f18f1c1241 | 81 | |
domkay97 | 8:93f18f1c1241 | 82 | if ( (myplayers[next_player].score % 10 == 0) || (myplayers[next_player].status == false)) { |
domkay97 | 8:93f18f1c1241 | 83 | myplayers[next_player].wait = display.get_wait(); //save speed |
domkay97 | 8:93f18f1c1241 | 84 | lcd.clear(); |
domkay97 | 8:93f18f1c1241 | 85 | |
domkay97 | 8:93f18f1c1241 | 86 | if (next_player == 0 && myplayers[1].status) { //chagne to next player |
domkay97 | 8:93f18f1c1241 | 87 | mynext_player = 1; |
domkay97 | 8:93f18f1c1241 | 88 | } |
domkay97 | 8:93f18f1c1241 | 89 | |
domkay97 | 8:93f18f1c1241 | 90 | if (next_player == 1 && myplayers[0].status) { //change to next player |
domkay97 | 8:93f18f1c1241 | 91 | mynext_player = 0; |
domkay97 | 8:93f18f1c1241 | 92 | } |
domkay97 | 8:93f18f1c1241 | 93 | |
domkay97 | 8:93f18f1c1241 | 94 | // if (mynext_player != next_player) { |
domkay97 | 8:93f18f1c1241 | 95 | char buff[14]; |
domkay97 | 8:93f18f1c1241 | 96 | display.put_wait(myplayers[mynext_player].wait); //put speed |
domkay97 | 8:93f18f1c1241 | 97 | sprintf(buff,"Player %d ",mynext_player+1); |
domkay97 | 8:93f18f1c1241 | 98 | lcd.printString(buff,3,2); |
domkay97 | 8:93f18f1c1241 | 99 | |
domkay97 | 8:93f18f1c1241 | 100 | lcd.refresh(); |
domkay97 | 8:93f18f1c1241 | 101 | lcd.printString("3",20,4); |
domkay97 | 8:93f18f1c1241 | 102 | ctrl.sound(50,1); |
domkay97 | 8:93f18f1c1241 | 103 | lcd.refresh(); |
domkay97 | 8:93f18f1c1241 | 104 | wait(1); |
domkay97 | 8:93f18f1c1241 | 105 | lcd.printString("2",20,4); |
domkay97 | 8:93f18f1c1241 | 106 | lcd.refresh(); |
domkay97 | 8:93f18f1c1241 | 107 | ctrl.sound(200,1); |
domkay97 | 8:93f18f1c1241 | 108 | wait(1); |
domkay97 | 8:93f18f1c1241 | 109 | lcd.printString("1",20,4); |
domkay97 | 8:93f18f1c1241 | 110 | lcd.refresh(); |
domkay97 | 8:93f18f1c1241 | 111 | ctrl.sound(300,1); |
domkay97 | 8:93f18f1c1241 | 112 | wait(1); |
domkay97 | 8:93f18f1c1241 | 113 | lcd.printString("0",20,4); |
domkay97 | 8:93f18f1c1241 | 114 | lcd.refresh(); |
domkay97 | 8:93f18f1c1241 | 115 | ctrl.sound(400,2); |
domkay97 | 8:93f18f1c1241 | 116 | wait(2); |
domkay97 | 8:93f18f1c1241 | 117 | // } |
domkay97 | 8:93f18f1c1241 | 118 | } |
domkay97 | 8:93f18f1c1241 | 119 | |
domkay97 | 8:93f18f1c1241 | 120 | |
domkay97 | 8:93f18f1c1241 | 121 | |
domkay97 | 8:93f18f1c1241 | 122 | } |
domkay97 | 8:93f18f1c1241 | 123 | return mynext_player; |
domkay97 | 8:93f18f1c1241 | 124 | } |
domkay97 | 8:93f18f1c1241 | 125 | |
domkay97 | 8:93f18f1c1241 | 126 | void Operator::setup_players(int num_of_players) |
domkay97 | 8:93f18f1c1241 | 127 | { |
domkay97 | 8:93f18f1c1241 | 128 | |
domkay97 | 8:93f18f1c1241 | 129 | |
domkay97 | 8:93f18f1c1241 | 130 | myplayers[0].wait = 0.3; |
domkay97 | 8:93f18f1c1241 | 131 | myplayers[1].wait = 0.3; |
domkay97 | 8:93f18f1c1241 | 132 | myplayers[0].status = true; |
domkay97 | 8:93f18f1c1241 | 133 | myplayers[0].score = 0; |
domkay97 | 8:93f18f1c1241 | 134 | myplayers[1].score = 0; |
domkay97 | 8:93f18f1c1241 | 135 | |
domkay97 | 8:93f18f1c1241 | 136 | if (num_of_players == 1) { |
domkay97 | 8:93f18f1c1241 | 137 | myplayers[1].status = false; |
domkay97 | 8:93f18f1c1241 | 138 | } else { |
domkay97 | 8:93f18f1c1241 | 139 | myplayers[1].status = true; |
domkay97 | 8:93f18f1c1241 | 140 | } |
domkay97 | 8:93f18f1c1241 | 141 | _num_players = num_of_players; |
domkay97 | 8:93f18f1c1241 | 142 | } |
domkay97 | 8:93f18f1c1241 | 143 | |
domkay97 | 8:93f18f1c1241 | 144 | |
domkay97 | 8:93f18f1c1241 | 145 | |
domkay97 | 7:5d9b9d0bc6e7 | 146 | void Operator::Game_Over(Controller &ctrl, N5110 &lcd) |
domkay97 | 6:9882809b0694 | 147 | { |
domkay97 | 8:93f18f1c1241 | 148 | ctrl.init(); //reset flags |
domkay97 | 8:93f18f1c1241 | 149 | |
domkay97 | 7:5d9b9d0bc6e7 | 150 | while(ctrl.check_event(Controller::BACK_PRESSED) == false) { |
domkay97 | 8:93f18f1c1241 | 151 | if (_num_players == 1) { |
domkay97 | 8:93f18f1c1241 | 152 | Assessment(lcd, myplayers[0].score); |
domkay97 | 8:93f18f1c1241 | 153 | printf("OPP num of Players = %d\n", _num_players); |
domkay97 | 8:93f18f1c1241 | 154 | } else { |
domkay97 | 8:93f18f1c1241 | 155 | MultiResults(lcd); |
domkay97 | 8:93f18f1c1241 | 156 | } |
domkay97 | 7:5d9b9d0bc6e7 | 157 | ctrl.ledsOFF(); |
domkay97 | 7:5d9b9d0bc6e7 | 158 | ctrl.led(4,1); |
domkay97 | 7:5d9b9d0bc6e7 | 159 | ctrl.led(5,1); |
domkay97 | 7:5d9b9d0bc6e7 | 160 | ctrl.led(6,1); |
domkay97 | 6:9882809b0694 | 161 | } |
domkay97 | 8:93f18f1c1241 | 162 | |
domkay97 | 8:93f18f1c1241 | 163 | setup_players(1); |
domkay97 | 3:7213eee4c093 | 164 | score = 0; |
domkay97 | 6:9882809b0694 | 165 | } |
domkay97 | 5:84dfbfb17ef2 | 166 | |
domkay97 | 6:9882809b0694 | 167 | void Operator::Assessment(N5110 &lcd, int score) |
domkay97 | 6:9882809b0694 | 168 | { |
domkay97 | 6:9882809b0694 | 169 | printf("OPP.ASSESSMENT score = %d\n", score); |
domkay97 | 6:9882809b0694 | 170 | lcd.printString("Game Over",16,0); |
domkay97 | 6:9882809b0694 | 171 | char buffer[14]; |
domkay97 | 6:9882809b0694 | 172 | if (score < 10) { |
domkay97 | 6:9882809b0694 | 173 | sprintf(buffer,"0%d",score); |
domkay97 | 6:9882809b0694 | 174 | lcd.printString("Newbie",26,4); |
domkay97 | 6:9882809b0694 | 175 | } else if (score < 20) { |
domkay97 | 6:9882809b0694 | 176 | sprintf(buffer,"%2d",score); |
domkay97 | 7:5d9b9d0bc6e7 | 177 | lcd.printString("Amuteur",24,4); |
domkay97 | 6:9882809b0694 | 178 | } else if (score < 30) { |
domkay97 | 6:9882809b0694 | 179 | sprintf(buffer,"%2d",score); |
domkay97 | 7:5d9b9d0bc6e7 | 180 | lcd.printString("Semi-Pro",23,4); |
domkay97 | 6:9882809b0694 | 181 | } else if (score < 30) { |
domkay97 | 6:9882809b0694 | 182 | sprintf(buffer,"%2d",score); |
domkay97 | 7:5d9b9d0bc6e7 | 183 | lcd.printString("Expert",26,4); |
domkay97 | 6:9882809b0694 | 184 | } else if (score < 40) { |
domkay97 | 6:9882809b0694 | 185 | sprintf(buffer,"%2d",score); |
domkay97 | 7:5d9b9d0bc6e7 | 186 | lcd.printString("Professional",10,4); |
domkay97 | 6:9882809b0694 | 187 | } else if (score < 50) { |
domkay97 | 6:9882809b0694 | 188 | sprintf(buffer,"%2d",score); |
domkay97 | 6:9882809b0694 | 189 | lcd.printString("Legendary",16,4); |
domkay97 | 6:9882809b0694 | 190 | } else { |
domkay97 | 6:9882809b0694 | 191 | sprintf(buffer,"%2d",score); |
domkay97 | 7:5d9b9d0bc6e7 | 192 | lcd.printString("World Class",14,4); |
domkay97 | 6:9882809b0694 | 193 | } |
domkay97 | 6:9882809b0694 | 194 | lcd.printString(buffer,37,2); |
domkay97 | 6:9882809b0694 | 195 | lcd.drawCircle(42,20,10,FILL_TRANSPARENT); |
domkay97 | 6:9882809b0694 | 196 | lcd.refresh(); |
domkay97 | 8:93f18f1c1241 | 197 | } |
domkay97 | 8:93f18f1c1241 | 198 | |
domkay97 | 8:93f18f1c1241 | 199 | |
domkay97 | 8:93f18f1c1241 | 200 | void Operator::MultiResults(N5110 &lcd) |
domkay97 | 8:93f18f1c1241 | 201 | { |
domkay97 | 8:93f18f1c1241 | 202 | printf("OPP.MULTIRESULTS\n"); |
domkay97 | 8:93f18f1c1241 | 203 | lcd.clear(); |
domkay97 | 8:93f18f1c1241 | 204 | lcd.printString("Game Over",16,0); |
domkay97 | 8:93f18f1c1241 | 205 | char buffer[14]; |
domkay97 | 8:93f18f1c1241 | 206 | sprintf(buffer,"Player 1 - %d",myplayers[0].score); |
domkay97 | 8:93f18f1c1241 | 207 | lcd.printString(buffer,5,2); |
domkay97 | 8:93f18f1c1241 | 208 | sprintf(buffer,"Player 2 - %d",myplayers[1].score); |
domkay97 | 8:93f18f1c1241 | 209 | lcd.printString(buffer,5,3); |
domkay97 | 8:93f18f1c1241 | 210 | int score1 = myplayers[0].score; |
domkay97 | 8:93f18f1c1241 | 211 | int score2 = myplayers[1].score; |
domkay97 | 8:93f18f1c1241 | 212 | |
domkay97 | 8:93f18f1c1241 | 213 | |
domkay97 | 8:93f18f1c1241 | 214 | if (score1 > score2) { |
domkay97 | 8:93f18f1c1241 | 215 | lcd.printString("Player 1 Wins", 5,4); |
domkay97 | 8:93f18f1c1241 | 216 | } else { |
domkay97 | 8:93f18f1c1241 | 217 | if (score2 > score1) { |
domkay97 | 8:93f18f1c1241 | 218 | lcd.printString("Player 2 Wins", 5,4); |
domkay97 | 8:93f18f1c1241 | 219 | } else { |
domkay97 | 8:93f18f1c1241 | 220 | lcd.printString("Draw",5,4); |
domkay97 | 8:93f18f1c1241 | 221 | } |
domkay97 | 8:93f18f1c1241 | 222 | } |
domkay97 | 8:93f18f1c1241 | 223 | |
domkay97 | 8:93f18f1c1241 | 224 | lcd.refresh(); |
domkay97 | 3:7213eee4c093 | 225 | } |