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
- Committer:
- domkay97
- Date:
- 2017-04-21
- Revision:
- 8:93f18f1c1241
- Parent:
- 7:5d9b9d0bc6e7
- Child:
- 9:54c620f7d736
File content as of revision 8:93f18f1c1241:
#include "Operator.h" Operator::Operator() { srand(time(NULL)); //seed random number so the sequence is not the same each run through. _num_players=0; } Operator::~Operator() { } 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(int next_player, Controller &ctrl) { 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); //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.init(); //reset flags while(ctrl.check_event(Controller::BACK_PRESSED) == false) { 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; } void Operator::Assessment(N5110 &lcd, int score) { printf("OPP.ASSESSMENT score = %d\n", score); lcd.printString("Game Over",16,0); char buffer[14]; if (score < 10) { sprintf(buffer,"0%d",score); lcd.printString("Newbie",26,4); } else if (score < 20) { sprintf(buffer,"%2d",score); lcd.printString("Amuteur",24,4); } else if (score < 30) { sprintf(buffer,"%2d",score); lcd.printString("Semi-Pro",23,4); } else if (score < 30) { sprintf(buffer,"%2d",score); lcd.printString("Expert",26,4); } else if (score < 40) { sprintf(buffer,"%2d",score); lcd.printString("Professional",10,4); } else if (score < 50) { sprintf(buffer,"%2d",score); lcd.printString("Legendary",16,4); } else { sprintf(buffer,"%2d",score); lcd.printString("World Class",14,4); } 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(); }