game.cpp 10 KB

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