I tried to play the C language dice game, but it didn't workPlease give me the part to modify.

Asked 2 years ago, Updated 2 years ago, 26 views

List item

Game Rules

Roll two 12-sided dice.

The player starts with 100 coins in the balance.

When the third die enters between the two dice, the coin is earned as much as the bet is made, and the balance is raised as much as the bet is made.

If the third dice deviates from the number of two dice, you lose as much coin as you bet, and as much as you bet, you lose your balance.

The game ends when the player's balance reaches zero.

You can choose whether the two dice are the same number (e.g. first dice: 1, second dice: 1) or not, or the game ends.

I tried to squeeze it, but it didn't work Please let me know if there's anything wrongㅠ<

void display_details(void);

void display_dice(int, int, int);

int roll_die(void);

int main(void) {

//variables that are used

int die1, die2, die3;
int  temp;

srand(int)time(NULL);//Determine seed using current time
die1 = land() %12; //12 divided by the remaining occurrences from 0 to 11
die2 = land() %12; //12 divided by the remaining occurrences from 0 to 11

die1 = die1 + 1; //1~12
die2 = die2 + 1; //1~12

printf("die1 : %d\n\n", die1);
printf("die2 : %d\n\n", die2);

int Games_Played = 0; //how many games played
int Games_Won = 0;  //how many games won
int Games_Lost = 0;  //how many games lost


int CurrentChips = MAX_CHIPS; //linking it to the acutal system input
int BetChips;

char PlayerName[100];   //player will enter their name.
char PlayAgain; //to play again


display_details(); //displays details

//this function uses real time as a random generator, every second will be a different outcome
//c4244 error anyother solution to fix this is 'srand( static_cast<unsigned int>(time(NULL)));'
srand(time(NULL));

//asked if want to play game
printf("\nWould you like to play Test your luck [y|n]? ");
//player chooses yes or no to play or not                 
scanf(" %c", &PlayAgain);

//This loop allows the game to be carry on as long as there are chips left and able to play more games
while ((PlayAgain == 'y') && (CurrentChips > 0))
{
    //shows how many games played which is obviously 0
    if (Games_Played == 0)
    {
        printf("\n A new chellenger! What is your name? ");
        scanf(" %s", &PlayerName);            //link to playername
        printf("\nHi %s!\n", PlayerName);     //Greting the player
    }

    //each new game it will increase by 1
    Games_Played = Games_Played + 1;

    //displaying current chips
    printf("\nNumber of chips: %d\n", CurrentChips);
    printf("\nPlace your bet: "); //betting
    scanf("%d", &BetChips); //how much the player want to bet
    //for validation as people might be like bet<0 or bet>100
    while ((BetChips < 0) || (BetChips > CurrentChips))  //using loop until valid
    {
        printf("\n uuh ....You can only bet the chips you have.. (0-%d)!\n", CurrentChips);
        printf("\nPlace your bet: ");
        scanf("%d", &BetChips);
    }

    if (die1 == die2)
        printf("Even-steven - let's play again!\n\n");
    else
        printf("Not the same, let's play!\n\n");


    if ((die1 > die3) && (die2 > die3))
    {
        //+1 to games won
        Games_Won = Games_Won + 1;
        //adding to bet balance
        CurrentChips = CurrentChips + BetChips;
        printf("\nXXXXX  Winner! d(^_^)  XXXXX\n");
    }
    else if ((die1 < die3) && (die2 < die3))
    {
        //+1 to games won
        Games_Won = Games_Won + 1;
        //adding to bet balance
        CurrentChips = CurrentChips + BetChips;
        printf("\nXXXXX  Winner! d(^_^)  XXXXX\n");
    }
    else if ((die1 <= die3) && (die2 <= die3))
    {
        //+1 to lose games
        Games_Lost = Games_Lost + 1;
        //-bet from balance
        CurrentChips = CurrentChips - BetChips;
        printf("\nXXXXX  Loser! :( XXXXX\n");
    }

    else

    {
        //+1 to lose games
        Games_Lost = Games_Lost + 1;
        //-bet from balance
        CurrentChips = CurrentChips - BetChips;
        printf("\nXXXXX  Loser! :( XXXXX\n");

        while (CurrentChips == 0)

            if (throw_sum == PointToMake)
            {
                printf("\nXXXXX  Winner! (x2 the bet) XXXXX\n");
                //x2 added to balance
                CurrentChips = CurrentChips + 2 * BetChips;
                Games_Won = Games_Won + 1;
            }
            else
            {
                //+1 in lost
                Games_Lost = Games_Lost + 1;
                //-chips that are were betted
                CurrentChips = CurrentChips - BetChips;
                printf("\nXXXXX  Loser!:(  XXXXX\n");
            }
    }

    printf("Thanks for playing! ");

    //chips count is checked after the end of game

    //if there are chips remainding

    if (CurrentChips > 0)

    {

        printf("You now have %d chips.\n", CurrentChips);

        printf("\n============================\n\n");

        printf("Play Again %s [y|n]? ", ", PlayerName);   //starting a new game again?

        scanf(" %c", &PlayAgain);   //for the user input y/n response

        printf("\n============================\n");

    }

    else   //when no chips are left

    {

        printf("\n you're out of chips.\n\n");

        printf("\nXXXXX  GG TRY AGAIN NEXT TIME  XXXXX\n");
    }

}

//display now many games are played

if (Games_Played == 0)

{

printf("\nOh well.... better luck next time!\n");

}

//only 1 game

else if (Games_Played == 1)

{ printf("\n%s played %d game and is cashing out with %d chips!\n", PlayerName, Games_Played, CurrentChips);

printf("   |---> Games won: %d\n", Games_Won);

printf("   |---> Games lost: %d\n", Games_Lost);

printf("\nThanks for playing, come again to test that luck of yours %s.\n", PlayerName);

}

//played games are more than 1

else

{

//display game results

printf("\n%s played %d games and is cashing out with %d chips!\n", PlayerName, 

Games_Played, CurrentChips);

printf("   |---> Games won: %d\n", Games_Won);

printf("   |---> Games lost: %d\n", Games_Lost);

printf("\nThanks for playing, come again to test that luck of yours %s!\n", PlayerName);

}

return 0;

}

void display_details(void)

{

printf("File :.... assignment.c\n");

printf("Author   :.... \n");

printf("Stud ID  :.... \n");

printf("Email ID :.... \n");

printf("This is my own work as defined by the \n");

printf("University's Academic Misconduct Policy.\n");

}

int throws_die(void)

{

return 1 + rand() % 12;

}

c c++

2022-09-21 20:10

1 Answers

There are parts that haven't been implemented yet, and I haven't done it, so I can't point out all the issues, but I wrote down only the parts that stand out.

1. scanf(" %s", &PlayerName);

%s must be omitted because PlayerName is declared charPlayerName[100]; This is because the array's name, PlayerName is the same as &PlayerName[0].

2. Undefined variables

Throw_sum and PointToMake are not defined in the code below. And if you use while as below, please specify {} block.

while (CurrentChips == 0)
    if (throw_sum == PointToMake)
    {
        printf("\nXXXXX  Winner! (x2 the bet) XXXXX\n");
        //x2 added to balance
        CurrentChips = CurrentChips + 2 * BetChips;
        Games_Won = Games_Won + 1;
    }
    else
    {
        //+1 in lost
        Games_Lost = Games_Lost + 1;
        //-chips that are were betted
        CurrentChips = CurrentChips - BetChips;
        printf("\nXXXXX  Loser!:(  XXXXX\n");
    }

3. Use uninitialized variables

The die3 variable was used uninitialized. Make it a habit to initialize all variables when declaring them.


2022-09-21 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.