Thread: Monopoly snippet can't find the problem

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Monopoly snippet can't find the problem

    Code:
    int dice; //total of 2 dices
    int doublestatus = 1; //shows whether it is a double or not, default is 1 so user can start rolling
    
    int dicerolling ()
    {
    		srand (time(NULL));
    
    
    		int dice1;
    		int dice2;
    
    
    		dice1 = rand() %6;
    		dice1 = dice1 + 1;
    
    
    		dice2 = rand() %6;
    		dice2 = dice2 + 1;
    
    
    		printf("You rolled");
    		printf(" %d",dice1);
    		printf(" and ");
    		printf("%d. ",dice2);
    
    
    		if (dice1 == dice2)
    		{
    		printf("It's a double!");
    		doublestatus = 1;
    		}
    		else
    		doublestatus = 0;
    	
    
    
    		printf("\nTotal is ");
    		
    		dice=dice1+dice2;
    		
    		
    		
    		printf("%d.\n",dice);
    		
    		
    }
    
    
    
    
    
    
    void menu ()
    {
    	printf("\nMake a decision:\n");
    	printf("1 - Roll the dice\n");
    	printf("2 - Check balance\n");
    	printf("3 - Manage money\n");
    	printf("4 - Show current position\n");
    	printf("5 - End turn\n");
    }	
    	
    
    
    void menualt () //for when user rolls a double
    {
    	printf("\nMake a decision:\n");
    	printf("Dice rolled\n");
    	printf("2 - Check balance\n");
    	printf("3 - Manage money\n");
    	printf("4 - Show current position\n");
    	printf("5 - End turn\n");
    }
    
    
    
    
    int main()
    
    
    {
    	
    
    
    int cursor = 0; //user selection
    
    
    int doublecount = 0; //counts the amount of double (if exceed 3 go to jail)
    
    
    int pos = 0;
    
    
    
    
    menu();	
    printf("Input: ");
    scanf ("%d",&cursor);
    
    
    
    
    	while (cursor != 5)
    	{
    		
    		
    		if (cursor == 1) //rolling the dice, moving and 3 doubles go to jail
    		{
    			while (doublestatus == 1 && doublecount < 3)
    			{
    			dicerolling();
    			doublecount++;
    			pos = pos + dice;
    			printf ("You landed on ");
    			poscount (pos);
    			
    				if (doublestatus == 1)
    				{
    					menu();
    					printf("Input: ");
    					scanf ("%d",&cursor);
    				}
    				else 
    				{
    					
    				
    					while (cursor !=5) //special loop when user already rolled the dice and can not roll again
    					{
    					
    						menualt();	
    						printf("Input: ");
    						scanf ("%d",&cursor);
    					
    						if (cursor == 1)
    						printf("Dice already rolled, try again.\n");
    					
    						else if (cursor == 2)
    						{ }
    					
    						else if (cursor == 3)
    						{ }
    					
    						else if (cursor == 4)
    						{
    						
    							printf("You are at ");
    							poscount (pos);
    							
    						
    						}
    					
    						else
    						{ }	
    					
    					}
    				
    				}
    			if (doublecount == 3)
    			pos = 100; //special position when you are in jail to seperate from visitors
    			}
    		}
    	
    		else if (cursor == 2)
    		{ }
    	
    		else if (cursor == 3)
    		{ }
    	
    		else if (cursor == 4)
    		{ 
    		
    			printf("You are at ");
    			poscount (pos);
    		
    		}
    		else
    		{ }
    	}
    }
    poscount is a function that compare position and generate the name

    what is broken is whenever user rolls a double and then hit 4 to check their position the dice rolls again even though it suppose to show their position
    things work fine if they roll again after the double then check for position

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > what is broken is whenever user rolls a double and then hit 4 to check their position the dice rolls again even though it suppose to show their position
    Perhaps it's because of doublestatus == 1 and that you then miss out all the "special loop when user already rolled the dice" code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the problem in this snippet
    By WoodSTokk in forum C Programming
    Replies: 1
    Last Post: 05-20-2015, 10:52 AM
  2. Help with understanding snippet
    By narendras in forum C Programming
    Replies: 14
    Last Post: 01-07-2012, 04:12 PM
  3. snippet of for loop code problem
    By rayrayj52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 02:36 PM
  4. Bill's Latest Monopoly Urge
    By salvelinus in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-01-2003, 06:10 PM
  5. monopoly..dont worry its not
    By iain in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-16-2001, 04:26 PM

Tags for this Thread