basic C编程生活游戏

basic C编程生活游戏,c,windows,C,Windows,我的代码有问题。我有一个动态框架的大小由用户,他需要输入一个细胞在这个框架的位置。我的问题是,我如何确保这个职位是有效的,或者如果它还没有被输入 代码如下: for (i = 0; i < a; i++) { while (x < 1 || y < 1) { printf("Entrez les coordonnees de la cellule %d: ", i+1); //The user gives the position of the

我的代码有问题。我有一个动态框架的大小由用户,他需要输入一个细胞在这个框架的位置。我的问题是,我如何确保这个职位是有效的,或者如果它还没有被输入

代码如下:

for (i = 0; i < a; i++)
{
    while (x < 1 || y < 1)
    {
        printf("Entrez les coordonnees de la cellule %d: ", i+1); //The user gives the position of the cell
        scanf("%d %d", &x, &y);
    }

    tab[x - 1][y - 1] = 1; //We affect 1 to the cell given by the user
}
(i=0;i { 而(x<1 | | y<1) { printf(“Entrez les coordones de la cellle%d:,i+1);//用户给出单元格的位置 scanf(“%d%d”,&x,&y); } tab[x-1][y-1]=1;//我们对用户给定的单元格影响1 }
您可以使用
memset
将整个
选项卡
矩阵设置为零

因此,当您想查看用户是否已输入此坐标时

如果(tab[x-1][y-1]!=0),您可以执行
,为了确保坐标有效,您可以

while(true){
   ....
   scanf("%d %d", &x, &y);
   if(x > 1 && x < X_MAX && y > 1 && y < Y_MAX){
     if(tab[x-1][y-1] != 0)
        printf("This coordinate was already typed.\n");
     else
        break;
    }
 }
 tab[x-1][y-1] = 1;
while(true){
....
scanf(“%d%d”,&x,&y);
如果(x>1&&x1&&y

其中X_MAX和Y_MAX指定
选项卡
矩阵

的最大边界(大小),假设
选项卡
的大小分别为N和M,您可以执行以下操作:

if (x < 0 || x>= N || y < 0 || y >= M) {
  printf("O-oh fell out of the field\n!");
  .. do stuff ...
}
if(x<0 | | x>=N | | y<0 | | y>=M){
printf(“O-oh掉到地上了”\n!”);
…做事。。。
}

至于一个字段是否已经输入,你必须设置一个算法来记住哪些字段已经输入。或者用布尔值再创建一个矩阵,指示是否已输入字段,或者使用哈希表存储输入的坐标对。

什么构成有效位置?其中
x
y
=0