Browse Source

Refactored some game code by moving parts into Olok, updated how olok stores internal data

wes 4 years ago
parent
commit
aaab62e18a
6 changed files with 103 additions and 44 deletions
  1. 28 38
      Qwixx/game.cpp
  2. 2 1
      Qwixx/game.h
  3. 55 2
      Qwixx/olok.cpp
  4. 15 1
      Qwixx/olok.h
  5. 1 1
      Qwixx/qwixx.h
  6. 2 1
      Qwixx/toDo.md

+ 28 - 38
Qwixx/game.cpp

@@ -1,5 +1,7 @@
 #include "game.h"
 Game::Game() {
+    // initialize the number of columns that each "color" will contain
+    lastColumnIndex = CHECKBOXES - 1;
     // set up lockOut
     for(int i = 0; i<white1;i++){
         lockOut.push_back(false);
@@ -31,19 +33,13 @@ void Game::score()
         players[i].score = 0;
         // calculate penalty amount
         players[i].score -= players[i].cumulativeOlok.penaltyCount*PENALTY_VALUE;
-        // check each color(row) on ScoreSheet
+        // count the Xs of each "color" and give me a score
         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].cumulativeOlok.grid[j][k])
-                {
-                    count++;
-                }
-            }
-            players[i].score += pointsGuide[count];
+            // determine if player qualifies for the lockout point (this is a rule)
+            int lockoutPoint = players[i].cumulativeOlok.getLastIndex(j) == lastColumnIndex;
+            // use pointsGuide to get the score for this user's "color" row
+            players[i].score += pointsGuide[players[i].cumulativeOlok.getXCount(j) + lockoutPoint];
         }
     }
     // **CHECK IF GAME IS DONE**
@@ -87,21 +83,7 @@ void Game::turn(ScoreSheet activePlayer)
     // save data from currentTurnOlok into cumulativeOlok
     for(int i = 0; i <players.size();i++)
     {
-        // find changes in currentTurnOlok and transfer to cumulativeOlok
-        for(int j = 0;j < players[i].currentTurnOlok.grid.size();j++)
-        {
-            for(int k = 0;k < players[i].currentTurnOlok.grid[j].size();k++)
-            {
-                // check for true values in currentTurnOlok's grid
-                if(true == players[i].currentTurnOlok.grid[j][k])
-                {
-                    // transfer true values from currentTurnOlok's grid to cumulativeOlok's grid
-                    players[i].cumulativeOlok.grid[j][k] = true;
-                }
-            }
-        }
-        // player selects to add penalty, increase the penalty count
-         players[i].cumulativeOlok.penaltyCount += players[i].currentTurnOlok.penaltyCount;
+        players[i].cumulativeOlok.addOlok(players[i].currentTurnOlok);
     }
     // score the cards
     score();
@@ -120,18 +102,7 @@ std::string Game::send()
     // look at each scoresheet
     for(int i = 0; i < players.size(); i++)
     {
-        // check each row on the grid on the scoresheet
-        for(int j = 0; j < players[i].cumulativeOlok.grid.size(); j++)
-        {
-            // check each box on the row and then store output
-            for(int k = 0; k < players[i].cumulativeOlok.grid[j].size();k++)
-            {
-                // look at checkbox value, add it to output
-                output.push_back(players[i].cumulativeOlok.grid[j][k] ? 'T' : 'F');
-            }
-        }
-        // add penalty count to output
-        output.push_back(players[i].cumulativeOlok.penaltyCount + '0');
+        output.append(players[i].cumulativeOlok.toString());
     }
     // add locked off row to output
     for(int i = 0; i < lockOut.size();i++)
@@ -160,4 +131,23 @@ void Game::receive(std::string userInput)
 
     // convert userInput into number
     // user number to change the boolean on corresponding spot on ScoreSheet
+}
+void Game::addX(int player, int color, int index)
+{
+    // TODO: check that index and color match rolled die
+    // player can add an x to olok in last column if there are 5 x
+    if(lastColumnIndex == index)
+    {
+        if(5 >= players[player].cumulativeOlok.getXCount(color))
+        {
+            players[player].currentTurnOlok.addX(color, index);
+        }
+        else{fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);}
+    }
+    // player can add an x to olok from left to right
+    if(players[player].cumulativeOlok.getLastIndex(color) > index)
+    {
+        fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
+    }
+    else{players[player].currentTurnOlok.addX(color, index);}
 }

+ 2 - 1
Qwixx/game.h

@@ -8,6 +8,7 @@
         public:
         std::vector<ScoreSheet> players;
         std::vector<bool> lockOut;
+        int lastColumnIndex;
         int state; 
         Dice dice;
         Game();
@@ -18,7 +19,7 @@
         // rules as methods of game
         bool addPlayer(std::string name);
         // rule for lockout to populate a locked out row so score function can check for locked rows
-
+        void addX(int player, int color, int index);
         // rule for being able to lockout a row, check for last box marked off and set condition where the next box has to be higher than that previous box check
     };
 #endif

+ 55 - 2
Qwixx/olok.cpp

@@ -10,13 +10,66 @@ void Olok::clear()
     // make 11 boxes for each colored row (not the white ones)
     for(int j = 0; j<white1; j++)
     {  
-        std::vector<bool> empty;
+        RowColor empty;
         grid.push_back(empty);  
         for (int i = 0; i<CHECKBOXES; i++)
         {
-            grid[j].push_back(false);
+            grid[j].row.push_back(false);
         }
     }
     // clear penalty count 
     penaltyCount = 0;
+}
+// get checked off box count for a specific color
+int Olok::getXCount(int color)
+{
+   return grid[color].xCount;
+}
+// add checked off box to a specific index on a color
+void Olok::addX(int color, int index)
+{
+    grid[color].row[index] = true;
+    grid[color].lastIndex = index;
+}
+// check for last value of index
+int Olok::getLastIndex(int color)
+{
+    return grid[color].lastIndex;
+}
+
+// add any Xs from input Olok into this Olok
+void Olok::addOlok(Olok o)
+{
+    // find changes in currentTurnOlok and transfer to cumulativeOlok
+    for(int j = 0;j <o.grid.size();j++)
+    {
+        // loop over all Xs in a color
+        for(int k = 0;k <o.grid[j].row.size();k++)
+        {
+            // check for true values in currentTurnOlok's grid
+            if(true ==o.grid[j].row[k])
+            {
+                // transfer true values from currentTurnOlok's grid to cumulativeOlok's grid
+                grid[j].row[k] = true;
+            }
+        }
+    }
+    // player selects to add penalty, increase the penalty count
+    penaltyCount += o.penaltyCount;
+}
+
+std::string Olok::toString(void) {
+    std::string output;
+    // check each row on the grid on the scoresheet
+    for(int j = 0; j < grid.size(); j++)
+    {
+        // check each box on the row and then store output
+        for(int k = 0; k < grid[j].row.size();k++)
+        {
+            // look at checkbox value, add it to output
+            output.push_back(grid[j].row[k] ? 'T' : 'F');
+        }
+    }
+    // add penalty count to output
+    output.push_back(penaltyCount + '0');
 }

+ 15 - 1
Qwixx/olok.h

@@ -3,12 +3,26 @@
     #include <vector>
     #include "qwixx.h"
     #include <string>
+    struct RowColor
+    {
+        bool direction;
+        std::string name;
+        int color;
+        int xCount;
+        int lastIndex;
+        std::vector<bool> row;
+    };
     class Olok
     {
         public:
-        std::vector<std::vector<bool>> grid;
+        std::vector<RowColor> grid;
         Olok();
         void clear();
         int penaltyCount;
+        int getXCount(int color);
+        void addX(int color, int index);
+        int getLastIndex(int color);
+        void addOlok(Olok o);
+        std::string toString(void);
     };
 #endif

+ 1 - 1
Qwixx/qwixx.h

@@ -1,7 +1,7 @@
 #ifndef QWIXX
     #define QWIXX
     enum{NOT_STARTED, STARTED, FINISHED, PLAYING};
-    enum{row, column};
+    enum{left, right};
     enum{penalty, everyone, active, selectionSize};
     // to add a color in the future, place it before white1 below
     enum {red, yellow, green, blue, white1, white2, colors};

+ 2 - 1
Qwixx/toDo.md

@@ -2,7 +2,7 @@
 ## the game is set up
 - each player gets a card
  - ~~called playerCard for player to mark their points~~
- - has 4 different colored rows (direction rule, add lockout, label boxes, rules for boxes)
+ - ~~has 4 different colored rows (direction rule, add lockout, label boxes, rules for boxes)~~
  - ~~rows are numbered 2-12 with boxes for scoring~~
 - ~~there are six six-sided dice~~
  - ~~two white die, a blue, a red, a yellow and green die.~~
@@ -28,6 +28,7 @@
 - (20AUG04) Started working on send function, come back and finish it to denote a 4x12 grid to save checkboxes per row
 - ~~(20AUG06) come back to `// add selection to players' cards` (kyle-plain it) to continue with `send/receive`~~
     - before writing received create the rules that will affect receive function (e.g. which checkboxes are allowed to be clicked)
+    - (20AUG09) TODO: check that index and color match rolled die (this will help finish addx) then continue work on send/receive
 - current make process involves compiling all object files from dice, game, playercard to create main.exe which has to be manually linked to these objects. update makefile to support object file code
 - what comes in from players: newest item checked off on row&column (maybe 2) or penalty