0

So I'm stuck on how to pull my vectors from shuffle into printDom. I want to display the playerDom only. I also don't know how I would start the game. I plan on putting the starting domino piece into its own vector, then whenever the user or computer plays a domino, it would either put it at the end or the beginning of the dominoes. I plan on using push_back and insert to place them into the played domino vector. I also plan on using empty to check the playerDom and computerDom to find out who wins. My main question is: How do I pass my vectors from shuffle into my functions? Any help would be appreciated.

#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <vector>
#include <algorithm>
//#include <cstdlib>
#include <ctime>
using namespace std;

class Domino {
public:
    vector<string> shuffle(vector<string> a, vector<string> b, vector<string> c);
    void goesFirst();
    void winner();
};  

class Player : public Domino {
public:
    vector <string> playerDom;
    void menu(int);
    void printDom(vector<string> playerDom);
    void addBoneyard();
    void head();
    void tail();
    void pass();
};

vector<string> Domino::shuffle(vector<string> a, vector<string> b, 
vector<string> c) {
    typedef map<int, string> boneyard;
    boneyard m;
    m[1] = "[0|1]";
    m[2] = "[0|2]";
    m[3] = "[0|3]";
    m[4] = "[0|4]";
    m[5] = "[0|5]";
    m[6] = "[0|6]";
    m[7] = "[1|2]";
    m[8] = "[1|3]";
    m[9] = "[1|4]";
    m[10] = "[1|5]";
    m[11] = "[1|6]";
    m[12] = "[2|3]";
    m[13] = "[2|4]";
    m[14] = "[2|5]";
    m[15] = "[2|6]";
    m[16] = "[3|4]";
    m[17] = "[3|5]";
    m[18] = "[3|6]";
    m[19] = "[4|5]";
    m[20] = "[4|6]";
    m[21] = "[5|6]";
    m[22] = "[0|0]";
    m[23] = "[1|1]";
    m[24] = "[2|2]";
    m[25] = "[3|3]"; 
    m[26] = "[4|4]"; 
    m[27] = "[5|5]";
    m[28] = "[6|6]";

    vector <string> bone;
    for (boneyard::iterator it = m.begin(); it != m.end(); it++) {
        bone.push_back(it->second);
    }

    random_shuffle(bone.begin(), bone.end());

    int n = 6, i = 0;
    vector<string>playerDom(bone.begin(), bone.begin() + n);
    for (i = 0; i < playerDom.size(); i++) {
        cout << playerDom[i];
    }
    cout << endl;
    a = playerDom;

    vector<string>compDom(bone.end() - n, bone.end());
    for (i = 0; i < compDom.size(); i++) {
        cout << compDom[i];

    }
    cout << endl;
    b = compDom;

    bone.erase(bone.begin(), bone.begin() + n);
    bone.erase(bone.end() - n, bone.end());

    for (i = 0; i < bone.size(); i++) {
        cout << bone[i];
        cout << endl;
    }
    c = bone;

    return a, b, c;
}

void Domino::goesFirst() {
    string AI;
    bool status = true;
    /*for (int i = 0; status && i < 6; i++) {
    if (AI[i] = '0,0') {
    //idk how to figure out WHICH player will go first, comparing their numbers to figure out
    //who has the HIGHER number. Ex. AI has '4,4' but player has '6,6', therefore player goes first
    }
    else if (AI[i] = '1,1') {
    //USE A BINARY SEARCH?
    }
    else if (AI[i] = '2,2') {
    //SUBSTRING USED TO FLIP DOMINOES TO MATCH NUMBERS
    }
    else if (AI[i] = '3,3') {// [ 2 | 3 ]
    //
    }
    else if (AI[i] = '4,4') {
    //
    }
    else if (AI[i] = '5,5') {
    //
    }
    else if (AI[i] = '6,6') {
    //
    }
    }
    // compare computer and player1 dominoes to find who goes first
    // whoever has the heaviest of (6,6), (5,5), (4,4), (3,3), (2,2), (1,1)
    // goes first*/
    return;
}

void Player::printDom(vector<string> a) {
    //a = shuffle(a);
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
    }
    return;
}

void Player::addBoneyard() {//BONEYARD MUST BE SHUFFLED FIRST
                            // get the first domino in the boneyard and add it
                            // if there are no more dominoes in the boneyard 
                            //then the turn passes
    return;
}

void Domino::winner() {
    int counter = 0;
    // counts elements in array to determine winner
    for (int index = 0; index <= 6; index++) {//idk what to add in to count each players pieces
        counter++;                              //it should look similar to the pass function
    }
    return;
}

void Player::head() {
    // place domino on the left, first in the array
    //ADD ELEMENT TO FRONT OF VECTOR USE INSERT
    return;
}

void Player::tail() {
    // place domino on the right, last in the array
    //ADD ELEMENT TO END OF VECTOR USE PUSH_BACK
    return;
}

void Player::pass() {
    //somehow figure out how to continue the game
    // passes turn, also passes if the boneyard is empty
    return;
}

int printMenu(int choice) {
    cout << "Domino Menu" << endl << endl;
    cout << "1. Print your dominoes" << endl;
    cout << "2. Add a domino to the head of the train" << endl;
    cout << "3. Add a domino to the tail of the train" << endl;
    cout << "4. Pick a domino from the boneyard" << endl;
    cout << "5. Pass your turn (only if bone yard is empty)" << endl;
    cout << "6. Print this menu" << endl;
    cout << "7. Quit" << endl << endl;
    cout << "Please enter a choice: ";
    cin >> choice;
    return choice;
}

void Player::menu(int choice) {
    choice = printMenu(choice);

    if (choice == 1) {
        //printDom(i);
    }
    else if (choice == 2) {
        head();
    }
    else if (choice == 3) {
        tail();
    }
    else if (choice == 4) {
        addBoneyard();
    }
    else if (choice == 5) {
        pass();
    }
    else if (choice == 6) {
        printMenu(choice);
    }
    else if (choice == 7) {
        exit(0);
    }

    return;
}

int main()
{
    Domino play;
    Player play1;
    play1.menu(0);

    return 0;
}
jgato
  • 9
  • 3

1 Answers1

0

To manipulate object-instances (in your case of type std::vector<std::string>) passed to function, simply change the function signature to accept references:

so void Player::printDom(vector<string> a) { ... } becomes void Player::printDom(vector<string>& a) { ... } (notice the &).

Another option would be to make member function of classes operate on their members. So instead of a std::vector<std::string> created (and managed) inside a class member function, convert them to class members, which in effect is just cutting and pasting the relevant declaration lines (as vector <string> playerDom; inside Player is), but I think the key to your problem is passing object-instances to function as references with &.

nada
  • 2,109
  • 2
  • 16
  • 23
  • If I pass it as a reference, I won't be able to modify the vector. I have to be able to permanently take out dominoes from the vectors so that they're not usable. – jgato Apr 20 '18 at 15:42
  • @jgato I might be misinterpreting your intentions, but passing it as a reference (not const!) will (contrary to your comment) enable you to 'take out dominoes' like so: 1. Pass vector to function as reference, 2. in function do `.erase(...)` on vector. 3. After function call- erased dominoes are still erased. I recommend reading about passing by value/reference [here](https://stackoverflow.com/questions/410593/pass-by-reference-value-in-c#410857). – nada Apr 20 '18 at 15:59
  • Thank you! I don't really understand passing as reference or pointers.. Thank you for the link and explaining it to me! – jgato Apr 20 '18 at 16:14
  • Ok, I've been trying to call the vector into the function using a reference and I think it works but I don't know how to call the function. I don't know what parameters to put in my printDom() function to see if I'm actually passing the vector properly. – jgato Apr 21 '18 at 02:07
  • @jgato Since your `printDom(...)` function does not operate on the vector it is given, the correct way would be to pass it as a *const reference*. Its signature would be `void Player::printDom(const vector& a) { ... }`. You would also have to change `cout << a[i];` to `cout << a.at(i);` [Why is that](http://en.cppreference.com/w/cpp/container/vector/at)? – nada Apr 21 '18 at 08:19