game.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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::turn(ScoreSheet activePlayer)
  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. void Game::receive(std::string userInput)
  130. {
  131. players[0].currentTurnOlok.clear();
  132. // penalties are marked to card if user string = 1
  133. // did the user send a number?
  134. // ## Range Check to go into Receive ##
  135. #ifdef WORKING_ON_RANGE_CHECK
  136. // if index is too high or too low, throw an error
  137. if(CHECKBOXES > index || 0 < index){fprintf(stdout,"Player[%d] %s index is too high or too low X\n", player, players[player].savedName);}
  138. // if color is not valid throw an error
  139. players[0].cumulativeOlok.isColor(red);
  140. if(color){fprintf(stdout,"Player[%d] %s invalid color selected X\n", player, players[player].savedName);}
  141. #endif
  142. // if a user did not send a number no changes take place
  143. // take the userinput (string) and parse it into parts
  144. // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
  145. // convert userInput into number
  146. // user number to change the boolean on corresponding spot on ScoreSheet
  147. }
  148. /* add a checkbox to a specified players scoresheet user specifies player's: XXXX, the color, and index of the checkbox on the colored row
  149. * dont know what XXXX is yet - but it will be used to find correct player
  150. *
  151. *
  152. *
  153. * the white die and color combo must match the corresponding RowColor and index
  154. *
  155. */
  156. /*
  157. * all players can do white die combination, active player can do a colored + white die combination.
  158. *
  159. * ## Things we need to know ##
  160. * sum of both white die
  161. * sums of 8 dice
  162. * who is active player
  163. *
  164. * ## How to do ##
  165. * on a given turn, sum the white die and sum each of the white die and all of the colored die
  166. * check to see if each players' requested move is valid
  167. * - does their input match the sum of the white die
  168. * check to see if active player requested move is valid
  169. * - for the active player check white die sum if one input. if it fails then check each white die + color die sums add item to scoresheet
  170. * - for the active player if two inputs are passsed, check white die sum first if it passes, add to scoresheet. then check the second input with white die + colored die
  171. * if it is valid, add it to player card
  172. * if it is not valid, return an error to player
  173. *
  174. * ## players' move
  175. * // this function has access to diceRoll and player input
  176. * // is the players' input a valid move <- what does valid mean? (is it represented in die, does it follow left to right, does it follow lockout, does it fit within the confines of a move)
  177. * // the confines of a move for non-actie player is (0 or one white die move)
  178. * // the confines of a move for active palyer is (penalty or (one or two die moves))
  179. *
  180. * //
  181. */
  182. bool Game::isMoveRepresentedInDie(int player, CollectionOfMoves collectionOfMoves)
  183. {
  184. // is this the non-active player
  185. if(player != activePlayer)
  186. {
  187. // if the move equals the sum of the two white die return true, if not return false
  188. return collectionOfMoves.white.index == dice.dice[white1] + dice.dice[white2];
  189. }
  190. // otherwise this is the active player
  191. else
  192. {
  193. // if one move
  194. if(1 == collectionOfMoves.numberOfMoves)
  195. {
  196. // check sum of white die
  197. if(collectionOfMoves.white.index != (dice.dice[white1] + dice.dice[white2]))
  198. {
  199. int c = collectionOfMoves.color.color;
  200. // if any white die plus the colored die equals the move
  201. if(collectionOfMoves.color.index == (dice.dice[white1] + dice.dice[c]) || collectionOfMoves.color.index == (dice.dice[white2] + dice.dice[c]))
  202. {
  203. return true;
  204. }
  205. // colored die plus either white die was not a move
  206. else {return false;}
  207. }
  208. // otherwise the white sum die was a move
  209. else {return true;}
  210. }
  211. // if two move
  212. else if(2 == collectionOfMoves.numberOfMoves)
  213. {
  214. // check first input with white die sum
  215. if(collectionOfMoves.white.index != (dice.dice[white1] + dice.dice[white2]))
  216. {
  217. // white die sum was not a move
  218. return false;
  219. }
  220. else
  221. {
  222. int c = collectionOfMoves.color.color;
  223. // check to see if color die + either white die sum equals a move
  224. if(collectionOfMoves.color.index == (dice.dice[white1] + dice.dice[c]) || collectionOfMoves.color.index == (dice.dice[white2] + dice.dice[c]))
  225. {
  226. return true;
  227. }
  228. else {return false;}
  229. }
  230. }
  231. }
  232. };
  233. void Game::lockOutRule(int player, int color, int index)
  234. {
  235. // player can add an x to olok in last column if there are 5 x
  236. if(lastColumnIndex == index)
  237. {
  238. if(LOCKOUT_QUALIFIER >= players[player].cumulativeOlok.getXCount(color))
  239. {
  240. players[player].currentTurnOlok.addX(color, index);
  241. }
  242. else{fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);}
  243. }
  244. }
  245. void Game::leftToRightRule(int player, int color, int index)
  246. {
  247. // player must add an x to olok from left to right
  248. if(players[player].cumulativeOlok.getLastIndex(color) > index)
  249. {
  250. fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
  251. }
  252. else{players[player].currentTurnOlok.addX(color, index);}
  253. }
  254. void Game::addX(int player, int color, int index)
  255. {
  256. }