为什么我的SetName只在main.cpp中返回一次名称?

时间:2017-09-14 16:00:51

标签: c++ class getter-setter

我是新来的,我一直在努力教自己C ++,而且我正在吵架。我现在已经积极学习了5个月,我的重点是游戏开发。我不寻求事业,只是为了好玩。在过去的几周里,我一直在研究一个简单的骰子控制台游戏,看看我的基础知识是否合适。直到今天我还好。我遇到了一些问题:

#pragma once
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>


class MexGame
{

public:
    MexGame();
    void setName();
    void Roll();
    void Display();
    void Reset();
    int SumScore();
    std::string GetName();
    ~MexGame();
private:
    int Dice1;
    int Dice2;
    int TotalScore;
    std::string PlayerName;
};

MexGame::MexGame() : Dice1(0), Dice2(0), TotalScore(0), PlayerName("")
{}

void MexGame::setName()
{
    std::string PlayerName;

    std::cout << "Hi what is your name?" << std::endl;

    std::cin >> PlayerName;

    std::cout << "Welcome "<< PlayerName;

}

void MexGame::Roll()
{
    int Dice1;
    int Dice2;
    int TotalScore;
    int i;
    char ThrowAgain;


    for (i = 1; i <= 3; ++i)
    {
        std::cout << "Press enter to roll: " << std::endl;
        std::cin.get();
        Dice1 = (rand() % 6) + 1;
        Dice2 = (rand() % 6) + 1;
        TotalScore = Dice1 + Dice2;

        std::cout << "You rolled " << Dice1 << " and " << Dice2 << std::endl;
        std::cout << "Do want to throw again? \n";
        std::cin >> ThrowAgain;

        //TODO FIX extra roll returning 0
        if ((ThrowAgain == 'y') || (ThrowAgain == 'Y'))
        {
            i = +i;
        }

        else
        {
            i = 3;
        }

    }

    if (TotalScore == 3)
    {
        TotalScore = TotalScore * 1000;
        std::cout << "MEX \n\n";
    }

    else if (Dice1 == Dice2)
    {
        TotalScore = (Dice1 + Dice2) * 10;
    }

    std::cout << "Your score is " << TotalScore << std::endl;

}
int MexGame::SumScore()
{
    TotalScore = Dice1 + Dice2;

    if (TotalScore == 3)
    {
        TotalScore = TotalScore * 1000;
        std::cout << "MEX \n\n";
    }

    else if (Dice1 == Dice2)
    {
        TotalScore = (Dice1 + Dice2) * 10;
    }

    return TotalScore;
}

std::string MexGame::GetName()
{
    std::cout << PlayerName;
    return std::string(PlayerName);
}

MexGame::~MexGame()
{
}

void MexGame::Display()
{
    std::cout << "Your roll was " << Dice1 << " and " << Dice2 << " .\n";
}

void MexGame::Reset()
{
    Dice1 = 0;
    Dice2 = 0;
    TotalScore = 0;
    PlayerName = "";
}

在上面的代码中,我有我的游戏类在我的main.cpp中设置玩家名称并获取它(我只会包含它的一个片段,因为它是多行的。  我两次调用GetName,第二次打印名称时第二次打印

case 2:

        cout << "Two players, allright lets begin!\n\n";

        MGamep1.setName();

        cout << **MGamep1.GetName**()<<" please press enter to roll dice: " << endl;
        cin.get();

        MGamep1.Roll();
        MGamep1.Display();
        cout << "Totaling " << MGamep1.SumScore() << "\n\n";

        MGamep2.setName();

        cout << MGamep2.GetName()<<" please press enter to roll dice: " << endl;
        cin.get();

        MGamep2.Roll();
        MGamep2.Display();
        cout << "Totaling " << MGamep2.SumScore() << "\n\n";

        //TODO fix getname returning empty
        if ((MGamep1.SumScore()) < (MGamep2.SumScore()))
        {
            cout << MGamep1.GetName()<< " lost, that is a shame, lets drink!!" << endl;
        }
        else
        {
            cout << **MGamep2.GetName**()<< " lost, come on drink up!!" << endl;
        }
        break;

我有两个例子。我希望这是足够的信息,我期待着你的回音

0 个答案:

没有答案