gpt4 book ai didi

通过在函数中传递字符串来创建获胜条件

转载 作者:行者123 更新时间:2023-11-30 14:52:02 25 4
gpt4 key购买 nike

每当我编译代码时,无论传递什么整数,它都会打印所有可能的场景。有关如何解决此问题以及为什么会发生这种情况的任何帮助吗?

#include <stdio.h>
void *get_plays(char *player1, char *player2)
{
printf ("Player 1 chooses to play: ");
scanf("%s", player1);
printf ("Player 2 chooses to play: ");
scanf("%s", player2);
return player1;
return player2;
}
int *check_for_winner(char *player1, char *player2, int condition)
{

if (player1==player2){
condition = 0;
}
else if(player1=="rock" && player2 == "scissors"){
condition = 1;
}
else if(player1=="paper" && player2 == "scissors"){
condition = 2;
}
else if(player1=="rock" && player2 == "paper"){
condition = 2;
}
else if(player1=="paper" && player2 == "rock"){
condition = 1;
}
else if(player1=="scissors" && player2 == "papper"){
condition = 1;
}
else (player1=="scissors" && player2 == "rock");{
condition = 1;
}
}
void *print_plays(char *player1, char *player2)
{
printf("Player 1 has played %s \n",player1);
printf("Player 2 has played %s \n",player2);
}
void *print_winner(int condition)
{
if (condition = 0){
printf("It's a tie\n");
}
else if (condition = 1){
printf("Player 1 wins\n");
}
else (condition = 2);{
printf("Player 2 wins\n");
}
}
int main(void)
{
char player1[10];
char player2[10];
int win;
get_plays(player1, player2);
check_for_winner(player1, player2, win);
print_plays(player1, player2);
print_winner(win);
return;
}

当我编译时,它会打印“玩家 1 获胜”和“玩家 2 获胜”,不知道为什么或如何修复它,甚至不知道为什么会发生。我对 C 还很陌生,所以任何帮助将不胜感激。谢谢。

最佳答案

#include <stdio.h>
#include <string.h>

void
get_plays (char *choice1, char *choice2)
{
printf ("Choices: 'paper' - 'rock' - 'scissors'\n");
printf ("Player 1 chooses to play: ");
scanf ("%s", choice1);

printf ("Player 2 chooses to play: ");
scanf ("%s", choice2);
}

int
check_for_winner (const char *choice1, const char *choice2)
{
// paper wins rock but scissors does not and equals itself
if (strcmp (choice1, "paper") == 0)
{
if (strcmp (choice2, "rock") == 0)
return 1;

if (strcmp (choice2, "scissors") == 0)
return 2;

return 0;
}

// rock wins scissors but paper does not and equals itself
if (strcmp (choice1, "rock") == 0)
{
if (strcmp (choice2, "scissors") == 0)
return 1;

if (strcmp (choice2, "paper") == 0)
return 2;

return 0;
}

// scissors wins paper but rock does not and equals itself
if (strcmp (choice1, "scissors") == 0)
{
if (strcmp (choice2, "paper") == 0)
return 1;

if (strcmp (choice2, "rock") == 0)
return 2;

return 0;
}

return 0;
}

int main ()
{
char choice1 [10];
char choice2 [10];
int winner;
char q;

char *result[3] = {
"No one is the winner",
"Player 1 is the winner!!!",
"Player 2 is the winner!!!"
};

printf ("Choose 'q' to exit\n");
printf ("------------------------------------------------------------\n");
while (q != 'q')
{
get_plays (&choice1[0], &choice2[0]);
winner = check_for_winner (choice1, choice2);

printf ("------------------------------------------------------------\n");
printf ("The result: %s\n", result[winner]);
printf ("------------------------------------------------------------\n");
getc (stdin);
scanf ("%c", &q);
}
return 0;
}

希望代码对您有所帮助!

关于通过在函数中传递字符串来创建获胜条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47804746/

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