2013-06-08 2 views
-1

나는 간단한 tic tac toe 게임을 만들고있다. 나는 사용자의 선택을 저장하는 문제에 봉착했다. 기본적으로 크기가 12 인 문자 배열을 가진 userBoard라는 구조체가 있습니다. 각 위치에 번호가 매겨진 보드가 사용자에게 표시됩니다. 그런 다음 사용자는 자신의 캐릭터를 넣을 위치를 선택해야합니다. 사용자의 캐릭터 (X 또는 O)는 무작위로 할당됩니다. 사용자가 숫자를 입력하면 updateUserBoard라는 함수로 전달됩니다. 그런 다음 updateUserBoard는 사용자가 선택한 위치에 사용자의 캐릭터를 배치합니다. 지금 내가 가진 문제는 배열의 한 위치에만 사용자 문자를 넣는 대신 사용자 문자로 전체 배열을 채 웁니다. 코드는 다음과 같습니다.문자 배열의 특정 지점에 문자를 넣는 방법은 무엇입니까?

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

void printBoard(void); 
void updateUserBoard(int location, char userCharacter, char computerCharacter); 

struct UserTicTacToeBoard { 
    char user[12]; // A user character array to store the users character. 
}; 

struct ComputerTicTacToeBoard { 
    char comp[12]; 
}; 

typedef struct UserTicTacToeBoard UserTicTacToeBoard; 
typedef struct ComputerTicTacToeBoard ComputerTicTacToeBoard; 

UserTicTacToeBoard userBoard; 
ComputerTicTacToeBoard compBoard; 

int main(int argc, char const *argv[]) 
{ 
    printf("Welcome to a game of tic tac toe.\n"); 
    printf("Let's see if you're smarter than the computer.\n"); 
    char computerCharacter, userCharacter; 
    /* Getting a random number needs some fixing! */ 
    int assignRandom = (rand() % 10000)/10000.0; 
    int userDecision; 
    switch(assignRandom) { 
     case 1: 
     strcpy(&userCharacter, "O"); 
     strcpy(&computerCharacter, "X"); 
     break; 

     default: 
     strcpy(&userCharacter, "X"); 
     strcpy(&computerCharacter, "O"); 
     break; 
    } 

    printf("Computer gets %c\n", computerCharacter); 
    printf("User gets %c\n", userCharacter); 
    printBoard(); 

    for(int i = 0; i <= 12; i++) { 
     userBoard.user[i] = computerCharacter; 
     compBoard.comp[i] = userCharacter; 
    } 

    while(1) { 
    printf("\nLadies first.\n"); 
    printf("Please enter a number from the table above which you would like to replace with\nNumber: "); 
    // Use some other function here instead of scanf. If the user types anything other than an int, 
    // scanf goes into this crazy loop. 
    scanf("%d", &userDecision); 
    updateUserBoard(userDecision, userCharacter, computerCharacter); 
    } 
    return 0; 
} 

void printBoard(void) 
{ 
    printf(" 0 | 1 | 2 | 3 |\n"); 
    printf("--------------------\n"); 
    printf(" 4 | 5 | 6 | 7 |\n"); 
    printf("--------------------\n"); 
    printf(" 8 | 9 | 10 | 11 |\n"); 
} 

void updateUserBoard(int location, char userCharacter, char computerCharacter) 
{ 
    if (location > 11) { 
     printf("ERROR: PUSSY DETECTED. GROW A PAIR.\n"); 
     exit(1); 
    } 

    userBoard.user[location] = userCharacter; // Here instead of putting the user's character to userBoard.user[location], it fills the whole array with the users location 

    printf(" %c | %c | %c | %c |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]); 
    printf("------------------------\n"); 
    printf(" %c | %c | %c | %c |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]); 
    printf("------------------------\n"); 
    printf(" %c | %c | %c | %c |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]); 
    printf("------------------------\n"); 
    printf(" %c | %c | %c | %c |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]); 
} 
+0

잘못된'의 strcpy (userCharacter, "O");', 단순히'userCharacter = 'O',' – BLUEPIXY

+0

이 때문에'단지 문자열을 strcpy'. 그래서 문자들에 대해서 나는 그들에게 평범한 방법으로 값을 할당 할 수 있습니다. 내가 맞습니까? –

+0

'strcpy (& userCharacter, "O");',이 코드는 예상치 못한 메모리를 파괴합니다. – BLUEPIXY

답변

0

변경 updateUserBoard 함수의 결말을 인쇄해야한다고 가정

일부 메모를

  • 찾는 루프 코드를 만든다 짧아. 그 내용은 12 (= 보드 너비 * 보드 높이) 시간에 실행됩니다.
  • 루프의 "if"블록은 한 줄의 모든 셀을 그릴 때 실행됩니다 (반복자 i은 0보다 크고 행 너비의 모듈러스 (즉, 다음 행을 그리는 시간입니다)). 행이 변경되는 곳입니다.

코드 자체 :

for(int i = 0; i < 12; i++) 
{ 
    printf("%c |", userBoard.user[i]); 
    if(i%4 == 0 && i > 0) 
    { 
     printf("\n------------------------\n"); 
    } 
} 
+0

고마워요! 코드가 작동했습니다. :) –

2

당신은 당신의 출력 코드에 문제가있다 : 당신의 printf들 (즉, 같은 문자를 16 번 인쇄) 16 개 셀에 대한 userBoard.user[location]를 사용합니다.

이러한 종류의 오류는 '복사 & 붙여 넣기 프로그래밍'에서 보통 발생하는 것입니다. 루프를 사용하십시오.

그리고 난 당신이 12 개 세포가 아닌 이런 일에 16

+0

헤드 업에 대한 감사합니다! :) –

관련 문제