C PRGMING 101:骰子游戏分配,当玩家掷出6时增加额外的转弯

时间:2014-11-13 01:30:01

标签: c arrays function fgets

#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 your might \n");

int sets;
srand(time(0));

// user input dialogue 

printf("Enter the number of rolls to 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 and 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");

    for (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);
    }

    for (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 You win! \n");
    if (g>c)
        fprintf(dice_rdme, " \n You lose\n ");

fclose(dice_rdme);
return(0);
}

//games are done, spit em back to 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);      
}

在我将plyrsum更改为= 0作为定义之前,我遇到了以下错误,我不明白这些值来自何处。玩家得分似乎默认或使用占位符或地址作为整数(?抓住吸管?)

kettingstad@esc_151:~/workspace $ gcc HW6.c -lm -o hw6test.o
kettingstad@esc_151:~/workspace $ ./hw6test.o
Commence gamble, test your might 
Enter the number of rolls to wager 
3
6 2 4 player score = 32779 
4 6 3  dealer score = 13 

You lose, here are some pamphlets for gambling addiction treatment.... 

kettingstad@esc_151:~/workspace $ ./hw6test.o
Commence gamble, test your might 
Enter the number of rolls to wager 
3
4 3 5 player score = 32779 
2 1 4  dealer score = 7 

这里我预先将plyrsum定义为= 0,但我仍然无法解决每次玩家掷出6时我的函数添加额外滚动的问题。我尝试使用嵌套在每个for中的if语句循环,其中为每个玩家生成结果,但我不断收集8 GB .txt文件和一个挂起的程序。该任务要求对txt文件中的分数进行更复杂的回读,但是我创建了现有函数,因此我可以测试函数“int dice(int sets)”

kettingstad@esc_151:~/workspace $ gcc HW6.c -lm -o hw6test.o
kettingstad@esc_151:~/workspace $ ./hw6test.o
Commence gamble, test your might 
Enter the number of rolls to wager 
 6
 6 4 2 6 1 player score = 25 
 1 6 3 3 1 6  dealer score = 20 

You win! 

`

2 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。基本上,您只想在发生6时设置标记,保存当前sum,再次滚动,然后take the higher of the two rolls。实现这一目标的一种方法是:

int eroll = 0;          /* flag for extra roll (when b = 6)         */
int esum = 0;           /* extra sum (to keep greater of 2 rolls)   */

dice_rdme = fopen ("dice_rdme.txt", "w");

do {
    eroll = 0;                                      /* reset eroll      */
    for (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) {
            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);

<强>输出:

$ ./bin/gameeroll
Commence gamble, test your might
Enter the number of rolls to wager
3
6 2 3 EXTRA ROLL! current sum 11
4 3 3 player score = 21
2 2 3  dealer score = 6

 You win!

 student info
class professor and ta
 HOMEWORK ASSIGNMENT#6 DICE GAME 2.0

注意:只会插入额外的fprintf进行测试。

如前所述,您的所有等效项if (a = whatever)都必须为if (a == whatever)。您需要验证代码中剩余的逻辑。 C是一种完全语言,花点时间考虑每一行。

答案 1 :(得分:0)

// I extracted the common dice roll/sum function for simplicity in the code
// I made use of the #define FILENAME rather than hardcoding the name in
// each call to fopen()
// I added checking of the returned values from the I/O functions
// I added #includes for the items I inserted into the code
// I moved the function prototypes to outside of an function body
// so the compiler will know the proper format of each function call
// at every function call, not just those within the main() function
// I set the return type to void for those functions those (possible)
// returned value is being ignored.
// I modified the format parameter for the call to scanf()
// to allow for any leading white space.
// I inserted the code to enable an additional dice roll when
// a 6 is rolled.


#include <stdio.h>  // for scanf(), printf(), etc.
//#include <string.h> // for memset()
#include <time.h>   // for time()
#include <errno.h> 
#include <stdlib.h>

#define FILENAME "dice_rdme.txt"

//prototypes
void dice(int sets);
void print_info(void);
void dicereadback(void);
int  rollSet( int sets, FILE* fp );

int main (void)
{

    //title
    printf("Commence gamble, test your might \n");

    int sets = 0;
    srand(time(0)); // initialize random functions

    // user input dialogue
    printf("Enter the number of rolls to wager \n");
    if( 1 != scanf(" %d", &sets) )
    {
        perror( "scanf" );
        exit(0);
    }

    // implied else, read of roll count successful

    if( 0 >= sets )
    { // then invalid value entered
        printf( "ERROR: rolls must be greater than 0, exiting");
        exit(0);
    }

    dice(sets);
    dicereadback();
    print_info();
    return(0);
}

//general print info
void print_info (void)
{
    printf("\n student info \n");
    printf("class professor and ta \n ");
    printf("HOMEWORK ASSIGNMENT#6 DICE GAME 2.0 \n");
    return;
}

//generate games, parse outcome, store
void dice (int sets)
{
    int playerSum = 0; // sum of all player rolls
    int dealerSum = 0; // sum of all dealer rolls

    FILE * fp;
    if( NULL == (fp = fopen(FILENAME,"w")) )
    { // then, open failed
        perror("fopen for write");
        exit(0);
    }

    // implied else

    playerSum = rollSet( sets, fp );
    dealerSum = rollSet( sets, fp );

    fprintf(fp, " player score = %d \n", playerSum);
    fprintf(fp, " dealer score = %d \n", dealerSum);

    if (playerSum > dealerSum)
    {
        fprintf(fp, " \n You win! \n");
    }
    else if (playerSum < dealerSum)
    {
        fprintf(fp, " \n You lose\n ");
    }
    else
    { // tie
        fprintf(fp, " \n you tied \n");
    }

    fclose(fp);
}

int rollSet( int sets, FILE* fp )
{
    int sum = 0;       // sum of all dice rolls
    int rollValue = 0; // value of individual dice roll
    int i = 0;         // loop counter

    // roll dice, if rollValue = 6, roll again
    for (i=0; i<sets; i++)
    {
         rollValue = 1 + rand() %6; // yields 1....6
         sum += rollValue;
         fprintf(fp,"%1d ", rollValue);

         if( 6 == rollValue )
         { // then allow another roll
             i--;
         }
    }
    return( sum );
}


//games are done, spit em back to stdout

void dicereadback(void)
{
    char buff[1000];

    FILE *fp;
    if( NULL == (fp = fopen(FILENAME,"r")) )
    { // then open failed
        perror( "fopen for read" );
        exit(0);
    }

    // implied else, open successful
    // note: fgets() appends a null terminator to the string
    //       so no need to pre-format the input buffer
    //memset( buff, '\0', sizeof buff);
    while( NULL != fgets(buff,1000,fp) )
    {
        printf("%s",buff);
        //memset( buff, '\0', sizeof buff);
    }

    fclose(fp);
}