Thread: yahtzee

  1. #1
    Unregistered
    Guest

    Question yahtzee

    I am an at home learner working in Dev C++ 4 as well as Builder, though the former right now. I am trying to work out some code to, for example, if I rolled 3 one's. I will just go to the part where I have the question.

    Here is how I envision it. Under each number is a place to chose where I want to put my rolls, say a button that I can chose under the "ones" as well as the "twos" ect., and I rolled 3 ones. How would I get the code to recognize the numbers on the die to know that there are two die that do not have one's and not to add them?

    I have not saw this question come up and was reluctant to post because I cannot figure the source for it for any to see.

    If you don't want to answer because I have no code, I understand.

    Thanks

  2. #2
    Unregistered
    Guest
    Are we talking about a console program here or something a bit more advanced?

  3. #3
    Unregistered
    Guest

    re:yahtzee

    I think first I console game. I am going to eventually put it in Builder with a GUI but first want to just write code for a console and understand what it all does.

    Kent

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    Array....

    Have an array -
    Code:
    int Dice[5] //5 dice in yahtzee?
    Use a for loop to fill the array w/ random values
    Code:
    int i;
    srand((unsigned)TIME(NULL));
    for(i=0;i<5;i++)
    {
    //fill w/ random number between 0&5 and add 1 (1-6)
    Dice[i] = (rand()%6)+1
    }
    Have your button send the dice value you want to count to a function

    The function should be similar to this
    Code:
    int Calculate(int value)
    {
    int n,count;
     for(n=0;n<5;n++)
    {
    if(Dice[n] == value)
    {
      count++;
    }
    return count;
    }
    then use something like
    Code:
    int ones,twos;
    ones = Calculate(1);
    twos = Calculate(2);
    ...
    hope this helped

  5. #5
    Unregistered
    Guest

    yahtzee

    Yes that helped somewhat, thank you! I follow what you have, just about, though I will know what it means by the time I get through with it. I have read 2 books and still use them as reference, but I am begining to wonder if I will ever figure this out. Would you take a look at my random source and tell me if you see anything better to do with it or am I on the right track?

    int rollDice(void)
    {

    int Dice1, Dice2, Dice3, Dice4, Dice5, ChanceSum;

    Dice1 = 1 + rand() % 6;
    Dice2 = 1 + rand() % 6;
    Dice3 = 1 + rand() % 6;
    Dice4 = 1 + rand() % 6;
    Dice5 = 1 + rand() % 6;

    ChanceSum = Dice1 + Dice2+ Dice4+ Dice4 + Dice5;

    cout << ChanceSum << endl;

    return ChanceSum;

    }

    See there is a catagory in Yahtzee that allows you to add all Dice and put score in a place called "chance", thus the example above.

    Does it look ok? How would I incorperate the code you gave me as example together with mine.

    So many questions... I am just glad I not have this to do for school and is just a hobby for me. Thanks>

  6. #6
    Unregistered
    Guest
    The code looks okay but if the example posted is repeated throughout your program it will lead to program bloat. Separating the roll dice functionality from the ChanceSum functionality would seem more logical.

    Setting up your program flow with pencil and paper before starting to write your code is the first thing to do. Here's a quick example of how to start.

    start turn.
    roll five dice.
    place results of all five dice in an array.
    display results of roll to user
    allow user to select which of the five results they wish to keep
    roll remaining dice
    display results of dice after second roll
    allow user to select which of the five dice they wish to keep
    roll remaining dice a third time
    display final results for all dice to user

    declare array of categories
    allow user to determine which of the open categories they wish to store the results
    verify category is open
    allow user to determine which value of dice they are going to store in that category
    verify selection is appropriate to category
    calculate value to store in that category
    store value in appropriate category

    next turn

    after all turns add together each value in each category.

    Now expand on each of these steps one small step at a time until you can't break down each step any further. this process will often lead to variable/function determinations and lead you into code writing. If you can't write in on paper, chances are you aren't going to be able to write the corresponding code either.

  7. #7
    Unregistered
    Guest

    yahtzee

    Thank you so much for your tip. I will do this and start the code over so to speak, from this standpoint, pencil and paper. I will post periodically what I have for any to check and give me tips and such. I want to do all the source myself but if I get hung up I don't mind asking for another perspective. Thanks again!!

    Kent

    Looking again at it I can see where it would lead to "bloating" and I do not want that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Yahtzee Program
    By infuser21 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 02:14 PM
  2. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  3. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  4. Yahtzee
    By CheesyMoo in forum Game Programming
    Replies: 3
    Last Post: 01-31-2003, 04:53 PM