game.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. PlayerCard 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. // **CALCULATE EACH PLAYERS' SCORE**
  25. std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
  26. // check players' card for marked boxes
  27. for(int i = 0;i <players.size();i++){
  28. // clear each players' previous score
  29. players[i].score = 0;
  30. // calculate penalty amount
  31. players[i].score -= players[i].penaltyCount*PENALTY_VALUE;
  32. // check each color(row) on playerCard
  33. for(int j = 0;j <white1;j++){
  34. int count = 0;
  35. // check each box in row
  36. for(int k =0;k <CHECKBOXES;k++){
  37. if(true == players[i].rowColors[j][k]){
  38. count++;
  39. }
  40. }
  41. players[i].score += pointsGuide[count];
  42. }
  43. }
  44. // **CHECK IF GAME IS DONE**
  45. // check each player's card for penalties
  46. for(int i = 0;i<players.size();i++){
  47. // check for maximum penalty to see if game is over
  48. if(players[i].penaltyCount == MAX_PENALTY){state = FINISHED;}
  49. }
  50. // check for lockout to see if game is over
  51. int lockCount = 0;
  52. for(int i = 0;i<lockOut.size();i++){
  53. if(true == lockOut[i]){lockCount++;}
  54. }
  55. if(MAX_LOCKOUT <= lockCount){
  56. state = FINISHED;
  57. }
  58. }
  59. void Game::turn(PlayerCard activePlayer) {
  60. // start all players' turns
  61. for(int i = 0; i <players.size();i++){
  62. players[i].isTurnDone = false;
  63. }
  64. // roll the dice
  65. dice.roll();
  66. // check to see if all players are done with turn
  67. while(true){
  68. bool temp = true;
  69. for(int i = 0; i <players.size();i++){
  70. temp = temp && players[i].isTurnDone;
  71. }
  72. if(temp){break;}
  73. Sleep(1);
  74. }
  75. // score the cards
  76. score();
  77. }
  78. std::string Game::send()
  79. {
  80. std::string output;
  81. // gernerate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
  82. // look at each player card
  83. for(int i = 0; i < players.size(); i++)
  84. {
  85. // check each row on the player's card
  86. for(int j = 0; j < white1; j++)
  87. {
  88. // check each box on the player card and then store output
  89. for(int k = 0; k < CHECKBOXES;k++)
  90. {
  91. // look at checkbox value, add it to output
  92. output.push_back(players[i].rowColors[j][k] ? 'T' : 'F');
  93. }
  94. }
  95. }
  96. fprintf(stdout, "the output is: %s\n", output.c_str());
  97. return output;
  98. }
  99. void Game::receive(std::string userInput){
  100. // newest item checked off on row&column (maybe 2) or penalty
  101. // once data is received, add it to the playerCard
  102. }