我已经完成了这个项目,这是一个在cmd中运行的RPG游戏。玩家通过键盘导航他的角色并与敌人战斗以升级等等。我使用2D array
作为grid/map
,在演示版中,一切正常。
问题:现在,在更高级的版本中,我有一个用于load game/start new game
的类。启动新游戏的功能基本上创建了存储信息的.txt
个保存文件。问题在于生成敌人列表的功能。正在生成的敌人特征以及问题所在的是X
和Y
坐标。以下是显示该过程的一些代码:
void enemyGenerator(int level)
/* Declare the random generator */
std::default_random_engine generator((unsigned)time(0));
std::uniform_int_distribution<int> coordsX(1, 28); //There are 30 rows, from which the first and the last are border
std::uniform_int_distribution<int> coordsY(1, 48); //50 columns; first and last are border
/* Declare some variables */
int x, y;
bool ready = "False";
/* Check what level is demanded, for example level 1 */
if (level == 1)
{
while(true)
{
//Generate X and Y
x = coordsX(generator);
y = coordsY(generator);
//Now where the problem appears to be
//There will be 600 enemies = 1200 coordinates, so I have declared an array in the .h file
//Called coordinates[1200] = {}; now I want to check it bottom to top if
//the newly generated coordinates are already existing, so:
for (int i = 0; i < 1200; i += 2) //1200 = array size; += 2, because we're checking x & y at once
{
if (x != coordinates[i] && y != coordinates[i + 1] && x + y != 2) //x + y can't be 2, because this is where the player starts (1, 1)
{
if (i == 1198) //If these are the last x and y in the array
{
ready = "True";
break;
//Break the for loop with status ready
}
else
{
continue;
//If it isn't the end of the array simply continue checking
}
}
else
{
ready = "False";
break;
//If the x and y match with the ones in the array, then break with status not ready
}
}
if (ready)
{
break;
//If status is ready then break the loop and assign the rest of the stats
}
else
{
continue;
//If status is not ready then continue generating random values
}
}
//Here I define the values of the stats in private variables of the class
eX = x;
eY = y;
eLVL = 1;
//etc...
}
这是生成代码。以下是我如何使用它:
void newGame()
....
//I've reached to the point where I want to deploy for example 10 enemies of level 1
for (int i = 0; i < 10; i++)
{
enemyGenerator(1);
//I have an already defined fileWriter (std::fstream; std::ios::out)
fileWriter << eX << " " << eY << " " << eLVL; //<< " " etc...
}
....
对我来说,一切似乎都是合乎逻辑的,唯一不合逻辑的是它不起作用。我得到的enemyList.txt
中的输出是例如3 5 1 (other stats) 3 5 1 (other stats) 3 5 1 (other stats)
,你得到它。
问题:您能发现任何错误吗?你能告诉我正确的方法吗?如果需要更多的代码我甚至可以发送源文件,只是为了治愈我的头痛。
答案 0 :(得分:0)
随机生成器存在问题。
每次使用当前时间调用enemyGenerator()时,您都在设置生成器的种子。但是由于你在相同的时间内多次调用enemyGenerator,时间值是相同的,因此随机生成器种子每次都是相同的,这将为每个连续调用提供相同的随机模式。
对所有通话使用相同的发生器
...
std::default_random_engine random_generator((unsigned)time(0));
//I've reached to the point where I want to deploy for example 10 enemies of level 1
for (int i = 0; i < 10; i++)
{
enemyGenerator(random_generator, 1);
//I have an already defined fileWriter (std::fstream; std::ios::out)
fileWriter << eX << " " << eY << " " << eLVL; //<< " " etc...
}
....
void enemyGenerator(std::default_random_engine& generator, int level)
或每次使用不同的值为您的生成器播种。
编辑:
好吧,它似乎不是你问题的原因,但你仍然应该考虑我写的内容。