Browse Source

added score function to game, includes lockout, points, check for penalties. improved how turns will take place in game.

wes 4 years ago
parent
commit
9bd77537ec
5 changed files with 81 additions and 10 deletions
  1. 68 6
      Qwixx/game.cpp
  2. 2 2
      Qwixx/playerCard.cpp
  3. 3 1
      Qwixx/playerCard.h
  4. 3 0
      Qwixx/qwixx.h
  5. 5 1
      Qwixx/toDo.md

+ 68 - 6
Qwixx/game.cpp

@@ -1,21 +1,28 @@
 #include "qwixx.h"
 #include "dice.h"
 #include "playerCard.h"
+#include <windows.h>
 class Game {
     public:
-    std::vector<PlayerCard> players; 
+    std::vector<PlayerCard> players;
+    std::vector<bool> lockOut;
     int state; 
     Dice dice;
+    void turn(PlayerCard activePlayer);
+    void score();
     // rules as methods of game
-    bool addPlayer(std::string name);  
+    bool addPlayer(std::string name);
 };
 Game::Game() {
-    // set the state of game
-    state = NOT_STARTED;
+    // set up lockOut
+    for(int i = 0; i<white1;i++){
+        lockOut.push_back(false);
+    }
+    // set the state of game [how to move from state to state ("game logic?")]
     // dice have been initialized with random seed
 };
 bool Game::addPlayer(std::string name){
-    if (NOT_STARTED == state){
+    if(NOT_STARTED == state){
         PlayerCard newplayer(name);
         players.push_back(newplayer);
         fprintf (stdout, "added: %s to the game", name);
@@ -26,4 +33,59 @@ bool Game::addPlayer(std::string name){
         return false;
     }
 
-};
+};
+void Game::score(){
+    // **CALCULATE EACH PLAYERS' SCORE**
+    std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
+    // check players' card for marked boxes
+    for(int i = 0;i <players.size();i++){
+        // clear each players' previous score
+        players[i].score = 0;
+        // calculate penalty amount
+        players[i].score -= players[i].penaltyCount*PENALTY_VALUE;
+        // check each color(row) on playerCard
+        for(int j = 0;j <white1;j++){
+            int count = 0;
+            // check each box in row
+            for(int k =0;k <CHECKBOXES;k++){
+                if(true == players[i].rowColors[j][k]){
+                    count++;
+                }
+            }
+            players[i].score += pointsGuide[count];
+        }
+    }
+    // **CHECK IF GAME IS DONE**
+    // check each player's card for penalties
+    for(int i = 0;i<players.size();i++){
+        // check for maximum penalty to see if game is over
+        if(players[i].penaltyCount == MAX_PENALTY){state = FINISHED;}      
+    }
+    // check for lockout to see if game is over
+    int lockCount = 0;
+    for(int i = 0;i<lockOut.size();i++){
+        if(true == lockOut[i]){lockCount++;}
+    }
+    if(MAX_LOCKOUT <= lockCount){
+        state = FINISHED;
+    }
+}
+void Game::turn(PlayerCard activePlayer) {
+    // start all players' turns
+    for(int i = 0; i <players.size();i++){
+        players[i].isTurnDone = false;
+    }
+    // roll the dice
+    dice.roll();
+    // check to see if all players are done with turn
+    while(true){
+        bool temp = true;
+        for(int i = 0; i <players.size();i++){
+            temp = temp && players[i].isTurnDone;
+        }
+        if(temp){break;}
+        Sleep(1);
+    }
+    // score the cards
+    score();
+}

+ 2 - 2
Qwixx/playerCard.cpp

@@ -3,9 +3,9 @@ PlayerCard::PlayerCard(std::string name){
     // make 12 boxes for each colored row (not the white ones)
     for(int j = 0; j<white1; j++){  
         std::vector<bool> empty;
-        rows.push_back(empty);  
+        rowColors.push_back(empty);  
         for (int i = 0; i<CHECKBOXES; i++){
-            rows[j].push_back(false);
+            rowColors[j].push_back(false);
         }
     }
     savedName = name;

+ 3 - 1
Qwixx/playerCard.h

@@ -3,8 +3,10 @@
 #include <string>
 class PlayerCard {
     public: 
-    std::vector<std::vector<bool>> rows;
+    std::vector<std::vector<bool>> rowColors;
     int penaltyCount;
     PlayerCard(std::string);
     std::string savedName;
+    bool isTurnDone;
+    int score;
 };

+ 3 - 0
Qwixx/qwixx.h

@@ -1,6 +1,9 @@
 #ifndef QWIXX
     #define QWIXX
     #define CHECKBOXES 12
+    #define PENALTY_VALUE 5
+    #define MAX_PENALTY 4
+    #define MAX_LOCKOUT 2
     enum{NOT_STARTED, STARTED, FINISHED, PLAYING};
     enum {red, yellow, green, blue, white1, white2, colors};
 #endif

+ 5 - 1
Qwixx/toDo.md

@@ -9,7 +9,7 @@
 
 ## players take turns
 1.  ~~roll the dice~~ (completed 20JUL28) **add a true random number for when dice is initialized**
-1.  all players may add points to their card
+1.  all players may add points to their card 
  - sum both white dice and optionally add points to any color on card
 1.  the player whose turn it is sums one white and one colored dice and may add points to that color on card
 1.  the player whose turn it is adds penalty if no points taken
@@ -18,5 +18,9 @@
      - if any one player has 4 penalties, the game is over
      - if any 2 rows are locked out, the game is over
 
+### notes on turns
+**create end of turn in game
+**add time constraint to game later**
+
 ## the game is ended
 - list the scores and indicate the winner