Thread: Yahtzee C++ programme help

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Yahtzee C++ programme help

    hi all,
    i would like to make a yahtzee game but i would like some help to get it started i dont know where to start from??
    I would like to keep it simple and use the following:
    While loop
    For loop
    Arrays
    RAND_MAX
    rand()%n
    srand(seed)
    Re roll only once.
    The programme takes 5 input the 5 dices rolls and determines the 13 different outcomes possible scores. The programme inputs 5 values in the range from 1 to 6 abd stores the 5 values in an integer array. then determine the 13 alternatives then stores in a suitable array.

    #include<iostream>
    #include<cstlib>
    using namespace std;

    int main()
    {

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You actually started quite well. Just write out the rest of what to do.

    Only thing to note is that, before rand() is called, make sure srand() is called. When you're testing, however, you may want to comment out the call to srand(). This allows your program to always have the same random numbers, so you can be sure your program works with the same set of numbers. After you're sure it appears to work, then you can add the call to srand() back in, and test your program with more random numbers.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    thanks,
    i feel too bored with my free time after bein retired lol
    this is the output i would like to acheive using the above criteria with loops.can someone write out the code so i can study it now??
    thank you.

    Enter seed? 123

    === Round#1 ===
    Total for the first six categories is 0 points.
    Current total is 0 points.

    The dice were 21211
    What re-rolls? hhhhh

    These are available categories:
    (1) Ones gives 3 points.
    (2) Twos gives 4 points.
    (3) Threes gives 0 points.
    (4) Fours gives 0 points.
    (5) Fives gives 0 points.
    (6) Sixs gives 0 points.
    (7) Threes of a kind gives 7 points.
    (8) Fours of a kind gives 0 points.
    (9) Full house gives 25 points.
    (10) Small straight gives 0 points.
    (11) Large straight gives 0 points.
    (12) Yahtzee gives 0 points.
    (13) Chances gives 7 points.

    Please enter a category#? 9

    === Round#2 ===
    Total for the first six categories is 0 points.
    Current total is 25 points.

    The dice were 25144
    What re-rolls? hhhhr

    The dice were 25143

    These are available categories:
    (1) Ones gives 1 points.
    (2) Twos gives 2 points.
    (3) Threes gives 3 points.
    (4) Fours gives 4 points.
    (5) Fives gives 5 points.
    (6) Sixs gives 0 points.
    (7) Threes of a kind gives 0 points.
    (8) Fours of a kind gives 0 points.
    (10) Small straight gives 30 points.
    (11) Large straight gives 40 points.
    (12) Yahtzee gives 0 points.
    (13) Chances gives 15 points.

    Please enter a category#? 11

    === Round#3 ===
    Total for the first six categories is 0 points.
    Current total is 65 points.

    The dice were 51552
    What re-rolls?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is a school assignment, right? Considering the similarity with this thread: http://cboard.cprogramming.com/showthread.php?t=93201

    I don't do other peoples homework, but I think the advice given in the other thread is suitable for you too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    no its not for school just happens we share the same game?i dont want to use class, just simple loops and iterations will do.
    im retired, im an old man lol
    i just would like the code so i can Analise it and learn from it, please someone help me out?

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by kenneth_888 View Post
    just happens we share the same game?
    Nice try
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kenneth_888 View Post
    no its not for school just happens we share the same game?i dont want to use class, just simple loops and iterations will do.
    im retired, im an old man lol
    i just would like the code so i can Analise it and learn from it, please someone help me out?
    Either way, I'm NOT going to write the code for you. And I think I gave good advice in the other thread, if you care to read it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    21
    thanks
    im just trying to learn give an old man a break

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I presume you are doing this because you want to learn how to write programs, yes? And not because you have to, as you are retired.

    Then I don't really see the point in me (or anyone else on this forum) writing the code for you.

    It also seems like you are asking all over the place for this code - a google showed this up: http://news.devx.com/showthread.php?p=487283
    http://www.codeguru.com/forum/showthread.php?t=433070

    Both by someone called Kenneth_888. Maybe it's just a commen user-name?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    21
    ive done this so far,
    but how do u keep the dice that i want and re roll the one i didnt keep?
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    
    int main()
    {
     	int nums[5];
    	int i, j, input;
     
    	time_t t;
    	int count[6];
    
    	srand(time(&t)); /* seed the random number generator */
    	
    	for(j= 0; j < 14; j++)
    	{
    	for(i = 0; i < 5; i++)
      	{
        	nums[i] = rand()&#37;6 + 1;
        	cout << nums[i] << endl;
    		switch(nums[i])
    		{
    			case 1: ++count[0]; break;
    			case 2: ++count[1]; break;
    			case 3: ++count[2]; break;
    			case 4: ++count[3]; break;
    			case 5: ++count[4]; break;
    			case 6: ++count[5]; break;
    			default: cout << "An error has occured!"; break;
    		}
    	}
    	cout << "Choose which numbers you want to keep?( -1 to keep all numbers ) \n";
    	
    	
    	"\n";
    	cin >> input;
    	
        if(input == -1)
             break;
    	if (input > 6) cout << "Please select number from 1 to 6 only \n";
    	
    	'\n';
    	}
    	
    	if ( 
    	
    
      return 0; 
    }

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, the array nums hold your current dice, right? Then you select one or more to roll again?

    In this case, you'll have to have an array of which dice to keep and which to roll again - and obviously just roll the ones that are listed as "roll again".

    I would also suggest that your move the counting of each digit to after you've rolled again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    21
    can u start me off ?? use switch statement?

    switch (keepRoll)
    case 1: if ( count[0] == input)
    ++count;

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Think about what you need it to do:
    You need an array of some sort to hold a list of which dies you keep (or which ones you roll). There are many solutions, but I don't see a switch being needed here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    '\n';
    While that's a valid statement, I doubt it does what you want it to. Put it in a cout.

    Code:
    srand(time(&t)); /* seed the random number generator */
    Since you're not using the variable t anywhere, just pass time() NULL and it won't use a variable.
    Code:
    srand(time(NULL));
    Code:
        	nums[i] = rand()&#37;6 + 1;
        	cout << nums[i] << endl;
    		switch(nums[i])
    		{
    			case 1: ++count[0]; break;
    			case 2: ++count[1]; break;
    			case 3: ++count[2]; break;
    			case 4: ++count[3]; break;
    			case 5: ++count[4]; break;
    			case 6: ++count[5]; break;
    			default: cout << "An error has occured!"; break;
    		}
    Why not this?
    Code:
        	nums[i] = rand()%6;
        	cout << nums[i] + 1 << endl;
        	++count[nums[i]];
    Code:
    if (
    Looks like it's a work in progress . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a programme with cin.get
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 02:35 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM
  5. Simple C++ GUI programme does'nt run from dos
    By Shadowhunt in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:30 AM