我被要求编写一个记忆游戏的代码,其中有一些具体细节,然后第一次显示被激活的字母,如果第二个提示中的用户猜到相应的匹配点,则董事会应保持这样直到用户完成游戏(通过猜测所有正确的匹配点),下面是一个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;
}
答案 0 :(得分:0)
你需要另一个电路板阵列 它只是为每个单元格保留一点,意思是“找到”或“转向” 使用它(和原板)显示板 仅显示已找到/已转动的单元格。
对于玩家转身,当某些单元格可能被转回时,只需记住哪个单元格被转动,这样你就可以将它翻转为无法翻转。这个阵列开始时全部都是不可思议的,当游戏全部转向时游戏结束。
(您可以使用结构将所有这些全部放在一个数组中。)
答案 1 :(得分:0)
问题在于这些问题(修复已经应用)。
if (boardmatch[i][j] == 1)
{
printf("%c ", board[i][j]);
}
如果之前已匹配,则应显示正确的值。不是row2
,column2
此外,这里有一个关于你正在做的事情的替代方案,将row
和column
的先前值存储为row_old
和column_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_old
和column_old
充当对项目的检查,就像它为游戏的标志行事一样。
每当它是-1
时,就意味着尚未进行上一场比赛,而当它存储一个值时,它代表前一场比赛。
我还建议作为改进和/或教训,使用结构修改代码:
#import <stdbool.h>
typedef struct
{
char value;
bool matched;
}tile;
typedef struct
{
int row, col;
}coordinate;