game.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "game.h"
  2. Game::Game() {
  3. // set up lockOut
  4. for(int i = 0; i<white1;i++){
  5. lockOut.push_back(false);
  6. }
  7. // set the state of game [how to move from state to state ("game logic?")]
  8. state = NOT_STARTED;
  9. // dice have been initialized with random seed
  10. };
  11. bool Game::addPlayer(std::string name){
  12. if(NOT_STARTED == state){
  13. ScoreSheet newplayer(name);
  14. players.push_back(newplayer);
  15. fprintf (stdout, "added: %s to the game\n", name.c_str());
  16. return true;
  17. }
  18. else{
  19. fprintf(stdout, "player %s was not added to the game\n", name.c_str());
  20. return false;
  21. }
  22. };
  23. void Game::score()
  24. {
  25. // **CALCULATE EACH PLAYERS' SCORE**
  26. std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
  27. // check players' card for marked boxes
  28. for(int i = 0;i <players.size();i++)
  29. {
  30. // clear each players' previous score
  31. players[i].score = 0;
  32. // calculate penalty amount
  33. players[i].score -= players[i].cumulativeOlok.penaltyCount*PENALTY_VALUE;
  34. // check each color(row) on ScoreSheet
  35. for(int j = 0;j <white1;j++)
  36. {
  37. int count = 0;
  38. // check each box in row
  39. for(int k =0;k <CHECKBOXES;k++)
  40. {
  41. if(true == players[i].cumulativeOlok.grid[j][k])
  42. {
  43. count++;
  44. }
  45. }
  46. players[i].score += pointsGuide[count];
  47. }
  48. }
  49. // **CHECK IF GAME IS DONE**
  50. // check each player's card for penalties
  51. for(int i = 0;i<players.size();i++)
  52. {
  53. // check for maximum penalty to see if game is over
  54. if(players[i].cumulativeOlok.penaltyCount == MAX_PENALTY){state = FINISHED;}
  55. }
  56. // check for lockout to see if game is over
  57. int lockCount = 0;
  58. for(int i = 0;i<lockOut.size();i++)
  59. {
  60. if(true == lockOut[i]){lockCount++;}
  61. }
  62. if(MAX_LOCKOUT <= lockCount)
  63. {
  64. state = FINISHED;
  65. }
  66. }
  67. void Game::turn(ScoreSheet activePlayer)
  68. {
  69. // start all players' turns
  70. for(int i = 0; i <players.size();i++)
  71. {
  72. players[i].isTurnDone = false;
  73. }
  74. // roll the dice
  75. dice.roll();
  76. // check to see if all players are done with turn
  77. while(true)
  78. {
  79. bool temp = true;
  80. for(int i = 0; i <players.size();i++)
  81. {
  82. temp = temp && players[i].isTurnDone;
  83. }
  84. if(temp){break;}
  85. Sleep(1);
  86. }
  87. // save data from currentTurnOlok into cumulativeOlok
  88. for(int i = 0; i <players.size();i++)
  89. {
  90. // find changes in currentTurnOlok and transfer to cumulativeOlok
  91. for(int j = 0;j < players[i].currentTurnOlok.grid.size();j++)
  92. {
  93. for(int k = 0;k < players[i].currentTurnOlok.grid[j].size();k++)
  94. {
  95. // check for true values in currentTurnOlok's grid
  96. if(true == players[i].currentTurnOlok.grid[j][k])
  97. {
  98. // transfer true values from currentTurnOlok's grid to cumulativeOlok's grid
  99. players[i].cumulativeOlok.grid[j][k] = true;
  100. }
  101. }
  102. }
  103. // player selects to add penalty, increase the penalty count
  104. players[i].cumulativeOlok.penaltyCount += players[i].currentTurnOlok.penaltyCount;
  105. }
  106. // score the cards
  107. score();
  108. }
  109. // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
  110. std::string Game::send()
  111. {
  112. std::string output;
  113. if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
  114. // add dice values to output
  115. for(int i = 0; i < dice.dice.size(); i++)
  116. {
  117. output.push_back(dice.dice[i] + '0');
  118. }
  119. // look at each scoresheet
  120. for(int i = 0; i < players.size(); i++)
  121. {
  122. // check each row on the grid on the scoresheet
  123. for(int j = 0; j < players[i].cumulativeOlok.grid.size(); j++)
  124. {
  125. // check each box on the row and then store output
  126. for(int k = 0; k < players[i].cumulativeOlok.grid[j].size();k++)
  127. {
  128. // look at checkbox value, add it to output
  129. output.push_back(players[i].cumulativeOlok.grid[j][k] ? 'T' : 'F');
  130. }
  131. }
  132. // add penalty count to output
  133. output.push_back(players[i].cumulativeOlok.penaltyCount + '0');
  134. }
  135. // add locked off row to output
  136. for(int i = 0; i < lockOut.size();i++)
  137. {
  138. output.push_back(lockOut[i] ? 'T' : 'F');
  139. }
  140. // add game end to output
  141. output.push_back(state == FINISHED ? 'T' : 'F');
  142. // print everything needed to send current game state to players
  143. fprintf(stdout, "the output is: %s\n", output.c_str());
  144. return output;
  145. }
  146. // kyle's intention is to check off red 12 and blue 5
  147. // " 1143"
  148. // emma's intention is to take a penalty
  149. // "1"
  150. void Game::receive(std::string userInput)
  151. {
  152. players[0].currentTurnOlok.clear();
  153. // penalties are marked to card if user string = 1
  154. // did the user send a number?
  155. // if a user did not send a number no changes take place
  156. // take the userinput (string) and parse it into parts
  157. // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
  158. // convert userInput into number
  159. // user number to change the boolean on corresponding spot on ScoreSheet
  160. }