-2

I made a code for Yahtzee. My code asks user if they want to play for a Yahtzee or a straight. It then rolls until the Yahtzee or straight is achieved.

It works well but it takes a long time and way too many rounds, so I want my code to be more efficient and able to reroll on its own. So if the numbers rolled are 1 2 2 3 2, I want the computer to reroll the 1 and 3 until they become 2's.

I just started learning how to program in c++ so I have limited knowledge on the topic of programming. I know how to use loops, switch, if and else statements. Any suggestions or easy solutions would help.

I have some ideas on how to get the computer to reroll, but they don't seem to function. If you are able to figure out how to get the computer to reroll, please provide me with the modifications to my code and guide me as to how you did it.

int die3 = 0;
int die4 = 0;
int die5 = 0;
int roundCounter = 0;
bool yahtzeeNotFound = true;
bool yahtzeeGame = true;
char game[4] = "";

//ask user if they want to play for Yahtzee or Straight
printf("Lets play Yahtzee!\n Do you want to play for a Yahtzee or a Straight?  (Y/S)");
scanf("%s", game);

//if user wants to play Yahtzee, program rolls for same numbers from all the dice.
if (tolower(game[0]) == 'y')
{
    yahtzeeGame = true;

    //this is included so that new numbers are rolled after each round.
    srand(time(0));

    //if the condition of an unfound Yahtzee is met, the program proceeds to enter the loop.
    while (yahtzeeNotFound)
    {
        roundCounter++;

        //the dice are all rolled at same time.
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1;
        die3 = rand() % 6 + 1;
        die4 = rand() % 6 + 1;
        die5 = rand() % 6 + 1;

        //the numbers rolled are printed for user to see.
        //the counter counts each round, so user knows how many rounds it take for a Yahtzee.
        printf("ROUND # %d\n\n", roundCounter);
        printf("   Die 1 = %d\n", die1);
        printf("   Die 2 = %d\n", die2);
        printf("   Die 3 = %d\n", die3);
        printf("   Die 4 = %d\n", die4);
        printf("   Die 5 = %d\n\n\n\n", die5);

        //when all the dice have rolled the same number, a Yahtzee is achieved.
        if ((die1 == die2) && (die1 == die3) && (die1 == die4) && (die1 == die5))

        {
            printf(" Congratulations! You finally reached Yahtzee after %d rounds!\n", roundCounter);
            //when the Yahtzee is achieved, the program exits the loop.
            break;
        }
    }
}

else
{
//if the user does not play for a Yahtzee, they must play for a straight.
yahtzeeGame = false;

//this ensures that new number are rolled after each round.
srand(time(0));

//the program enters the loop.
while (yahtzeeNotFound)
{
    //the counter is declared.
    roundCounter++;

    //the 5 dice are rolled at the same time.
    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1;
    die3 = rand() % 6 + 1;
    die4 = rand() % 6 + 1;
    die5 = rand() % 6 + 1;

    //the numbers are printed out with the round number for the user to see.
    printf("ROUND # %d\n\n", roundCounter);
    printf("   Die 1 = %d\n", die1);
    printf("   Die 2 = %d\n", die2);
    printf("   Die 3 = %d\n", die3);
    printf("   Die 4 = %d\n", die4);
    printf("   Die 5 = %d\n\n\n\n", die5);

    //if the numbers rolled have unique values, the user has a Straight.
    if ((die1 != die2) && (die1 != die3) && (die1 != die4) && (die1 != die5) && (die2 != die3)
            && (die2 != die4) && (die2 != die5) && (die3 != die4) && (die3 != die5) && (die4 != die5))
    {
        // The user is told how many rounds it took for them to get a Straight.
        printf(" Congratulations! You finally reached a Straight after %d rounds!\n", roundCounter);

        //when the user has a Straight, the program exits the loop.
        break;
    }
}
}
return 0;
}
Trinity
  • 1
  • 1
  • 1
    you might find some ideas in questions about a similar topic such as https://stackoverflow.com/questions/28893319/c-small-straight-like-yahtzee-or-poker-5-dice-roll-1234-2345-3456?rq=1. This is a fairly complex problem to solve when working in decimal, but is relatively trivial when working in hex. This is may be the time to take a crash course in hexadecimal math. – Claies Oct 15 '18 at 01:55
  • Fyi, this: `srand(time(0));` belongs in **one**, non-repetitive place in your entire program, ideally at//near the beginning of `main()`. Seeding the pseudo-random number generator is only needed once unless you're purposely truing to repeat a sequence by resetting the seed to a known value (and you're not doing that). – WhozCraig Oct 15 '18 at 02:09
  • so I should only include srand(time(0)) at the start and take it out from my if and else statements? – Trinity Oct 15 '18 at 02:12

1 Answers1

0

I help teach people how to code, this is a common problem.

The answer is... to just start.

You know roughly where to start, you know the basic structures, that's all you need.

Don't try to do it all in one go, just take a small step, test that it worked, then take another one.

You will make mistakes, things won't work, that is entirely normal and expected. Learning to code is a bit like learning to walk by doing a marathon, you stumble, you fall flat on your face but you just have to pick yourself up and throw yourself forward again. Every time you try you will be able to run a bit further, the progress can be exhilarating.

Edit: Responding to the Q's edit, specifically asking how to reroll.

So to roll the second dice, you have the following line of code:

die2 = rand() % 6 + 1;

To reroll it, use the same line.

Eventually you are going to want to transition the code to using an array for the six dice. This will allow for more dynamic and smarter code with less repetition. But that is version two, small steps, get something working, then get something else working, then tweak them to work better etc.

lod
  • 1,098
  • 10
  • 13
  • Thx can you provide me some guidance. I got most of my knowledge of c++ off of google so I don't really know what I'm doing. I thought about using switch statements to ask the user which dice to reroll but that overcomplicated my code. – Trinity Oct 15 '18 at 01:51
  • Maybe try some tutorials if you want a more structured approach. I wouldn't worry too much about overcomplicating your code and there is no wrong way of going about it. Use a switch statement, get it working, then look for repeated elements of your code and restructure to improve it. – lod Oct 15 '18 at 01:54
  • I have no idea how to use arrays. I found this assignment on the internet so I decided to give it a try. Is there a way to make the computer reroll for efficiency by using what I already have? – Trinity Oct 15 '18 at 01:58
  • I don't want to cheat my learning process or the purpose of this site, but I just want a solution. If anyone can edit my code to make it work, it would help me a lot. I've been sitting on this for a couple days and I have had no progress. It would be easier for me if I could see how someone else approached and solved the same problem. – Trinity Oct 15 '18 at 02:07