// Steve Jun, z3216638 // 8th April, 2007 // Yahtzee game for CS1911 #include #include #define ZERO 48 #define NINE 57 #define SPACE 32 #define TAB 11 #define MINUS 45 #define ENTER 10 void errorChecking (int c, int whiteSpace, int incorrectInput, int valueRange, int numberValues, int count, int dicerolls[]); void theRerollmenu (int reRollmenu, int diceRange, int diceMenu, int reMenu[], int count, int c, int incorrectInput, int numberValues, int whiteSpace, int i, int diceRolls[]); int main (int argc, char **argv) { int c = 0; int count = 0; int i=0; int diceRolls[5]; int incorrectInput=0; int valueRange=0; int numberValues=0; int whiteSpace=0; int diceMenu = 0; int menu[3]; int invalidChoice=0; int diceRange=0; int reRollmenu=0; int reMenu[5]; int optionOne=0; int optionTwo=0; int optionThree=0; //frequency start int d=0; int threeofaKind=0; int tokScore=0; //Three of a kind score int fourofaKind=0; int fokScore=0; //Four of a kind score int yahtzeeScore=0; int yahtzeePoints=0; // Actual Yahtzee score int fullhouseHalf=0; int fullHouse=0; int fullhouseScore=0; //Full house score int smallSt=0; int straight=0; int stScore=0; //Straight Score int smStraight=0; int smstScore=0; //Small Straight Score int chance=7; int chanceScore=0; int flag1=0; int flag2=0; int flag3=0; int flag4=0; int flag5=0; int flag6=0; //frequency end int score=0; //score of points at the end //sorting hands out start int k=0; int temp=0; int optionCounter=0; //(If there are 5 of these out of 7 then 3rd option is null value) int scoreArray[3]; int handArray[7]={threeofaKind,fourofaKind,fullHouse,smStraight,straight,yahtzeeScore,chance}; //sorting hands out end printf("Please enter dice values:\n> "); errorChecking(c,whiteSpace,incorrectInput,valueRange,numberValues,count,diceRolls); i=0; printf("\nYour dice values are: "); while (i < 5){ printf("%d ",diceRolls[i]); i++; } printf("\n\n"); count=0; c=0; i=0; printf("Please choose:\n 1 - > Reroll some dice \n 2 - > Reroll all dice\n 3 - > Keep dice\n> "); diceMenu=getchar(); if(!((diceMenu >= ZERO && diceMenu <= NINE) || diceMenu==SPACE || diceMenu==ENTER)){ invalidChoice=1; } if(diceMenu >= ZERO && diceMenu <= NINE){ if (diceMenu==MINUS){ incorrectInput=1; } diceMenu = diceMenu - 48; if(!(diceMenu >= 1 && diceMenu <= 3)){ invalidChoice=1; } else{ menu[count] = c; } count++; } if (invalidChoice==1){ printf("Invalid Choice."); return(1); } if (diceMenu==1){ printf("Please enter dice to reroll (1 - 5):\n"); theRerollmenu (reRollmenu,diceRange,diceMenu,reMenu,count,c,incorrectInput,numberValues,whiteSpace,i, diceRolls); } if (diceMenu==2){ printf("Please enter dice values:\n> "); errorChecking(c,whiteSpace,incorrectInput,valueRange,numberValues,count,diceRolls); i=0; printf("\nYour dice values are: "); while (i < 5){ printf("%d ",diceRolls[i]); i++; } printf("\n"); printf("Please choose:\n 1 - > Reroll some dice \n 2 - > Reroll all dice\n 3 - > Keep dice\n> "); diceMenu=getchar(); if(!((diceMenu >= ZERO && diceMenu <= NINE) || diceMenu==SPACE || diceMenu==ENTER)){ invalidChoice=1; } if(diceMenu >= ZERO && diceMenu <= NINE){ if (diceMenu==MINUS){ incorrectInput=1; } diceMenu = diceMenu - 48; if(!(diceMenu >= 1 && diceMenu <= 3)){ invalidChoice=1; } else{ menu[count] = c; } count++; } if (invalidChoice==1){ printf("Invalid Choice."); return(1); } if (diceMenu==1){ printf("Please enter dice to reroll (1 - 5):\n"); theRerollmenu (reRollmenu,diceRange,diceMenu,reMenu,count,c,incorrectInput,numberValues,whiteSpace,i, diceRolls); } if (diceMenu==2){ printf("Please enter dice values:\n> "); errorChecking(c,whiteSpace,incorrectInput,valueRange,numberValues,count,diceRolls); i=0; printf("\nYour dice values are: "); while (i < 5){ printf("%d ",diceRolls[i]); i++; } printf("\n"); } } //end of dice menu while (d<6){ if (diceRolls[d] == 1){ flag1++; } d++; } d=0; while (d<6) { if (diceRolls[d] == 2){ flag2++; } d++; } d=0; while (d<6) { if (diceRolls[d] == 3){ flag3++; } d++; } d=0; while (d<6) { if (diceRolls[d] == 4){ flag4++; } d++; } d=0; while (d<6) { if (diceRolls[d] == 5){ flag5++; } d++; } d=0; while (d<6) { if (diceRolls[d] == 6){ flag6++; } d++; } //Chance! d=0; while (d<5){ chanceScore=diceRolls[d]+chanceScore; d++; } diceRolls[0]=flag1; diceRolls[1]=flag2; diceRolls[2]=flag3; diceRolls[3]=flag4; diceRolls[4]=flag5; diceRolls[5]=flag6; //////////Hands function //three of a kind! d=0; while (d<6){ if (diceRolls[d]>2){ threeofaKind=1; tokScore=1*diceRolls[0]+2*diceRolls[1]+3*diceRolls[2]+4*diceRolls[3]+5*diceRolls[4]+6*diceRolls[5]; } d++; } //four of a kind! d=0; while (d<6){ if (diceRolls[d]>3){ fourofaKind=2; fokScore=1*diceRolls[0]+2*diceRolls[1]+3*diceRolls[2]+4*diceRolls[3]+5*diceRolls[4]+6*diceRolls[5]; } d++; } //fullhouse! d=0; while (d<6){ if (diceRolls[d]==2){ fullhouseHalf=1; } d++; } if (fullhouseHalf==1&&threeofaKind==1){ fullHouse=3; fullhouseScore=25; } //Small Straight and Straight d=0; while (d<6){ if (diceRolls[d]==0){ smallSt=0; } smallSt++; d++; } if (smallSt>4){ smStraight=4; smstScore=30; } if (smallSt==6){ straight=5; stScore=40; } if (diceRolls[1]>0&&diceRolls[2]>0&&diceRolls[3]>0&&diceRolls[4]>0&&diceRolls[5]>0){ straight=5; smStraight=4; } if (diceRolls[0]>0&&diceRolls[1]>0&&diceRolls[2]>0&&diceRolls[3]>0&&diceRolls[4]>0){ straight=5; stScore=40; smStraight=4; smstScore=30; } if (diceRolls[1]>0&&diceRolls[2]>0&&diceRolls[3]>0&&diceRolls[4]>0){ smStraight=4; smstScore=30; } if (diceRolls[0]>0&&diceRolls[1]>0&&diceRolls[2]>0&&diceRolls[3]>0){ smStraight=4; smstScore=30; } //Yahtzee! d=0; while (d<6){ if (diceRolls[d]==5){ yahtzeeScore=6; yahtzeePoints=50; } d++; } /////// all these unmarked above d=0; k=0; while (d<7){ if (handArray[d]>0){ scoreArray[k]=handArray[d]; k++; } if (handArray[d]==0){ optionCounter++; } d++; } if (scoreArray[1]>scoreArray[2]){ temp=scoreArray[1]; scoreArray[1]=scoreArray[2]; scoreArray[2]=temp; } if (scoreArray[0]>scoreArray[1]) { temp=scoreArray[0]; scoreArray[0]=scoreArray[1]; scoreArray[1]=temp; } if (scoreArray[1]>scoreArray[2]){ temp=scoreArray[1]; scoreArray[1]=scoreArray[2]; scoreArray[2]=temp; } optionOne=scoreArray[0]; optionTwo=scoreArray[1]; optionThree=scoreArray[2]; if (optionCounter==5){ optionThree=0; } if (optionCounter==6){ optionThree=0; optionTwo=0; if (optionOne!=7){ optionOne=6; } } //sorting hands out end printf("\nYour score options are:\n"); if (optionOne==1){ printf("1 -> Three of a kind: (%d points)\n", tokScore); } if (optionOne==2){ printf("1 -> Four of a kind: (%d points)\n", fokScore); } if (optionOne==3){ printf("1 -> Full House: (%d points)\n", fullhouseScore); } if (optionOne==4){ printf("1 -> Small Straight: (%d points)\n", smstScore); } if (optionOne==7){ printf("1 -> Chance: (%d points)\n", chanceScore); } if (optionTwo==1){ printf("2 -> Three of a kind: (%d points)\n", tokScore); } if (optionTwo==2){ printf("2 -> Four of a kind: (%d points)\n", fokScore); } if (optionTwo==3){ printf("2 -> Full House: (%d points)\n", fullhouseScore); } if (optionTwo==4){ printf("2 -> Small Straight: (%d points)\n", smstScore); } if (optionTwo==5){ printf("2 -> Straight: (%d points)\n", stScore); } if (optionTwo==6){ printf("2 -> Yahtzee: (%d points)\n", yahtzeeScore); } if (optionTwo==7){ printf("2 -> Chance: (%d points)\n", chanceScore); } if (optionThree==5){ printf("3 -> Straight: (%d points)\n> ", stScore); } if (optionThree==6){ printf("3 -> Yahtzee: (%d points)\n> ", yahtzeePoints); } if (optionThree==7){ printf("3 -> Chance: (%d points)\n> ", chanceScore); } float scoreMenu; scanf("%f",&scoreMenu); if (!(scoreMenu==1||scoreMenu==2||scoreMenu==3)){ printf("\nInvalid Choice."); return(1); } if (scoreMenu==1){ if (optionOne==1){ score=tokScore; } if (optionOne==2){ score=fokScore; } if (optionOne==3){ score=fullhouseScore; } if (optionOne==4){ score=smstScore; } if (optionOne==7){ score=chanceScore; } } if (scoreMenu==2){ if (optionTwo==1){ score=tokScore; } if (optionTwo==2){ score=fokScore; } if (optionTwo==3){ score=fullhouseScore; } if (optionTwo==4){ score=smstScore; } if (optionTwo==5){ score=stScore; } if (optionTwo==6){ score=yahtzeePoints; } if (optionTwo==7){ score=chanceScore; } } if (scoreMenu==3){ if (optionThree==5){ score=stScore; } if (optionThree==6){ score=yahtzeePoints; } if (optionThree==7){ score=chanceScore; } } printf("Your score is: %d points.\n",score); return(0); } void errorChecking (int c, int whiteSpace, int incorrectInput, int valueRange, int numberValues, int count, int diceRolls[5]){ c=0; count=0; while((c = getchar()) != EOF){ if(!((c >= ZERO && c <= NINE) || c==SPACE || c==ENTER)){ incorrectInput=1; } if (c==SPACE){ whiteSpace++; } if(c >= ZERO && c <= NINE){ c = c - 48; if(!(c >= 1 && c <= 6)){ valueRange=1; } else { diceRolls[count]=c; count++; } } } if (count!=5){ numberValues=1; } if (!(whiteSpace==4&&count==5)){ incorrectInput=0; } if (count==5&&whiteSpace!=4){ incorrectInput=1; } if (incorrectInput==1){ printf("\nIncorrect Input Format.\n"); valueRange=0; numberValues=0; } if (valueRange==1){ printf("\nValue out of Range.\n"); numberValues=0; } if (numberValues==1){ printf("\nIncorrect Number of Values.\n"); } } void theRerollmenu (int reRollmenu,int diceRange,int diceMenu,int reMenu[5],int count,int c,int incorrectInput, int numberValues, int whiteSpace, int i, int diceRolls[5]){ int valueRange=0; //flag for if value is in range int p=0; // variable for count int reRoll[5]; // reroll error checking array int l=1; //reroll variable when rerolling some dice (for replacing elements in main array with new reroll) reRollmenu=0; count=0; incorrectInput=0; diceRange=0; while ((reRollmenu = getchar())!=EOF){ if(!((reRollmenu >= ZERO && reRollmenu <= NINE) || reRollmenu==SPACE || reRollmenu==ENTER)){ incorrectInput=1; } if (reRollmenu==SPACE){ whiteSpace++; } if(reRollmenu >= ZERO && reRollmenu <= NINE){ if (reRollmenu==MINUS){ incorrectInput=1; } reRollmenu = reRollmenu - 48; if(!(reRollmenu >= 1 && reRollmenu <= 5)){ diceRange=1; } else{ reMenu[count] = reRollmenu; count++; } } if (!(whiteSpace==4&&count==5)){ incorrectInput=0; } if (count==5&&whiteSpace<4){ incorrectInput=1; } if (count>5){ numberValues=1; incorrectInput=0; } else { numberValues=0; } } if (incorrectInput==1){ printf("\nIncorrect Input Format.\n"); diceRange=0; numberValues=0; } if (diceRange==1){ printf("\nDice number out of Range.\n"); numberValues=0; } if (numberValues==1){ printf("\nIncorrect Number of Values.\n"); } if (count==1){ printf("Please enter 1 value:\n> "); } i=2; while (i>1&&i<6){ if (count==i){ printf("Please enter %d values:\n> ",i); } i++; } incorrectInput=0; numberValues=0; c=0; p=count; count=0; whiteSpace=0; while((c = getchar()) != EOF){ if(!((c >= ZERO && c <= NINE) || c==SPACE || c==ENTER)){ incorrectInput=1; } if (c==SPACE){ whiteSpace++; } if(c >= ZERO && c <= NINE){ c = c - 48; if(!(c >= 1 && c <= 6)){ valueRange=1; } else { reRoll[count]=c; count++; } } } if (count!=p){ numberValues=1; } if (!(whiteSpace==(p-1)&&count==p)){ incorrectInput=0; } if (count==p&&whiteSpace!=(p-1)){ incorrectInput=1; } if (incorrectInput==1){ printf("\nIncorrect Input Format.\n"); valueRange=0; numberValues=0; } if (valueRange==1){ printf("\nValue out of Range.\n"); numberValues=0; } if (numberValues==1){ printf("\nIncorrect Number of Values.\n"); } i=0; while (l<7){ while (i<5){ if (reMenu[i]==l){ diceRolls[l-1]=reRoll[i]; } i++; } l++; i=0; } i=0; printf("\nYour dice values are: "); while (i < 5){ printf("%d ",diceRolls[i]); i++; } }