Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is partially finished game, at this point there are 5 dice that are rolled. Add the following code: - There is a robot(apponent): the

This is partially finished game, at this point there are 5 dice that are rolled. Add the following code:

- There is a robot(apponent): the ai(robot) needs to roll the five dice and choose which dice, out of the five, that it will keep. It chooses the dice to save based on the odds of getting the highest score(5 of a kind, 6-2 streight, 5-1 streight, 6's, 5's, the higerarchy is in the code below), it then rolls the dice that are not saved. It compares all five dice again and saves the dice that have the highest odds, again for highest score. Repeat this for a third roll of the five dice.

- By the third roll the five dice need to be compared to the scoring higherarchy, that score is then applied to the robots score card, which is printed to the .txt file. This then passes the turn off to the user.

- The user then follows the same procedure only this time the user needs to see the whole proces throughout the 3 rolls and needs to be asked which dice to keep etc.

!!!NONE of the AI rolls or selections need to be printed to the screen only the final selection and the score need to be printed.!!!

- Please, make sure you are leaving notes in the code about what each line/function is doing.

example: int main () { //This line is dioing this thing here for this reason.

#include

#include

#include /*Defining my functions. */

int find(int x[], int a);

void score_1_to_6(int score[], int x[]);

int three_of_a_kind(int x[], int kind);

int four_of_a_kind(int x[], int kind);

void sort(int x[]);

int full_house(int x[]);

int main(void);

void change(int number1[], int number2[]);

int chance_sum(int x[]);

int small_straight(int x[]);

int large_straight(int x[]);

int chance_sum(int x[]);

int options(int taken[], int x[], int choice[]);

void read_score(void);

void roll_dice(int dice[]);

void move(int number1[], int number2[], int taken[], int turn);

enum names {Ones=0, Twos=1, Threes=2, Fours=3, Fives=4, Sixes=5, Three_of_a_Kind=6, Four_of_a_Kind=7, Full_House=8, Small_Straight=9, Large_Straight=10, Chance=11, Yahtzee=12} n;

int main(void)

{

int taken[14];

int turn=0;

srand (time(NULL));

int number1[]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* Wipes the score board clean. */

int number2[]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

change(number1, number2); /*Creates the clean file.*/

move(number1, number2, taken, turn);

read_score(); /*Reads the current score sheet*/

move(number1, number2, taken, turn+1);

read_score(); /*Reads the current score sheet*/

}

void move(int number1[], int number2[], int taken[], int turn) {

int choice[3];

int dice[5];

int a;

char space[100];

if (turn!=0) { /*This fgets is used for the return from the previous move*/

fgets(space,100, stdin);

}

printf( "Press enter to start your turn. " );

fgets(space,100, stdin);

for (a=0; a<3; a++) {

roll_dice(dice);

printf("You Rolled the Following: %d, %d, %d, "

"%d, %d ",dice[0], dice[1], dice[2], dice[3], dice[4]);

}

sort(dice); /*Sorts the dice values into ascending order.*/

options(taken,dice, choice); /*Determines what scoring options are allowed*/

number1[choice[0]]=choice[1];

taken[choice[0]]=1;

change(number1, number2); /*Adds the new score to the score sheet*/

}

void change(int number1[], int number2[]) { /*This function saves any score sheet changes*/

FILE *fptr;

int a;

char option[]= {"abcdefghijklm"};

fptr=fopen("Yahtzee_game.txt", "w");

for (a=0; a<13; a++) {

n=a;

switch (n) {

case Ones:

fprintf(fptr, "Ones\t");

break;

case Twos:

fprintf(fptr, "Twos\t");

break;

case Threes:

fprintf(fptr, "Threes\t");

break;

case Fours:

fprintf(fptr, "Fours\t");

break;

case Fives:

fprintf(fptr, "Fives\t");

break;

case Sixes:

fprintf(fptr, "Sixes\t");

break;

case Three_of_a_Kind:

fprintf(fptr, "Three_of_a_Kind");

break;

case Four_of_a_Kind:

fprintf(fptr, "Four_of_a_Kind");

break;

case Full_House:

fprintf(fptr, "Full_House");

break;

case Small_Straight:

fprintf(fptr, "Small_Straight");

break;

case Large_Straight:

fprintf(fptr, "Large_Straight");

break;

case Chance:

fprintf(fptr, "Chance\t");

break;

case Yahtzee:

fprintf(fptr, "Yahtzee\t");

break;

}

fprintf(fptr, "\t %d\t \t %d ", number1[a], number2[a]);

}

fclose(fptr);

}

void score_1_to_6(int score[], int x[]) { /*Determines the scores for any of the six values*/

int *num, value, found, number=0;

for (num=&score[0]; num<&score[6]; num++) {

number+=1;

found=find(x, number);

*num=found*number;

}

}

int three_of_a_kind(int x[], int kind) { /*Determines if there is a three of a kind*/

int total_score, same=0;

same=find(x, kind);

if (same>=3) {

total_score=x[0]+x[1]+x[2]+x[3]+x[4];

}

else {

total_score=0;

}

return total_score;

}

int four_of_a_kind(int x[], int kind) { /*Determines if there is a four of a kind*/

int total, same=0;

same=find(x, kind);

if (same>=4) {

total=x[0]+x[1]+x[2]+x[3]+x[4];

}

else {

total=0;

}

return total;

}

int find(int x[], int a) { /*Determines if a value is present in the matrix*/

int *z, found=0;

for (z=&x[0]; z<&x[6]; z++) {

if (a==*z) {

found+=1;

}

}

return found;

}

void sort(int x[]) { /*Sorts the dice values into ascending order.*/

int *i, *y, a, z, moves=1;

while (moves!=0) {

moves=0;

for (i=&x[1]; i<&x[6]; i++) {

y=i-1;

if (*y>*i) {

z=*y;

a=*i;

*y=a;

*i=z;

moves+=1;

}

}

}

}

int small_straight(int x[]) { /*Determines if there is a small straight*/

int ss, *y, *z, count=0;

for (y=&x[1]; y<&x[6]; y++) {

z=(y-1);

if (*z==*y-1) {

count=count+1;

}

else {

if (*z==*y) {

count=count;

}

else {

if (y!=x[4]) {

count=0;

}

}

}

}

if (count>=3) {

ss=30;

} else {

ss=0;

}

return ss;

}

int large_straight(int x[]) { /*Determines if there is a large straight*/

int ss, *y, *z, count=0;

for (y=&x[1]; y<&x[6]; y++) {

z=(y-1);

if (*z==*y-1) {

count=count+1;

}

else {

if (*z==*y) {

count=count;

}

else {

count=0;

}

}

}

if (count>=4) {

ss=40;

} else {

ss=0;

}

return ss;

}

int yahtzee(int x[]) { /*Determines if there is a yahtzee*/

int f, num, yahtzee=0;

for (num=1; num<7; num++) {

f=find(x, num);

if (f==5) {

yahtzee=50;

}

}

return yahtzee;

}

int full_house(int x[]) { /*Determines if there is a full house*/

int num, house=0, f, found, three=0, two=2, element;

for (element=1; element<7; element++) {

f=find(x,element);

if (f==3) {

three=1;

}

if (f==2) {

two=1;

}

}

if (two==1 && three==1) {

house=25;

}

return house;

}

int chance_sum(int x[]) { /*Determines the chance value*/

int *a;

int sum=0;

for (a=&x[0]; a<&x[5]; a++) {

sum=sum+*a;

}

return sum;

}

int options(int taken[], int x[], int d[]) { /*Presents the user with all available options*/

int nums[7], kind3=0, kind4=0, house=0 ,s_straight, l_straight;

int chance=0, Yahtzee=0;

int a;

int *choice=&d[0];

int again=1;

int *value=&d[1];

char answer;

char header[100]=" Option Rule Points";

score_1_to_6(nums, x);

house=full_house(x);

for(a=1; a<7; a++) {

kind3=three_of_a_kind(x, a);

kind4=four_of_a_kind(x,a);

if (kind3!=0) {

break;

}

}

s_straight=small_straight(x);

l_straight=large_straight(x);

chance=chance_sum(x);

Yahtzee=yahtzee(x);

printf("You may choose one of the following options. ");

printf("%s ", header);

if (taken[0]!=1) {

printf("\t a) \t Ones\t \t %d\t ", nums[0]);

}

if (taken[1]!=1) {

printf("\t b) \t Twos\t \t %d\t ", nums[1]);

}

if (taken[2]!=1) {

printf("\t c) \t Threes\t \t %d\t " , nums[2]);

}

if (taken[3]!=1) {

printf("\t d) \t Fours\t \t %d\t ", nums[3]);

}

if (taken[4]!=1) {

printf("\t e) \t Fives\t \t %d\t ", nums[4]);

}

if (taken[5]!=1) {

printf("\t f) \t Sixes\t \t %d\t " ,nums[5]);

}

if (taken[6]!=1) {

printf("\t g) \t Three of a kind %d\t " ,kind3);

}

if (taken[7]!=1) {

printf("\t h) \t Four of a kind\t %d\t ", kind4);

}

if (taken[8]!=1) {

printf("\t i) \t Full House\t %d\t " , house);

}

if (taken[9]!=1) {

printf("\t j) \t Small Straight\t %d\t " ,s_straight);

}

if (taken[10]!=1) {

printf("\t k) \t Large Straight\t %d\t ", l_straight);

}

if (taken[11]!=1) {

printf("\t l) \t Chance\t \t %d\t ", chance);

}

if (taken[12]!=1) {

printf("\t m) \t Yahtzee\t %d\t ", Yahtzee);

}

while (again==1) {

printf(" Please select your move by entering your options letter. " );

answer=getchar();

again=0;

switch (answer){

case 'a':

if (taken[0]!=1) {

*value=nums[0];

*choice=0;}

if (taken[0]==1) {

again=1;}

break;

case 'b':

if (taken[1]!=1) {

*value=nums[1];

*choice=1;}

if (taken[1]==1) {

again=1;}

break;

case 'c':

if (taken[2]!=1) {

*value=nums[2];

*choice=2;}

if (taken[2]==1) {

again=1;}

break;

case 'd':

if (taken[3]!=1) {

*value=nums[3];

*choice=3;}

if (taken[3]==1) {

again=1;}

break;

case 'e':

if (taken[4]!=1) {

*value=nums[4];

*choice=4;}

if (taken[4]==1) {

again=1;}

break;

case 'f':

if (taken[5]!=1) {

*value=nums[5];

*choice=5;}

if (taken[5]==1) {

again=1;}

break;

case 'g':

if (taken[6]!=1) {

*value=kind3;

*choice=6;}

if (taken[6]==1) {

again=1;}

break;

case 'h':

if (taken[7]!=1) {

*value=kind4;

*choice=7;}

if (taken[7]==1) {

again=1;}

break;

case 'i':

if (taken[8]!=1) {

*value=house;

*choice=8;}

if (taken[8]==1) {

again=1;}

break;

case 'j':

if (taken[9]!=1) {

*value=s_straight;

*choice=9;}

if (taken[9]!=1) {

again=1;}

break;

case 'k':

if (taken[10]!=1) {

*value=l_straight;

*choice=10;}

if (taken[10]==1) {

again=1;}

break;

case 'l':

if (taken[11]!=1) {

*value=chance;

*choice=11;}

if (taken[11]==1) {

again=1;}

break;

case 'm':

if (taken[12]!=1) {

*value=Yahtzee;

*choice=12;}

if (taken[12]==1) {

again=1;}

break;

}

if (again==1) {

answer=getchar();

}

}

}

void read_score(void) { /*Reads the current score card*/

int a;

FILE *fptr;

char line[200];

printf("Rule \t Players Score\t AIs Score ");

fptr=fopen("Yahtzee_game.txt", "r");

for (a=0; a<13; a++) {

fgets(line, 200, fptr);

printf("%s" ,line);

}

fclose(fptr);

}

void roll_dice(int dice[]) { /*Generates six random numbers*/

int num;

int a;

int kept=0;

int saved[5]; /*Saved dice array*/

int n=5-kept; /*How many new dice to roll*/

int rolled[5];

for (a=0; a

rolled[a]=(rand()%6)+1;

}

for (a=0; a

dice[a]=rolled[a];

}

for (a=n; a<5; a++) {

dice[a]=saved[a-n];

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions