Topics: UNIX and C Programming Game Specification: Write a C program that allows
ID: 3828655 • Letter: T
Question
Topics: UNIX and C Programming
Game Specification:
Write a C program that allows a single Player (the user) to play a simple three dice game of chance against "The Odds".
There is a single player, with three eight sided dice.
The sides of each die are labeled with the numbers from 1 to 8, we will call this the value of the die.
A game is made up of rounds, a single round is played as such:
The player rolls their three dice
The dice are displayed, in some reasonable format.
A determination is made as to whether or not the player won the round, this determination is made via the following rules:
A Triple is when all the dice have the same number on their top faces. If the player has any Triple then they win the round.
A Straight is when the numbers on the three dice faces can be arranged to form a consecutive sequence like 1 2 3 or 3 4 5 If the player has any Straight then they win the round.
A Pair is when any (exactly) two dice have the same number on their top faces. If the player has any Pair then they neither win nor lose the round.
A Junker is then anything that is not a Triple, a Straight or a Pair. If the player has any Junker then they lose the round.
The result of the round (with respect to the Player) is reported.
The player is asked if they wish to play another round.
Once the player indicates that they do not wish to play another round: Before exiting, the program displays a short report stating how many rounds were played, of those - how many were won and how many were lost.
Notes(s):
Make sure to include <stdio.h> and <stdlib.h>
Use (rand() % 8) + 1 to generate the die values.
Make sure to include <math.h> and <time.h>
If you want your game to play differently each time:
Use srand(time(NULL)); As the first line of your main() function.
You may find fmin(<a>, <b>) and fmax(<a>, <b>) useful for sorting the dice values.
You must also compile with the added -lm directive:
gcc -lm -Wall program10.c
Your .c file will be many lines long
Sample Run(s):
Welcome to Computer Dice
-------------------------------------
You will first roll your dice
Next the outcome of your roll will
be determined:
any Triple and you Win
any Straight and you Win
any Pair and you just roll again
anything else and you Lose
-------------------------------------
Player
----------
5 5 6
Pair - Its a push!
Do you wish to play again [y, n] : y
Player
----------
3 4 4
Pair - Its a push!
Do you wish to play again [y, n] : y
Player
----------
7 8 8
Pair - Its a push!
Do you wish to play again [y, n] : y
Player
----------
3 6 7
Junker - Sorry, you lose!
Do you wish to play again [y, n] : y
Player
----------
2 3 4
Straight - Congrats, you win!
Do you wish to play again [y, n] : n
Computer Dice Results
---------------------
You played 5 rounds
Rounds won : 1
Rounds lost : 1
---------------------
Explanation / Answer
/*Dice.c file*/
#include<stdio.h>
#include<ctype.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
int main()
{
/*Variable Declaration*/
long int n,iloop,count=0;
int arr[100],a,b,c,count_0=0,count_1=0;/*int arr i have used to store result in the form of -1, 1 & 0*/
char ch='y';
printf("Welcome to Computer Dice --------------------------- You will first roll your dice Next the outcome of your roll will be determined:");
printf(" any Triple and you Win any Straight and you Win any Pair and you just roll again anything else and you Lose ------------------");
printf(" Player ------------------ ");
do
{
a=((rand()%8)+1);/*store random number for each dice*/
b=((rand()%8)+1);
c=((rand()%8)+1);
printf(" %d %d %d",a,b,c);
if(a==b&&b==c&&a==c)/*Condition of Triple same*/
{
arr[count]=1;
printf(" Triple! Congrats You Win");
}
else if(a==b||b==c||a==c)/*If Both are same.*/
{
arr[count]=-1;
printf(" Pair-Its a Push!");
}
else if(a+1==b&&b+1==c)/*if its straight*/
{
arr[count]=1;
printf(" Straight-Congrats You win!");
}
else/*for junk*/
{
arr[count]=0;
printf(" Junker-Sorry, you lose!");
}
count++;
printf(" Do you wish to play again [y, n]:");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
for(iloop=0;iloop<count;iloop++)
{
if(arr[iloop]==0)
{
count_0=count_0+1;/*to find the loose and store it*/
}
if(arr[iloop]==1)
{
count_1=count_1+1;/*to find the win and store it*/
}
}
printf(" Computer Dice Results ---------------- You played %d rounds Rounds won : %d Rounds lost : %d",count,count_1,count_0);
return 1;
}//end of main
/*************OUTOUT***************************
Welcome to Computer Dice
---------------------------
You will first roll your dice
Next the outcome of your roll will
be determined:
any Triple and you Win
any Straight and you Win
any Pair and you just roll again
anything else and you Lose
------------------
Player
------------------
2 4 7
Junker-Sorry, you lose!
Do you wish to play again [y, n]:y
5 2 5
Pair-Its a Push!
Do you wish to play again [y, n]:y
7 7 3
Pair-Its a Push!
Do you wish to play again [y, n]:y
1 2 2
Pair-Its a Push!
Do you wish to play again [y, n]:y
2 4 2
Pair-Its a Push!
Do you wish to play again [y, n]:y
4 4 7
Pair-Its a Push!
Do you wish to play again [y, n]:y
4 5 8
Junker-Sorry, you lose!
Do you wish to play again [y, n]:n
Computer Dice Results
----------------
You played 7 rounds
Rounds won : 0
Rounds lost : 2
--------------------------------
Process exited after 9 seconds with return value 1
Press any key to continue . . .
**********************************************/
/*Hope you got all your requirement fulfilled.*/