C++ rand()函数的问题

C++ rand()函数的问题,c++,c,C++,C,我的随机生成的“蛇丸”有些问题。我想把*当作蛇的食物。它不是在我用作游戏板的char数组中生成的。我不确定;我可能会错误地调用它或不正确地使用该函数 #include<iostream> #include<Windows.h> #include<stdlib.h> #include<time.h> #include<stdio.h> using namespace std; char Map[11][22] = { "-----

我的随机生成的“蛇丸”有些问题。我想把
*
当作蛇的食物。它不是在我用作游戏板的
char
数组中生成的。我不确定;我可能会错误地调用它或不正确地使用该函数

#include<iostream>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h> 
using namespace std;

char Map[11][22] =
{
  "---------------------",
  "|S              *   |",
  "|                   |",
  "|                   |",
  "|                   |",
  "|                   |",
  "|                   |",
  "|                   |",
  "|                   |",
  "|                   |",
  "---------------------"
};
int x = 1, y = 1;
bool GameRunning = true;
int main()
{
  srand(time(NULL));
  int pellet = rand();

  while (GameRunning == true)
  {
    for (int pellet = 0; pellet % Map[11][22]; pellet++)
    {
      cout << '*';
    }
    system("cls");
    for (int display = 0; display < 11; display++)
    {
      cout << Map[display] << endl;
    }
    system("pause>nul");

    if (GetAsyncKeyState(VK_DOWN))
    {
      int y2 = y + 1;
      if (Map[y2][x] == ' ')
      {
        Map[y][x] = ' ';
        y++;
        Map[y][x] = 'S';
      }
    }
    if (GetAsyncKeyState(VK_UP))
    {
      int y2 = y - 1;
      if (Map[y2][x] == ' ')
      {
        Map[y][x] = ' ';
        y--;
        Map[y][x] = 'S';
      }
    }
    if (GetAsyncKeyState(VK_RIGHT))
    {
      int x2 = x+1;
      if (Map[y][x2] == ' ')
      {
        Map[y][x] = ' ';
        x++;
        Map[y][x] = 'S';
      }
    }
    if (GetAsyncKeyState(VK_LEFT))
    {
      int x2 = x - 1;
      if (Map[y][x2] == ' ')
      {
        Map[y][x] = ' ';
        x--;
        Map[y][x] = 'S';
      }
    }
  }
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符映射[11][22]=
{
"---------------------",
“|S*|”,
"|                   |",
"|                   |",
"|                   |",
"|                   |",
"|                   |",
"|                   |",
"|                   |",
"|                   |",
"---------------------"
};
int x=1,y=1;
bool GameRunning=true;
int main()
{
srand(时间(空));
int=rand();
while(GameRunning==true)
{
对于(int pellet=0;pellet%Map[11][22];pellet++)
{

您是否可以用0覆盖for循环中的
pellet
,因此它总是用0初始化,而不是
rand()
。此外,
rand()
产生一些介于0到至少32767之间的随机数,因此您需要修改它以获得所需的范围。例如,从0到20,您可以执行
pellet=rand()%21
或4-16您可以执行
颗粒=(rand()%13)+4
(因为4-16可以重写为范围0-12加4)。以下是有关
rand()
的文档:

第二,你真的需要一个for循环吗?它真的应该在游戏循环中吗?似乎在开始时设置一次会更有意义


您可能希望在开始时使用类似于
map[random\u x\u val][random\u y\u val][random\u y\u val][random\u y\u val]='*'
的方法将地图中的一些随机框设置为
random\u x\u val
random\u y\u\u val
是矩阵的有效范围。

我不确定您希望看到什么
map[11][22]
要做,但它只是超出了数组的界限。Clang也给了我一个警告:警告:数组索引22超过了数组的末尾(包含22个元素)(奇怪的是,它对超出界限的第一个维度没有这样做)这是
C
还是
C++
?我以为它会在数组中循环并显示“*”我没有正确使用它吗?你从兰德公司设置了pellet”,然后立即覆盖了for()语句中关于该语句的值:“for(int pellet=0;pellet%Map[11][22];pellet++)”Map[11][22]超出数组的边界。最大映射为:“Map[10][21]”(因为“C”索引从0开始,并通过size-1I扩展),我将其更改为
for(int pellet=rand();pellet%Map[11][22];pellet++)
现在我收到一条警告,现在我收到一个空白控制台,指向地图边框,有效范围是1…20,而不是0…20。我在上面添加了一个示例,说明如何在0以外的数字上开始随机数范围。我在我的my main的开头添加了这个示例。你是说在我创建字符数组的main之外,因为它给了我错误吗在main中,在您输入
while(GameRunning==true)
之前,如果您将它放入while循环中,它将在while循环每次执行循环时设置它。