gpt4 book ai didi

c - 如何在C中标记要选择的字符

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

这是迄今为止我的刽子手游戏代码。我有几个问题,其中一些是如何将所选字母标记为“已选择”,以便当用户选择另一个字母时,无法使用他已经使用过的字母。而且,我有一个失败条件,如果满足,游戏应该中断并显示一条消息,但它永远不会中断。

我已经尝试创建另一个数组,我将在其中推送所有已选择的字符。但是,它不起作用,因为我收到错误,因为我无法将字符放在随机位置,因为之前的插槽不会被填充。

这是我的主要方法:

int main() {
srand(time(NULL));
// the randomly selected word
char *word = generateStrings();
char sampleArray[strlen(word)];
sampleArray[strlen(word)] = '\0';
for (int i = 0; i < strlen(word); i++) {
sampleArray[i] = '_';
}
printf("%s\n", word);
char *printedWord = printWord(sampleArray, word);
return 0;
}

这是基本上完成所有逻辑的方法:

char *printWord(char sampleArray[], char *word) {
char letter;
int stopLoop=0;
int x = 0;
int winningSpree = 0;
int lost = 0;

while (1) {

system("@cls||clear"); // clear cmd window every itereation of the loop
printf("%s\n", word);

if (winningSpree==strlen(word)) {
printf("Congratulations! You've won!\n");
printf("%s", sampleArray);
break;
}
if (lost == 6) {
printf("You lost!\n");
printf("The word was: ", word, "\n");
break;
}
if (x == 0)
for (int i = 0; i < strlen(word); i++)
printf("_");

if (x > 0)
printf("Current stage of word: %s", sampleArray);

printf("\n");
printf("Give me a letter: ");
letter = askforinput();
printf("\n");

for (int j = 0; j < strlen(word); j++) {
if (word[j] == letter) {
sampleArray[j] = letter;
winningSpree++;
} else {
lost ++;
}
}
x++;
}
printf("\n");
return sampleArray;
}

这是要求输入的方法:

// ask user for character input
char askforinput() {
char chosenLetter;
scanf("%c", &chosenLetter);
return chosenLetter;
}

最佳答案

要将选定的字母标记为“SELECTED”,您可以实现以下代码:-

int count=0; //for keeping track of how many letters the user has selected.
int maxnumberofinputs=10;//for maximum number of times user can guess.
char selected[maxnumberofinputs]; //the selected letters will go here.

char letter=askforinput();

//when user selects a letter
if(count<maxnumberofinputs)
{
selected[count]=letter;
count++;
}

关于c - 如何在C中标记要选择的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55327122/

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