War*_*ner 3 c++ random console srand
我正在制作基于C++文本的战舰游戏.我使用rand()函数随机放置计算机的船只.我在main()的开头给种子生成器播种一次,下面的行:
srand(static_cast<unsigned>(time(0)));
Run Code Online (Sandbox Code Playgroud)
我稍后使用rand()函数来选择每艘船将要去的空间.(船的一端将开始的坐标).然后我在下面的函数中使用rand(),它决定了它们的扩展方向(取决于船的长度):
char randDirection()
{
int randNumber = rand();
int x = (randNumber % 4) + 1;
if (x = 1)
return 'u';
else if (x = 2)
return 'd';
else if (x = 3)
return 'l';
else
return 'r';
}
Run Code Online (Sandbox Code Playgroud)
它随机获取1到4之间的数字,并根据数字返回一个方向(由char表示).虽然我成功地随机选择了碎片的位置,但这个功能总是垂直设置碎片.他们总是上去.谁能告诉我为什么?