123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #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){
- PlayerCard 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].penaltyCount*PENALTY_VALUE;
- // check each color(row) on playerCard
- 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].rowColors[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].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(PlayerCard 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);
- }
- // score the cards
- score();
- }
- std::string Game::send()
- {
- std::string output;
- // gernerate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
- // look at each player card
- for(int i = 0; i < players.size(); i++)
- {
- // check each row on the player's card
- for(int j = 0; j < white1; j++)
- {
- // check each box on the player card and then store output
- for(int k = 0; k < CHECKBOXES;k++)
- {
- // look at checkbox value, add it to output
- output.push_back(players[i].rowColors[j][k] ? 'T' : 'F');
- }
- }
- }
- fprintf(stdout, "the output is: %s\n", output.c_str());
- return output;
- }
- void Game::receive(std::string userInput){
- // newest item checked off on row&column (maybe 2) or penalty
- // once data is received, add it to the playerCard
- }
|