game.cpp 5.1 KB

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