game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // technically this is a sleep, milliseconds is for how long
  89. somethingNeedDoing(1000 /* milliseconds */, storage);
  90. }
  91. // save data from currentTurnOlok into cumulativeOlok
  92. for(int i = 0; i <players.size();i++)
  93. {
  94. players[i].cumulativeOlok.addOlok(players[i].currentTurnOlok);
  95. }
  96. // score the cards
  97. score();
  98. // go to next player's turn
  99. activePlayer++;
  100. activePlayer %= players.size();
  101. // adding in entropy from human players of THIS round to dice so they are slightly more random
  102. dice.diceTropy(entropyPool);
  103. }
  104. // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
  105. std::string Game::send()
  106. {
  107. std::string output;
  108. if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
  109. // add dice values to output
  110. for(int i = 0; i < dice.dice.size(); i++)
  111. {
  112. output.push_back(dice.dice[i] + '0');
  113. }
  114. // look at each scoresheet
  115. for(int i = 0; i < players.size(); i++)
  116. {
  117. output.append(players[i].cumulativeOlok.toString());
  118. }
  119. // add locked off row to output
  120. for(int i = 0; i < lockOut.size();i++)
  121. {
  122. output.push_back(lockOut[i] ? 'T' : 'F');
  123. }
  124. // add game end to output
  125. output.push_back(state == FINISHED ? 'T' : 'F');
  126. // print everything needed to send current game state to players
  127. fprintf(stdout, "the output is: %s\n", output.c_str());
  128. return output;
  129. }
  130. // kyle's intention is to check off red 12 and blue 5
  131. // " kylesecret,1143"
  132. // emma's intention is to take a penalty
  133. // "emmasecret,1"
  134. // convert a user string into a turn
  135. struct Turn Game::receive(std::string userInput)
  136. {
  137. Turn turnInProgress;
  138. std::string playerSecret, original = userInput;
  139. int playerNumber = -1, pos = 0;
  140. // check for user request to add themselves to game
  141. // userInput == add+stringOfAnyLength
  142. if("add" == userInput.substr(0,3))
  143. {
  144. // add player to game
  145. addPlayer(userInput.substr(3));
  146. ScoreSheet lastPlayer = players.back();
  147. // send player secret -> for now it is only printed below
  148. fprintf(stdout, "Player %s has a secret of: %d",lastPlayer.savedName, lastPlayer.secretNumber);
  149. return turnInProgress;
  150. }
  151. // look for the position of a comma in userInput
  152. for(int i = 0;i < userInput.size();i++)
  153. {
  154. // a comma is found, set position and stop
  155. if(',' == userInput[i])
  156. {
  157. pos = i; break;
  158. }
  159. }
  160. // assuming there is a comma and it is not the first character, split userInput into two parts on the comma
  161. if(0<pos)
  162. {
  163. playerSecret = userInput.substr(0,pos-1);
  164. userInput = userInput.substr(pos);
  165. int secret = stoi(playerSecret);
  166. // check each player's secret against submitted secret to find player number
  167. for(int i = 0;i < players.size(); i++)
  168. {
  169. if(secret == players[i].secretNumber)
  170. {
  171. playerNumber = i; break;
  172. }
  173. }
  174. }
  175. if(-1 == playerNumber)
  176. {
  177. fprintf(stdout, "Could not find player: %s", original);
  178. return turnInProgress;
  179. }
  180. // userinput is one, add a penalty
  181. if(1 == userInput.size())
  182. {
  183. turnInProgress.penalty = 1;
  184. }
  185. // userinput size is two, add one moves
  186. else if(2 == userInput.size())
  187. {
  188. turnInProgress.moves[0] = translateToMove(userInput);
  189. turnInProgress.numberOfMoves = 1;
  190. }
  191. // userinput size is four, add two moves
  192. else if(4 == userInput.size())
  193. {
  194. turnInProgress.moves[0] = translateToMove(userInput.substr(0,2));
  195. turnInProgress.moves[1] = translateToMove(userInput.substr(2,2));
  196. turnInProgress.numberOfMoves = 2;
  197. }
  198. else {fprintf(stdout, "Form an opinion: WRONG!");}
  199. // check the rules to make sure turn is valid
  200. if(true == checkTurn(playerNumber, turnInProgress))
  201. {
  202. // currentTurnOlok can only have one valid turn in it at a time
  203. players[playerNumber].currentTurnOlok.clear();
  204. // check the moves in turn
  205. for(int i = 0;i < turnInProgress.numberOfMoves;i++)
  206. {
  207. // add turn to currentTurnOlok
  208. players[playerNumber].currentTurnOlok.addX(turnInProgress.moves[i]);
  209. }
  210. // player chose to take a penalty
  211. players[playerNumber].currentTurnOlok.penaltyCount += turnInProgress.penalty;
  212. }
  213. // adding time to entropyPool to increase entropy in the system
  214. entropyPool += time(NULL);
  215. }
  216. bool Game::isMoveRepresentedInDie(int player, Turn turn)
  217. {
  218. // is this the non-active player
  219. if(player != activePlayer)
  220. {
  221. // if the move equals the sum of the two white die return true, if not return false
  222. return turn.moves[0].index == dice.dice[white1] + dice.dice[white2];
  223. }
  224. // otherwise this is the active player
  225. else
  226. {
  227. // if one move
  228. if(1 == turn.numberOfMoves)
  229. {
  230. // check sum of white die
  231. if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
  232. {
  233. int c = turn.moves[1].color;
  234. // if any white die plus the colored die equals the move
  235. if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
  236. {
  237. return true;
  238. }
  239. // colored die plus either white die was not a move
  240. else {return false;}
  241. }
  242. // otherwise the white sum die was a move
  243. else {return true;}
  244. }
  245. // if two moves
  246. else if(2 == turn.numberOfMoves)
  247. {
  248. // check first input with white die sum
  249. if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
  250. {
  251. // white die sum was not a move
  252. return false;
  253. }
  254. else
  255. {
  256. int c = turn.moves[1].color;
  257. // check to see if color die + either white die sum equals a move
  258. if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
  259. {
  260. return true;
  261. }
  262. else {return false;}
  263. }
  264. }
  265. }
  266. };
  267. bool Game::lockOutRule(int player, Turn turn)
  268. {
  269. // if the player has selected the last column, check it
  270. for(int i = 0; i<turn.moves.size(); i++)
  271. {
  272. // player can take their move in the last column if there are 5 recorded moves in that color
  273. if(LOCKOUT_QUALIFIER > players[player].cumulativeOlok.getXCount(turn.moves[i].color)
  274. {
  275. fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);
  276. return false;
  277. }
  278. }
  279. return true;
  280. }
  281. bool Game::leftToRightRule(int player, Turn turn)
  282. {
  283. for(int i = 0; i<turn.moves.size(); i++)
  284. {
  285. // player must add an x to olok from left to right
  286. if(players[player].cumulativeOlok.getLastIndex(turn.moves[i].color) > turn.moves[i].index)
  287. {
  288. fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. bool Game::checkTurn (int player, Turn turn)
  295. {
  296. // check all rules for turn verification
  297. return lockOutRule( player, turn) && leftToRightRule( player, turn) && isMoveRepresentedInDie(player, turn);
  298. }
  299. void Game::addX(int player, int color, int index)
  300. {
  301. }
  302. struct Move Game::translateToMove(std::string temp)
  303. {
  304. int temp2 = stoi(temp);
  305. struct Move newMove;
  306. // find the color
  307. newMove.color = (temp2/colors);
  308. // find the index, subtracted 1 from checkboxes because it is lockout bonus box - only interested in scored boxes
  309. newMove.index = (temp2%(CHECKBOXES-1));
  310. return newMove;
  311. }