Browse Source

added diceTropy funciton to dice.cpp to increase entropy of dice. changed Turn turn to Turn turnInProgress - can check the turn and then now add it to a player's currentTurnOlok. scoreSheet will have a secretNumber to stop impersonation.

wes 4 years ago
parent
commit
b45c97e9f6
9 changed files with 58 additions and 23 deletions
  1. 2 1
      Qwixx/ScoreSheet.cpp
  2. 2 1
      Qwixx/ScoreSheet.h
  3. 10 2
      Qwixx/dice.cpp
  4. 6 1
      Qwixx/dice.h
  5. 27 11
      Qwixx/game.cpp
  6. 3 5
      Qwixx/game.h
  7. 2 1
      Qwixx/olok.cpp
  8. 1 1
      Qwixx/olok.h
  9. 5 0
      Qwixx/qwixx.h

+ 2 - 1
Qwixx/ScoreSheet.cpp

@@ -1,5 +1,6 @@
 #include "ScoreSheet.h"
 #include "ScoreSheet.h"
-ScoreSheet::ScoreSheet(std::string name)
+ScoreSheet::ScoreSheet(std::string name, int secret)
 {
 {
     savedName = name;
     savedName = name;
+    secretNumber = secret;
 }
 }

+ 2 - 1
Qwixx/ScoreSheet.h

@@ -9,9 +9,10 @@
         public: 
         public: 
         Olok cumulativeOlok;
         Olok cumulativeOlok;
         Olok currentTurnOlok;
         Olok currentTurnOlok;
-        ScoreSheet(std::string);
+        ScoreSheet(std::string name, int secret);
         std::string savedName;
         std::string savedName;
         bool isTurnDone;
         bool isTurnDone;
         int score;
         int score;
+        int secretNumber;
     };
     };
 #endif
 #endif

+ 10 - 2
Qwixx/dice.cpp

@@ -9,10 +9,12 @@ int Dice::roll()
     // generate 6 random numbers to assign to each die
     // generate 6 random numbers to assign to each die
     for (int i = 0; i<colors; i++)
     for (int i = 0; i<colors; i++)
     {
     {
+        // store the previous state of the pseudo-random number generator
+        previousSeed = rand();
         // store numbers generated from rand into output
         // store numbers generated from rand into output
-        dice.push_back((rand() % 6) + 1);
+        dice.push_back((previousSeed % 6) + 1);
     }    
     }    
-    return 15;
+    return previousSeed;
 }
 }
 void Dice::print() 
 void Dice::print() 
 {
 {
@@ -22,4 +24,10 @@ void Dice::print()
     fprintf (stdout, "Red Die: %d\n", dice[red]);
     fprintf (stdout, "Red Die: %d\n", dice[red]);
     fprintf (stdout, "Blue Die: %d\n", dice[blue]);
     fprintf (stdout, "Blue Die: %d\n", dice[blue]);
     fprintf (stdout, "Green Die: %d\n", dice[green]);
     fprintf (stdout, "Green Die: %d\n", dice[green]);
+}
+
+// non-destrucitvely add additional entropy to pseudo-random number generator
+void Dice::diceTropy(int entropy)
+{
+    srand(previousSeed + entropy);
 }
 }

+ 6 - 1
Qwixx/dice.h

@@ -4,11 +4,16 @@
     #include <iostream>
     #include <iostream>
     #include <time.h>
     #include <time.h>
     #include "qwixx.h"
     #include "qwixx.h"
-    class Dice {
+    class Dice 
+    {
         public:
         public:
             std::vector<int> dice;
             std::vector<int> dice;
             Dice();
             Dice();
             int roll();
             int roll();
             void print();
             void print();
+            // add entropy to the dice rolls
+            void diceTropy(int entropy);
+        private:
+            int previousSeed;
     }; 
     }; 
 #endif
 #endif

+ 27 - 11
Qwixx/game.cpp

@@ -10,7 +10,8 @@ Game::Game()
     }
     }
     // set the state of game [how to move from state to state ("game logic?")]
     // set the state of game [how to move from state to state ("game logic?")]
     state = NOT_STARTED;
     state = NOT_STARTED;
-    // dice have been initialized with random seed
+    // dice have been initialized with random seed, copy this into entropyPool
+    entropyPool = dice.roll();
 };
 };
 bool Game::addPlayer(std::string name)
 bool Game::addPlayer(std::string name)
 {
 {
@@ -94,7 +95,9 @@ void Game::round()
     score();
     score();
     // go to next player's turn
     // go to next player's turn
     activePlayer++;
     activePlayer++;
-    activePlayer %= players.size(); 
+    activePlayer %= players.size();
+    // adding in entropy from human players of THIS round to dice so they are slightly more random
+    dice.diceTropy(entropyPool);
 }
 }
 
 
 // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
 // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
@@ -131,29 +134,42 @@ std::string Game::send()
 // convert a user string into a turn
 // convert a user string into a turn
 struct Turn Game::receive(std::string userInput)
 struct Turn Game::receive(std::string userInput)
 {
 {
-    players[0].currentTurnOlok.clear();
-    Turn turn;
+    Turn turnInProgress;
     // userinput is one, add a penalty
     // userinput is one, add a penalty
     if(1 == userInput.size())
     if(1 == userInput.size())
     {
     {
-        turn.penalty = 1;
+        turnInProgress.penalty = 1;
     }
     }
     // userinput size is two, add one moves
     // userinput size is two, add one moves
     else if(2 == userInput.size())
     else if(2 == userInput.size())
     {
     {
-        turn.moves[0] = translateToMove(userInput);
-        turn.numberOfMoves = 1;
+        turnInProgress.moves[0] = translateToMove(userInput);
+        turnInProgress.numberOfMoves = 1;
     }
     }
     // userinput size is four, add two moves
     // userinput size is four, add two moves
     else if(4 == userInput.size())
     else if(4 == userInput.size())
     {
     {
-        turn.moves[0] = translateToMove(userInput.substr(0,2));
-        turn.moves[1] = translateToMove(userInput.substr(2,2));
-        turn.numberOfMoves = 2;
+        turnInProgress.moves[0] = translateToMove(userInput.substr(0,2));
+        turnInProgress.moves[1] = translateToMove(userInput.substr(2,2));
+        turnInProgress.numberOfMoves = 2;
     }
     }
     else {fprintf(stdout, "Form an opinion: WRONG!");}
     else {fprintf(stdout, "Form an opinion: WRONG!");}
     // check the rules to make sure turn is valid
     // check the rules to make sure turn is valid
-    // add turn to currentTurnOlok if the turn is valid
+    if(true == checkTurn(0, turnInProgress))
+    {
+        // currentTurnOlok can only have one valid turn in it at a time
+         players[0].currentTurnOlok.clear();
+        // check the moves in turn
+        for(int i = 0;i < turnInProgress.numberOfMoves;i++)
+        {
+            // add turn to currentTurnOlok
+            players[0].currentTurnOlok.addX(turnInProgress.moves[i]);
+        }
+        // player chose to take a penalty
+        players[0].currentTurnOlok.penaltyCount += turnInProgress.penalty;
+    }
+    // adding time to entropyPool to increase entropy in the system
+    entropyPool += time(NULL);
 }
 }
 
 
 bool Game::isMoveRepresentedInDie(int player, Turn turn)
 bool Game::isMoveRepresentedInDie(int player, Turn turn)

+ 3 - 5
Qwixx/game.h

@@ -5,11 +5,7 @@
     #include "ScoreSheet.h"
     #include "ScoreSheet.h"
     #include <windows.h>
     #include <windows.h>
     #include <array>
     #include <array>
-    struct Move 
-    {
-        int index;
-        int color;
-    };
+    #include <time.h>
     struct Turn 
     struct Turn 
     {
     {
         std::array<Move, 2> moves;
         std::array<Move, 2> moves;
@@ -45,5 +41,7 @@
          *  2. if you give this set of moves to an AI, it could pick a move
          *  2. if you give this set of moves to an AI, it could pick a move
          *  3. could give hints to an end user about what move they can take
          *  3. could give hints to an end user about what move they can take
          */
          */
+        private:
+        int entropyPool;
     };
     };
 #endif
 #endif

+ 2 - 1
Qwixx/olok.cpp

@@ -40,8 +40,9 @@ int Olok::getXCount(int color)
 }
 }
 
 
 // add checked off box to a specific index on a color
 // add checked off box to a specific index on a color
-void Olok::addX(int color, int index)
+void Olok::addX(struct Move move)
 {
 {
+    int color = move.color, index = move.index;
     for(int i = 0; i < grid.size(); i++)
     for(int i = 0; i < grid.size(); i++)
     {
     {
         if(color != grid[i].color){continue;}
         if(color != grid[i].color){continue;}

+ 1 - 1
Qwixx/olok.h

@@ -21,7 +21,7 @@
         void clear();
         void clear();
         int penaltyCount;
         int penaltyCount;
         int getXCount(int color);
         int getXCount(int color);
-        void addX(int color, int index);
+        void addX(struct Move move);
         int getLastIndex(int color);
         int getLastIndex(int color);
         void addOlok(Olok o);
         void addOlok(Olok o);
         std::string toString(void);
         std::string toString(void);

+ 5 - 0
Qwixx/qwixx.h

@@ -15,4 +15,9 @@
     #define DICE_SUM_MAX 12
     #define DICE_SUM_MAX 12
     #define DICE_SUM_MIN 2
     #define DICE_SUM_MIN 2
     #define LOCKOUT_QUALIFIER 5
     #define LOCKOUT_QUALIFIER 5
+    struct Move 
+    {
+        int index;
+        int color;
+    };
 #endif
 #endif