123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include "game.h"
- Game::Game() {
- // set up lockOut
- for(int i = 0; i<white1;i++){
- lockOut.push_back(false);
- }
- // set the state of game [how to move from state to state ("game logic?")]
- state = NOT_STARTED;
- // dice have been initialized with random seed
- };
- bool Game::addPlayer(std::string name){
- if(NOT_STARTED == state){
- ScoreSheet newplayer(name);
- players.push_back(newplayer);
- fprintf (stdout, "added: %s to the game\n", name.c_str());
- return true;
- }
- else{
- fprintf(stdout, "player %s was not added to the game\n", name.c_str());
- return false;
- }
- };
- void Game::score()
- {
- // **CALCULATE EACH PLAYERS' SCORE**
- std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
- // check players' card for marked boxes
- for(int i = 0;i <players.size();i++)
- {
- // clear each players' previous score
- players[i].score = 0;
- // calculate penalty amount
- players[i].score -= players[i].cumulativeOlok.penaltyCount*PENALTY_VALUE;
- // check each color(row) on ScoreSheet
- for(int j = 0;j <white1;j++)
- {
- int count = 0;
- // check each box in row
- for(int k =0;k <CHECKBOXES;k++)
- {
- if(true == players[i].cumulativeOlok.grid[j][k])
- {
- count++;
- }
- }
- players[i].score += pointsGuide[count];
- }
- }
- // **CHECK IF GAME IS DONE**
- // check each player's card for penalties
- for(int i = 0;i<players.size();i++)
- {
- // check for maximum penalty to see if game is over
- if(players[i].cumulativeOlok.penaltyCount == MAX_PENALTY){state = FINISHED;}
- }
- // check for lockout to see if game is over
- int lockCount = 0;
- for(int i = 0;i<lockOut.size();i++)
- {
- if(true == lockOut[i]){lockCount++;}
- }
- if(MAX_LOCKOUT <= lockCount)
- {
- state = FINISHED;
- }
- }
- void Game::turn(ScoreSheet activePlayer)
- {
- // start all players' turns
- for(int i = 0; i <players.size();i++)
- {
- players[i].isTurnDone = false;
- }
- // roll the dice
- dice.roll();
- // check to see if all players are done with turn
- while(true)
- {
- bool temp = true;
- for(int i = 0; i <players.size();i++)
- {
- temp = temp && players[i].isTurnDone;
- }
- if(temp){break;}
- Sleep(1);
- }
- // save data from currentTurnOlok into cumulativeOlok
- for(int i = 0; i <players.size();i++)
- {
- // find changes in currentTurnOlok and transfer to cumulativeOlok
- for(int j = 0;j < players[i].currentTurnOlok.grid.size();j++)
- {
- for(int k = 0;k < players[i].currentTurnOlok.grid[j].size();k++)
- {
- // check for true values in currentTurnOlok's grid
- if(true == players[i].currentTurnOlok.grid[j][k])
- {
- // transfer true values from currentTurnOlok's grid to cumulativeOlok's grid
- players[i].cumulativeOlok.grid[j][k] = true;
- }
- }
- }
- // player selects to add penalty, increase the penalty count
- players[i].cumulativeOlok.penaltyCount += players[i].currentTurnOlok.penaltyCount;
- }
- // score the cards
- score();
- }
- // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
- std::string Game::send()
- {
- std::string output;
- if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
- // add dice values to output
- for(int i = 0; i < dice.dice.size(); i++)
- {
- output.push_back(dice.dice[i] + '0');
- }
- // look at each scoresheet
- for(int i = 0; i < players.size(); i++)
- {
- // check each row on the grid on the scoresheet
- for(int j = 0; j < players[i].cumulativeOlok.grid.size(); j++)
- {
- // check each box on the row and then store output
- for(int k = 0; k < players[i].cumulativeOlok.grid[j].size();k++)
- {
- // look at checkbox value, add it to output
- output.push_back(players[i].cumulativeOlok.grid[j][k] ? 'T' : 'F');
- }
- }
- // add penalty count to output
- output.push_back(players[i].cumulativeOlok.penaltyCount + '0');
- }
- // add locked off row to output
- for(int i = 0; i < lockOut.size();i++)
- {
- output.push_back(lockOut[i] ? 'T' : 'F');
- }
- // add game end to output
- output.push_back(state == FINISHED ? 'T' : 'F');
- // print everything needed to send current game state to players
- fprintf(stdout, "the output is: %s\n", output.c_str());
- return output;
- }
- // kyle's intention is to check off red 12 and blue 5
- // " 1143"
- // emma's intention is to take a penalty
- // "1"
- void Game::receive(std::string userInput)
- {
- players[0].currentTurnOlok.clear();
- // penalties are marked to card if user string = 1
- // did the user send a number?
- // if a user did not send a number no changes take place
- // take the userinput (string) and parse it into parts
- // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
- // convert userInput into number
- // user number to change the boolean on corresponding spot on ScoreSheet
- }
|