dice problem

This is a discussion on dice problem within the C Programming forums, part of the General Programming Boards category; i am trying to make a program to keep running (rolling of two dice) until the total of the two ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    dice problem

    i am trying to make a program to keep running (rolling of two dice) until the total of the two is 10 or 11.
    which function and how can i use it to make the program stop running when the total of two dice equals 10 or 11.
    i tried with the while loop but that didn't seen to work.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You may actually want to use a do-while loop here, so that the dice get rolled at least once. But either way should work. Post what you have.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    dice ???

    here is what i have. i can not figure out how to make the program stop running when (the point) or 7 are the newTotal.
    i know how to do the count loops, but how do i take the count out.

    insert
    Code:
    while (count<5){
    
    		
    		rollADie=rand()%6+1;
    		rollADie2=rand()%6+1;
    		newTotal=rollADie+rollADie2;
    
    		printf("The new randon is %i\n",newTotal);
    
    		if (newTotal ==thePoint)
    		{printf("You win \n");
    		break;}
    		else if (newTotal==7)
    		{	printf("You loose\n");
    		break;}
    		else 
    		{printf("Nothing\n");}
    
    		count++;}

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you want to stop when newTotal is the point or 7, why don't you do
    Code:
    do
        /* all your dice rolling */
    while (newTotal != 7 && newTotal != thePoint)
    /* Check results here */
    Remember de Morgan's law: -(x or y) = -x and -y.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    Code:
    var faces
    while faces != 10 AND faces != 11
       do reroll
    end while
    The rest is basically arithmatic. And might I recommend that you use

    rand() &#37; (6 - 1) + 1

    because otherwise you would occassionally get 7 on a die.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i tried that
    and this is what the compiler gives me
    fatal error C1075: end of file found before the left brace '{' at (the file name)

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by citizen View Post
    Code:
    var faces
    while faces != 10 AND faces != 11
       do reroll
    end while
    The rest is basically arithmatic. And might I recommend that you use

    rand() % (6 - 1) + 1

    because otherwise you would occassionally get 7 on a die.
    ? % is remainder, so rand()%6 could only give 0, 1, 2, 3, 4, or 5.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i did that and still the same problem

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by goran00 View Post
    i tried that
    and this is what the compiler gives me
    fatal error C1075: end of file found before the left brace '{' at (the file name)
    When you tried ... what? The do-while? You still need to enclose the statements in the loop in curly braces.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    when i click the error this is the line it takes me to
    #include <stdio.h>

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i got the loops, the curly braces and still the same problem

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    Quote Originally Posted by tabstop View Post
    ? % is remainder, so rand()%6 could only give 0, 1, 2, 3, 4, or 5.
    Hm, well that's right. But a normal six sided die does not have a zero face, so some effort to squeeze the range of rand into 1 to 6 inclusive is necessary. That's pretty much the only way I know how to do it also.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thats good. it works not its giving me another error
    error C2061: syntax error : identifier 'rollADie2'
    what does that mean

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Post your code. You do have a main() function, right? And you have all your variables declared somewhere, right?

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    Code:
    include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main(){
    	int rollADie;
    	int rollADie2;
    	int comeOutRoll;
    	int count;
    	int thePoint;
    	int newTotal;
    
    	srand(time(NULL));
    	count=0;
    	thePoint=0;
    
    	rollADie=rand()&#37;6+1;
    	rollADieA=rand()%6+1;
    	comeOutRoll=rollADie+rollADie2;
    
    	printf("%i\n",rollADie);
    	printf("%i\n",rollADie2);
    	printf("%i\n",comeOutRoll);
    
    	if (comeOutRoll == 7)
    	{printf("Winner\n");}
    	else if (comeOutRoll == 11)
    	{printf("Winner\n");}
    	else if (comeOutRoll ==2)
    	{printf("You loose\n");}
    	else if (comeOutRoll ==3)
    	{printf("You loose\n");}
    	else if (comeOutRoll ==12)
    	{printf("You loose\n");}
    
    
    	else 
    	{printf("You might have a chance \n");
    	thePoint=comeOutRoll;
    	printf("The point is %i\n",thePoint);
    	do
    
    		rollADie=rand()%6+1;
    		rollADie2=rand()%6+1;
    		newTotal=rollADie+rollADie2;
    
    		printf("The new random is %i\n",newTotal);
    
    		if (newTotal ==thePoint)
    		{printf("You win \n");}
    		else if (newTotal==7)
    		{printf("You loose\n");}
    		else 
    		{printf("Nothing\n");}
    
    		count++;
    		while (newTotal != thePoint && newTotal != 7);
    
    
    	}
    
    
    	printf("The answer worked\n");
    	system ("Pause");
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Dice Rolling Problem
    By EdSquareCat in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2007, 01:53 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21