Explorar el Código

refactored the LockoutRule method. changed the method Turn into Round so we could then we could rename CollectionOfMoves to Turn.

wes hace 4 años
padre
commit
f6e60d5524
Se han modificado 2 ficheros con 26 adiciones y 33 borrados
  1. 20 27
      Qwixx/game.cpp
  2. 6 6
      Qwixx/game.h

+ 20 - 27
Qwixx/game.cpp

@@ -65,7 +65,7 @@ void Game::score()
         state = FINISHED;
     }
 }
-void Game::turn(ScoreSheet activePlayer)
+void Game::round()
 {
     // start all players' turns
     for(int i = 0; i <players.size();i++)
@@ -185,26 +185,26 @@ void Game::receive(std::string userInput)
  * // 
  */
 
-bool Game::isMoveRepresentedInDie(int player, CollectionOfMoves collectionOfMoves)
+bool Game::isMoveRepresentedInDie(int player, Turn turn)
 {
     // is this the non-active player
     if(player != activePlayer) 
     {
         // if the move equals the sum of the two white die return true, if not return false
-        return collectionOfMoves.white.index == dice.dice[white1] + dice.dice[white2];
+        return turn.moves[0].index == dice.dice[white1] + dice.dice[white2];
     }
     // otherwise this is the active player
     else
     {
         // if one move
-        if(1 == collectionOfMoves.numberOfMoves)
+        if(1 == turn.numberOfMoves)
         { 
             // check sum of white die
-            if(collectionOfMoves.white.index != (dice.dice[white1] + dice.dice[white2]))
+            if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
             {
-                int c = collectionOfMoves.color.color;
+                int c = turn.moves[1].color;
                 // if any white die plus the colored die equals the move
-                if(collectionOfMoves.color.index == (dice.dice[white1] + dice.dice[c]) || collectionOfMoves.color.index == (dice.dice[white2] + dice.dice[c]))
+                if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
                 {
                     return true;
                 }
@@ -214,20 +214,20 @@ bool Game::isMoveRepresentedInDie(int player, CollectionOfMoves collectionOfMove
             // otherwise the white sum die was a move
             else {return true;}
         }
-        // if two move 
-        else if(2 == collectionOfMoves.numberOfMoves)
+        // if two moves
+        else if(2 == turn.numberOfMoves)
         {            
             // check first input with white die sum
-            if(collectionOfMoves.white.index != (dice.dice[white1] + dice.dice[white2]))
+            if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
             {
                 // white die sum was not a move
                 return false;
             }
             else
             {
-                    int c = collectionOfMoves.color.color;
+                    int c = turn.moves[1].color;
                 // check to see if color die + either white die sum equals a move
-                if(collectionOfMoves.color.index == (dice.dice[white1] + dice.dice[c]) || collectionOfMoves.color.index == (dice.dice[white2] + dice.dice[c]))
+                if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
                 {
                     return true;
                 }
@@ -238,26 +238,19 @@ bool Game::isMoveRepresentedInDie(int player, CollectionOfMoves collectionOfMove
     }      
 };
 
-
-
-
-
-
-
-
-
-
-void Game::lockOutRule(int player, int color, int index)
+bool Game::lockOutRule(int player, Turn turn)
 {
- // player can add an x to olok in last column if there are 5 x
-    if(lastColumnIndex == index)
+    // if the player has selected the last column, check it
+    for(int i = 0; i<turn.moves.size(); i++)
     {
-        if(LOCKOUT_QUALIFIER >= players[player].cumulativeOlok.getXCount(color))
+        // player can take their move in the last column if there are 5 recorded moves in that color
+        if(LOCKOUT_QUALIFIER > players[player].cumulativeOlok.getXCount(turn.moves[i].color)
         {
-            players[player].currentTurnOlok.addX(color, index);
+            fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);
+            return false;
         }
-        else{fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);}
     }
+    return true;
 }
 
 void Game::leftToRightRule(int player, int color, int index)

+ 6 - 6
Qwixx/game.h

@@ -4,15 +4,15 @@
     #include "dice.h"
     #include "ScoreSheet.h"
     #include <windows.h>
+    #include <array>
     struct Move 
     {
         int index;
         int color;
     };
-    struct CollectionOfMoves 
+    struct Turn 
     {
-        Move white;
-        Move color;
+        std::array<Move, 2> moves;
         // this can be zero, one or two
         int numberOfMoves = 0; 
         bool penalty;
@@ -26,7 +26,7 @@
         Dice dice;
         int activePlayer;
         Game();
-        void turn(ScoreSheet activePlayer);
+        void round();
         void score();
         std::string send();
         void receive(std::string userInput);
@@ -35,9 +35,9 @@
         // 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
-        void lockOutRule(int player, int color, int index);
+        bool Game::lockOutRule(int player, Turn turn);
         void leftToRightRule(int player, int color, int index);
-        bool Game::isMoveRepresentedInDie(int player, CollectionOfMoves collectionOfMoves);
+        bool Game::isMoveRepresentedInDie(int player, Turn turn);
         /*  TODO: generate a set of all possible moves a player can take given a: ScoreSheet & diceRoll
          *  1. one good reason to do this would be to check if a player can make a given move
          *  2. if you give this set of moves to an AI, it could pick a move