c - 如何存储掩码数组

标签 c arrays storage

我被要求编写一个内存游戏,有一些细节,其中第一次显示 Revel 的字母,然后,如果用户在第二个提示中猜测相应的匹配点,那么棋盘应该保持这样,直到用户完成游戏(通过猜测所有正确的匹配点),这是一个 2x2 网格的示例

    Your program:
    * *
    * *
    Enter a pair of integers in the range [1, 2]
    Player: 1 1
    Your program:
    A *
    * *
    (then it clears the screen and displays)
    * *
    * *
    Enter another pair of integers in the range [1, 2]
    Player: 2 1
    Your program:
    * *
    C *
    (then it clears screen and displays)
    * *
    * *
    Enter a pair of integers in the range [1, 2]
    Player: 1 2
    Your program:
    * C
    * *
    (then it clears screen and displays)
    * *
    * *
    Enter another pair of integers in the range [1, 2]
    Player: 2 1
    Your program:
    * *
    C *
    (then it clears the screen and displays)
    * C
    C *
    Enter a pair of integers in the range [1, 2]
    Player: 1 1
    Your program:
    A C
    C *
    (then it clears the screen and displays)
    * C
    C *
    Enter another pair of integers in the range [1, 2]
    Player: 1 1
    Your program:
    A C
    C *
    (then it clears the screen and displays)
    * C
    C *
    Enter a pair of integers in the range [1, 2]
    Player: 1 1
    Your program:
    A C
    C *3
    (then it clears the screen and displays)
    * C
    C *
    Enter another pair of integers in the range [1, 2]
    Player: 2 2
    Your program:
    A C
    C A
    CONGRATULATIONS. YOU SUCCEEDED

我需要一个 4x4,我知道如何显示正确的匹配,但我似乎无法存储新板,所以用户看到最新的板,我无法理解它......

char board[4][4] = { {'A','B','A','D'},{'C','E','H','G'},{'B','D','G','C'},{'F','H','F','E'} };
int i, j, row, column, row2, column2;
char boardmatch[4][4];

int tempX,tempY;

for(tempX = 0; tempX < 4; tempX++){
    for(tempY = 0; tempY < 4; tempY++){
        boardmatch[tempX][tempY] = 0;
    }
}



for (i=0; i < 4 ; i++){
  for (j=0; j<4; j++){
      printf("* ");
  }
  printf("\n");
}

do {

    printf("\nEnter a pair of integers in the range [1, 4]: ");
    scanf("%d %d", &row, &column);

    row--;
    column--;
    printf("\n");

    for (i=0; i < 4 ; i++){
      for (j=0; j < 4 ; j++){


          if ( i == row && j == column){
              printf("%c ", board[row][column]);
          }else{
              printf("* ");
          }

      }
      printf("\n");
    }

    printf("\n");
    system("pause");
    system("cls");

    for (i=0; i < 4 ; i++){
      for (j=0; j<4; j++){
          printf("* ");
      }
      printf("\n");
    }

    printf("\nEnter another pair of integers in the range [1, 4]: ");
    scanf("%d %d", &row2, &column2);

    row2--;
    column2--;
    printf("\n");

    for (i=0; i < 4 ; i++){
      for (j=0; j < 4 ; j++){


          if (i == row2 && j == column2){
              printf("%c ", board[row2][column2]);
          }else{
              printf("* ");
          }

      }
      printf("\n");
    }

    system("pause");
    system("cls");

    if(board[row][column]==board[row2][column2]){
       boardmatch[row][column] = 1;
       boardmatch[row2][column2] = 1;
    }

    for (i=0; i < 4 ; i++){
        for (j=0; j<4; j++){

            if (boardmatch[i][j] == 1){
                printf("%c ", board[row2][column2]);
            }else{
                printf("* ");
            }

        }
        printf("\n");
    }                                                                

    printf("\n");
    system("pause");
    system("cls");

}while(1);

system("PAUSE");    
return 0;
}

最佳答案

您需要另一个用于板的阵列。
它只为每个单元格保存一个位,意思是“找到”或“转变”。
使用它(和原始板)来显示板。
仅显示已找到/打开的单元格。

对于玩家回合,当某些单元格可能被转回时,只需记住哪个单元格被转回,以便您可以将其转回未发现/未转回。该数组开始时全部未翻转,当全部翻转时游戏结束。

(您也可以使用结构将所有内容放入一个数组中。)

关于c - 如何存储掩码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17871730/

相关文章:

c++ - 如何防止使用 ptrace 执行系统调用

c - 当我们将 char * 转换为 int * 时,后台或内存中会发生什么

c - 加号 (++) 运算符应用于结构时的行为是什么?

c - 参加事件时 sleep

c++ - 如何分隔数组中的奇数和偶数?

java - 如何轻松打印数组?

ruby - 如何在 Ruby 中声明一个二维数组

c - 使用 C union 到数据中的 "reserve"空间 - 合理吗?

Azure:如何计算存储事务

c - 在 C 编程期间如何存储数据?