C++ C++;:数组索引没有增加

C++ C++;:数组索引没有增加,c++,arrays,C++,Arrays,我想做一个3人的简单游戏,每个人根据随机函数在一个区块内移动,每次移动1到6个区块,当第一个玩家移动时,第二个玩家开始,然后第三个玩家开始。要做到这一点,我会在玩家完成移动时增加数组rach的索引 我的问题是索引器似乎没有增加,即使我增加它,它也会在播放器1中堆叠。我在C#中有完全相同的代码,而且它工作得很好 这里是C++的代码。 int main () { string namesofplayers[] = {"one","two","three"}; int

我想做一个3人的简单游戏,每个人根据随机函数在一个区块内移动,每次移动1到6个区块,当第一个玩家移动时,第二个玩家开始,然后第三个玩家开始。要做到这一点,我会在玩家完成移动时增加数组rach的索引

我的问题是索引器似乎没有增加,即使我增加它,它也会在播放器1中堆叠。我在C#中有完全相同的代码,而且它工作得很好

这里是C++的代码。

int main ()
{
        string namesofplayers[] = {"one","two","three"};

        int movementofplayers[] = {0,0,0}; // start position of players is 
        int gamesize = 32; //32 blocks-steps of game
        int random;
        int y = 0;

        a:

        y++;
        if (y >= 3) 
        {
            y = 0;
        }
        cout << "it's" << namesofplayers[y] << "turn to play";
        int R = (rand() % 6 + 1);
        cout << "player " << namesofplayers[y] << " moves to block" << R << endl;
        movementofplayers[y] += random;
        cout << movementofplayers[y];


        if (movementofplayers[y] < gamesize)
        {
             goto a;
        }
        else 
        { 
              cout << "Player " << namesofplayers[y] << " wins the game" << endl;
        }
}
int main()
{
字符串名称层[]={“一”、“二”、“三”};
int movementofplayers[]={0,0,0};//玩家的起始位置为
int gamesize=32;//32阻止游戏步骤
int随机;
int y=0;
a:
y++;
如果(y>=3)
{
y=0;
}

由于不太可能完成您的工作,我冒昧地编写了一个替代实现,该实现修复了您以前代码的一些问题,并产生了更可读的输出。我还丢弃了一行程序,因为它们让我发疯,但这是我个人的偏好。此外,我倾向于明确限定sta中的符号使用适当范围的标准库

  • 摆脱
    goto
    。你可以出于多种原因浏览SO和web为什么不使用那样的显式跳转。只需使用循环即可

  • 修复伪随机数生成器缺少的初始种子。如果设置不同的种子,即通过使用某个变量值(例如
    time(nullptr)
    )调用它,每次程序调用都会得到相同的“random”值序列

  • 修复变量
    random
    的使用。您试图将一些垃圾初始化值
    random
    添加到
    movementofplayers[y]
    。有趣的是,
    g++-4.7
    似乎确保变量在用于算术运算之前设置为
    1
    。但是,您需要的正确变量是
    R

  • main()
    返回定义良好的值

  • 我希望代码仍能达到您的预期目的:

    #include <string>
    #include <iostream>
    
    int main ()
    {
      srand(time(NULL));
      std::string namesofplayers[] = {"one","two","three"};
    
      int movementofplayers[] = {0,0,0}; // start position of players is
      int gamesize = 32;                 //32 blocks-steps of game
      int y = 1;
    
      while(movementofplayers[y] < gamesize)
      {
        if (y >= 3)
        {
          y = 0;
        }
    
        std::cout << "it's " << namesofplayers[y] << " turn to play" << std::endl;
        int R = (rand() % 6 + 1);
        std::cout << "player " << namesofplayers[y] << " moves to block " << R << std::endl;
        movementofplayers[y] += R;
        std::cout << "movements of player " << namesofplayers[y] <<": " << movementofplayers[y] << std::endl;
    
        y++;
      }
    
      std::cout << "Player " << namesofplayers[y] << " wins the game" << std::endl;
    
      return 0;
    }
    
    #包括
    #包括
    int main()
    {
    srand(时间(空));
    std::string namesofplayers[]={“一”、“二”、“三”};
    int movementofplayers[]={0,0,0};//玩家的起始位置为
    int gamesize=32;//32阻止游戏步骤
    int y=1;
    while(movementofplayers[y]=3)
    {
    y=0;
    }
    
    std::cout以下是我将如何做到这一点

    添加了随机数生成器,因此您不会每次都得到相同的游戏

    增加了一个恒定的玩家数量,以摆脱魔术数字,还可以更容易地扩大玩家数量,如果需要的话

    摆脱了goto。虽然可以以合理的方式使用goto,但它容易被意外误用,使代码更难理解,并使人们愤怒。:)

    我对输出和名称进行了一些调整,只是为了让我更容易测试。在这样做的过程中,我纠正了一个问题,即球员移动到阻挡R,这是他们在该回合的掷骰,而不是他们在游戏中的实际位置

    #include <string>
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        std::srand(static_cast<unsigned int>(std::time(0)));
    
        const int gamesize = 32;
        const int num_players = 3;
        const std::string namesofplayers[num_players] = {"1", "2", "3"};
        int movementofplayers[num_players] = {0, 0, 0};
    
        int current_player = 0;
        for(;;) //Loop forever, the game logic will exit the loop when a winner is found
        {
            const int roll = rand() % 6 + 1;
            movementofplayers[current_player] += roll;
    
            std::cout << "Player " << namesofplayers[current_player] << " rolls a " << roll << " and moves to block " << movementofplayers[current_player] << std::endl;
            //Check if they won and if so, end the game
            if(movementofplayers[current_player] >= gamesize)
            {
                std::cout << "Player " << namesofplayers[current_player] << " wins the game!" << std::endl;
                break;
            }
            current_player = (current_player + 1) % num_players;
        }
        return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    int main()
    {
    标准::srand(静态_cast(标准::时间(0));
    常数int gamesize=32;
    const int num_players=3;
    const std::string namesofplayers[num_players]={“1”、“2”、“3”};
    int movementofplayers[num_players]={0,0,0};
    int当前_播放器=0;
    对于(;;)//永远循环,当找到赢家时,游戏逻辑将退出循环
    {
    常量int roll=rand()%6+1;
    movementofplayers[当前玩家]+=掷骰;
    
    std::cout哪里是
    random
    集?您将随机值分配给
    R
    ,但将未初始化的
    random
    添加到该位置。在某些情况下,使用
    goto
    可以。您使用它不是这些情况之一。请改用循环。@Antho然后,将实际和预期的输出放入问题中。这是正确的d来找出你想要什么,代码什么时候实际运行。@luk32:我没有过度解释任何东西。参数“现在是2013年,使用向量”也是无效的;年份是不相关的。使用正确的工具来完成这项工作。如果这是向量,很好,但不是因为日历上的某个数字。问题与编译器无关,所以你可以添加(我想你应该加上:))一个使用C++11随机库的版本。我对你的代码有问题。它会永远运行。请看看我的C#代码运行这种逻辑的效果如何@Manu343726:嗯,我认为OP现在有不同的问题。C++11 RNG可能不是其中之一。@Antho:它在这里运行得很好。没有任何终止问题,这不是问题令人惊讶的是,终止条件是基于当前“选定”玩家的移动次数。@thokra.srandom和时间未定义。您是否遗漏了代码中的某些内容?我正在使用代码块进行调试谢谢!它不知道(;;)似乎有太多人在看到go to循环时会生气。我也在使用一个小的汇编,汇编代码是完整的o JMP命令。是的goto真的是个坏主意,但我这么做更多的是为了好玩,而不是为了严肃的原因。我主要是一个C#编码器。我为这个游戏编写了一个程序,它在C#代码中运行得很好,但当我决定在C++中尝试它我遇到了太多的问题