保持分数并在文字游戏中宣布获胜者协助

时间:2016-04-28 17:39:56

标签: c++

我已经完成了大部分工作,但是只有两件事情仍然让我感到兴奋,并且朝着正确的方向努力推动可能会非常有帮助。我应该创造一个与球员和轮次的文字游戏,然后最后它应该总计得分并宣布胜利者。除了计算得分并宣布获胜者之外,我已经能够完成所有工作。我已经能够让他们显示一个人每轮获得多少积分,但我似乎无法将总数延续到每一轮。我尝试了各种各样的东西,但我似乎无法得到它。任何朝着正确方向前进的帮助都将不胜感激!

编辑:我还尝试添加:

total_points[player_counter][round_counter] = input_word.length();  

到我的代码,但是它给了我一个“表达式必须有一个指向对象的指针类型”,只有player_counter,所以我不知道在哪里添加内容以使其消失。

完整代码:

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

int round_number;
int scores;
int players;
int player_scores;
string input_word;
int player_counter = 0;
int total_points = 0;
int winner = 0;
int win;

int main()
{
cout << "Welcome to the Word Game!" << endl << endl;

cout << "How many players will be participating?" << endl;
cin >> players;

cout << "\nHow many rounds will be played?" << endl;
cin >> round_number;
cin.ignore();

srand(time(0));

for (int round_counter = 1; round_counter <= round_number; round_counter++)
{
    cout << "\nRound: " << round_counter << endl;

    for (int player_counter = 1; player_counter <= players; player_counter++)
    {
        char beginning_letter = (rand() % 26) + 'a';

        cout << "\nPlayer " << player_counter << ": Please enter a word that begins with: " << beginning_letter << endl;
        getline(cin, input_word);

        cout << "Player " << player_counter << " has earned: " << input_word.length() << " points." << endl;

        total_points = input_word.length();
        cout << "\nPlayer " << player_counter << " Points: " << total_points << endl;
    }

}

cout << "\nThese are the end results!";

int sum = 0;
for (int end_score = 0; end_score < total_points; ++end_score)
{
    sum += total_points;
    end_score = total_points;

    for (int player_counter = 1; player_counter <= players; player_counter++)
    {
        cout << "\nPlayer " << player_counter << " Points: " << end_score << endl;
    }
}

cout << "Player " << player_counter << " is the winner!" << endl;

system("PAUSE");

4 个答案:

答案 0 :(得分:1)

注意:当我最终完成时,2个帖子已经出现了“数组”仍然没有接受我的“与玩家匹配”这一点,所以我会发帖

对我来说,我觉得程序结构错了,我看到了:

游戏:

  • X玩家
  • X轮
  • 跟踪球员得分

我的第一个假设是,不应该在轮次之前决定球员吗?在你的循环中,你反过来做吗?

我看到的问题:

  • 你怎么能说在Y轮比赛中出场的人X等于在Y + 1比赛中出场的人Z?

  • “单点”变量中的所有内容,即使您拥有未知数量的玩家。然后我会高信心地说,用“单点”变量做你想做的事情

解决方案:

使用数组进行存储! (需要的地方)

程序结构(像这样)

Init player count
for player count
-Init player
Init round count
for round
-save round score to player
display highest score

答案 1 :(得分:1)

除非你必须(你的任务可能是强制性的)必须在不同的循环中计算总得分,否则你真的不需要2D数组来存储每轮的得分。事实上,你只能存储每个玩家的总得分,并在每一轮更新它们:

int max_score = 0;

for (int i = 0; i < players; ++i )
{
    // Note that you already have printed the final scores in previous loop...
    cout << "Player " << ( i + 1 ) << " Points: " << scores[i] << '\n';

    // find max score
    if ( scores[i] > max_score )
        max_score = scores[i];
}

cout << "\nThe winner is...\n";
for (int i = 0; i < players; ++i )
{
    if ( scores[i] == max_score )
        cout << "Player " << ( i + 1 ) << '\n';
}

接下来的任务是找出谁赢了比赛。在我看来,这个游戏可以有更多的胜利者,事实上,不同的玩家可能已经获得相同的总积分。在这种情况下,找到具有最高分数的玩家的索引的简单循环将仅返回第一个(或最后一个,它取决于实现),因此我将找出最大点然后打印出所有在另一个循环中得分的球员:

{{1}}

答案 2 :(得分:0)

你似乎只粘贴了一些不会编译的东西。通常在Stack Overflow,我们需要可编译的示例,但我理解您要做的事情......

你熟悉数组了吗?现在,内存中只有1个得分变量,所有玩家得分都会转到同一个位置。通过一系列分数,可以很容易地跟踪每个玩家,然后他们可能不会被踩踏。就像现在一样,total_points将跟踪所有玩家的分数,而不仅仅是当前玩家。当您使用数组时,从1开始到最终值为<=的所有for循环都会导致问题。您可能希望切换到从0开始并以<结束最终值。

最后一个循环

for(int end_score = 0; end_score < total_points; ++end_score)

也根本没有做你想要的。它将按总分值迭代一次,你的sum将会离开。相反,如果你要跟踪游戏循环中每个玩家的得分(数组中),你根本不需要外部总得分循环,只需要内部玩家循环。

我不想为你编写代码,因为这对你来说是一个学习项目,但我会说你现在不是太远了。祝你好运!

答案 3 :(得分:0)

我使用了上面的所有建议,因此您可以将您的代码与我的代码进行比较。

int main()
{
    /*
    1. Init player count
    2. For player count
        init player
    3. Init round count
    4. For round
        save round score to player
    5. Display highest score
    */

    cout << "Welcome to the Word Game!" << endl << endl;

    // 1. Init player count
    cout << "How many players will be participating?> " << flush;
    int player_count;
    cin >> player_count;

    // 2. For player count, init player;
    vector<int> players;
    for (int i = 0; i < player_count; ++i) {

        players.push_back(i + 1);
    }

    cout << "\nHello!" << endl;
    for (vector<int>::const_iterator iter = players.begin(); iter != players.end(); ++iter) {
        cout << "Player: " << *iter << endl;
    }

    // 3. Init round count
    cout << "\nHow many rounds will be played?> " << flush;
    int round_number;
    cin >> round_number;

    // 4. For round, save round score to player
    srand(time(NULL));
    string input_word;
    vector<int> scores(player_count);       // initialize 'player_count' intergers in the vector with their default value = 0

    for (int i = 0; i < round_number; ++i) {
        cout << "\nRound: " << i + 1 << endl;
        char beginning_letter = 'a' + (rand() % 26);

        for (int j = 0; j < players.size(); ++j) {

            cout << "\nPlayer " << players[j] << ": Please enter a word that begins with: " << beginning_letter << " > " << flush;
            cin >> input_word;
            cout << "\nPlayer " << players[j] << " has earned: " << input_word.length() << " points in this round." << endl;
            scores[j] += input_word.length();
            cout << "Total: " << scores[j] << endl;
        }
    }
    // 5. Display highest score
    cout << "\nThese are the end results!";

    int high = 0;
    int play = 0;
    for (int k = 0; k < scores.size(); ++k)
    {
        if (scores[k] > high) {
            high = scores[k];
            play = k;
        }
    }

    cout << "\nPlayer " << players[play] << " is the winner!" << " Score is: " << high << endl;


    system("PAUSE");
    return 0;
}