2013-07-26 2 views
-1

메모리 게임을 코딩하라는 메시지가 표시됩니다. 몇 가지 구체적인 사항이 있습니다. 두 번째 프롬프트의 사용자가 해당하는 일치하는 지점을 추측하면 게시판은 계속 머물러 있어야합니다. 즉 사용자까지마스크 된 배열을 저장하는 방법

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 

내가 4 × 4 필요 × 2 그리드 (올바른 일치하는 지점을 모두 추측하여)를 heres 예를 게임을 완료처럼, 나는 정확한 일치를 보여하지만 난 수없는 것 방법을 이해 새로운 보드를 저장하기 위해 사용자가 가장 최근의 보드를 볼 수 있으므로 내 머리를 감쌀 수 없습니다 ....

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

(다른 입력 바로 '후 첫 세트를 포함 : 그리고 나는 또한 개선 및/또는 교훈 제안

는 구조체를 사용하여 코드를 수정합니다. 마지막 세트가 잘못되어 있어야합니다 : printf ("% c", board [i] [j]); // row2, column2가 아닙니다 – Jiminion

+0

"Enter a pair ..."후 보드 매치를 사용해야합니다. "다른 쌍을 입력 ....". 그게 당신의 주요 문제입니다. – Jiminion

답변

0

보드의 다른 어레이가 필요합니다.
각 셀에 대해 '찾음'또는 '전환됨'을 의미하는 비트가 있습니다.
보드 (원래 보드)를 사용하여 보드를 표시하십시오.
발견되었거나 바뀐 셀만 표시하십시오.

플레이어가 돌아 왔을 때, 일부 셀이 되돌아 왔을 때 어떤 셀이 돌아 왔는지 기억하기 만하면 되돌리기/뒤집기로 되돌릴 수 있습니다. 이 배열은 모두 턴테이블로 시작하고 게임이 모두 돌아 왔을 때 게임이 끝납니다.

는 (당신은뿐만 아니라 모두 하나 개의 배열에 넣고하는 구조체를 사용할 수 있습니다.)

+0

어떻게 그것을 시각화 할 수, 난 단지 그것을 구현할 수 없습니다 ... – ace

+1

당신은 아스키 데이터를 저장할 계획이 있다면 두 번째 배열이 필요하지 않습니다; 당신은 플래그에 대해 MSB를 사용할 수 있습니다. –

+0

Alex, 예. 그는 이미 다른 ar 레이. 그는 다만 그것을 조금 더 잘 검사 할 필요가있다. 그러나 그렇습니다. 당신의 아이디어는 훌륭하고 유효합니다. – Jiminion

0

문제는 (이미 적용 FIX)이 라인에있다.

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

이전에 일치하는 경우 올바른 값이 표시되어야합니다. 아니 row2 아래에있는 사람, column2

또한, 여기 당신이 무슨 일을하는지에 대한 대안은 row_oldcolumn_oldrowcolumn의 이전 값을 저장합니다.

#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 일 때마다 이전의 일치가 없었 음을 의미하고 값을 저장할 때 이전 일치를 나타냅니다. "... 당신은 항상 boardcheck을해야

#import <stdbool.h> 

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

typedef struct 
{ 
    int row, col; 
}coordinate; 
+0

hmmm, ok im gona 시도해주세요. – ace

+0

메신저 아직도 이걸 얻지 못했습니다 ... – ace

+0

@ace 무엇이 문제입니까? – Can

관련 문제