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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Operator.cpp Source File

Operator.cpp

00001 #include "Operator.h"
00002 
00003 Operator::Operator()
00004 {
00005     srand(time(NULL)); //seed random number so the sequence is not the same each run through.
00006     _num_players=0;
00007 
00008 }
00009 
00010 Operator::~Operator()
00011 {
00012 
00013 }
00014 
00015 
00016 bool Operator::test_player(int next_player, Controller &ctrl, Display &display, N5110 &lcd)
00017 {
00018 
00019     ctrl.ledsOFF();
00020     int instruction_val = random_instruction(display, lcd);     //sets instruction_val as the random instruction from display
00021     //printf("OPP:test_player instruction_val = %d\n", instruction_val);
00022     display.drawCircle(ctrl, lcd);                                    //Draws circle, displays instruction and allows the circle to be drawn faster with time
00023     int button_val = ctrl.check_for_buttons();                       //sets button_val as the instruction performed by the user
00024     //printf("OPP:test_player button_val = %d\n", button_val);
00025 
00026 
00027     if (button_val == instruction_val) {         //if the user performs the instruction correctly return true
00028 
00029         return true;
00030     } else {                                    //otherwise the user has performed the instruction incorrectly return false
00031         return false;
00032     }                                              
00033 }
00034 
00035 int Operator::random_instruction(Display &display, N5110 &lcd)
00036 {
00037 
00038     int ran = rand() % 7 + 1;                   //generates random numbers between 0 and 8.
00039     //printf("OPP ran = %d\n", ran);
00040     display.display_instruction(lcd, ran);
00041     return ran;
00042 }
00043 
00044 void Operator::correct(int current_player, Controller &ctrl)
00045 {
00046     myplayers[current_player]._score++;                                                //increment score for player
00047     _score = myplayers[current_player]._score;
00048     double _freqChange = _score*20;
00049     //printf("OPP score = %d\n", score);
00050     ctrl.led(1,1);
00051     ctrl.led(2,1);
00052     ctrl.led(3,1);
00053     ctrl.sound(50.0 + _freqChange,0.2);                                             //update speed of reaction
00054 }
00055 
00056 void Operator::inCorrect(int current_player, Controller &ctrl)
00057 {
00058     //printf("OPP.Incorrect player = %d\n", next_player);
00059     ctrl.sound(200,1);
00060     myplayers[current_player].status = false;                                          //set player to dead (false)
00061 }
00062 
00063 bool Operator::both_dead()
00064 {
00065     //printf("OPP.both_dead P1 %d\n", myplayers[0].status);
00066     //printf("OPP.both_dead P2 %d\n", myplayers[1].status);
00067     
00068     if (!myplayers[0].status && !myplayers[1].status) {                         // if both players false then both dead = true
00069         return true;
00070     } else {
00071         return false;
00072     }
00073 
00074 }
00075 
00076 
00077 
00078 int Operator::check_next_player(int current_player, N5110 &lcd, Controller &ctrl, Display display)
00079 {
00080 
00081     int mynext_player = current_player;
00082     //printf("OPP.check_next_player(1) current_player %d\n", mynext_player);
00083     if (_num_players > 1) {
00084 
00085         if ( (myplayers[current_player]._score % 10 == 0) || (!myplayers[current_player].status)) {  //on 0, and multiples of 10 swap player unless other player is dead
00086             myplayers[current_player].wait = display.get_wait();      //save speed
00087             
00088             if (current_player == 0 && myplayers[1].status) {                             //change to next player
00089                 mynext_player = 1;
00090             }
00091 
00092             if (current_player == 1 && myplayers[0].status) {                             //change to next player
00093                 mynext_player = 0;
00094             }
00095             //printf("OPP.check_next_player(2) next_player %d\n", mynext_player);
00096             display.put_wait(myplayers[mynext_player].wait);                            //put speed
00097             _displayNextPlayer(ctrl,lcd,mynext_player);                                  //Display next player
00098         }
00099 
00100     }
00101     return mynext_player;
00102 }
00103 
00104 void Operator::_displayNextPlayer(Controller &ctrl, N5110 &lcd, int thenext_player)
00105 { 
00106 //Screen telling user to swap players
00107     printf("OPP.DisplayNextPlayer threnext_player %d\n", thenext_player); 
00108     char buff[14];
00109     lcd.clear();
00110     sprintf(buff,"Player %d ",thenext_player+1);
00111     lcd.printString(buff,20,2);
00112     lcd.refresh();
00113     lcd.printString("3",40,4);
00114     ctrl.sound(50,1);
00115     lcd.refresh();
00116     wait(1);
00117     lcd.printString("2",40,4);
00118     lcd.refresh();
00119     ctrl.sound(200,1);
00120     wait(1);
00121     lcd.printString("1",40,4);
00122     lcd.refresh();
00123     ctrl.sound(300,1);
00124     wait(1);
00125     lcd.printString("0",40,4);
00126     lcd.refresh();
00127     ctrl.sound(400,2);
00128     wait(2);
00129 }
00130 
00131 
00132 void Operator::setup_players(int num_of_players)
00133 {
00134 
00135     myplayers[0].wait = 0.3;
00136     myplayers[0].status = true;
00137     myplayers[0]._score = 0;
00138     myplayers[1]._score = 0; 
00139     myplayers[1].wait = 0.3;
00140 
00141     if (num_of_players == 1) {
00142         myplayers[1].status = false;
00143     } else {
00144         myplayers[1].status = true;
00145     }
00146     _num_players = num_of_players;
00147 }
00148 
00149 
00150 
00151 void Operator::gameOver(Controller &ctrl, N5110 &lcd)
00152 {
00153     ctrl.init();                                            //reset flags
00154 
00155     while(ctrl.check_event(Controller::BACK_PRESSED) == false) {
00156         if (_num_players == 1) {
00157             _assessment(lcd, myplayers[0]._score);                //Display results for single player
00158             //printf("OPP num of Players = %d\n", _num_players);
00159         } else {
00160             _multiResults(lcd);                                 //display results for multiplayer
00161         }
00162         ctrl.ledsOFF();
00163         ctrl.led(4,1);
00164         ctrl.led(5,1);
00165         ctrl.led(6,1);
00166     }
00167 
00168     setup_players(1);
00169     _score = 0;
00170 }
00171 
00172 void Operator::_assessment(N5110 &lcd, int _score) 
00173 { 
00174 //Awarding the user a level based on score achieved
00175     //printf("OPP.ASSESSMENT score = %d\n", _score);
00176     lcd.printString("Game Over",16,0);
00177     char buffer[14];
00178     if (_score < 10) {
00179         sprintf(buffer,"0%d",_score);
00180         lcd.printString("Newbie",26,4);
00181     } else if (_score < 20) {
00182         sprintf(buffer,"%2d",_score);
00183         lcd.printString("Amuteur",24,4);
00184     } else if (_score < 30) {
00185         sprintf(buffer,"%2d",_score);
00186         lcd.printString("Expert",26,4);
00187     } else if (_score < 40) {
00188         sprintf(buffer,"%2d",_score);
00189         lcd.printString("Professional",10,4);
00190     } else if (_score < 50) {
00191         sprintf(buffer,"%2d",_score);
00192         lcd.printString("World Class",14,4);
00193     } else {
00194         sprintf(buffer,"%2d",_score); 
00195         lcd.printString("Legendary",16,4);
00196         
00197     }
00198     lcd.printString(buffer,37,2);
00199     lcd.drawCircle(42,20,10,FILL_TRANSPARENT); 
00200     lcd.refresh();
00201 }
00202 
00203 
00204 void Operator::_multiResults(N5110 &lcd)
00205 { 
00206 //Displaying scores
00207     //printf("OPP.MULTIRESULTS\n");
00208     lcd.clear();
00209     lcd.printString("Game Over",16,0);
00210     char buffer[14];
00211     sprintf(buffer,"Player 1 - %d",myplayers[0]._score);
00212     lcd.printString(buffer,5,2);
00213     sprintf(buffer,"Player 2 - %d",myplayers[1]._score);
00214     lcd.printString(buffer,5,3);
00215     int score1 = myplayers[0]._score;   
00216     int score2 = myplayers[1]._score;   
00217 
00218 //Identifying the winner
00219     if (score1 > score2) { 
00220         lcd.printString("Player 1 Wins", 5,4);
00221     } else {
00222         if (score2 > score1) {
00223             lcd.printString("Player 2 Wins", 5,4);
00224         } else {
00225             lcd.printString("Draw",5,4);
00226         }
00227     }
00228 
00229     lcd.refresh();
00230 }