在C中遇到战舰游戏的麻烦

时间:2012-02-01 14:08:57

标签: c

所以我一直在尝试在C中写下战舰游戏。我已经写了一个随机生成的布尔人的简单版本但是我不满意船只只有一个块,而且它们太多了,但我离题了。

在这里,我写了我认为是一段混乱的代码,它有效,有时候......

这是:

void generate_field(int *i, int *j, int n)
{
    *i=rand()%n;
    *j=rand()%n;
}
void map_gen(struct game *data,int n)
{
    int i,j,k,l;
    int return_value=0;

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            data->tiles[i][j].ship=0;
            data->tiles[i][j].uncovered=0;
        }
    }

//    **4**
    generate_field(&k,&l,n);
    if(k==0 || k==1)
    {
        data->tiles[k][l].ship=4;
        data->tiles[k+1][l].ship=4;
        data->tiles[k+2][l].ship=4;
        data->tiles[k+3][l].ship=4;
        data->shipcount++;
    }
    else if(k==(n-1) || k==(n-2))
    {
        data->tiles[k][l].ship=4;
        data->tiles[k-1][l].ship=4;
        data->tiles[k-2][l].ship=4;
        data->tiles[k-3][l].ship=4;
        data->shipcount++;
    }
    else if(l==0 || l==1)
    {
        data->tiles[k][l].ship=4;
        data->tiles[k][l+1].ship=4;
        data->tiles[k][l+2].ship=4;
        data->tiles[k][l+3].ship=4;
        data->shipcount++;
    }
    else if(l==(n-1) || l==(n-2))
    {
        data->tiles[k][l].ship=4;
        data->tiles[k][l-1].ship=4;
        data->tiles[k][l-2].ship=4;
        data->tiles[k][l-3].ship=4;
        data->shipcount++;
    }
//    **3**
    do{
    generate_field(&k,&l,n);
    }while(data->tiles[k][l].ship!=0 && (data->tiles[k+1][l].ship!=0 || data->tiles[k-1][l].ship!=0 || data->tiles[k][l+1].ship!=0 || data->tiles[k][l-1].ship!=0) && (data->tiles[k+2][l].ship!=0 || data->tiles[k-2][l].ship!=0 || data->tiles[k][l+2].ship!=0 || data->tiles[k][l-2].ship!=0));
    if((k==0 || k==1) && (data->tiles[k+1][l].ship==0 && data->tiles[k+2][l].ship==0))
    {
        data->tiles[k][l].ship=3;
        data->tiles[k+1][l].ship=3;
        data->tiles[k+2][l].ship=3;
        data->shipcount++;
    }
    else if((k==(n-1) || k==(n-2)) && (data->tiles[k-1][l].ship==0 && data->tiles[k-2][l].ship==0))
    {
        data->tiles[k][l].ship=3;
        data->tiles[k-1][l].ship=3;
        data->tiles[k-2][l].ship=3;
        data->shipcount++;
    }
    else  if((l==0 || l==1) && (data->tiles[k][l+1].ship==0 && data->tiles[k][l+2].ship==0))
    {
        data->tiles[k][l].ship=3;
        data->tiles[k][l+1].ship=3;
        data->tiles[k][l+2].ship=3;
        data->shipcount++;
    }
    else if((l==(n-1) || l==(n-2)) && (data->tiles[k][l-1].ship==0 && data->tiles[k][l-2].ship==0))
    {
        data->tiles[k][l].ship=3;
        data->tiles[k][l-1].ship=3;
        data->tiles[k][l-2].ship=3;
        data->shipcount++;
    }
//    **2**
    do{
    generate_field(&k,&l,n);
    }while(data->tiles[k][l].ship!=0 && (data->tiles[k+1][l].ship!=0 || data->tiles[k-1][l].ship!=0 || data->tiles[k][l+1].ship!=0 || data->tiles[k][l-1].ship!=0));
    if((k==0 || k==1) && (data->tiles[k+1][l].ship==0))
    {
        data->tiles[k][l].ship=2;
        data->tiles[k+1][l].ship=2;
        data->shipcount++;
    }
    else if((k==(n-1) || k==(n-2)) && (data->tiles[k-1][l].ship==0))
    {
        data->tiles[k][l].ship=2;
        data->tiles[k-1][l].ship=2;
        data->shipcount++;
    }
    else if((l==0 || l==1) && (data->tiles[k][l+1].ship==0))
    {
        data->tiles[k][l].ship=2;
        data->tiles[k][l+1].ship=2;
        data->shipcount++;
    }
    else if((l==(n-1) || l==(n-2)) && (data->tiles[k][l-1].ship==0))
    {
        data->tiles[k][l].ship=2;
        data->tiles[k][l-1].ship=2;
        data->shipcount++;
    }

//    **1**
    do{
    generate_field(&k,&l,n);
    }while(data->tiles[k][l].ship!=0);
    data->tiles[k][l].ship=1;
    data->shipcount++;
}

**#**是发货尺寸。

int n是矩阵数组维度的大小(我有两个大小:Normal为5x5,大为8x8)

无论如何,我知道这可以用更简单的方式编写,并且它实际上可以工作。 do-while循环太长了,很多时候我的船只不会产生一两次。我认为这是因为我通过使用(k==0k==n-1)来限制他们的产卵,但是我不知道该怎么做。这里的任何人都可以给我一些提示,告诉我如何以不同的方式编写这些内容,更紧凑,以及它实际上是否正常工作的方式?

1 个答案:

答案 0 :(得分:1)

问题在于如何确定船舶的位置和方向。

我会这样做:

void place_ship(struct game* data, int n, int shipsize)
{
    int x = 0, y = 0;  // Uesd for ship direction

    // Generate a direction
    if (rand()%2) {
        i=rand()%(n - shipsize + 1);
        j=rand()%n;
        x = 1;
        y = 0;
    } else {
        i=rand()%n;
        j=rand()%(n - shipsize + 1);
        x = 0;
        y = 1;
    }

    for (k = 0; k < shipsize; k++) {
        if (data->tiles[i + x * k][j + y * k].ship != 0) {
            // Space already occupied - retry!
            return place_ship(data, n, shipsize);
        }
    }
    for (k = 0; k < shipsize; k++) {
         data->tiles[i + x * k][j + y * k].ship = shipsize;
    }
}