我正在用C语言编写大学代码,涉及游戏rock(0)-paper(1)-scissor(2)-ecshe(3)。对于两个玩家,游戏均应使用随机数运行。最后,我必须统计一个玩家赢得的频率,以及摇滚(0)-paper(1)-scissor(2)-ecshe(3)获胜的频率。当我编写一个代码时,我可以获得所有信息。但是当我使用一个函数时,我只能得到零来计算静态数据。
Player_1的值在每一回合中始终设置为rock(1)。该函数用于计算Player_2获得纸张(2)或剪刀(3)的次数]
简而言之:如何根据情况创建一个函数使我返回可能的答案?
int gewinn_summe_a(int spl_2, int a, int b , int c, int d){
if(spl_2== a){
return ++b; //the counter for a variable that i have in the main function
}else if(spl_2==c){
return ++d;
}
// return 0;
if (player1 == 0) //this if set the value for player 1
if (player2 == 1 || player2 == 3){ //this 'if' says that the player2 can have this two option
//When the code below inside the program it does what is suppose to do:
//calculate how many time player2 gave option 1 or how many time he gave option3
if(spl_b==1){
count_sh_b++;
}else if(spl_b==3){
count_ec_b++;
}
//However, when i use the function, I always get zero as the counter.
gewinn_summe_a(1, count_sh_b , count_ec_b, spl_b)
printf("Contador SH B %d\n", count_sh_b);
printf("Contador EC B %d\n", count_ec_b);
printf("Spieler A: %d - %s\n", taste ,spiel_name[taste]);
printf("Spieler B: %d - %s\n", spl_b ,spiel_name[spl_b]);
printf("Player 1 A WON\n\n");
您需要通过计数器通过引用,如下所示:
int gewinn_summe_a(int spl_2, int a, int *b, int c, int *d)
{
// ...
}
然后您通过提供调用者变量的地址来调用它:
gewinn_summe_a(spl_b, 1, &count_sh_b, 3, &count_ec_b)
顺便说一句,您的源代码很糟糕,因为它不完整,缩进不好,并且变量名被盲目选择。