-1

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

2 回答 2

0

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

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

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

此外,这是您正在做的另一种选择,将 and 的先前值存储为rowand 。columnrow_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;
于 2013-07-26T02:58:15.460 回答
0

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

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

(您也可以使用结构将它们全部放在一个数组中。)

于 2013-07-26T02:38:02.533 回答