确保船只不重叠

Making sure the ships do not overlap

我已经被困在这个任务上很长时间了,所以我想是时候寻求帮助了。我正在制作一款战舰游戏,目前正在编写一个函数,将战舰随机放置在 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 循环的组合来尝试让飞船重置,但我就是想不出一种方法来完成这项工作

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

重叠的原因是,当您放置第二艘船时,您绝不会检查随机生成的单元格(根据行和列)是否实际上是空的。 也就是说,当你为你的战舰调用 rand() 时,你永远不会检查你得到的列或行(以及相邻的,取决于船的方向)是否实际上是空的或者它们是否已经被使用您的运营商。

一个简单的方法是实现一个函数来检查随机生成的单元格是否足够。可能的签名类似于:

int isAvailableCell(int cell, int direction,  int ship_length);

此函数采用随机生成的单元格、船的方向和船长,它会检查是否所有 "ship_length" 个单元格(即船需要的单元格数适合)从 "cell" 开始在 "direction" 可用。这仅涉及检查您的 2D 向量在这些单元格中是否具有非零元素(因为您在放置船只时填充它们)。

如果cell不够用,需要重新调用rand()。显然,这是一种极其低效的蛮力方法,因为您最终可能会多次生成不合适的单元格,但我认为此时您不必担心性能。

还有,很多代码是多余的,可以把放船封装在一个函数里。它会让你的生活更轻松,你的代码看起来更干净。 希望这对您有所帮助,祝您好运!

我喜欢 Joud 的回答。此外,创建一个带有名称和长度字段的 "ship" 结构。然后你可以有一个飞船数组,并在 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 是否该单元格可用。