gpt4 book ai didi

带有段错误的客户端服务器代码

转载 作者:太空宇宙 更新时间:2023-11-04 02:59:32 25 4
gpt4 key购买 nike

<分区>

我正在从事一个网络编程项目,以编写客户端服务器剪刀石头布代码。在我将此行添加到代码之前,我完成了代码并且它在测试期间运行良好。

findWinner(gameType,pcChoice);

当我将这行代码添加到代码中时,代码开始给我一个关于服务器端段错误的错误。这是我添加该行的地方。

while(1)
{
int gameType;
printf("Paper, Scissors, Rock game start.\n");

rc = read(client_sockfd, &gameType, 1);
srand(time(NULL));
pcChoice = (rand() % 3)+1;
findWinner(gameType,pcChoice);
gameType = pcChoice;
write(client_sockfd, &gameType, 1);

}

我是 C 语言中的一个电枢,不知道该怎么做。

int pcChoice;

保留1到3之间的随机整数(石头布或剪刀)是一个整数

找到赢家():

void findWinner(int player,int pc)
{
const char *items[3]={"Paper","Scissors","Rock"};
printf("Client: %s\n",items[player-1]);
printf ("Computer: %s\n",items[pc-1]);

switch (player)
{
case 1:
switch (pc)
{
case 1:
printf("it is a DRAW\n");
break;
case 2:
printf("Computer Wins\n");
break;
case 3:
printf("Computer Loses\n");
break;
default:
printf("ERROR\n");
exit(0);
};
break;
case 2:
switch (pc)
{
case 1:
printf("Computer Loses\n");
break;
case 2:
printf("it is a DRAW\n");
break;
case 3:
printf("Computer Wins\n");
break;
default:
printf("ERROR\n");
exit(0);
};
break;
case 3:
switch (pc)
{
case 1:
printf("Computer Wins\n");
break;
case 2:
printf("Computer Loses\n");
break;
case 3:
printf("it is a draw\n");
break;
default:
printf("ERROR\n");
exit(0);
};
break;
default:
printf("ERROR\n");
exit(0);
}
}

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