C 如何在字符数组中的特定点放置字符?
我正在做一个简单的井字游戏。我在存储用户的选择时遇到问题。 我基本上有一个名为userBoard的结构,它有一个大小为12的字符数组。将向用户展示一块标有每个位置编号的板。然后用户必须选择一个位置来放置他们的角色。用户的字符X或O将随机分配给他们。当用户键入一个数字时,它被传递给一个名为updateUserBoard的函数。updateUserBoard然后将用户的角色放置到用户选择的任何位置。我现在遇到的问题是,它不是将users字符只放在数组的一个位置,而是用users字符填充整个数组。代码如下C 如何在字符数组中的特定点放置字符?,c,C,我正在做一个简单的井字游戏。我在存储用户的选择时遇到问题。 我基本上有一个名为userBoard的结构,它有一个大小为12的字符数组。将向用户展示一块标有每个位置编号的板。然后用户必须选择一个位置来放置他们的角色。用户的字符X或O将随机分配给他们。当用户键入一个数字时,它被传递给一个名为updateUserBoard的函数。updateUserBoard然后将用户的角色放置到用户选择的任何位置。我现在遇到的问题是,它不是将users字符只放在数组的一个位置,而是用users字符填充整个数组。代码
#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]);
}
您的输出代码有问题:您的printfs对所有16个单元格使用userBoard.user[location],也就是说,您打印了16次相同的字符 这种错误是“复制粘贴编程”通常会遇到的。使用循环
我想您应该打印12个单元格,而不是16个。将updateUserBoard函数的结尾更改为如下内容: 一些注释 For循环使代码更短。其内容为运行12=板宽*板高倍 循环中的if块在一行中的所有单元格都已绘制时执行,迭代器i大于零,其和行宽度的模数为零,即绘制下一行的时间到了。这是行更改的地方。 守则本身:
for(int i = 0; i < 12; i++)
{
printf("%c |", userBoard.user[i]);
if(i%4 == 0 && i > 0)
{
printf("\n------------------------\n");
}
}
strcpy和userCharacter错误,O,简单的userCharacter='O';所以strcpy只适用于字符串。因此,对于字符,我可以用正常的方式给它们赋值。我说的对吗?case strcpy&userCharacter,O;,此代码将破坏意外事件的内存。我真的不理解你。。。为什么它会破坏意外事件的内存?O是{'O','\0'}2字符。但是userCharacter是一个字符区域。strcpy不复制包括“\0”。