gpt4 book ai didi

c - 如何将字符放在字符数组中的特定点?

转载 作者:行者123 更新时间:2023-11-30 18:58:22 25 4
gpt4 key购买 nike

我正在制作一个简单的井字游戏。我在存储用户的选择时遇到了问题。我基本上有一个名为 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]);
}

最佳答案

您的输出代码存在问题:您的 printf 对所有 16 个单元格使用 userBoard.user[location] (即您打印相同的字符 16 次) )。

这种错误是“复制粘贴编程”时通常会遇到的错误。使用循环。

我想你应该打印 12 个单元格,而不是 16 个。

关于c - 如何将字符放在字符数组中的特定点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16997982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com