Sfoglia il codice sorgente

fixed olok so the colors are accessible as colors instead of an index. added isColor function. on game.cpp added active player. working on pseudocode for turn game code.

wes 4 anni fa
parent
commit
575fc6a0ba
8 ha cambiato i file con 177 aggiunte e 29 eliminazioni
  1. 47 0
      Qwixx/.vscode/settings.json
  2. 14 10
      Qwixx/dice.cpp
  3. 57 8
      Qwixx/game.cpp
  4. 6 0
      Qwixx/game.h
  5. 44 8
      Qwixx/olok.cpp
  6. 2 0
      Qwixx/olok.h
  7. 5 2
      Qwixx/qwixx.h
  8. 2 1
      Qwixx/toDo.md

+ 47 - 0
Qwixx/.vscode/settings.json

@@ -0,0 +1,47 @@
+{
+    "files.associations": {
+        "array": "cpp",
+        "atomic": "cpp",
+        "*.tcc": "cpp",
+        "cctype": "cpp",
+        "clocale": "cpp",
+        "cmath": "cpp",
+        "cstdarg": "cpp",
+        "cstddef": "cpp",
+        "cstdint": "cpp",
+        "cstdio": "cpp",
+        "cstdlib": "cpp",
+        "cwchar": "cpp",
+        "cwctype": "cpp",
+        "deque": "cpp",
+        "unordered_map": "cpp",
+        "vector": "cpp",
+        "exception": "cpp",
+        "algorithm": "cpp",
+        "functional": "cpp",
+        "iterator": "cpp",
+        "memory": "cpp",
+        "memory_resource": "cpp",
+        "numeric": "cpp",
+        "optional": "cpp",
+        "random": "cpp",
+        "string": "cpp",
+        "string_view": "cpp",
+        "system_error": "cpp",
+        "tuple": "cpp",
+        "type_traits": "cpp",
+        "utility": "cpp",
+        "fstream": "cpp",
+        "initializer_list": "cpp",
+        "iosfwd": "cpp",
+        "iostream": "cpp",
+        "istream": "cpp",
+        "limits": "cpp",
+        "new": "cpp",
+        "ostream": "cpp",
+        "sstream": "cpp",
+        "stdexcept": "cpp",
+        "streambuf": "cpp",
+        "typeinfo": "cpp"
+    }
+}

+ 14 - 10
Qwixx/dice.cpp

@@ -1,21 +1,25 @@
 #include "dice.h"
 #include "dice.h"
-Dice::Dice(){
+Dice::Dice()
+{
     srand(time(NULL));
     srand(time(NULL));
     roll();
     roll();
 }
 }
-int Dice::roll() {
+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 numbers generated from rand into output
         // store numbers generated from rand into output
         dice.push_back((rand() % 6) + 1);
         dice.push_back((rand() % 6) + 1);
     }    
     }    
     return 15;
     return 15;
 }
 }
-void Dice::print() {
-    fprintf (stdout, "White1 Dice: %d\n", dice[white1]);
-    fprintf (stdout, "White2 Dice: %d\n", dice[white2]);
-    fprintf (stdout, "Yellow Dice: %d\n", dice[yellow]);
-    fprintf (stdout, "Red Dice: %d\n", dice[red]);
-    fprintf (stdout, "Blue Dice: %d\n", dice[blue]);
-    fprintf (stdout, "Green Dice: %d\n", dice[green]);
+void Dice::print() 
+{
+    fprintf (stdout, "White1 Die: %d\n", dice[white1]);
+    fprintf (stdout, "White2 Die: %d\n", dice[white2]);
+    fprintf (stdout, "Yellow Die: %d\n", dice[yellow]);
+    fprintf (stdout, "Red Die: %d\n", dice[red]);
+    fprintf (stdout, "Blue Die: %d\n", dice[blue]);
+    fprintf (stdout, "Green Die: %d\n", dice[green]);
 }
 }

+ 57 - 8
Qwixx/game.cpp

@@ -1,23 +1,28 @@
 #include "game.h"
 #include "game.h"
-Game::Game() {
+Game::Game() 
+{
     // initialize the number of columns that each "color" will contain
     // initialize the number of columns that each "color" will contain
     lastColumnIndex = CHECKBOXES - 1;
     lastColumnIndex = CHECKBOXES - 1;
     // set up lockOut
     // set up lockOut
-    for(int i = 0; i<white1;i++){
+    for(int i = 0; i<white1;i++)
+    {
         lockOut.push_back(false);
         lockOut.push_back(false);
     }
     }
     // 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
 };
 };
-bool Game::addPlayer(std::string name){
-    if(NOT_STARTED == state){
+bool Game::addPlayer(std::string name)
+{
+    if(NOT_STARTED == state)
+    {
         ScoreSheet newplayer(name);
         ScoreSheet newplayer(name);
         players.push_back(newplayer);
         players.push_back(newplayer);
         fprintf (stdout, "added: %s to the game\n", name.c_str());
         fprintf (stdout, "added: %s to the game\n", name.c_str());
         return true;
         return true;
     }
     }
-    else{
+    else
+    {
         fprintf(stdout, "player %s was not added to the game\n", name.c_str());
         fprintf(stdout, "player %s was not added to the game\n", name.c_str());
         return false;
         return false;
     }
     }
@@ -87,6 +92,9 @@ void Game::turn(ScoreSheet activePlayer)
     }
     }
     // score the cards
     // score the cards
     score();
     score();
+    // go to next player's turn
+    activePlayer++;
+    activePlayer %= players.size(); 
 }
 }
 
 
 // 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
@@ -125,6 +133,14 @@ void Game::receive(std::string userInput)
     players[0].currentTurnOlok.clear();
     players[0].currentTurnOlok.clear();
     // penalties are marked to card if user string = 1
     // penalties are marked to card if user string = 1
     // did the user send a number?
     // did the user send a number?
+    // ## Range Check to go into Receive ##
+    #ifdef WORKING_ON_RANGE_CHECK
+    // if index is too high or too low, throw an error
+    if(CHECKBOXES > index || 0 < index){fprintf(stdout,"Player[%d] %s index is too high or too low X\n", player, players[player].savedName);}
+    // if color is not valid throw an error
+    players[0].cumulativeOlok.isColor(red);
+    if(color){fprintf(stdout,"Player[%d] %s invalid color selected X\n", player, players[player].savedName);}
+    #endif
     // if a user did not send a number no changes take place
     // if a user did not send a number no changes take place
     // take the userinput (string) and parse it into parts
     // take the userinput (string) and parse it into parts
     // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
     // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
@@ -132,19 +148,52 @@ void Game::receive(std::string userInput)
     // convert userInput into number
     // convert userInput into number
     // user number to change the boolean on corresponding spot on ScoreSheet
     // user number to change the boolean on corresponding spot on ScoreSheet
 }
 }
+
+/* add a checkbox to a specified players scoresheet user specifies player's: XXXX, the color, and index of the checkbox on the colored row
+ * dont know what XXXX is yet - but it will be used to find correct player
+ * 
+ * 
+ * 
+ * the white die and color combo must match the corresponding RowColor and index
+ * 
+ */
+
+/* 
+ * all players can do white die combination, active player can do a colored + white die combination. 
+ * 
+ * ## Things we need to know ##
+ * sum of both white die
+ * 8 sums of each of the white die and all of the colored die
+ * who is the active player for this turn
+ * 
+ * ## How to do ##
+ * on a given turn, sum the white die and sum each of the white die and all of the colored die
+ * check to see if each players' requested move is valid
+ *      does the player's requested move match the sum of the white die "satisfies all players can do white die combination"
+ *      if the active player, check both white die and the color die they requested for move
+ *      TODO: deal with subtley of active player not being able to re-use dice
+ *      record if active player took at least a white dice move, a colored die move, or both
+ * if it is valid, add it to player card
+ * if it is not valid, return an error to player
+ */
 void Game::addX(int player, int color, int index)
 void Game::addX(int player, int color, int index)
 {
 {
-    // TODO: check that index and color match rolled die
+    /* TODO: check that index and color match rolled die
+    * ## Rule Check ##
+    * if index is not the sum of two dice throw an error
+    * if index is a sum AND color is correct allow it
+    */
+    
     // player can add an x to olok in last column if there are 5 x
     // player can add an x to olok in last column if there are 5 x
     if(lastColumnIndex == index)
     if(lastColumnIndex == index)
     {
     {
-        if(5 >= players[player].cumulativeOlok.getXCount(color))
+        if(LOCKOUT_QUALIFIER >= players[player].cumulativeOlok.getXCount(color))
         {
         {
             players[player].currentTurnOlok.addX(color, index);
             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);}
         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
+    // player must add an x to olok from left to right
     if(players[player].cumulativeOlok.getLastIndex(color) > index)
     if(players[player].cumulativeOlok.getLastIndex(color) > index)
     {
     {
         fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
         fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);

+ 6 - 0
Qwixx/game.h

@@ -11,6 +11,7 @@
         int lastColumnIndex;
         int lastColumnIndex;
         int state; 
         int state; 
         Dice dice;
         Dice dice;
+        int activePlayer;
         Game();
         Game();
         void turn(ScoreSheet activePlayer);
         void turn(ScoreSheet activePlayer);
         void score();
         void score();
@@ -21,5 +22,10 @@
         // rule for lockout to populate a locked out row so score function can check for locked rows
         // 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);
         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
         // 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
+        /*  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
+         *  3. could give hints to an end user about what move they can take
+         */
     };
     };
 #endif
 #endif

+ 44 - 8
Qwixx/olok.cpp

@@ -9,9 +9,16 @@ void Olok::clear()
     grid.clear();
     grid.clear();
     // make 11 boxes for each colored row (not the white ones)
     // make 11 boxes for each colored row (not the white ones)
     for(int j = 0; j<white1; j++)
     for(int j = 0; j<white1; j++)
-    {  
-        RowColor empty;
-        grid.push_back(empty);  
+    {
+        RowColor tempRow;
+        tempRow.color = j;
+        tempRow.direction = j < (white1/2) ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
+        // make 11 numbers for each row based on direction
+        for (int i = 0; i<CHECKBOXES; i++)
+        {
+            tempRow.number.push_back(RIGHT_TO_LEFT == tempRow.direction ? (DICE_SUM_MIN+i) : (DICE_SUM_MAX+i));
+        }
+        grid.push_back(tempRow);  
         for (int i = 0; i<CHECKBOXES; i++)
         for (int i = 0; i<CHECKBOXES; i++)
         {
         {
             grid[j].row.push_back(false);
             grid[j].row.push_back(false);
@@ -20,21 +27,38 @@ void Olok::clear()
     // clear penalty count 
     // clear penalty count 
     penaltyCount = 0;
     penaltyCount = 0;
 }
 }
+
 // get checked off box count for a specific color
 // get checked off box count for a specific color
 int Olok::getXCount(int color)
 int Olok::getXCount(int color)
 {
 {
-   return grid[color].xCount;
+    for(int i = 0; i < grid.size(); i++)
+    {
+        if(color != grid[i].color){continue;}
+        return grid[i].xCount;
+    }
+    return 0;
 }
 }
+
 // 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(int color, int index)
 {
 {
-    grid[color].row[index] = true;
-    grid[color].lastIndex = index;
+    for(int i = 0; i < grid.size(); i++)
+    {
+        if(color != grid[i].color){continue;}
+        grid[i].row[index] = true;
+        grid[i].lastIndex = index;
+    }
+
 }
 }
 // check for last value of index
 // check for last value of index
 int Olok::getLastIndex(int color)
 int Olok::getLastIndex(int color)
 {
 {
-    return grid[color].lastIndex;
+    for(int i = 0; i < grid.size(); i++)
+    {
+        if(color != grid[i].color){continue;}
+        return grid[i].lastIndex;
+    }
+    return CHECKBOXES;
 }
 }
 
 
 // add any Xs from input Olok into this Olok
 // add any Xs from input Olok into this Olok
@@ -58,7 +82,8 @@ void Olok::addOlok(Olok o)
     penaltyCount += o.penaltyCount;
     penaltyCount += o.penaltyCount;
 }
 }
 
 
-std::string Olok::toString(void) {
+std::string Olok::toString(void) 
+{
     std::string output;
     std::string output;
     // check each row on the grid on the scoresheet
     // check each row on the grid on the scoresheet
     for(int j = 0; j < grid.size(); j++)
     for(int j = 0; j < grid.size(); j++)
@@ -73,4 +98,15 @@ std::string Olok::toString(void) {
     // add penalty count to output
     // add penalty count to output
     output.push_back(penaltyCount + '0');
     output.push_back(penaltyCount + '0');
     return output;
     return output;
+}
+
+// check to see if color matches one of the RowColor(s) on grid
+boolean Olok::isColor(int color)
+{
+    for(int i = 0; i < grid.size(); i++)
+    {
+        if(color != grid[i].color){continue;}
+        return true;
+    }
+    return false;
 }
 }

+ 2 - 0
Qwixx/olok.h

@@ -11,6 +11,7 @@
         int xCount;
         int xCount;
         int lastIndex;
         int lastIndex;
         std::vector<bool> row;
         std::vector<bool> row;
+        std::vector<int> number;
     };
     };
     class Olok
     class Olok
     {
     {
@@ -24,5 +25,6 @@
         int getLastIndex(int color);
         int getLastIndex(int color);
         void addOlok(Olok o);
         void addOlok(Olok o);
         std::string toString(void);
         std::string toString(void);
+        boolean isColor(int color);
     };
     };
 #endif
 #endif

+ 5 - 2
Qwixx/qwixx.h

@@ -1,14 +1,17 @@
 #ifndef QWIXX
 #ifndef QWIXX
     #define QWIXX
     #define QWIXX
     enum{NOT_STARTED, STARTED, FINISHED, PLAYING};
     enum{NOT_STARTED, STARTED, FINISHED, PLAYING};
-    enum{left, right};
+    enum{LEFT_TO_RIGHT, RIGHT_TO_LEFT};
     enum{penalty, everyone, active, selectionSize};
     enum{penalty, everyone, active, selectionSize};
     // to add a color in the future, place it before white1 below
     // to add a color in the future, place it before white1 below
-    enum {red, yellow, green, blue, white1, white2, colors};
+    enum {red = 9, yellow = 19, green = 0, blue = 55, white1 = 4, white2, colors};
     // checkboxes represents the count of the sample space of the sum of rolling 2 dice
     // checkboxes represents the count of the sample space of the sum of rolling 2 dice
     #define CHECKBOXES 11
     #define CHECKBOXES 11
     #define CHECKBOX_COUNT (CHECKBOXES*white1)
     #define CHECKBOX_COUNT (CHECKBOXES*white1)
     #define PENALTY_VALUE 5
     #define PENALTY_VALUE 5
     #define MAX_PENALTY 4
     #define MAX_PENALTY 4
     #define MAX_LOCKOUT 2
     #define MAX_LOCKOUT 2
+    #define DICE_SUM_MAX 12
+    #define DICE_SUM_MIN 2
+    #define LOCKOUT_QUALIFIER 5
 #endif
 #endif

+ 2 - 1
Qwixx/toDo.md

@@ -28,7 +28,8 @@
 - (20AUG04) Started working on send function, come back and finish it to denote a 4x12 grid to save checkboxes per row
 - (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`~~
 - ~~(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)
     - 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
+    - (20AUG09) TODO: check that index and color, match rolled die (this will help finish addx) then continue work on send/receive
+    - (20AUG11) TODO: write psuedo code for line 170-175 (174 specifically) in game.cpp
 - 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
 - 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
 - what comes in from players: newest item checked off on row&column (maybe 2) or penalty