Thread: Program to count throws needed for a yahtzee.

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Program to count throws needed for a yahtzee.

    Hello, I am trying to write a program that counts how many dice rolls are needed to get a yahtzee(all dice with the same number) but when I try to run it, it seems like it gets stuck in a loop. Can someone help me find what's causing this?

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    
    
    bool diceroll(int *pcount)
    {
        const int size=5;
        int high=7, low=1, dice[size];
        bool yahtzee=false;
    
    
        
        for (int i=0; i<size;i++)
            {
                dice[i]=rand()%((high+1)-low)+low;
            }
        
        for (int j=0;j<size;j++)
        {
            if(dice[j]!=dice[j+1])
                break;
            if (j==5)
          {
                    return true;
                    *pcount++;
          }
        }
        *pcount++;
        return false;    
    }
    
    
    int main()
    {
        int count, *p_count=&count;
        bool yahtzee=false;
    
    
        srand(time(NULL));
        while(yahtzee==false)
        {
            yahtzee=diceroll(p_count);
        }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I can not understand why you need p_count.
    In your function, size is 5. Then you have this (which is the only case returning true)
    Code:
    for (int j=0;j<size;j++) // Reason that j can not be five!
        {
            if(dice[j]!=dice[j+1])
                break;
            if (j==5) // Can not be five.
          {
                    return true;
                    *pcount++;
          }
    The maximum value of j can be size - 1. You know that, but I guess you just missed it.

    Also in main(), place a return 0; before the end of main().

    Welcome to the forum!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Yep, as std said you should get rid of the p_count pointer. Just count the attempts with a counter in main.


    And you presumably mean to test j == 5 after the for loop (to see if the loop broke early or not, although there's a better way to do it...) So you'll have to declare j before the for loop so it'll be accessible after.


    Another problem is that you're testing one beyond the end of the dice array. When j is size - 1 you test dice[j+1] which is beyond the end. You should only loop while j < size - 1.

    And your high is 7 whereas it should be 6 (if these are regular dice).


    And you'll probably want to print out the result too!


    Note that it should give a yahtzee about 1 in every 1296 (6 to the 4th power) times, although any particular run can be quite different. On a 10000 trial run I got:
    Avg: 1294.62 Min: 1 Max: 12324
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Thanks so much guys! I fixed the for loop and removed the pointer for a simple counter in main. I'm still getting the hang of c++ as you can see hehe. I hope one day I can help new users in the forums like you guys

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Javier Medina View Post
    Thanks so much guys! I fixed the for loop and removed the pointer for a simple counter in main. I'm still getting the hang of c++ as you can see hehe. I hope one day I can help new users in the forums like you guys
    When I joined the forum, in 2010, I started programming. In 2010 I got chosen to participate in my CS degree (I am still undergraduate, since the course is one of 4 years). So, what you hope for, is not far from your reach I guess.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-13-2012, 12:46 AM
  2. Replies: 2
    Last Post: 01-18-2012, 12:27 AM
  3. Yahtzee C++
    By egochan90 in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2010, 12:19 PM
  4. Help with Yahtzee Program
    By infuser21 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 02:14 PM
  5. program throws assert after deleting a resource
    By hanhao in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2007, 09:57 PM