Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中的网格和指针_C_Pointers_Grid - Fatal编程技术网

c语言中的网格和指针

c语言中的网格和指针,c,pointers,grid,C,Pointers,Grid,我用C语言编写了这个程序,其中一个对象R被放置在一个网格上,它应该通过键盘上的输入来移动。例如,如果你按N,就会发生这种情况 0 1 2 0 - - - R - - - - - 1 R - - PRESS N -> GO UP -> - - - PRESS N AGAIN -> - - -

我用C语言编写了这个程序,其中一个对象
R
被放置在一个网格上,它应该通过键盘上的输入来移动。例如,如果你按N,就会发生这种情况

         0 1 2
       0 - - -                      R - -                  - - -                               
       1 R - -  PRESS N -> GO UP -> - - - PRESS N AGAIN -> - - -
       2 - - -                      - - -                  R - -
所以R使它上升。对象必须左右移动,因此当它位于[A0][B0]时,例如,它需要一直向下移动[A2][B0]。见上文。 它会上下左右移动

现在我正在创建一个函数,使它向上移动,但我遇到了很多麻烦:有时它会随机冻结到2:0和0:0,而不向上移动,当它在a=2时,它会变为0,而不是向上移动1,尽管我将它设置为2-1(向上移动必须减去1)

我不明白是什么引起了这些麻烦,有什么建议吗

#include <stdio.h>
#include <time.h>
#include <stdlib.h>


#define X 3
#define Y 3

struct coords{
    int a;
    int b;
};

typedef struct coords cord;

// Print the array
char printArray(char row[][Y], size_t one, size_t two, struct coords cord)
{  

   row[cord.a][cord.b] = 'X';


   // output column heads
   printf("%s", "       [0]  [1]  [2]");
   // output the row in tabular format
   for (size_t i = 0; i < one; ++i) {

      printf("\nrow[%lu] ", i);

      for (size_t j = 0; j < two; ++j) {
         printf("%-5c", row[i][j]);
      } 
   } 
} 


int moveUp(struct coords * cord);


int main(void)
{  
   struct coords cord;


   char row[X][Y] =  
      { { '-', '-', '-'},
        { '-', '-', '-'},
        { '-', '-', '-'} };


   srand(time(NULL));


   cord.a = (rand() % 3); 
   cord.b = (rand() % 3);
   printf("\nValori rand: A %d, B %d\n", cord.a, cord.b);

   // output the row

   //printf("\nrobot:%c\n", robot);
   puts("The array is:");
   printf("\n");

   printArray(row, X, Y, cord);
   row[cord.a][cord.b] = '-';


   //printArray(row, X, Y, &m, &n);
   char h;

   while(h != '3'){


    switch (h) {

      case 'N':

        moveUp(&cord);
        printArray(row, X, Y, cord);
        row[cord.a][cord.b] = '-';

        break;
    }
    scanf("%s", &h);

  }

  printf("\n");
}

int moveUp(struct coords * cord)
{

   cord->a - 1;


   if (cord->a == 2){
      cord->a - 1;
   } else if (cord->a == 1){
      cord->a - 1;
   } else if (cord->a == 0){
      cord->a + 2;
   }



   /*
   if (cord->a == 0) {
    cord-> a = 2;
   } else {
    cord->a - 1;
   }
   */

   printf("\n A = %d, B = %d\n", cord->a, cord->b);



}
#包括
#包括
#包括
#定义x3
#定义y3
结构坐标{
INTA;
int b;
};
typedef结构坐标线;
//打印阵列
字符打印数组(字符行[][Y],大小为1,大小为2,结构坐标线)
{  
行[cord.a][cord.b]='X';
//输出列标题
printf(“%s”,“[0][1][2]”);
//以表格格式输出行
对于(尺寸i=0;i<1;++i){
printf(“\n路径[%lu]”,i);
对于(尺寸j=0;j<2;++j){
printf(“%-5c”,第[i][j]行);
} 
} 
} 
int moveUp(结构坐标*跳线);
内部主(空)
{  
结构协调线;
字符行[X][Y]=
{ { '-', '-', '-'},
{ '-', '-', '-'},
{ '-', '-', '-'} };
srand(时间(空));
cord.a=(rand()%3);
cord.b=(rand()%3);
printf(“\nValori rand:A%d,B%d\n”,cord.A,cord.B);
//输出该行
//printf(“\n机器人:%c\n”,机器人);
puts(“数组为:”);
printf(“\n”);
打印阵列(行、X、Y、线);
行[cord.a][cord.b]='-';
//打印阵列(行、X、Y、m和n);
char h;
而(h!=“3”){
开关(h){
案例“N”:
向上移动(&cord);
打印阵列(行、X、Y、线);
行[cord.a][cord.b]='-';
打破
}
scanf(“%s”、&h);
}
printf(“\n”);
}
int moveUp(结构坐标*跳线)
{
跳线->a-1;
如果(跳线->a==2){
跳线->a-1;
}否则如果(跳线->a==1){
跳线->a-1;
}否则如果(跳线->a==0){
跳线->a+2;
}
/*
如果(跳线->a==0){
跳线->a=2;
}否则{
跳线->a-1;
}
*/
printf(“\n A=%d,B=%d\n”,跳线->A,跳线->B);
}

在下面的代码中,在读入任何内容之前,您正在检查
h的值。如果
h
的未初始化值恰好是
3
,则执行将不会进入while循环

   char h;
   while(h != '3')
因此,将一个值读入
h
,然后在
while
循环中进行检查

moveUp
功能中,您可以使用操作符指定下一个位置或对象
R

cord->a = (cord->a)? (cord->a - 1): 2;

试着调试你的代码.cord->a-1;这条线的目的是什么?为了在网格上显示,A需要从1code->A=code->A-1中删除;试一试简单地说:如果你有一个非负整数
i
,它允许在
0
n-1
之间变化,那么
i=(i+1)%n增量和
i=(i+n-1)%n递减<代码>i
,带换行符。也就是说,递减
0
产生
n-1
,递增
n-1
产生
0
。这是可行的,因为
表达式%n
(模运算符)在表达式除以
n
后产生余数。我们只需要确保论点是肯定的,我们可以通过添加
n
(当然,除非
i<-n
)来做到这一点。