我已经被困在这个任务上很长一段时间了,所以我想是时候寻求帮助了。我正在制作一个战舰游戏,我目前正在编写一个函数,其中船只随机放置在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);

功能。我只包括前两艘因为裁员的原因
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循环的组合,试图让飞船复位,但我就是想不出一种方法来实现这一点
为了完成这项任务,我真的需要一些指导或者朝着正确的方向迈出一步。提前谢谢你!

最佳答案

我喜欢朱德的回答。此外,创建一个包含名称和长度字段的“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
}

伪代码可能有用。检查游戏板->板是否有可用的单元格。

10-06 07:45