gpt4 book ai didi

c - 确保船只不重叠

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:30 25 4
gpt4 key购买 nike

我已经被困在这个任务上很长时间了,所以我想是时候寻求帮助了。我正在制作一款战舰游戏,目前正在编写一个函数,将战舰随机放置在 10 x 10 的网格上。我已完成此操作,但我的问题是它们何时重叠。

我想不出一种方法来获取不与之前随机放置的飞船重叠的坐标。在我当前的代码中,我试图找出一种方法来实现它,以便如果坐标重叠,它将再次循环船,直到它具有不与任何其他船重叠的正确数量的单元格。我以 3 文件格式编写,因此我将包含每个文件中所需的代码。

在函数中,上下左或右是随机的,所以在战舰中我的方向 = 0 所以我只使用向上

这是头文件

typedef struct game_board
{
int board[10][10];
int row;
int col;
char symbol;
}Game_Board;

Game_Board initalize_game_board(Game_Board *player);

//Game_Board manually_place_ships_on_board(Game_Board *player);
Game_Board randomlly_place_ships_on_board(Game_Board *player);

主要

Game_Board person, computer;
int who_goes_first = 0;

person.symbol = '~';
person.row = 10;
person.col = 10;
computer.symbol = '-';
computer.row = 10;
computer.col = 10;

welcome_screen(outfile);

printf("Player 1\n");
initalize_game_board(&person);
printf("\nPlayer 2\n");
initalize_game_board(&computer);
who_goes_first = select_who_starts_first();
//manually_place_ships_on_board(&person);
randomlly_place_ships_on_board(&computer);

函数。出于冗余原因,我只包括前 2 艘船

int direction = 0, i = 0, cell_row = 0, cell_col = 0;


//Carrier
printf("CARRIER\n");
direction = rand() % 4;
printf("Direction: %d\n", direction);
player->symbol = 'c';
if (direction == 0) // up
{
cell_row = rand() % 10;
if (cell_row <= 4)
{
cell_row += 4;
}
cell_col = rand() % 10;
for (i = 0; i < 5; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("UP: Row:%d Col:%d\n", cell_row, cell_col);
cell_row -= 1;
}
}
else if (direction == 1) // down
{
cell_row = rand() % 6;
cell_col = rand() % 10;
for (i = 0; i < 5; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("DOWN: Row:%d Col:%d\n", cell_row, cell_col);
cell_row += 1;
}
}
else if (direction == 2) // left
{
cell_row = rand() % 10;
cell_col = rand() % 10;
if (cell_col <= 4)
{
cell_col += 4;
}
for (i = 0; i < 5; i++)
{
player->board[cell_row][cell_col] = player->symbol;
cell_col -= 1;
printf("LEFT: Row:%d Col:%d\n", cell_row, cell_col);
}
}
else if (direction == 3) // right
{
cell_row = rand() % 10;
cell_col = rand() % 6;
for (i = 0; i < 5; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("RIGHT: row:%d Col:%d\n", cell_row, cell_col);
cell_col += 1;
}
}

//Battle Ship
printf("BATTLE SHIP\n");

direction = rand() % 4;
printf("Direction: %d\n", direction);
player->symbol = 'b';
if (direction == 0) // up
{

cell_row = rand() % 10;

if (cell_row <= 3)
{
cell_row += 3;
}

cell_col = rand() % 10;


for (i = 0; i < 4; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("UP: Row:%d Col:%d\n", cell_row, cell_col);
cell_row -= 1;
}
}
else if (direction == 1) // down
{
cell_row = rand() % 7;
cell_col = rand() % 10;
for (i = 0; i < 4; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("DOWN: Row:%d Col:%d\n", cell_row, cell_col);
cell_row += 1;
}
}
else if (direction == 2) // left
{
cell_row = rand() % 10;
cell_col = rand() % 10;
if (cell_col <= 3)
{
cell_col += 3;
}
for (i = 0; i < 4; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("LEFT: Row:%d Col:%d\n", cell_row, cell_col);
cell_col -= 1;
}
}
else if (direction == 3) // right
{
cell_row = rand() % 10;
cell_col = rand() % 7;
for (i = 0; i < 4; i++)
{
player->board[cell_row][cell_col] = player->symbol;
printf("RIGHT: row:%d Col:%d\n", cell_row, cell_col);
cell_col += 1;
}
}

我已经尝试了 do while、while 和 for 循环的组合来尝试让飞船重置,但我就是想不出一种方法来完成这项工作

我真的可以使用一些指导或朝着正确方向迈出的一步来解决这个任务。提前致谢!

最佳答案

我喜欢 Joud 的回答。此外,制作一个带有名称和长度字段的“船”结构。然后你可以拥有一组飞船,并在 for 循环中将每艘飞船传递给函数:

loop(i) //pseudocode
{
randomlly_place_ship_on_board(&computer, &ship[i])
}
...
randomlly_place_ship_on_board(Game_Board *player, Ship * sh)
{
// Only takes care of one ship, sh.
// Call isAvailableCells to determine placement, like
while (!isAvailableCells...)
// re-attempt placement
}

伪代码可能会有所帮助。检查 gameboard->board 以了解该单元格是否可用。

关于c - 确保船只不重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com