简体   繁体   中英

Quit function for C dice game

Hi this is my first post to stackoverflow i'm creating a Dice Game in C and i'm having difficulty adding a quit feature after each round. once the user makes their prediction the game loop runs again and re-roles 4 new dice values.

I would like to include a function that asks the player if they want to keep going or exit via a (y/n) input.

below is the code for my dice game

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>

int main ()
{
    while(1)
    {
        srand ( time(NULL) );
        int i;
        int DiceR[4]; // Dice Array
        char prediction; //prediction input for high low or equal


        while (1)
        {

          for (i=0; i<1; i++)
    {
        DiceR[0] = (rand()%6) + 1;
        printf("\n%d \n", DiceR[0]);

        DiceR[1] = (rand()%6) + 1;
        printf("%d \n", DiceR[1]);

        DiceR[2] = (rand()%6) + 1;
        printf("%d \n", DiceR[2]);

        DiceR[3] = (rand()%6) + 1;

    }

        printf("\nDo you think the next dice roll will be higher or lower than %d", DiceR[2]);
        printf("\nPlease enter L = Lower E = Equal H = higher or X = Exit:\t");
        scanf("\n%c", &prediction);

        switch(prediction)
    {
    case 'L' : case 'l':
        if (DiceR[3] < DiceR[2]) //Lower
            printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
        else
            printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
        break;
    case 'E' : case 'e':
        if (DiceR[3] == DiceR[2]) //Equal
            printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
        else
            printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
        break;
    case 'H' : case 'h':
        if (DiceR[3] > DiceR[2]) //Higher
            printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
        else
            printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
        break;

    default:
        printf("that value is not recognized");
        break;


    }



        }
    }
    }

You probably want a flag for this before the first while , like, bool running = true; , set it to false when/where you wanted to exit, then check that in the while s instead of an infinite loop.

You can create a flag and use the condition (flag==true); to keep the code running while the user wants it

I would like to include a function that asks the player if they want to keep going or exit via a (y/n) input.

A function which asks the user at the start of each roll to continue or stop

int cont()
{
    char ch;
    printf("\n want to continue or stop? y/n : \n");
    scanf(" %c",&ch); //note a space before %c to consume white spaces
    if(ch=='y'||ch=='Y')
        return 0; //if yes i.e, 'y' return 0
    return 1; //else return 1
}

just check for the return value of function at the start of each roll this way :

(follow the comments to understand)

//#include .......

int cont()
{
    char ch;
    printf("\n want to start/continue? y/n : \n");
    scanf(" %c",&ch);
    if(ch=='y'||ch=='Y')
        return 0;
    return 1;
}

int main ()
{
    int flag=0; //initialize flag with a value of zero

    while(1) //first while loop
    {
        //srand and other variables initialization

        while (1) //second while loop
        {    
          //don't use if(cont()) here....

          //for loop

          //printing and scanning prediction

            switch(prediction)
            {

             //all other cases........              

             case 'X': case 'x': //you seem to have forgotten mentioning this case :)
                 flag=1;
                 break;

             //default case
            }


            //use it here instead sso that you'd get it at the end of each turn 
            if(cont()) //function call : here you ask user for y/n input
            {
              //if user enter no i.e, 'n', then you enter the if block
               flag=1;
               break;
            }

            if (flag==1) //checking for flag; useful when user enters 'X'
            break; //break out of first while loop
        }

        if(flag==1) //checking for flag
            break; //break out of second while loop
    }

}

here is a 'corrected' version of the code.

Note, printing the values of the calls to rand() might not be the best way, especially as the user plays the game awhile and recognizes the pattern(s)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//#include <string.h>
//#include <math.h>
#include <time.h>

#define NUM_OF_DICE (4)
#define MAX_DICE_VALUE (6)

int main ()
{
    srand ((unsigned) time(NULL) );
    int DiceR[ NUM_OF_DICE ]; // Dice Array
    char prediction; //prediction input for high low or equal


    while (1)
    {
        for( size_t i=0; i<NUM_OF_DICE; i++)
        {
            DiceR[i] = (rand()%MAX_DICE_VALUE) + 1;
            printf("\n%d \n", DiceR[i]);
        }

        printf("\nDo you think the next dice roll will be higher or lower than %d", DiceR[2]);
        printf("\nPlease enter L = Lower E = Equal H = higher or X = Exit:\t");

        if( 1 != scanf("\n%c", &prediction) )
        { // then scanf failed
            perror( "scanf for inputting prediction failed");
            exit( EXIT_FAILURE );
        }

        // implied else, scanf successful

        prediction = (char)toupper(prediction);

        if( 'X' == prediction )
        {
            return 0;
        }

        switch(prediction)
        {

            case 'L' :
                if (DiceR[3] < DiceR[2]) //Lower
                    printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
                else
                    printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
                break;

            case 'E' :
                if (DiceR[3] == DiceR[2]) //Equal
                    printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
                else
                    printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
                break;

            case 'H' :
                if (DiceR[3] > DiceR[2]) //Higher
                    printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
                else
                    printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
                break;

            default:
                printf("input must be 'L' or 'H' or 'E' or 'X'\n");
                break;
        } // end switch
    }
} // end function: main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM