我正在制作一个简单的井字游戏。我在存储用户的选择时遇到了问题。我基本上有一个名为 userBoard 的结构,它有一个大小为 12 的字符数组。用户将看到一个带有每个位置编号的板。然后用户必须选择一个位置来放置他们的角色。用户的角色(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]);
}