All pastes #430659 Raw Edit

Unnamed

public c v1 · immutable
·published 2007-04-09 04:30 UTC
rendered paste body
// Steve Jun, z3216638// 8th April, 2007// Yahtzee game for CS1911#include <stdio.h>#include <ctype.h>#define ZERO 48#define NINE 57#define SPACE 32#define TAB 11#define ENTER 10void errorchecking (int c, int whitespace, int incorrectinput, int valuerange, int numbervalues, int count, int dicerolls[]);void rerollmenu (int rerollmenu, int dicerange, int dicemenu, int remenu[], int count, int c, int invalidchoice);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;      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");   int dicemenu = 0;int menu[3];int invalidchoice=0;    count=0;    c=0;    i=0;   int dicerange=0;   printf("Please choose:\n 1 - > Reroll some dice \n 2 - > Reroll all dice\n 3 - > Keep dice\n > ");  // while((dicemenu=getchar())!= EOF){     dicemenu=getchar();        if(!((dicemenu >= ZERO && dicemenu <= NINE) || dicemenu==SPACE || dicemenu==ENTER)){        invalidchoice=1;        }        if(dicemenu >= ZERO && dicemenu <= NINE){           dicemenu = dicemenu - 48;           if(!(dicemenu >= 1 && dicemenu <= 3)){              dicerange=1;           }           else{              menu[count] = c;           }           count++;        }        if (invalidchoice==1){          printf("Invalid Choice.");           return(1);        }        if (dicerange==1){          printf("Dice Number Out of Range.");          return(1);        }           //}//   printf("dicemenu= %d",dicemenu);   int rerollmenu=0;   int remenu[5];     if (dicemenu==1){      printf("Please enter dice to reroll (1 - 5):\n");      rerollmenu(rerollmenu,dicerange,dicemenu,remenu,count,c,invalidchoice);//        if (invalidchoice==1){//          printf("Invalid Choice.");//           return(1);//        }//        if (dicerange==1){//          printf("Dice Number Out of Range.");//          return(1);//        }   }  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");  }    //frequency function//void freqFunction (int optionone, int optiontwo, int optionthree)  {  int d=0;//  int a=0;//  int flag=0;  int threeofakind=0;  int tokscore=0; //Three of a kind score  int fourofakind=0;  int fokscore=0; //Four of a kind score  int yahtzee=0;  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;   ///// all these unmarked  int optionone=0;  int optiontwo=0;  int optionthree=0;  int flag1=0;  int flag2=0;  int flag3=0;  int flag4=0;  int flag5=0;  int flag6=0;     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;//       printf("The chance score is: %d\n" ,chancescore);        d++;    }     dicerolls[0]=flag1;     dicerolls[1]=flag2;     dicerolls[2]=flag3;     dicerolls[3]=flag4;     dicerolls[4]=flag5;     dicerolls[5]=flag6;//////////Hands function//    d=0;//    while (d<6){//      printf("%d\n",dicerolls[d]);//      d++;//    }    //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++;//        printf("\ndice rolls: %d\n",dicerolls[d]);//        printf("\n5th dice roll: %d\n",dicerolls[5]);       d++;//      printf("small st: %d\n",smallst);//      printf("straight: %d\n", straight);    }    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   //sorting hands out start   d=0;   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,yahtzee,chance};   d=0;   k=0;//   while (d<7){//     printf("This is the handarray : %d\n", handarray[d]);//     d++;//   }   d=0;   while (d<7){     if (handarray[d]>0){       scorearray[k]=handarray[d];       k++;     }     if (handarray[d]==0){       optioncounter++;     }     d++;   }//  d=0;//  while (d<3){//    printf("scorearray: %d\n", scorearray[d]);//    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;   }//  printf("\n%d, %d, %d\n",scorearray[0],scorearray[1],scorearray[2]);   optionone=scorearray[0];   optiontwo=scorearray[1];   optionthree=scorearray[2];   if (optioncounter==5){     optionthree=0;   }   if (optioncounter==6){     optionthree=0;     optiontwo=0;     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);   }   int score=0;   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==6){         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==7){         score=yahtzeepoints;      }      if (optiontwo==6){         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){      incorrectinput=1;      }      if (whitespace==3&&count==4){        incorrectinput=0;      }      if (incorrectinput==1){         printf("\nIncorrect Input Format.\n");         valuerange=0;         numbervalues=0; //     return(1);      }      if (valuerange==1){         printf("\nValue out of Range.\n");         numbervalues=0;//      return(1);      }      if (numbervalues==1){         printf("\nIncorrect Number of Values.\n");//      return(1);      } }void rerollmenu (int rerollmenu,int dicerange,int dicemenu,int remenu[5],int count,int c,int invalidchoice){      while ((rerollmenu = getchar())!=EOF);{      c=0;      count=0;         if(!((rerollmenu >= ZERO && rerollmenu <= NINE) || rerollmenu==SPACE || rerollmenu==ENTER)){         invalidchoice=1;         }         if(rerollmenu >= ZERO && rerollmenu <= NINE){            dicemenu = dicemenu - 48;            if(!(rerollmenu >= 1 && rerollmenu <= 5)){               dicerange=1;            }            else{               remenu[count] = c;            }            count++;         }      }}