0

此代码适用于两个玩家 - 计算机 vs 用户,每个人都掷骰子两次,然后函数完成工作。游戏 1 => 获得更高总和的人。获胜。游戏 2=> 获得较低 diff.wins 的人。游戏 3 => 先得到两个相同数字的人获胜。游戏 4 => 在这个阶段不相关。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
int throwDice(); //helping function - throwing a dice
int diceSum();//game 1
int diceDiff();//game 2
int firstToDouble();//game 3
int polynomialDice();//game 4
void WLT(int x);//helping function - printing - win lose or tie
int main()
{
char g;
printf("Please choose a game to play:\n1-diceSum-to play press 1.\n2-diceDiff-to play press 2.\n3-firstToDouble-to play press 3.\n4-first-to-Double-to play press p. \n");
do
{
    scanf("%c",&g);//input for the g
    if(g=='1')
        WLT(diceSum());
    else if(g=='2')
        WLT(diceDiff());
    else if(g=='3')
        WLT(firstToDouble());
    else if(g=='p')
        printf("The polynomial's result is:  %d\n",polynomialDice());
    else if(g=='E')
        break;
    else
        break;
    scanf("%c",&g);
 }while(g>'0' || g<'4'||g=='p');//good input !
 }
 int throwDice()//helping function - input- numbers from 1 to 6 randomly.
  {
 int value;
 srand(time(NULL));
 value=(rand() % 6) + 1; // numbers from 1 to 6 
 return value;
  }
  int diceSum()//Q1
  { 
 int x,y,z,w;
 //two numbers for the user.
 x=throwDice();
 y=throwDice();
 //two numbers for the computer.
 z=throwDice();
 w=throwDice();
 printf("You got %d and %d.\n",x,y);
 printf("Your opponent got %d and %d.\n",z,w);
 if(x+y>z+w)//The user wins
    return 1;
 else if(x+y<z+w)//The computer wins
    return -1;
 else//A tie
    return 0;
  }
  int diceDiff()//Q2
  {
 int x,y,z,w;
 //two numbers for the user.
 x=throwDice();
 y=throwDice();
 //two numbers for the computer.
 z=throwDice();
 w=throwDice();
 printf("You got %d and %d.\n",x,y);
 printf("Your opponent got %d and %d.\n",z,w);
 if(abs(x-y)>abs(z-w))//computer wins 
    return -1;
 else if(abs(x-y)<abs(z-w))//user wins
    return 1;
 else
    return 0;//tie
  }
  int firstToDouble()//Q3
  {
int x,y,z,w;
//two numbers for the user.
x=throwDice();
y=throwDice();
//two numbers for the computer.
z=throwDice();
w=throwDice();
printf("You got %d and %d.\n",x,y);
printf("Your opponent got %d and %d.\n",z,w);
if((x==y)&&(z==w))//if its a tie
    return 0;
else if(x==y)//if the user gets same numbers
    return 1;
else if(z==w)//if the computer got same numbers
    return -1;
else
    return firstToDouble();//if neither the computer nor the user gets same num
   }
   int polynomialDice()//Q-cyber
   {
int a,b,c,d,e,f;
a=throwDice();
b=throwDice();
c=throwDice();
d=throwDice();
e=throwDice();
f=throwDice();
printf("%d*%d^4+%d*%d^3+%d*%d^2+%d*%d^1+%d*%d^0\n",a,f,b,f,c,f,d,f,e,f);
return a*pow(f,4)+b*pow(f,3)+c*pow(f,2)+d*pow(f,1)+e*pow(f,0);
   }
   void WLT(int x)//helping function (it prints win,lose or tie)
   {
if(x==1)
    printf("You Won!\n");
else if(x==0)
        printf("It's a tie!\n");
else if(x==-1)
        printf("You Lost!\n");
   }

问题的图片

4

3 回答 3

1

你的问题在这里

int throwDice()//helping function - input- numbers from 1 to 6 randomly.
{
    int value;
    srand(time(NULL));
    value=(rand() % 6) + 1; // numbers from 1 to 6 
    return value;
}

你每次都播种RNG。当您单步执行代码时,随着F10时间的推移,您会得到新的数字。当您在没有调试ctrl+F5的情况下运行时,代码运行速度太快而无法更改,并且自从您重新播种 RNG 以来,您一直得到相同的数字。

要解决这个问题,请在程序启动或类似的情况下播种 RNG,然后再使用它。

于 2013-11-11T14:58:10.137 回答
0

您的 scanf 语句不正确,因为有额外的换行符没有以正确的方式使用。通常最好使用gets读入一个char数组缓冲区然后sscanf这个

要理解这一点,请编译并运行此示例并尝试输入 a 然后 b 然后 c

#include <stdio.h>

main() {

    char c1, c2, c3;
    scanf("%c", &c1);
    scanf("%c", &c2);
    scanf("%c", &c3);

    printf("here's the output\n");
    printf ("1 %c\n", c1);
    printf ("2 %c\n", c2);
    printf ("3 %c\n", c3);

}

这是与 fgets 相同的程序(sukhvir 是对的)

#include <stdio.h>
#define SIZE 80

main() {

    char c1, c2, c3;
    char buff[SIZE];
    fgets(buff, SIZE, stdin);
    sscanf(buff,"%c", &c1);
    fgets(buff, SIZE, stdin);
    sscanf(buff,"%c", &c2);
    fgets(buff, SIZE, stdin);
    sscanf(buff,"%c", &c3);


    printf("here's the output\n");
    printf ("1 %c\n", c1);
    printf ("2 %c\n", c2);
    printf ("3 %c\n", c3);

}
于 2013-11-11T09:45:10.683 回答
0

问题是:随机给出相同的值。srand(time(NULL)) - 应该在主函数中 `void main() { srand(time(NULL)) ... } 而不是在帮助函数 throwDice() 中,该函数的正确形式是:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
int throwDice(); //throwing a dice
int diceSum();//game 1
int diceDiff();//game 2
int firstToDouble();//game 3
int polynomialDice();//cyber
void WLT(int x);//win-lose-tie
void main()
{
    char g;
    srand(time(NULL));//the line we moved form throwDice()<--this was the problem.
    printf("Please choose a game to play:\n1-diceSum-to play press 1.\n2-diceDiff-to play press 2.\n3-firstToDouble-to play press 3.\n4-first-to-Double-to play press p. \n");
    do
    {
        scanf("%c",&g);//input
        if(g=='1')
            WLT(diceSum());
        else if(g=='2')
            WLT(diceDiff());
        else if(g=='3')
            WLT(firstToDouble());
        else if(g=='p')
            printf("The polynomial's result is:  %d\n",polynomialDice());
        else if(g=='E')
            break;
        else
            break;
        scanf("%c",&g);
    }
    while(g>'0'||g<'4'||g=='p');//right input
}
int throwDice()//helping function - input- numbers from 1 to 6 randomly.
{
    int value;
    value=(rand() % 6) + 1; // numbers from 1 to 6 
    return value;
}
int diceSum()//Q1
{
    int x,y,z,w;
    //two numbers for the user.
    x=throwDice();
    y=throwDice();
    //two numbers for the computer.
    z=throwDice();
    w=throwDice();
    printf("You got %d and %d.\n",x,y);
    printf("Your opponent got %d and %d.\n",z,w);
    if(x+y>z+w)//The user wins
        return 1;
    else if(x+y<z+w)//The computer wins
        return -1;
    else//A tie
        return 0;
}
int diceDiff()//Q2
{
    int x,y,z,w;
    //two numbers for the user.
    x=throwDice();
    y=throwDice();
    //two numbers for the computer.
    z=throwDice();
    w=throwDice();
    printf("You got %d and %d.\n",x,y);
    printf("Your opponent got %d and %d.\n",z,w);
    if(abs(x-y)>abs(z-w))//computer wins 
        return -1;
    else if(abs(x-y)<abs(z-w))//user wins
        return 1;
    else
        return 0;//tie
}
int firstToDouble()//Q3
{
    int x,y,z,w;
    //two numbers for the user.
    x=throwDice();
    y=throwDice();
    //two numbers for the computer.
    z=throwDice();
    w=throwDice();
    printf("You got %d and %d.\n",x,y);
    printf("Your opponent got %d and %d.\n",z,w);
    if((x==y)&&(z==w))//if tie
        return 0;
    else if(x==y)//user wins
        return 1;
    else if(z==w)//computer wins
        return -1;
    else
        return firstToDouble();//no body wins in this round
}
int polynomialDice()//Q-cyber
{
    int a,b,c,d,e,f;
    a=throwDice();
    b=throwDice();
    c=throwDice();
    d=throwDice();
    e=throwDice();
    f=throwDice();
    printf("%d*%d^4+%d*%d^3+%d*%d^2+%d*%d^1+%d*%d^0\n",a,f,b,f,c,f,d,f,e,f);
    return a*pow(f,4)+b*pow(f,3)+c*pow(f,2)+d*pow(f,1)+e*pow(f,0);
}
void WLT(int x)//helping function (it prints win,lose or tie)
{
    if(x==1)
        printf("You Won!\n");
    else if(x==0)
            printf("It's a tie!\n");
    else if(x==-1)
            printf("You Lost!\n");
}
于 2013-11-11T18:11:01.707 回答