game.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "qwixx.h"
  2. #include "dice.h"
  3. #include "playerCard.h"
  4. #include <windows.h>
  5. class Game {
  6. public:
  7. std::vector<PlayerCard> players;
  8. std::vector<bool> lockOut;
  9. int state;
  10. Dice dice;
  11. void turn(PlayerCard activePlayer);
  12. void score();
  13. // rules as methods of game
  14. bool addPlayer(std::string name);
  15. };
  16. Game::Game() {
  17. // set up lockOut
  18. for(int i = 0; i<white1;i++){
  19. lockOut.push_back(false);
  20. }
  21. // set the state of game [how to move from state to state ("game logic?")]
  22. // dice have been initialized with random seed
  23. };
  24. bool Game::addPlayer(std::string name){
  25. if(NOT_STARTED == state){
  26. PlayerCard newplayer(name);
  27. players.push_back(newplayer);
  28. fprintf (stdout, "added: %s to the game", name);
  29. return true;
  30. }
  31. else{
  32. fprintf(stdout, "player %s was not added to the game", name);
  33. return false;
  34. }
  35. };
  36. void Game::score(){
  37. // **CALCULATE EACH PLAYERS' SCORE**
  38. std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
  39. // check players' card for marked boxes
  40. for(int i = 0;i <players.size();i++){
  41. // clear each players' previous score
  42. players[i].score = 0;
  43. // calculate penalty amount
  44. players[i].score -= players[i].penaltyCount*PENALTY_VALUE;
  45. // check each color(row) on playerCard
  46. for(int j = 0;j <white1;j++){
  47. int count = 0;
  48. // check each box in row
  49. for(int k =0;k <CHECKBOXES;k++){
  50. if(true == players[i].rowColors[j][k]){
  51. count++;
  52. }
  53. }
  54. players[i].score += pointsGuide[count];
  55. }
  56. }
  57. // **CHECK IF GAME IS DONE**
  58. // check each player's card for penalties
  59. for(int i = 0;i<players.size();i++){
  60. // check for maximum penalty to see if game is over
  61. if(players[i].penaltyCount == MAX_PENALTY){state = FINISHED;}
  62. }
  63. // check for lockout to see if game is over
  64. int lockCount = 0;
  65. for(int i = 0;i<lockOut.size();i++){
  66. if(true == lockOut[i]){lockCount++;}
  67. }
  68. if(MAX_LOCKOUT <= lockCount){
  69. state = FINISHED;
  70. }
  71. }
  72. void Game::turn(PlayerCard activePlayer) {
  73. // start all players' turns
  74. for(int i = 0; i <players.size();i++){
  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. bool temp = true;
  82. for(int i = 0; i <players.size();i++){
  83. temp = temp && players[i].isTurnDone;
  84. }
  85. if(temp){break;}
  86. Sleep(1);
  87. }
  88. // score the cards
  89. score();
  90. }