このコードは 2 人のプレイヤー用です。コンピューターとユーザーの 2 人で、全員がサイコロを 2 回投げてから関数が処理を行います。ゲーム1 =>より高い合計を獲得する人。勝つ。ゲーム 2 => より低い diff.wins を取得する人。ゲーム 3 => 2 つの同じ番号を最初に取得した人が勝ちます。ゲーム 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");
}