我正在编写客户端服务器的网络编程项目Rock Paper Scissors代码。我完成了代码,在测试过程中它运行良好,然后将此行添加到代码中。带分段的客户端服务器代码错误错误
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个(岩纸或剪刀)
findwinner():
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);
}
}
现在可能是学习如何使用调试器的好时机。 – melpomene
请提供findWinner函数代码以及pcChoice的类型和用法信息。 – Jay
我编辑了问题 – femchi