我使用 ncurses 库和 C++ 创建了一个简单的游戏。主要思想是一个黄色方块的玩家在棋盘上行走并收集尽可能多的“*”。它也不能撞到障碍物和边界。开始时,会生成 100 个,收集完一个后,会在随机位置生成另一个。我的问题是:在生成星号之前,我正在检查障碍物的坐标,以避免星号在障碍物内生成。它在大多数情况下都有效,但在一些非常罕见的情况下,星号会在障碍物内生成。我有任何逻辑错误吗?还是我做错了什么?我的解决方案有什么替代方案?
这些是关于障碍物的数据
mainwin = initscr();
obscatle=subwin(mainwin,7,2,1,6);
obscatle2=subwin(mainwin,10,2,8,20);
obscatle3=subwin(mainwin,2,2,17,50);
obscatle4=subwin(mainwin,6,2,25,70);
obscatle5=subwin(mainwin,12,2,10,90);
obscatle6=subwin(mainwin,20,2,8,110);
obscatle7=subwin(mainwin,22,2,4,130);
obscatle8=subwin(mainwin,17,2,18,150);
这些是函数。
//At the start of the game, spawn asterisks on random positions
vector<pair<int,int>> generate_asterisk(int numberOfLines, int numberOfColumns) {
vector<pair<int,int>> coordinates;
srand(time(0));
while(coordinates.size() < 500) {
int x = (rand() % (COLS - 3)) + 1;
int y = (rand() % (LINES - 5)) +1;
// Check if x and y match the given values, and generate new random values until they don't match
while((y >= 1 && y <= 7 && (x >= 5 && x <= 9)) ||
(y >= 8 && y <= 17 && (x >= 19 && x <= 23)) ||
(y >= 17 && y <= 18 && (x >= 49 && x <= 53)) ||
(y >= 25 && y <= 30 && (x >= 69 && x <= 73)) ||
(y >= 10 && y <= 21 && (x >= 89 && x <= 93)) ||
(y >= 8 && y <= 27 && (x >= 109 && x <= 114)) ||
(y >= 4 && y <= 25 && (x >= 129 && x <= 134)) ||
(y >= 18 && y <= 34 && (x >= 149 && x <= 154))) {
x = (rand() % (COLS - 3)) + 1;
y = (rand() % (LINES - 5)) + 1;
}
pair<int,int> coord = make_pair(x,y);
if(find(coordinates.begin(), coordinates.end(), coord) == coordinates.end()) {
coordinates.push_back(coord);
}
}
return coordinates;
}
//after collecting, spawn a new one
void spawnAsterisk(WINDOW* win, int x_max, int y_max, std::vector<std::pair<int, int>>& asterisk_coords)
{
// Generate new coordinates for the asterisk
int new_x = (rand() % (COLS - 3)) + 1;
int new_y = (rand() % (LINES - 5)) +1;
while((new_y >= 1 && new_y <= 7 && (new_x >= 5 && new_x <= 9)) ||
(new_y >= 8 && new_y <= 17 && (new_x >= 19 && new_x <= 23)) ||
(new_y >= 17 && new_y <= 18 && (new_x >= 49 && new_x <= 53)) ||
(new_y >= 25 && new_y <= 30 && (new_x >= 69 && new_x <= 73)) ||
(new_y >= 10 && new_y <= 21 && (new_x >= 89 && new_x <= 93)) ||
(new_y >= 8 && new_y <= 27 && (new_x >= 109 && new_x <= 114)) ||
(new_y >= 4 && new_y <= 25 && (new_x >= 129 && new_x <= 134)) ||
(new_y >= 18 && new_y <= 34 && (new_x >= 149 && new_x <= 154))) {
new_x = (rand() % (COLS - 3)) + 1;
new_y = (rand() % (LINES - 5)) + 1;
}
// Check if the new coordinates conflict with existing asterisks
bool conflict = false;
for (const auto& coord : asterisk_coords)
{
if (coord.first == new_x && coord.second == new_y)
{
conflict = true;
break;
}
}
// If there's a conflict, generate new coordinates until there's no conflict
while (conflict)
{
new_x = rand() % x_max;
new_y = rand() % y_max;
conflict = false;
for (const auto& coord : asterisk_coords)
{
if (coord.first == new_x && coord.second == new_y)
{
conflict = true;
break;
}
}
}
asterisk_coords.push_back(std::make_pair(new_x, new_y));
mvwprintw(win, new_y, new_x, "*");
wrefresh(win);
}
我正在手动检查,因为这是我现在的水平。我试过了,因为它在大部分时间都有效,我想我已经接近答案了。或者也许代码是正确的,但错误是因为屏幕尺寸。