Thread: Dice Game (Fifty)

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by PhantomJoe View Post
    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).
    1. check your code, will it even reach that value (50) while it is looping like crazy? Look at your numbers. If that would stop it at 50 and it is not. one of two things, 1, it is not reaching 50, or that is bad code.

    because that is not stopping your loop.

    2. How will/does quit ever have an opportunity to equal 'q'?

    3. check post #13 for your bracket assignment and unused char's

  2. #17
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by userxbw View Post
    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.
    This was helpful, it helped me see that the issue with the Player 2 code being left outside the loop and that was an easy fix so thank you.

    Here's what it looks like now.
    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);
    
    
    
    
    			return 0;
    		}
    
    
    	}
    }
    This code executes with each player's results being displayed for the first round of dice. I still need to incorporate a button-press for each player to create the back and forth part of the program.

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    as
    the_jackass points out as well

    the key points (so far): how does your variable 'quit' get a q so it can be matched within your while loop condition?
    I already left a CLUE in my post of your code on how to do that "button-press for each player" , something you did not code, look into its return value that will give you your answer to how to get something to quit your while loop. whence you get that worked out, then you can see where you're at on the program.

  4. #19
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Theres a problem with the code as you have written, otherwise its fine. When both players cross 50 in one after next turn, the first player should win. More concretely say the current scores are these:

    player 1 = 46
    player 2 = 48

    player 1 rolls 4,4, new score is 51

    Game must stop right here. But the code continues, say,

    player 2 rolls 2,2, new score is 53

    Obviously player 1 is the winner but game continued despite this and you have no way to determine who won "first" when you get outside the loop.

    The solution is to check the players score immediately after corresponding dice roll and break out of loop with appropriate message if they have won. Also of course keep the loop condition as only quit!='q'. As I have given in my previous post.

    Edit: Fixed the math

    Edit 2: I dont think the return 0; is where you want it to be.
    Last edited by the_jackass; 10-25-2017 at 02:44 PM.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    after each roll of each user check total of that user , if first user, don't know if this is legal in the context of your programming skills vs class lesion but
    Code:
    while ( quit != 'q')
    {
    code to tally up first user die roll then check it, 
        if (my_result >= 50)
            { // continue causes what behavior within a loop?
                quit = 'q';
                continue;
            // kicks it striaght up to check main loop
            // then quits.
            }
            printf("\nplayer two turns\n"
                     "press enter to roll or q to quit\n");
    
    
    // something here to hold it's place until second user
    // hits enter to roll dice, or get a q 
    
    // now second users rolls dice does tally of that users total.
    //what code should go here to get the same needed effect?
    // would it still need a continue or no?
    
    } // end while loop
    
    print results.
    return 0;
    } // end main.

    link to see how continue and loop flow works.



    C Programming break and continue Statement
    Last edited by userxbw; 10-25-2017 at 02:47 PM.

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Oh my looky looky

    brackets brackets brackets
    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;
        
        
        
       // NOT BEING USED, WHY?
      //ARE THEY REALLY NEEDED?
      //  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("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)
            { check you end brackets 
                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);
     
     
     /**************************************************************************
      * what's all of this here ?????????????????
      * *********************************************************************/
     
                return 0;
            }
     
     
        }
    }
    results
    Code:
    Player 1's turn.  Press 's' to start.
    s
    My first die is 3
    My second die is 5
    Player 2's turn
    My first die is 1
    My second die is 1
    Your score is 5 
    userx@slackwhere:~/bin
    still does not work to specs. do you have any idea what caused that? I do.
    Last edited by userxbw; 10-25-2017 at 03:06 PM.

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    furthermore, just to nit pick look at this output and see if you notice who's seems to be always playing, and who is being left out, and whose score is always being shown.
    Code:
    Player 1's turn.  Press 's' to start.
    s
    My first die is 5
    My second die is 3
    Your score is 0 
    player 2 hit enter to roll
    
    Player 2's turn
    My first die is 6
    My second die is 1
    Your score is 0 
    player 1 hit enter to roll
    
    My first die is 3
    My second die is 2
    Your score is 0 
    
    player 2 hit enter to roll
    does not need brackets because only one line to execute after if statement, brackets are for two or more lines to execute
    after an if statement.
    Code:
                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);
    
    
                total = (My_dice1 + My_dice2);
     
                //Check if players has doubles
                if (My_dice1 == My_dice2)
                {
                    //Check for total 6+6 = 12 double sixes
                    if (total == 12)  //Add 25 points to the sum
                        My_result += 25; //Check for double threes               
                    else if (total == 6) // 3 + 3 = 6
                        My_result = 0; //Assign zero to the sum 
                    else
                        My_result += 5; //the only thing left for doubles
                }
    it results in faster code execution, because it does not have to check each value in the two vars against the value it is being check against. that cuts it down from, four checks to two checks then your else clause.
    Last edited by userxbw; 10-25-2017 at 03:38 PM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw
    does not need brackets because only one line to execute after if statement, brackets are for two or more lines to execute
    One school of thought is that it is good to always use parentheses so as to avoid mistakes later, e.g.,
    Code:
    if (x)
        foo();
    could be later modified by accident to be:
    Code:
    if (x)
        foo();
        bar();
    when in fact this was intended:
    Code:
    if (x)
    {
        foo();
        bar();
    }
    Placing the parentheses from the start, even when not required, may help to avoid such a mistake.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    One school of thought is that it is good to always use parentheses so as to avoid mistakes later, e.g.,
    Code:
    if (x)
        foo();
    could be later modified by accident to be:
    Code:
    if (x)
        foo();
        bar();
    when in fact this was intended:
    Code:
    if (x)
    {
        foo();
        bar();
    }
    Placing the parentheses from the start, even when not required, may help to avoid such a mistake.
    if one pays attention to details then they do not need be so rigged in using only one school of thought. put brackets around everything just to play it safe.

    I am just point out how C is written to be.
    Code:
    Syntax
    
    
    selection-statement:
    if (  expression  )  statement
    if (  expression  )  statement  else  statement
    
    
    if ( i > 0 )  
        y = x / i;  
    else   
    {  
        x = i;  
        y = f( x );  
    }  
    
    if ( i > 0 )           /* Without braces */  
        if ( j > i )  
            x = j;  
        else  
            x = i;  
    
    if ( i > 0 )   
    {                      /* With braces */  
        if ( j > i )  
            x = j;  
    }  
    else  
        x = i;
    where here:
    Code:
                    for (; left > 0; left -= newW);
                        for (; top > 0; top -= newH);
    
                    for (x = left; x < img.screenW; x += newW)
                        for (y = top; y < img.screenH; y += newH)
    
                    imlib_blend_image_onto_image (imgdata.image, 0, 0, 0, h,w, x, y, newW / space_tile_gap((rand() % 3)) , newH / space_tile_gap((rand() % 3)));
    if I was to bracket my for loops then it would not render properly .

    if Statement (C)
    Last edited by userxbw; 10-25-2017 at 06:00 PM.

  10. #25
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by userxbw View Post
    furthermore, just to nit pick look at this output and see if you notice who's seems to be always playing, and who is being left out, and whose score is always being shown.
    Code:
    Player 1's turn.  Press 's' to start.
    s
    My first die is 5
    My second die is 3
    Your score is 0 
    player 2 hit enter to roll
    
    Player 2's turn
    My first die is 6
    My second die is 1
    Your score is 0 
    player 1 hit enter to roll
    
    My first die is 3
    My second die is 2
    Your score is 0 
    
    player 2 hit enter to roll
    does not need brackets because only one line to execute after if statement, brackets are for two or more lines to execute
    after an if statement.
    Code:
                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);
    
    
                total = (My_dice1 + My_dice2);
     
                //Check if players has doubles
                if (My_dice1 == My_dice2)
                {
                    //Check for total 6+6 = 12 double sixes
                    if (total == 12)  //Add 25 points to the sum
                        My_result += 25; //Check for double threes               
                    else if (total == 6) // 3 + 3 = 6
                        My_result = 0; //Assign zero to the sum 
                    else
                        My_result += 5; //the only thing left for doubles
                }
    it results in faster code execution, because it does not have to check each value in the two vars against the value it is being check against. that cuts it down from, four checks to two checks then your else clause.
    Again, thank you for the tip. I've incorporated the use of total as a variable in place of the 2 variables I had been using. I've also corrected the issue where Player 1's score was never printing to the screen. My current code goes through 1 iteration performing the correct checks of the die, calculating the score, and printing the score to the screen.

    What I'm still struggling to solve for is how to add the back and forth portion of the program (where player is prompted to either roll his dice or quit and then the same for player 2 once it is their turn.)

    Here's what my code looks like now:
    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;
    	int total; 
    	char quit = NULL;
    	char start;
    	char toPlay1 = NULL;
    	char toPlay2 = NULL;
    
    
    	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);
    				total = (My_dice1 + My_dice2);
    			
    			//Check if players has doubles
    			if (My_dice1 == My_dice2)
    			{
    				//Check for double sixes
    				if (total == 12) {
    					My_result = My_result + 25;
    					//Add 25 points to the sum
    				}
    				//Check for doule threes
    				else if (total == 6) {
    					My_result = 0;
    					printf("You rolled double 3s and your score has been reset to 0.\n");
    					//Assign zero to the sum
    				}
    				else {
    					My_result = My_result + 5;
    					//Executes for remaining other doubles
    					//Add 5 points to the sum
    
    
    				}//this brace closes off the check for doubles if statement
    			
    			}
    			printf("Player 1's score is %d \n", My_result);
    
    
    			printf("\nPlayer 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);
    			total = (Your_dice1 + Your_dice2);
    
    
    			//Check if players has doubles
    			if (Your_dice1 == Your_dice2)
    			{
    				//Check for double sixes
    				if (total == 12)
    				{
    					Your_result = Your_result + 25;
    					//Add 25 points to the sum
    				}
    				//Check for doule threes
    				else if (total == 6)
    				{
    					Your_result = 0;
    					printf("You rolled double 3s and your score has been reset to 0.\n");
    					//Assign zero to the sum
    				}
    				else {
    					Your_result = Your_result + 5;
    					//Executes for remaining other doubles
    					//Add 5 points to the sum
    				}
    			}
    			printf("Player 2's score is %d \n", Your_result);
    
    
    			return 0;
    		}
    	}
    }

  11. #26
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by PhantomJoe View Post
    Again, thank you for the tip. I've incorporated the use of total as a variable in place of the 2 variables I had been using. I've also corrected the issue where Player 1's score was never printing to the screen. My current code goes through 1 iteration performing the correct checks of the die, calculating the score, and printing the score to the screen.

    What I'm still struggling to solve for is how to add the back and forth portion of the program (where player is prompted to either roll his dice or quit and then the same for player 2 once it is their turn.)

    Here's what my code looks like now:
    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;
        int total; 
        char quit = NULL;
        char start;
        char toPlay1 = NULL;
        char toPlay2 = NULL;
    
    
        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);
                    total = (My_dice1 + My_dice2);
                
                //Check if players has doubles
                if (My_dice1 == My_dice2)
                {
                    //Check for double sixes
                    if (total == 12) {
                        My_result = My_result + 25;
                        //Add 25 points to the sum
                    }
                    //Check for doule threes
                    else if (total == 6) {
                        My_result = 0;
                        printf("You rolled double 3s and your score has been reset to 0.\n");
                        //Assign zero to the sum
                    }
                    else {
                        My_result = My_result + 5;
                        //Executes for remaining other doubles
                        //Add 5 points to the sum
    
    
                    }//this brace closes off the check for doubles if statement
                
                }
                printf("Player 1's score is %d \n", My_result);
    
    
                printf("\nPlayer 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);
                total = (Your_dice1 + Your_dice2);
    
    
                //Check if players has doubles
                if (Your_dice1 == Your_dice2)
                {
                    //Check for double sixes
                    if (total == 12)
                    {
                        Your_result = Your_result + 25;
                        //Add 25 points to the sum
                    }
                    //Check for doule threes
                    else if (total == 6)
                    {
                        Your_result = 0;
                        printf("You rolled double 3s and your score has been reset to 0.\n");
                        //Assign zero to the sum
                    }
                    else {
                        Your_result = Your_result + 5;
                        //Executes for remaining other doubles
                        //Add 5 points to the sum
                    }
                }
                printf("Player 2's score is %d \n", Your_result);
    
    
                return 0;
            }
        }
    }
    Also, the chars toPlay1 and toPlay2 were added thinking I would need 2 variables for each player for the button press piece of the code. I.e. - "Player press 'a' to roll your dice or press 'q' to quit" where 'a' would be the value for toPlay1. But I'm not sure where or how to implement this for player1 or for player2.

  12. #27
    Banned
    Join Date
    Aug 2017
    Posts
    861
    have you actually compiled and ran this?
    Code:
    Press q to quit.
    Player 1's turn.  Press 's' to start.
    s
    My first die is 2
    My second die is 5
    Player 1's score is 0 
    
    Player 2's turn
    My first die is 4
    My second die is 1
    Player 2's score is 0 
    userx@slackwhere:~/bin
    I too was speaking of your terminology for player1 and player2
    Code:
    Player 1's turn.  Press 's' to start.
    s
    player 1 first die is 4
    player 1 die is 4
    player 1 score is 5 
    
    player 2 hit enter to roll
    
    Player 2's turn
    player 2 die is 3
    player 2 die is 5
    player 2 score is 0 
    
    player 1 hit enter to roll
    Plus your code only run's ( loops) once then ends ( stops, quits) before any of your conditionals are met, 50 or q. Check your brackets again, where is the return 0 suppose to be? if you're writing a function as soon as it gets to the return it ends the function. main is a function.


    Code:
     char toPlay1 = NULL; char toPlay2 = NULL;
    you will / should / will not need them to get your loop to stop mid stream.

    a means to get the q to quit the loop is not yet been implemented.
    Last edited by userxbw; 10-25-2017 at 06:28 PM.

  13. #28
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Please don't use things such as char toPlay1 = NULL; Assigning NULL to a character variable is extremely uncommon (I can't recall ever seeing that used) and makes scanning code for mistakes more difficult.

  14. #29
    Banned
    Join Date
    Aug 2017
    Posts
    861
    blocking code using brackets and if statements.

    Code:
    printout_for_input
    
    get_input
    
       if ( input == "condition")
       {
    
            while ( condition is true  )
           { 
                      // code out here will always be ran within this loop
                  
                    if ( input == 'condition') 
                    {
                            // code in here will only be ran when you
                            // want / need it to be ran depending on  your 
                           // if statement that is acting as protective wrapper 
                            // around the code inside of it
                                   
                               
                    } // end if //<-- good practice to comment your end brackets
                       // as needed to keep track of matching open / close brackets. along with alignment. 
    
                   // code out here will always be ran within this loop
    
                  if( a<d)
                 {
                         // code in here will only be ran when you
                            // want / need it to be ran depending on  your 
                           // if statement that is acting as protective wrapper 
                            // around the code inside of it
                  } 
         // code out here will always be ran within this loop
        
    
    
    } // end loop
    }// end if
    Last edited by userxbw; 10-25-2017 at 07:07 PM.

  15. #30
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by userxbw View Post
    have you actually compiled and ran this? //Yes, my code in its current format will output the below but just a single time without prompting for the continuation of play.
    Code:
    Press q to quit.
    Player 1's turn.  Press 's' to start.
    s
    My first die is 2
    My second die is 5
    Player 1's score is 0 
    
    Player 2's turn
    My first die is 4
    My second die is 1
    Player 2's score is 0 
    userx@slackwhere:~/bin
    I too was speaking of your terminology for player1 and player2
    Code:
    Player 1's turn.  Press 's' to start.
    s
    player 1 first die is 4
    player 1 die is 4
    player 1 score is 5 
    
    player 2 hit enter to roll
    
    Player 2's turn
    player 2 die is 3
    player 2 die is 5
    player 2 score is 0 
    
    player 1 hit enter to roll
    Plus your code only run's ( loops) once then ends ( stops, quits) before any of your conditionals are met, 50 or q. Check your brackets again, where is the return 0 suppose to be? if you're writing a function as soon as it gets to the return it ends the function. main is a function. //Honestly I thought the return 0; was in the right location. I just tested moving it further down (outside of the next brace and then the next brace) and all that did was create an infinite loop.


    Code:
     char toPlay1 = NULL; char toPlay2 = NULL;
    you will / should / will not need them to get your loop to stop mid stream. //So I don't need those variables at all?

    a means to get the q to quit the loop is not yet been implemented.
    I've added some comments to your reply to address each of your points to help me organize my thoughts.

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