Repost-如何使用开关盒保持总计运行

时间:2015-10-22 00:27:32

标签: c++ class object methods switch-statement

我正在编写一个类似于猜测模具和纸牌游戏的程序。它模拟用户掷骰子,并根据他们所生成的卡片生成的卡数量相当于他们在该轮次中收到的积分数量。例如,滚动1有一张牌,表示用户捕获了一条大鱼,并获得了20分。我遇到的问题是增加积分并保持总计。该程序仅显示滚动的骰子编号,而不是该回合获得的点数。它也不会打印正确的最终总数。我已经修了好几天,这让我相信我犯了一个简单的错误。请帮忙。我的代码粘贴在下面。

#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<string>

using namespace std;

class Dice {
private:

public:
    int dicevalue;

    int rollDice() {
        srand(time(NULL));
        int dicevalue = rand() % 6 + 1;
        return dicevalue;
    }
};

class Points {

public:
    int total;
    int dicevalue;

    int Points::getpoints(int dicevalue) {
        switch (dicevalue) {

            case 1:
                cout << "\nYou rolled " << dicevalue << " \n" << endl;
                cout << "Huge Fish \n";

                total = 0 + 20;


                break;
            case 2:
                cout << "\nYou rolled " << dicevalue << " \n" << endl;
                cout << "Old shoe \n";

                total = 0 + 5;

                break;
            case 3:
                cout << "\nYou rolled " << dicevalue << " \n" << endl;
                cout << "Little Fish\n";

                total = 0 + 10;

                break;
            case 4:
                cout << "\nYou rolled " << dicevalue << "\n" << endl;
                cout << "Small prawn\n";

                total = 0 + 7;

                break;
            case 5:
                cout << "\nYou rolled " << dicevalue << "\n" << endl;
                cout << "Shark\n";

                total = 0 + 40;

                break;
            case 6:
                cout << "\nYou rolled " << dicevalue << "\n" << endl;
                cout << "Huge Catfish\n";

                total = 0 + 30;

                break;
        }

        return total;

    }

    int getPoints() {
        return dicevalue;
    }


};

class Game : public Points {


private:

    Dice dice;
    Points points;

public:

    int Game::playgame() {

        int choice, points_to_add;

        total = 0;
        points_to_add = 0;
        int dicevalue = dice.rollDice();
        total = total;
        points_to_add = total + points.getpoints(dicevalue);
        cout << "The value of total is: " << total << endl;
        //total = total + points_to_add;



        cout << "You earned:" << total << " points this round" << endl;


        //system("pause");

        return total;
    }
/*
        int getPoints() {
            return total;
        }
        */

};

int main() {
    int choice;
    int mytotal = 0, total, points_to_add = 0;
    Game game;

    do {


        cout << "Welcome to Go Fish 2.0!" << endl;
        game.playgame();

        cout << "Would you like to play again?" << endl;
        cout << "1 to play, 0 to exit: " << endl;
        cin >> choice;

    } while (choice == 1);
    mytotal = points_to_add + game.playgame();
    cout << "My Total game points are" << mytotal << endl;
    system("pause");

    return 0;

}

0 个答案:

没有答案