game.cpp 10 KB

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