game.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // check for user request to add themselves to game
  140. // userInput == add+stringOfAnyLength
  141. if("add" == userInput.substr(0,3))
  142. {
  143. // add player to game
  144. addPlayer(userInput.substr(3));
  145. ScoreSheet lastPlayer = players.back();
  146. // send player secret -> for now it is only printed below
  147. fprintf(stdout, "Player %s has a secret of: %d",lastPlayer.savedName, lastPlayer.secretNumber);
  148. return turnInProgress;
  149. }
  150. // look for the position of a comma in userInput
  151. for(int i = 0;i < userInput.size();i++)
  152. {
  153. // a comma is found, set position and stop
  154. if(',' == userInput[i])
  155. {
  156. pos = i; break;
  157. }
  158. }
  159. // assuming there is a comma and it is not the first character, split userInput into two parts on the comma
  160. if(0<pos)
  161. {
  162. playerSecret = userInput.substr(0,pos-1);
  163. userInput = userInput.substr(pos);
  164. int secret = stoi(playerSecret);
  165. // check each player's secret against submitted secret to find player number
  166. for(int i = 0;i < players.size(); i++)
  167. {
  168. if(secret == players[i].secretNumber)
  169. {
  170. playerNumber = i; break;
  171. }
  172. }
  173. }
  174. if(-1 == playerNumber)
  175. {
  176. fprintf(stdout, "Could not find player: %s", original);
  177. return turnInProgress;
  178. }
  179. // userinput is one, add a penalty
  180. if(1 == userInput.size())
  181. {
  182. turnInProgress.penalty = 1;
  183. }
  184. // userinput size is two, add one moves
  185. else if(2 == userInput.size())
  186. {
  187. turnInProgress.moves[0] = translateToMove(userInput);
  188. turnInProgress.numberOfMoves = 1;
  189. }
  190. // userinput size is four, add two moves
  191. else if(4 == userInput.size())
  192. {
  193. turnInProgress.moves[0] = translateToMove(userInput.substr(0,2));
  194. turnInProgress.moves[1] = translateToMove(userInput.substr(2,2));
  195. turnInProgress.numberOfMoves = 2;
  196. }
  197. else {fprintf(stdout, "Form an opinion: WRONG!");}
  198. // check the rules to make sure turn is valid
  199. if(true == checkTurn(playerNumber, turnInProgress))
  200. {
  201. // currentTurnOlok can only have one valid turn in it at a time
  202. players[playerNumber].currentTurnOlok.clear();
  203. // check the moves in turn
  204. for(int i = 0;i < turnInProgress.numberOfMoves;i++)
  205. {
  206. // add turn to currentTurnOlok
  207. players[playerNumber].currentTurnOlok.addX(turnInProgress.moves[i]);
  208. }
  209. // player chose to take a penalty
  210. players[playerNumber].currentTurnOlok.penaltyCount += turnInProgress.penalty;
  211. }
  212. // adding time to entropyPool to increase entropy in the system
  213. entropyPool += time(NULL);
  214. }
  215. bool Game::isMoveRepresentedInDie(int player, Turn turn)
  216. {
  217. // is this the non-active player
  218. if(player != activePlayer)
  219. {
  220. // if the move equals the sum of the two white die return true, if not return false
  221. return turn.moves[0].index == dice.dice[white1] + dice.dice[white2];
  222. }
  223. // otherwise this is the active player
  224. else
  225. {
  226. // if one move
  227. if(1 == turn.numberOfMoves)
  228. {
  229. // check sum of white die
  230. if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
  231. {
  232. int c = turn.moves[1].color;
  233. // if any white die plus the colored die equals the move
  234. if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
  235. {
  236. return true;
  237. }
  238. // colored die plus either white die was not a move
  239. else {return false;}
  240. }
  241. // otherwise the white sum die was a move
  242. else {return true;}
  243. }
  244. // if two moves
  245. else if(2 == turn.numberOfMoves)
  246. {
  247. // check first input with white die sum
  248. if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
  249. {
  250. // white die sum was not a move
  251. return false;
  252. }
  253. else
  254. {
  255. int c = turn.moves[1].color;
  256. // check to see if color die + either white die sum equals a move
  257. if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
  258. {
  259. return true;
  260. }
  261. else {return false;}
  262. }
  263. }
  264. }
  265. };
  266. bool Game::lockOutRule(int player, Turn turn)
  267. {
  268. // if the player has selected the last column, check it
  269. for(int i = 0; i<turn.moves.size(); i++)
  270. {
  271. // player can take their move in the last column if there are 5 recorded moves in that color
  272. if(LOCKOUT_QUALIFIER > players[player].cumulativeOlok.getXCount(turn.moves[i].color)
  273. {
  274. fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);
  275. return false;
  276. }
  277. }
  278. return true;
  279. }
  280. bool Game::leftToRightRule(int player, Turn turn)
  281. {
  282. for(int i = 0; i<turn.moves.size(); i++)
  283. {
  284. // player must add an x to olok from left to right
  285. if(players[player].cumulativeOlok.getLastIndex(turn.moves[i].color) > turn.moves[i].index)
  286. {
  287. fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293. bool Game::checkTurn (int player, Turn turn)
  294. {
  295. // check all rules for turn verification
  296. return lockOutRule( player, turn) && leftToRightRule( player, turn) && isMoveRepresentedInDie(player, turn);
  297. }
  298. void Game::addX(int player, int color, int index)
  299. {
  300. }
  301. struct Move Game::translateToMove(std::string temp)
  302. {
  303. int temp2 = stoi(temp);
  304. struct Move newMove;
  305. // find the color
  306. newMove.color = (temp2/colors);
  307. // find the index, subtracted 1 from checkboxes because it is lockout bonus box - only interested in scored boxes
  308. newMove.index = (temp2%(CHECKBOXES-1));
  309. return newMove;
  310. }