Browse Source

finished receive method for game.

wes 4 years ago
parent
commit
7742e87084
2 changed files with 42 additions and 9 deletions
  1. 39 7
      Qwixx/game.cpp
  2. 3 2
      Qwixx/toDo.md

+ 39 - 7
Qwixx/game.cpp

@@ -17,7 +17,8 @@ bool Game::addPlayer(std::string name)
 {
     if(NOT_STARTED == state)
     {
-        ScoreSheet newplayer(name);
+        // add a new player with a name they choose and a secret
+        ScoreSheet newplayer(name, dice.roll());
         players.push_back(newplayer);
         fprintf (stdout, "added: %s to the game\n", name.c_str());
         return true;
@@ -128,13 +129,44 @@ std::string Game::send()
 }
 
 // kyle's intention is to check off red 12 and blue 5
-// " 1143"
+// " kylesecret,1143"
 // emma's intention is to take a penalty
-// "1"
+// "emmasecret,1"
 // convert a user string into a turn
 struct Turn Game::receive(std::string userInput)
 {
     Turn turnInProgress;
+    std::string playerSecret, original = userInput;
+    int playerNumber = -1, pos = 0;
+    // look for the position of a comma in userInput
+    for(int i = 0;i < userInput.size();i++)
+    {
+        // a comma is found, set position and stop
+        if(',' == userInput[i])
+        {
+            pos = i; break;
+        }
+    }
+    // assuming there is a comma and it is not the first character, split userInput into two parts on the comma
+    if(0<pos)
+    {
+        playerSecret = userInput.substr(0,pos-1);
+        userInput = userInput.substr(pos);
+        int secret = stoi(playerSecret);
+        // check each player's secret against submitted secret to find player number
+        for(int i = 0;i < players.size(); i++)
+        {
+            if(secret == players[i].secretNumber)
+            {
+                playerNumber = i; break;
+            }
+        }
+    }
+    if(-1 == playerNumber)
+    {
+        fprintf(stdout, "Could not find player: %s", original);
+        return turnInProgress;
+    }
     // userinput is one, add a penalty
     if(1 == userInput.size())
     {
@@ -155,18 +187,18 @@ struct Turn Game::receive(std::string userInput)
     }
     else {fprintf(stdout, "Form an opinion: WRONG!");}
     // check the rules to make sure turn is valid
-    if(true == checkTurn(0, turnInProgress))
+    if(true == checkTurn(playerNumber, turnInProgress))
     {
         // currentTurnOlok can only have one valid turn in it at a time
-         players[0].currentTurnOlok.clear();
+         players[playerNumber].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]);
+            players[playerNumber].currentTurnOlok.addX(turnInProgress.moves[i]);
         }
         // player chose to take a penalty
-        players[0].currentTurnOlok.penaltyCount += turnInProgress.penalty;
+        players[playerNumber].currentTurnOlok.penaltyCount += turnInProgress.penalty;
     }
     // adding time to entropyPool to increase entropy in the system
     entropyPool += time(NULL);

+ 3 - 2
Qwixx/toDo.md

@@ -33,9 +33,10 @@
     - ~~(20AUG13) TODO: fix the leftToRightRule and the lockoutRule functions to return bools and change parameters so they use collectionOfMoves.~~
         1. ~~create master function for to call all rules~~ - remove addx dependencies from currentTurnOlok
     - (20AUG13) TODO: refactor dice so it is not vector of ints and add different property to access the die
-    - (20AGU13) TODO: receive should parse input and create a collectionOfMoves (20AUG18, collectionOfMoves has been refactored to Turn) from it
+    - ~~(20AGU13) TODO: receive should parse input and create a collectionOfMoves (20AUG18, collectionOfMoves has been refactored to Turn) from it~~
     - (20AUG20) TODO: only the active player can take two turns
-    - (20AUG20) TODO: refer to line 155/156 (in game.cpp) figure out functions and parameters needed for the pseudocode
+    - ~~(20AUG20) TODO: refer to line 155/156 (in game.cpp) figure out functions and parameters needed for the pseudocode~~
+    - (20AUG23) TODO: set up front end with send/receive or write a receieve type funciton to handle new players
 - 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