Question: For the following add a function that acomplishes the following: Create an algorythm for the ai to choose the dice (out of five) with highest
For the following add a function that acomplishes the following:
Create an algorythm for the ai to choose the dice (out of five) with highest possible potential score threw out its three turns of rolling dice. in this order from lowest to highest:
Ones
Twos
Threes
Fours
Fives
Sixes
Three of a kind
Four of a kind
Full house
Small straight
Large straight
Chance
Y@HTZEE
USING the following C program
#include
#include
#include
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 taken1[], int x[], int choice[], int r[], int who);
void read_score(void);
void roll_dice(int dice[]);
void move(int number1[], int number2[], int taken1[], int taken2[], int turn, int r[], int who, int num[]);
int robot_decide(int taken2[], int choice[], int r[], int num[]);
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 taken1[14];
int taken2[14];
int num[14]={0,1,2,3,4,5,6,7,8,9,10,12,11};
int who=0;
int r[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, taken1, taken2, turn, r, who, num);
read_score(); /*Reads the current score sheet*/
move(number1, number2, taken1, taken2, turn+1, r, who+1, num);
read_score(); /*Reads the current score sheet*/
}
void move(int number1[], int number2[], int taken1[], int taken2[], int turn, int r[], int who, int num[]) {
int choice[3];
int dice[5];
int a;
char space[100];
if (who==0) {
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(taken1,dice, choice, r, who); /*Determines what scoring options are allowed*/
number1[choice[0]]=choice[1];
taken1[choice[0]]=1;
}
if (who==1) {
for (a=0; a<3; a++) {
roll_dice(dice);
printf("The Computer 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(taken1,dice, choice, r, who); /*Determines what scoring options are allowed*/
robot_decide(taken2, choice, r, num);
number2[choice[0]]=choice[1];
taken2[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[], int r[], int who) { /*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);
int *b=&r[0], *c=&r[1], *e=&r[2], *f=&r[3], *g=&r[4], *h=&r[5];
int *i=&r[6], *j=&r[7], *k=&r[8], *l=&r[9], *m=&r[10], *n=&r[12];
int *o=&r[11];
*b=nums[0], *c=nums[1], *e=nums[2], *f=nums[3], *g=nums[4], *h=nums[5];
*k=house=full_house(x);
for(a=1; a<7; a++) {
*i=kind3=three_of_a_kind(x, a);
*j=kind4=four_of_a_kind(x,a);
if (kind3!=0) {
break;
}
}
*l=s_straight=small_straight(x);
*m=l_straight=large_straight(x);
*n=chance=chance_sum(x);
*o=Yahtzee=yahtzee(x);
if (who==0) {
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]; } } int robot_decide(int taken2[], int choice[], int r[], int num[]) { int c, d, e, f, moves=1; int a, z, y; for (a=0; a<13; a++) { if (taken2[a]==1) { r[a]=-1; } } while (moves!=0) { moves=0; for (z=7; z<12; z++) { y=z-1; if (r[y]>r[z]) { c=r[y]; d=r[z]; e=num[y]; f=num[z]; r[y]=d; r[z]=c; num[y]=f; num[z]=e; moves+=1; } } } if (r[11]>0) { choice[0]=num[11]; choice[1]=r[11]; } else { moves=1; while (moves!=0) { moves=0; for (z=1; z<6; z++) { y=z-1; if (r[y]>r[z]) { c=r[y]; d=r[z]; e=num[y]; f=num[z]; r[y]=d; r[z]=c; num[y]=f; num[z]=e; moves+=1; } } } if (r[5]>0) { choice[0]=num[5]; choice[1]=r[5]; } else { choice[0]=num[12]; choice[1]=r[12]; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
