game.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. {
  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].penaltyCount*PENALTY_VALUE;
  34. // check each color(row) on playerCard
  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].rowColors[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].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(PlayerCard 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. // add selection to players' cards
  88. for(int i = 0; i <players.size();i++)
  89. {
  90. int t;
  91. // everyone can select a color and a check box
  92. if(-1<(t = players[i].selection[everyone][row]))
  93. {
  94. int color = players[i].selection[everyone][row];
  95. int checkbox = players[i].selection[everyone][column];
  96. players[i].rowColors[color][checkbox] = true;
  97. }
  98. // active player can select a color and check box
  99. if(-1<(t = players[i].selection[active][row]))
  100. {
  101. int color = players[i].selection[active][row];
  102. int checkbox = players[i].selection[active][column];
  103. players[i].rowColors[color][checkbox] = true;
  104. }
  105. // player selects to add penalty, increase the penalty count
  106. if(0<(t = players[i].selection[penalty][0]))
  107. {
  108. players[i].penaltyCount++;
  109. }
  110. }
  111. // score the cards
  112. score();
  113. }
  114. // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
  115. std::string Game::send()
  116. {
  117. std::string output;
  118. if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
  119. // add dice values to output
  120. for(int i = 0; i < dice.dice.size(); i++)
  121. {
  122. output.push_back(dice.dice[i] + '0');
  123. }
  124. // look at each player card
  125. for(int i = 0; i < players.size(); i++)
  126. {
  127. // check each row on the player's card
  128. for(int j = 0; j < white1; j++)
  129. {
  130. // check each box on the player card and then store output
  131. for(int k = 0; k < CHECKBOXES;k++)
  132. {
  133. // look at checkbox value, add it to output
  134. output.push_back(players[i].rowColors[j][k] ? 'T' : 'F');
  135. }
  136. }
  137. // add penalty count to output
  138. output.push_back(players[i].penaltyCount + '0');
  139. }
  140. // add locked off row to output
  141. for(int i = 0; i < lockOut.size();i++)
  142. {
  143. output.push_back(lockOut[i] ? 'T' : 'F');
  144. }
  145. // add game end to output
  146. output.push_back(state == FINISHED ? 'T' : 'F');
  147. // print everything needed to send current game state to players
  148. fprintf(stdout, "the output is: %s\n", output.c_str());
  149. return output;
  150. }
  151. // kyle's intention is to check off red 12 and blue 5
  152. // " 1143"
  153. // emma's intention is to take a penalty
  154. // "1"
  155. void Game::receive(std::string userInput)
  156. {
  157. players[0].newSelection();
  158. // penalties are marked to card if user string = 1
  159. players[0].selection[penalty][0] = 1;
  160. // did the user send a number?
  161. // if a user did not send a number no changes take place
  162. // take the userinput (string) and parse it into parts
  163. // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
  164. players[0].selection[everyone][row] = green;
  165. players[0].selection[everyone][column] = 5;
  166. players[0].selection[active][row] = yellow;
  167. players[0].selection[active][column] = 11;
  168. // convert userInput into number
  169. // user number to change the boolean on corresponding spot on playerCard
  170. }