【问题标题】:how to store a masked array如何存储掩码数组
【发布时间】:2013-07-26 02:05:25
【问题描述】:

我被要求编写一个记忆游戏,其中包含一些细节,然后,如果第二个提示中的用户猜到相应的匹配点,则板应该保持不变,直到用户完成游戏(通过猜测所有正确的匹配点),这里有一个 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;
}

【问题讨论】:

  • 你应该总是做boardcheck(包括'Enter another...'之后的第一组。最后一组不正确;应该是:printf(“%c”,board[i] [j]); // 不是第 2 行,第 2 列
  • 您需要在“输入一对...”和“输入另一对...”之后使用棋盘匹配。这是你的主要问题。

标签: c arrays storage


【解决方案1】:

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

对于玩家回合,当某些单元格可能会被转回时,只需记住哪个单元格被转过,以便您可以将其转回未找到/未转回。这个数组开始时都没有转身,当他们都转身时游戏结束。

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

【讨论】:

  • 如果您只打算存储 ASCII 数据,则不需要第二个数组;您可以只使用 MSB 作为标志。
  • 亚历克斯,是的。他已经有了另一个阵法。他只需要检查一下就好了。但是,是的,你的想法是好的/有效的。
【解决方案2】:

问题出在这些行中(已应用修复)。

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

如果之前匹配过,它应该显示正确的值。不是row2column2下的那些

此外,还有一个替代方法,将 rowcolumn 的先前值存储为 row_oldcolumn_old

#include <stdio.h>

// Prints the matrix
void board_print(char board[4][4], char boardmatch[4][4], int revealRow, int revealColumn, int revealRow2, int revealColumn2)
{
    int row, col;

    printf("  0 1 2 3\n");

    // Printing code
    for (row=0; row < 4 ; row++)
    {
        printf ("%d ", row);

        for (col=0; col<4; col++)
        {
            // Print the value if there's a board match
            // or there's a revealRow/revealColumn
            if (boardmatch[col][row] == 1
                || (row == revealRow  && col == revealColumn)
                || (row == revealRow2 && col == revealColumn2))
            {
                printf("%c ", board[col][row]);
            }
            else
            {
                printf("* ");
            }
        }

        printf("\n");
    }
}

// Clears board
void board_clear(char board[4][4])
{
    int i,j;

    for(i = 0; i < 4; i++)
    {
        for(j = 0; j < 4; j++)
        {
            board[i][j] = 0;
        }
    }
}

// Main game loop
void game_loop()
{
    char board[4][4] = { {'A','B','A','D'},{'C','E','H','G'},{'B','D','G','C'},{'F','H','F','E'} };
    int row, column, row_old=-1, column_old=-1;
    char boardmatch[4][4];

    board_clear(boardmatch);

    // Reveal the matrix
    board_print(board, boardmatch, -1, -1, -1, -1);

    do
    {
        // Ask for input
        printf("\n[column row]: ");
        fflush(stdin);
        scanf("%d %d", &column, &row);

        // Adjust the values

        // Check the previous value
        if (row_old == -1 || column_old == -1)
        {
            // There was nothing stored
            // Store the previous values
            row_old    = row;
            column_old = column;

            // Print only the selected value
            board_print(board, boardmatch, row, column, -1, -1);
        }
        else
        {
            // There was something stored
            // Check if he did it
            if (board[column][row] == board[column_old][row_old])
            {
                // You did it! Store as matched
                boardmatch[column][row]         = 1;
                boardmatch[column_old][row_old] = 1;

                // Present the result to the user
                board_print(board, boardmatch, row, column, -1, -1);
                printf("Match!\n");
            }
            else
            {
                // Nope, you didn't
                // Present the two items marked (old and selected)
                board_print(board, boardmatch, row, column, row_old, column_old);
                printf("YOU SUCK!\n");
            }

            // Now print the matrix with the selected values


            // Finally, kill the previous values
            row_old    = -1;
            column_old = -1;
        }

        // (Check the boardmatch if every value is 1, and break if it does)
    } while (1);
}

int main(int argc, const char * argv[])
{
    game_loop();

    return 0;
}

这里的关键是,row_oldcolumn_old 充当物品的检查,就像它充当游戏的标志一样。

只要是-1,就表示上一次匹配还没有,当它存储一个值时,就代表上一次匹配。

我还建议作为改进和/或教训,使用结构修改代码:

#import <stdbool.h>

typedef struct
{
    char value;
    bool matched;
}tile;

typedef struct
{
    int row, col;
}coordinate;

【讨论】:

  • 嗯,好的,我去试试
  • @ace 有什么问题?
  • 在中间步骤(用户正在输入的地方)中存储新板
  • 我不知道在你说的地方放什么(打印以前的板)或打印新板\
  • @ace 我编辑了我的答案,我修复了所有问题,我实际上编译并运行了它以更加确定。对不起,我让你失去了之前的答案。另外,我觉得你是初级程序员,所以我避免使用任何复杂的东西,函数是这里唯一的新东西。
猜你喜欢
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 2020-11-19
  • 2013-10-23
相关资源
最近更新 更多