
- 15th Jul 2022
- 15:19 pm
The provided code appears to be a C program implementing the game of Yahtzee. It includes functions to initialize dice, play the game, display dice, reset keep values, roll dice, display game menu, roll a die, display game rules, and clear the screen.
The Yahtzee game involves rolling five dice and trying to obtain specific combinations of values to score points. Players have multiple turns to roll and re-roll the dice to achieve the desired combinations and score the maximum points.
#include //preprocessor command to include header file stdio.h #include //preprocessor command to include header file stdlib.h #include //preprocessor command to include header file time.h int RULES=1; // initialize global variable RULES=1 int GAME=2;// initialize global variable GAME=2 int EXIT=3;// initialize global variable EXIT=3 int ROLLS=4;// initialize global variable ROLLS=4 int CATEGORIES=13;// initialize global variable CATEGORIES=13 int DICE=5;// initialize global variable DICE=5 int ONE=1;// initialize global variable ONE=1 int TWO=2;// initialize global variable TWO=2 int THREE=3;// initialize global variable THREE=3 int FOUR=4;// initialize global variable FOUR=4 int FIVE=5;// initialize global variable FIVE=5 int SIX=6;// initialize global variable SIX=6 int THREEKIND=7;// initialize global variable THREEKIND=7 int FOURKIND=8;// initialize global variable FOURKIND=8 int FULLHOUSE=9;// initialize global variable FULLHOUSE=9 int SMSTRAIGHT=10;// initialize global variable SMSTRAIGHT=10 int LGSTRAIGHT=11;// initialize global variable LGSTRAIGHT=11 int YAHTZEE=12;// initialize global variable YAHTZEE=12 int CHANCE=13;// initialize global variable CHANCE=13 int TURNS=13;// initialize global variable TURNS=13 void initializeDice(int dice[DICE]);// function declaration function name : initializeDice void playGame();// function declaration function name : playGame void displayDice(int dice[DICE]);// function declaration function name : displayDice void resetKeep(int keep[DICE]);// function declaration function name : resetKeep void rollDice(int dice[DICE],int keep[DICE]);// function declaration function name : rollDice int displayGameMenu();// function declaration function name : displayGameMenu int rollDie();// function declaration function name : rollDie void gameRules();// function declaration function name : gameRules, return type : void, parameter : no parameters passed void clearScreen();// function declaration function name : clearScreen, return type : void, parameter : no parameters passed int main()// main function with return type int { int play=1;//initialize play=1 srand(time(0));//call seed function while (play==1)//check whether play is equal to 1 or not { int disp=displayGameMenu();// call displayGameMenu function switch(disp)//switch case { case 1: gameRules();//call gameRules function break; case 2: clearScreen();//call clearScreen function playGame();//call playGame function break; case 3: printf("\nThank you for playing!"); play=0;// set play=0 break; default: printf("\nIncorrect option, hit enter and try again"); char enter; fflush(stdin);//flush the input scanf("%c",&enter); break; } } return 0;// return type of main function that is int } void initializeDice(int dice[DICE])// function definition of initializeDice() { int die;//declare die variable for(die=0;die { dice[die]=0;//set dice[die]=0 } } void playGame()// function definition of playGame() { int roll,turn;//declare roll,turn variable int dice[DICE],keep[DICE];//declare dice,keep array of size DICE for(roll=0;roll { int current=roll;//initialize current =roll printf("\n Turn %d of game",current+1);//print turn of game initializeDice(dice);//call initializeDice(dice) function for(turn=0;turn { resetKeep(keep);//call resetKeep(keep) function printf("\n Rolling the dice...\n");//print rolling the dice rollDice(dice,keep);//call rollDice(dice,keep) function displayDice(dice);//call displayDice function } } } void displayDice(int dice[DICE])// function definition of displayDice() { int die;//declare die char enter;//declare enter printf("+-------+ +-------+ +-------+ +-------+ +-------+ \n"); printf("| | | | | | | | | | \n"); for(die=0;die { printf("| %d | ",dice[die]);//print values of dice } printf("\n"); printf("| | | | | | | | | | \n"); printf("+-------+ +-------+ +-------+ +-------+ +-------+ \n"); scanf("%c",&enter);//wait for user input } void resetKeep(int keep[DICE])// function definition of resetKeep() { int die;//declare die for(die=0;die { keep[die]=0;//set keep[die]=0 } } void rollDice(int dice[DICE],int keep[DICE])// function definition of rollDice() { int die;//declare die for(die=0;die { if(keep[die]==0) { dice[die]=rollDie();//call rollDie function and store its value in dice[die] } } } int displayGameMenu()// function definition of displayGameMenu() { int sheet=0;//initialize sheet =0 do { printf("\n%d. Display game rules",RULES); printf("\n%d. Start the game of yahtzee",GAME); printf("\n%d. Exit\n",EXIT); scanf("%d",&sheet);//get the user input }while(sheet<0 || sheet>4);//check whether user enter b/w 1 - 3 or not, if it's not repeat the loop. return sheet;//return value of sheet } int rollDie()// function definition of rollDie() { int dieValue=0;//initialize dieValue =0 dieValue=1+(rand()%6);//generate random number between 1 and 6 return dieValue;//return dievalue } void gameRules()// function definition of gameRules() { // print the rules of the game using a series of printf() statements with \n used for new lines printf("\nRULES OF THE CAME:"); printf("\n\t1. The scorecard used for Yahtlee is composed of an upper section and a lower section."); printf("\n\t2. A total of 13 scoring combinations are divided amongst the sections."); printf("\n\t3. The upper action consists of boxes that are scored by summing the value of tho dice notching the faces of t box."); printf("\n\t4. If a player rolls four 3's, then the score placed in the 5's box is the sum of the dice which is 12."); printf("\n\t5. Once a player has chosen to score a box, it may not be changed and the combination is no longer in play for uture rounds."); printf("\n\t6. If the sun of the scores in the upper section is greater than or equal to 63, then 35 more points are added to the players overall score as a bonus. The lower section contains a number of poker like combinations."); } void clearScreen()// function definition of clearScreen() { printf("\n Hit To Continue! ");// this statement is used to display the text on the screen char c;// declaring a variable of type character fflush(stdin); scanf("%c",&c);// storing the input given by user in c system("cls");// calling function system with "cls" passed as argument as it clears the screen return; }
Note: Before running this code, ensure that the necessary header files (stdio.h
, stdlib.h
, and time.h
) are included at the beginning of the program. Additionally, make sure to provide implementations for any missing functions like displayGameMenu
, initializeDice
, resetKeep
, etc., as they are declared but not defined in the given code.
If you have any specific questions or need further assistance with the code or any other aspect of programming, feel free to ask our programming assignment help experts.