game.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "game.h"
  2. Game::Game()
  3. {
  4. // initialize the number of columns that each "color" will contain
  5. lastColumnIndex = CHECKBOXES - 1;
  6. // set up lockOut
  7. for(int i = 0; i<white1;i++)
  8. {
  9. lockOut.push_back(false);
  10. }
  11. // set the state of game [how to move from state to state ("game logic?")]
  12. state = NOT_STARTED;
  13. // dice have been initialized with random seed
  14. };
  15. bool Game::addPlayer(std::string name)
  16. {
  17. if(NOT_STARTED == state)
  18. {
  19. ScoreSheet newplayer(name);
  20. players.push_back(newplayer);
  21. fprintf (stdout, "added: %s to the game\n", name.c_str());
  22. return true;
  23. }
  24. else
  25. {
  26. fprintf(stdout, "player %s was not added to the game\n", name.c_str());
  27. return false;
  28. }
  29. };
  30. void Game::score()
  31. {
  32. // **CALCULATE EACH PLAYERS' SCORE**
  33. std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
  34. // check players' card for marked boxes
  35. for(int i = 0;i <players.size();i++)
  36. {
  37. // clear each players' previous score
  38. players[i].score = 0;
  39. // calculate penalty amount
  40. players[i].score -= players[i].cumulativeOlok.penaltyCount*PENALTY_VALUE;
  41. // count the Xs of each "color" and give me a score
  42. for(int j = 0;j <white1;j++)
  43. {
  44. // determine if player qualifies for the lockout point (this is a rule)
  45. int lockoutPoint = players[i].cumulativeOlok.getLastIndex(j) == lastColumnIndex;
  46. // use pointsGuide to get the score for this user's "color" row
  47. players[i].score += pointsGuide[players[i].cumulativeOlok.getXCount(j) + lockoutPoint];
  48. }
  49. }
  50. // **CHECK IF GAME IS DONE**
  51. // check each player's card for penalties
  52. for(int i = 0;i<players.size();i++)
  53. {
  54. // check for maximum penalty to see if game is over
  55. if(players[i].cumulativeOlok.penaltyCount == MAX_PENALTY){state = FINISHED;}
  56. }
  57. // check for lockout to see if game is over
  58. int lockCount = 0;
  59. for(int i = 0;i<lockOut.size();i++)
  60. {
  61. if(true == lockOut[i]){lockCount++;}
  62. }
  63. if(MAX_LOCKOUT <= lockCount)
  64. {
  65. state = FINISHED;
  66. }
  67. }
  68. void Game::round()
  69. {
  70. // start all players' turns
  71. for(int i = 0; i <players.size();i++)
  72. {
  73. players[i].isTurnDone = false;
  74. }
  75. // roll the dice
  76. dice.roll();
  77. // check to see if all players are done with turn
  78. while(true)
  79. {
  80. bool temp = true;
  81. for(int i = 0; i <players.size();i++)
  82. {
  83. temp = temp && players[i].isTurnDone;
  84. }
  85. if(temp){break;}
  86. Sleep(1);
  87. }
  88. // save data from currentTurnOlok into cumulativeOlok
  89. for(int i = 0; i <players.size();i++)
  90. {
  91. players[i].cumulativeOlok.addOlok(players[i].currentTurnOlok);
  92. }
  93. // score the cards
  94. score();
  95. // go to next player's turn
  96. activePlayer++;
  97. activePlayer %= players.size();
  98. }
  99. // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
  100. std::string Game::send()
  101. {
  102. std::string output;
  103. if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
  104. // add dice values to output
  105. for(int i = 0; i < dice.dice.size(); i++)
  106. {
  107. output.push_back(dice.dice[i] + '0');
  108. }
  109. // look at each scoresheet
  110. for(int i = 0; i < players.size(); i++)
  111. {
  112. output.append(players[i].cumulativeOlok.toString());
  113. }
  114. // add locked off row to output
  115. for(int i = 0; i < lockOut.size();i++)
  116. {
  117. output.push_back(lockOut[i] ? 'T' : 'F');
  118. }
  119. // add game end to output
  120. output.push_back(state == FINISHED ? 'T' : 'F');
  121. // print everything needed to send current game state to players
  122. fprintf(stdout, "the output is: %s\n", output.c_str());
  123. return output;
  124. }
  125. // kyle's intention is to check off red 12 and blue 5
  126. // " 1143"
  127. // emma's intention is to take a penalty
  128. // "1"
  129. // convert a user string into a turn
  130. struct Turn Game::receive(std::string userInput)
  131. {
  132. players[0].currentTurnOlok.clear();
  133. Turn turn;
  134. // userinput is one, add a penalty
  135. if(1 == userInput.size())
  136. {
  137. turn.penalty = 1;
  138. }
  139. // userinput size is two, add one moves
  140. else if(2 == userInput.size())
  141. {
  142. turn.moves[0] = translateToMove(userInput);
  143. turn.numberOfMoves = 1;
  144. }
  145. // userinput size is four, add two moves
  146. else if(4 == userInput.size())
  147. {
  148. turn.moves[0] = translateToMove(userInput.substr(0,2));
  149. turn.moves[1] = translateToMove(userInput.substr(2,2));
  150. turn.numberOfMoves = 2;
  151. }
  152. else {fprintf(stdout, "Form an opinion: WRONG!");}
  153. // check the rules to make sure turn is valid
  154. // add turn to currentTurnOlok if the turn is valid
  155. }
  156. bool Game::isMoveRepresentedInDie(int player, Turn turn)
  157. {
  158. // is this the non-active player
  159. if(player != activePlayer)
  160. {
  161. // if the move equals the sum of the two white die return true, if not return false
  162. return turn.moves[0].index == dice.dice[white1] + dice.dice[white2];
  163. }
  164. // otherwise this is the active player
  165. else
  166. {
  167. // if one move
  168. if(1 == turn.numberOfMoves)
  169. {
  170. // check sum of white die
  171. if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
  172. {
  173. int c = turn.moves[1].color;
  174. // if any white die plus the colored die equals the move
  175. if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
  176. {
  177. return true;
  178. }
  179. // colored die plus either white die was not a move
  180. else {return false;}
  181. }
  182. // otherwise the white sum die was a move
  183. else {return true;}
  184. }
  185. // if two moves
  186. else if(2 == turn.numberOfMoves)
  187. {
  188. // check first input with white die sum
  189. if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
  190. {
  191. // white die sum was not a move
  192. return false;
  193. }
  194. else
  195. {
  196. int c = turn.moves[1].color;
  197. // check to see if color die + either white die sum equals a move
  198. if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
  199. {
  200. return true;
  201. }
  202. else {return false;}
  203. }
  204. }
  205. }
  206. };
  207. bool Game::lockOutRule(int player, Turn turn)
  208. {
  209. // if the player has selected the last column, check it
  210. for(int i = 0; i<turn.moves.size(); i++)
  211. {
  212. // player can take their move in the last column if there are 5 recorded moves in that color
  213. if(LOCKOUT_QUALIFIER > players[player].cumulativeOlok.getXCount(turn.moves[i].color)
  214. {
  215. fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. bool Game::leftToRightRule(int player, Turn turn)
  222. {
  223. for(int i = 0; i<turn.moves.size(); i++)
  224. {
  225. // player must add an x to olok from left to right
  226. if(players[player].cumulativeOlok.getLastIndex(turn.moves[i].color) > turn.moves[i].index)
  227. {
  228. fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. bool Game::checkTurn (int player, Turn turn)
  235. {
  236. // check all rules for turn verification
  237. return lockOutRule( player, turn) && leftToRightRule( player, turn) && isMoveRepresentedInDie(player, turn);
  238. }
  239. void Game::addX(int player, int color, int index)
  240. {
  241. }
  242. struct Move Game::translateToMove(std::string temp)
  243. {
  244. int temp2 = stoi(temp);
  245. struct Move newMove;
  246. // find the color
  247. newMove.color = (temp2/colors);
  248. // find the index, subtracted 1 from checkboxes because it is lockout bonus box - only interested in scored boxes
  249. newMove.index = (temp2%(CHECKBOXES-1));
  250. return newMove;
  251. }