【问题标题】:Dominoes program. I can't figure out how to pull my vector into a void to print it out多米诺骨牌计划。我不知道如何将我的矢量拉到一个空白中以打印出来
【发布时间】:2018-09-30 16:20:16
【问题描述】:

所以我一直纠结于如何将我的向量从 shuffle 拉到 printDom 中。我只想显示 playerDom。我也不知道我将如何开始游戏。我计划将起始多米诺片放入自己的向量中,然后每当用户或计算机播放多米诺骨牌时,它会将其放在多米诺骨牌的末尾或开头。我计划使用 push_back 和 insert 将它们放入播放的多米诺骨牌向量中。我还计划使用 empty 检查 playerDom 和 computerDom 找出谁赢了。我的主要问题是:如何将我的向量从 shuffle 传递到我的函数中?任何帮助,将不胜感激。

#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;
}

【问题讨论】:

    标签: c++ string class vector void


    【解决方案1】:

    要操作传递给函数的对象实例(在您的情况下为 std::vector&lt;std::string&gt; 类型),只需更改函数签名以接受引用:

    所以void Player::printDom(vector&lt;string&gt; a) { ... } 变成了void Player::printDom(vector&lt;string&gt;&amp; a) { ... }(注意&amp;)。

    另一种选择是让类的成员函数对其成员进行操作。因此,不是在类成员函数中创建(和管理)std::vector&lt;std::string&gt;,而是将它们转换为类成员,这实际上只是剪切和粘贴相关的声明行(就像 vector &lt;string&gt; playerDom;Player 中一样),但我认为您的问题的关键是传递对象实例以作为 &amp; 的引用。

    【讨论】:

    • 如果我将它作为参考传递,我将无法修改向量。我必须能够从向量中永久取出多米诺骨牌,这样它们就无法使用了。
    • @jgato 我可能会误解您的意图,但是将其作为参考(不是 const!)传递(与您的评论相反)将使您能够像这样“取出多米诺骨牌”: 1. 传递向量作为参考,2. 在函数中对向量执行.erase(...)。 3. 函数调用擦除后,多米诺骨牌仍然被擦除。我建议阅读有关按值/引用传递here
    • 谢谢!我真的不明白作为参考或指针传递..感谢您提供链接并向我解释!
    • 好的,我一直在尝试使用引用将向量调用到函数中,我认为它可以工作,但我不知道如何调用该函数。我不知道在我的 printDom() 函数中放入什么参数来查看我是否真正正确地传递了向量。
    • @jgato 由于您的printDom(...) 函数不对给定的向量进行操作,因此正确的方法是将其作为const 引用 传递。它的签名是void Player::printDom(const vector&lt;string&gt;&amp; a) { ... }。您还必须将 cout &lt;&lt; a[i]; 更改为 cout &lt;&lt; a.at(i); Why is that?
    猜你喜欢
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    相关资源
    最近更新 更多