Thread: Dice Game (Fifty)

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    23

    Dice Game (Fifty)

    I'm working to write a program that will allow 2 players to play the dice game called "Fifty".

    I apologize if this has been covered in another post but I did spend the last hour looking through previous posts that referenced dice games and could not find exactly what I was looking for.

    Here are the rules of the game:
    This game requires 2 players with each player taking turns to roll a pair (2) of dice. The goal is to be the first player to reach 50 points. You can only score points by rolling doubles. All doubles except for double 3s and double 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score and that player starts over again at 0. Non double rolls are 0 points.

    Some program specific notes:
    • The program should allow the users to continue to play the game until they choose to quit.
    • After each roll, show the player the result (the values of the 2 die).
    • Output the scores for each player after each turn.
    • Once a player reaches 50, the program should print out a congratulatory message that includes which player won and the final score.
    • And lastly this is an exercise that should utilize a sentinel while loop.


    This is a class assignment in Intro to Programming and is my first exposure to writing code (this class not this assignment) and quite frankly I am really struggling. So for this assignment I've simply started with what I know (or with what I think I know) which is to start with declaring what variables I think I will need, the printf statements to the users explaining the rules along with an attempt at the while loop.

    Here's what I have so far:
    Code:
    int main()
    {
        //declare variables
        int My_dice1;
        int My_dice2;
        int Your_dice1;
        int Your_dice2;
        int My_result;
        int Your_result;
        char quit;
    
    
        srand((unsigned)time(NULL));    //See random function with current time
    
    
        //Introductory message explaining what the program does and how the game is played.
        printf_s("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n");
        printf_s("The goal of Fifty is to be the first player to reach 50 points.  You get points by rolling doubles.\n");
        printf_s("All doubles except 3s and 6s score 5 points.  Double 6s are worth 25 points and double 3s wipe out the players entire score\n");
        printf_s("and the player must start again at 0.  Non-double rolls are 0 points.\n");
        printf_s("Press q to quit.\n");
    
    
        //Ask the players to roll the dice (not sure how to implement this element)
    
    
        while (quit != 'q' || My_result < 50 || Your_result < 50)
        {
            printf("Player 1's turn.\n");
            int My_dice1 = rand() % 6 + 1;
            printf("My first throw is %d\n", My_dice1);
            int My_dice2 = rand() % 6 + 1;
            printf("My second throw is %d\n", My_dice2);
            if (My_dice1 == My_dice2)
            {
    
    
            }
        }
        
    
    
        printf("Player 2's turn\n");
        int Your_dice1 = rand() % 6 + 1;
        printf("My first throw is %d\n", Your_dice1);
        int Your_dice2 = rand() % 6 + 1;
        printf("My second throw is %d\n", Your_dice2);
    
    
        My_result = My_dice1 + My_dice2;
        Your_result = Your_dice1 + Your_dice2;
    
    
        return 0;
    }
    Any help in any form is much appreciated.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Okay, well, if I understand the rules, you only score when both dice are the same, so you should write something like
    Code:
    /* Both people take a turn, then... */
    if (My_dice1 == My_dice2) {
       My_result = My_result + My_dice1 + My_dice2; 
    }
    else if (Your_dice1 == Your_dice2) {
       Your_result = Your_result + Your_dice1 + Your_dice2;
    }
    You should initilize the numbers to 0. What I did was add Your_result to the sum so the score will accumulate. If you roll 4s and then 2s it will add 0 + 4 + 4, then 8 + 2 + 2, so you have 12 points.

    Everything else seems okay though. Let me know if you have more problems.
    Last edited by whiteflags; 10-24-2017 at 02:14 PM.

  3. #3
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    I dont think there should be an else here. And I believe the bottom code must be within the loop.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by PhantomJoe View Post
    I'm working to write a program that will allow 2 players to play the dice game called "Fifty".

    I apologize if this has been covered in another post but I did spend the last hour looking through previous posts that referenced dice games and could not find exactly what I was looking for.

    Here are the rules of the game:
    This game requires 2 players with each player taking turns to roll a pair (2) of dice. The goal is to be the first player to reach 50 points. You can only score points by rolling doubles. All doubles except for double 3s and double 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score and that player starts over again at 0. Non double rolls are 0 points.

    Some program specific notes:
    • The program should allow the users to continue to play the game until they choose to quit.
    • After each roll, show the player the result (the values of the 2 die).
    • Output the scores for each player after each turn.
    • Once a player reaches 50, the program should print out a congratulatory message that includes which player won and the final score.
    • And lastly this is an exercise that should utilize a sentinel while loop.


    This is a class assignment in Intro to Programming and is my first exposure to writing code (this class not this assignment) and quite frankly I am really struggling. So for this assignment I've simply started with what I know (or with what I think I know) which is to start with declaring what variables I think I will need, the printf statements to the users explaining the rules along with an attempt at the while loop.

    Here's what I have so far:
    Code:
    int main()
    {
        //declare variables
        int My_dice1; // declared
        int My_dice2; // declared
        int Your_dice1;  // declared
        int Your_dice2; // declared
        int My_result;
        int Your_result;
        char quit;
    
    
        srand((unsigned)time(NULL));    //See random function with current time
    
    
        //Introductory message explaining what the program does and how the game is played.
        printf_s("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n");
        printf_s("The goal of Fifty is to be the first player to reach 50 points.  You get points by rolling doubles.\n");
        printf_s("All doubles except 3s and 6s score 5 points.  Double 6s are worth 25 points and double 3s wipe out the players entire score\n");
        printf_s("and the player must start again at 0.  Non-double rolls are 0 points.\n");
        printf_s("Press q to quit.\n");
    
    
        //Ask the players to roll the dice (not sure how to implement this element)
    
    
        while (quit != 'q' || My_result < 50 || Your_result < 50)
        {
            printf("Player 1's turn.\n");
            int My_dice1 = rand() % 6 + 1; // declared twice
            printf("My first throw is %d\n", My_dice1);
            int My_dice2 = rand() % 6 + 1; // declared twice
            printf("My second throw is %d\n", My_dice2);
            if (My_dice1 == My_dice2)
            {
    
    
            }
        }
        
    
    
        printf("Player 2's turn\n");
        int Your_dice1 = rand() % 6 + 1; // declared thrice
        printf("My first throw is %d\n", Your_dice1);
        int Your_dice2 = rand() % 6 + 1; // declared thrice
        printf("My second throw is %d\n", Your_dice2);
    
    
        My_result = My_dice1 + My_dice2;
        Your_result = Your_dice1 + Your_dice2;
    
    
        return 0;
    }
    Any help in any form is much appreciated.
    I'd use one big control while loop, and two little while loops, and two for loops, to start with, then work it out as I went along.

    this way two user interaction can be had. the other person would too have to hit the enter key to do his or her roll.

    Code:
    while ( not  quitting )
    {
    
         while ( user one)
         {
               do user 1 stuff
         }
         while ( user 2 )
         {
           do user 2 stuff
         }
    
    }
    remember to set your conditions then reset your conditions for the loops to keep working until quit. then I'd toss in a switch or two as well to handle the totals and resetting the values if needed depending on the "total" value. Requires two or three case 'tags' along with all of the other stuff you got a do. that is a prototype and I am sure others may have their own different ides on how to do this.

    I'd make a function for random too. it would return the number.

    that's about it off the top of my head.
    and a little "trick" so you do not have to keep writing "printf" for each line.
    Code:
      printf("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n"
                "The goal of Fifty is to be the first player to reach 50 points.  You get points by rolling doubles.\n"
                "All doubles except 3s and 6s score 5 points.  Double 6s are worth 25 points and double 3s wipe out the players entire score\n"
                "and the player must start again at 0.  Non-double rolls are 0 points.\n"
                "Press q to quit s to start.\n"); // just close if off on the last line. 
    if you noticed I didn't put the for loops in that example, you're correct,and it was not by accident.
    I might not even use them depending on how I'd write.
    Code:
    int total = 0;
    
        if (your_dice1 == your_dice2)
         {
             total = (your_dice1+your_dice2);
    
              switch (total)
              {
                  case 6:
                       your_result = 0;
                        break;
                  case 12:
                       your_result += 25;
                       break;
                  default:
                      your_result += 5;
                      break;
                }
        }
    where to put it and make two maybe and change it to make it work for both users well ... think about it.
    Last edited by userxbw; 10-24-2017 at 03:43 PM.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by userxbw View Post
    I'd use one big control while loop, and two little while loops, and two for loops, to start with, then work it out as I went along.

    this way two user interaction can be had. the other person would too have to hit the enter key to do his or her roll.

    Code:
    while ( not  quitting )
    {
    
         while ( user one)
         {
               do user 1 stuff
         }
         while ( user 2 )
         {
           do user 2 stuff
         }
    
    }
    remember to set your conditions then reset your conditions for the loops to keep working until quit. along with all of the other stuff you got a do. that is a prototype and I am sure others may have their own different ides on how to do this.

    I'd make a function for random too. it would return the number.

    that's about it off the top of my head.
    and a little "trick" so you do not have to keep writing "printf" for each line.
    Code:
      printf("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n"
                "The goal of Fifty is to be the first player to reach 50 points.  You get points by rolling doubles.\n"
                "All doubles except 3s and 6s score 5 points.  Double 6s are worth 25 points and double 3s wipe out the players entire score\n"
                "and the player must start again at 0.  Non-double rolls are 0 points.\n"
                "Press q to quit s to start.\n"); // just close if off on the last line. 
    if you noticed I didn't put the for loops in that example, you're correct,and it was not by accident.
    I might not even use them depending on how I'd write.
    First off thank you for the tip on printf, that looks much cleaner.

    Secondly, for the inner while loops you recommended, do I need to have an additional variable that identifies user 1 and user 2 to make those work?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by PhantomJoe View Post
    First off thank you for the tip on printf, that looks much cleaner.

    Secondly, for the inner while loops you recommended, do I need to have an additional variable that identifies user 1 and user 2 to make those work?
    ohhhh yes of course, remember they are inside of another loop. So that control(outer) loop will always be running as long as its separate condition is NOT met, it controls the
    code within the loop in as much as keeps running it. Now you have 2 separate loops within it, they both have their own means for controlling them separately. by the same method that outer loops needs its condition to be met to either run or not run.

    you just need to switch that condition to a false to get into the loop, and a true to get out of the loop. then back to false to get back into the loop, example. ok I cannot think of an example that will not give it to you so you'll have to beat your brains on the table until it comes to you. I'll just give it to you.
    Code:
    int user1 = 0; 
    
    //then down in your loop
    
    while ( not quitting )
    {
    user1 = 0;
    
    while ( user1 == 0)
    {
           do you user 1 stuff.
    
          when done
        user1 = 1; // kicks it out, 
    }
    //goes here runs code but if no other  code or loop it will loop
    // back around and reset itself and then run again, and again, and again .. 
    // then it 'fails' because that reset defeats itself. 
    // and you do have your other checks you need to put
    // in to determine when to leave each loop
    // then ultimately your outer main ( control ) loop
    // that keeps you from going into an infinite loop(s)
    
    
    } // end outer loop
    you'll just need to find the function that stops the main loop while keeping you inside of it so you can get it to interact with the users, before going into their loops, ie, print out your its your turn and then wait for them to hit enter to "roll" the dice on their turn. it belongs to the stdio.h
    when you find it checks its return value so hopefully you can figure out how to put it to use for two purposes,and look into the continue for loops, maybe you'll come up with a use for that too. Plus I do not think your caught that switch I put in there afterwords.
    Last edited by userxbw; 10-24-2017 at 05:06 PM.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Since this is a beginner's exercise I doubt they'd be allowed to use switch. Besides, "break" is not structured programming (it's a local jump) and they're probably learning structured programming.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    then he'd better stick to his if else 's then. It's still doable without the switch, just (thought) it's a quicker cleaner way of doing it.

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by userxbw View Post
    then he'd better stick to his if else 's then. It's still doable without the switch, just (thought) it's a quicker cleaner way of doing it.
    Yes, we haven't learned about switch. I did meet with my teacher tonight and got a good deal of help. I'll post my new code tomorrow and hopefully get some feedback here.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by PhantomJoe View Post
    Yes, we haven't learned about switch. I did meet with my teacher tonight and got a good deal of help. I'll post my new code tomorrow and hopefully get some feedback here.
    looking forward to seeing it.

  11. #11
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    So here is my new code. The good news is it does compile but the bad news is it appears to be an infinite loop that never gets past the player 1 portion of the code.

    The goal is to have each player take a turn and I know I haven't cared for that yet but I was trying to get this piece working before tackling that. I'm not sure what I've done wrong and any help is much appreciated.

    Code:
    int main()
    {
    	//declare variables
    	int My_dice1;
    	int My_dice2;
    	int Your_dice1;
    	int Your_dice2;
    	int My_result = 0;
    	int Your_result = 0;
    	char quit = NULL;
    	char start;
    	char toPlay1;
    	char toPlay2;
    
    
    	srand((unsigned)time(NULL));	//Seed random function with current time
    
    
    									//Introductory message explaining what the program does and how the game is played.
    	printf_s("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n"
    	"The goal of Fifty is to be the first player to reach 50 points.  You get points by rolling doubles.\n"
    	"All doubles except 3s and 6s score 5 points.  Double 6s are worth 25 points and double 3s wipe out the players entire score\n"
    	"and the player must start again at 0.  Non-double rolls are 0 points.\n"
    	"Press q to quit.\n");
    
    
    	
    
    
    	//Start game rules	
    	printf("Player 1's turn.  Press 's' to start.\n");
    	scanf_s("%c", &start);
    
    
    	if (start == 's')  //check if player actually pressed 's', only then execute the loop
    	{
    		while (quit != 'q' || My_result < 50 || Your_result < 50)
    		{
    			My_dice1 = rand() % 6 + 1;
    			printf("My first die is %d\n", My_dice1);
    			My_dice2 = rand() % 6 + 1;
    			printf("My second die is %d\n", My_dice2);
    
    
    			//Check if players has doubles
    			if (My_dice1 == My_dice2)
    			{
    				//Check for double sixes
    				if (My_dice1 == 6 && My_dice2 == 6) {
    					My_result = My_result + 25;
    					//Add 25 points to the sum
    				}
    				//Check for doule threes
    				else if (My_dice1 == 3 && My_dice2 == 3) {
    					My_result = 0;
    					//Assign zero to the sum
    				}
    				else {
    					My_result = My_result + 5;
    					//Executes for remaining other doubles
    					//Add 5 points to the sum
    				}
    			}
    		}
    			printf("Your score is %d \n", My_result);
    		}
    
    
    		printf("Player 2's turn\n");
    		Your_dice1 = rand() % 6 + 1;
    		printf("My first die is %d\n", Your_dice1);
    		Your_dice2 = rand() % 6 + 1;
    		printf("My second die is %d\n", Your_dice2);
    		//Check if players has doubles
    		if (Your_dice1 == Your_dice2)
    		{
    			//Check for double sixes
    			if (Your_dice1 == 6 && Your_dice2 == 6)
    			{
    				Your_result = Your_result + 25;
    				//Add 25 points to the sum
    			}
    			//Check for doule threes
    			else if (Your_dice1 == 3 && Your_dice2 == 3)
    			{
    				Your_result = 0;
    				//Assign zero to the sum
    			}
    			else {
    				Your_result = Your_result + 5;
    				//Executes for remaining other doubles
    				//Add 5 points to the sum
    			}
    		}
    			printf("Your score is %d \n", Your_result);
    		
    		if (Your_dice1 == Your_dice2)
    		{
    			Your_result = Your_result + Your_dice1 + Your_dice2;
    			printf("Your score is %d \n", Your_result);
    		}
    		return 0;
    	}

  12. #12
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Look at the condition of the loop.

    OR gives true if any of the conditions are true
    AND gives true only if all of the conditions are true

    And the game must proceed as player 1, player 2, player 1, player 2, ... The loop you have made runs as player 1, player 1, player 1,...
    Think how you'll fix this, its quite easy.
    Last edited by the_jackass; 10-25-2017 at 12:36 PM.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861

    OOP's loopy gone loopy

    look at my comments within your code, your brackets are off as well.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    int main()
    {
        //declare variables
        int My_dice1;
        int My_dice2;
        int Your_dice1;
        int Your_dice2;
        int My_result = 0;
        int Your_result = 0;
        char quit = '\0';
        char start;
        char toPlay1; // not used
        char toPlay2; // not used
     
     
        srand((unsigned)time(NULL));    //Seed random function with current time
     
     
                                        //Introductory message explaining what the program does and how the game is played.
        printf("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n"
        "The goal of Fifty is to be the first player to reach 50 points.  You get points by rolling doubles.\n"
        "All doubles except 3s and 6s score 5 points.  Double 6s are worth 25 points and double 3s wipe out the players entire score\n"
        "and the player must start again at 0.  Non-double rolls are 0 points.\n"
        "Press q to quit.\n");
     
     
         
     
     
        //Start game rules  
        printf("Player 1's turn.  Press 's' to start.\n");
        scanf("%c", &start);
     
     
        if (start == 's')  //check if player actually pressed 's', only then execute the loop
        {
            while (quit != 'q' || My_result < 50 || Your_result < 50)
            {
                My_dice1 = rand() % 6 + 1;
                printf("My first die is %d\n", My_dice1);
                My_dice2 = rand() % 6 + 1;
                printf("My second die is %d\n", My_dice2);
     
     
                //Check if players has doubles
                if (My_dice1 == My_dice2)
                {
                    //Check for double sixes
                    if (My_dice1 == 6 && My_dice2 == 6) {
                        My_result = My_result + 25;
                        //Add 25 points to the sum
                    }
                    //Check for doule threes
                    else if (My_dice1 == 3 && My_dice2 == 3) {
                        My_result = 0;
                        //Assign zero to the sum
                    }
                    else {
                        My_result = My_result + 5;
                        //Executes for remaining other doubles
                        //Add 5 points to the sum
                    }
                }
                getchar();
            } // end of while loop *********************************
             // the rest is dead code *************************************
                printf("Your score is %d \n", My_result); 
            } // end of  if (start == 's') *************************************
     
    
     
            printf("Player 2's turn\n");
            Your_dice1 = rand() % 6 + 1;
            printf("My first die is %d\n", Your_dice1);
            Your_dice2 = rand() % 6 + 1;
            printf("My second die is %d\n", Your_dice2);
            //Check if players has doubles
            if (Your_dice1 == Your_dice2)
            {
                //Check for double sixes
                if (Your_dice1 == 6 && Your_dice2 == 6)
                {
                    Your_result = Your_result + 25;
                    //Add 25 points to the sum
                }
                //Check for doule threes
                else if (Your_dice1 == 3 && Your_dice2 == 3)
                {
                    Your_result = 0;
                    //Assign zero to the sum
                }
                else {
                    Your_result = Your_result + 5;
                    //Executes for remaining other doubles
                    //Add 5 points to the sum
                }
            }
                printf("Your score is %d \n", Your_result);
             
            if (Your_dice1 == Your_dice2)
            {
                Your_result = Your_result + Your_dice1 + Your_dice2;
                printf("Your score is %d \n", Your_result);
            }
            return 0;
        }
    it all needs to be inside of the loop, 'breaks put on to stop it between users, players 1 and 2, don't forget to print out totals on completion of loop.
    Last edited by userxbw; 10-25-2017 at 01:21 PM.

  14. #14
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by the_jackass View Post
    Look at the condition of the loop.

    OR gives true if any of the conditions are true
    AND gives true only if all of the conditions are true

    And the game must proceed as player 1, player 2, player 1, player 2, ... The loop you have made runs as player 1, player 1, player 1,...
    Think how you'll fix this, its quite easy.
    The loop condition in the while statement, doesn't that need each argument to be separated with an "OR" or did I misunderstand?
    Code:
     while (quit != 'q' || My_result < 50 || Your_result < 50)
    So while quit != q (if true execute the loop); OR while my_result is <50 (if true execute the loop); OR while your_result is <50 (if true execute the loop).

  15. #15
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Actually, I was thinking wrong...forget what I said about AND and OR. It is not good to check the victory in the loop conditions since you have to check it after each turn. You need to have the code for second player inside of loop too, thats why the loop is running like My first, My second, My first, My second, My ... instead of the correct My first, My second, Your first, Your second, My...


    You also never ask the user if they want to quit, thus the quit!='q' is always true.


    Overall structure can be like this:

    Code:
    while(quit!='q') {
    
        // calculate roll for My_*
    
    
        if(My_result>=50) ...
    
        // calculate roll for Your_*
    
    
        if(Your_result>=50) ...
    
    
        // ask for quitting here
    
    }

    I believe you can fill in the rest yourself.
    Last edited by the_jackass; 10-25-2017 at 01:35 PM.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help On Keeping Score For Dice Game
    By AdAstra in forum C Programming
    Replies: 1
    Last Post: 03-18-2011, 06:08 PM
  2. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2009, 04:08 PM
  3. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2009, 04:35 PM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  5. Craps the Dice Game
    By ketchup57v in forum Game Programming
    Replies: 11
    Last Post: 12-01-2001, 04:53 AM

Tags for this Thread