arrays - C PRGMING 101: Dice game Assignment that adds an extra turn when players roll a 6 -


#include <stdio.h> #include <math.h> #include <time.h> #define filename "dice_rdme.txt"  int main (void) { //prototypes  int dice(int sets); void print_info(void); int dicereadback(void);  //title  printf("commence gamble, test might \n");  int sets; srand(time(0));  // user input dialogue   printf("enter number of rolls wager \n"); scanf("%i", &sets);  dice(sets); dicereadback(); print_info(); return; }  //general print info  void print_info (void) {     printf("\n student info \n");     printf("class professor , ta \n ");     printf("homework assignment#6 dice game 2.0 \n");     return; }  //generate games, parse outcome, store     int dice (int sets) {  file * dice_rdme;  int a, b; int plyrsum = 0; int j = 0; int k = 0; int d, f, dlrsum; int c[sets], g[sets]; int plyrset = sets; int dlrset = sets;  dice_rdme = fopen("dice_rdme.txt","w");      (a=0; a<plyrset; a++)     {          b = 1 + rand() %6;          c[j] = b;          plyrsum += c[j];          fprintf(dice_rdme,"%i ", c[j]);          ++j;                     // if (b=6)                    // a--;     }      if (a == plyrset)     {          fprintf(dice_rdme, "player score = %i \n", plyrsum);     }      (d=0; d<dlrset; d++)     {          f = 1 + rand() %6;          g[k] = f;          dlrsum += g[k];          fprintf(dice_rdme,"%i ", g[k]);              ++k;                     // if (f=6)                     //d--;     }      if (d == dlrset)         fprintf(dice_rdme, " dealer score = %i \n", dlrsum);     if (c>g)         fprintf(dice_rdme, " \n win! \n");     if (g>c)         fprintf(dice_rdme, " \n lose\n ");  fclose(dice_rdme); return(0); }  //games done, spit em stdout  int dicereadback(void) { file * dice_rdme; char buff[1000];  dice_rdme = fopen("dice_rdme.txt","r");      while(fgets(buff,1000,dice_rdme)!=null)     printf("%s",buff);  fclose(dice_rdme); return(0);       } 

before changed plyrsum = 0 definition experienced following errors, not understand these values coming from. player score seems have defaulted or used placeholder or address integer(?grasping @ straws?)

kettingstad@esc_151:~/workspace $ gcc hw6.c -lm -o hw6test.o kettingstad@esc_151:~/workspace $ ./hw6test.o commence gamble, test might  enter number of rolls wager  3 6 2 4 player score = 32779  4 6 3  dealer score = 13   lose, here pamphlets gambling addiction treatment....   kettingstad@esc_151:~/workspace $ ./hw6test.o commence gamble, test might  enter number of rolls wager  3 4 3 5 player score = 32779  2 1 4  dealer score = 7  

here pre defined plyrsum = 0, still couldn't sort out problem of making function add roll every time player rolls 6. tried using if statement nested in each of loops outcomes generated each player kept winding 8 gb .txt files, , hung program. assignment called more sophisticated read of scores txt file made existing function test function "int dice(int sets)"

kettingstad@esc_151:~/workspace $ gcc hw6.c -lm -o hw6test.o kettingstad@esc_151:~/workspace $ ./hw6test.o commence gamble, test might  enter number of rolls wager   6  6 4 2 6 1 player score = 25   1 6 3 3 1 6  dealer score = 20   win!  

`

there several ways this. basically, want set flag if 6 occurs, save current sum, roll-again, , take higher of 2 rolls. 1 way implement be:

int eroll = 0;          /* flag roll (when b = 6)         */ int esum = 0;           /* sum (to keep greater of 2 rolls)   */  dice_rdme = fopen ("dice_rdme.txt", "w");  {     eroll = 0;                                      /* reset eroll      */     (a = 0; < plyrset; a++) {         b = 1 + rand () % 6;         c[j] = b;         plyrsum += c[j];         fprintf (dice_rdme, "%i ", c[j]);         ++j;         if (b == 6) {             eroll = 1;                              /* set eroll flag   */         }     }     if (eroll) {         esum = plyrsum;                             /* save plyrsum     */         fprintf (dice_rdme, "extra roll! current sum %i \n", plyrsum);     }     plyrsum = (plyrsum > esum) ? plyrsum : esum;    /* keep best roll   */ } while (eroll); 

output:

$ ./bin/gameeroll commence gamble, test might enter number of rolls wager 3 6 2 3 roll! current sum 11 4 3 3 player score = 21 2 2 3  dealer score = 6   win!   student info class professor , ta  homework assignment#6 dice game 2.0 

note: fprintf inserted testing.

as noted, of equivalences if (a = whatever) need if (a == whatever). need verify remainder of logic in code. c exact language, take time , think each line.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -