나는 간단한 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]);
}
잘못된'의 strcpy (userCharacter, "O");', 단순히'userCharacter = 'O',' – BLUEPIXY
이 때문에'단지 문자열을 strcpy'. 그래서 문자들에 대해서 나는 그들에게 평범한 방법으로 값을 할당 할 수 있습니다. 내가 맞습니까? –
'strcpy (& userCharacter, "O");',이 코드는 예상치 못한 메모리를 파괴합니다. – BLUEPIXY