Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Pig Dice Game with the Computer / User Input

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Pig Dice Game with the Computer / User Input

    I have been working on this program for about a week now and I think I have it down pack. The issue I am having when I ask the user at the beginning of the game (human vs computer) every time I run the program it asks me what my name is again. Here is what I have thus far:

     import java.util.Scanner;  
     
     public class Assignment  
     {  
         Scanner usersName;  
         Boolean humanTurn = true;  
         Boolean computerTurn = true;  
         int dice;  
         int humanTurnPoints, computerTurnPoints;  
         int humanTotalPoints = 0;
         int computerTotalPoints = 0;
    	 private Scanner keyboard;
    	 private Scanner key;	
     
    	 //System.out.print("Please enter your name: ");
    	 //usersName = new Scanner(System.in);  
         //setStart(usersName.nextLine());
     
         public void roll()  
             {  
             dice = (int)(Math.random()*6) + 1;                   
             }  	
     
         public int humanTurnScore()  
             {  
                 {  
                     humanTurnPoints = dice + humanTurnPoints;  
                     System.out.println("You threw: " + dice);
                     System.out.println("You have scored: " + humanTurnPoints + " in your turn.");  
                 } return humanTurnPoints;  
             } 
         public void humanTurnZero()
         {
         	humanTurnPoints = 0;
         } 
     
         public int computerTurnScore()  
             {  
                 {  
                     computerTurnPoints = dice + computerTurnPoints;  
                     System.out.println("Computer has scored: " + computerTurnPoints + " in its turn.");  
                 } return computerTurnPoints;  
             }     
          public void computerTurnZero()
        	 {
         		computerTurnPoints = 0;
        	 } 
     
         public Assignment()  
         {  
             humanGame();  
             if(!humanTurn)  
             {  
                 computerTurn();  
             }  
         }  
         public int humanGame()
         	{ 
     
             System.out.println("To start the game please press 'r'.");  
             key = new Scanner(System.in);  
             String start = key.nextLine();  
             if(!start.equalsIgnoreCase("R")) 
             {
             		System.out.println("Make sure you are pressing 'r'.");
             		humanGame();
             }
     
             if(start.equalsIgnoreCase("R"))
             	{                          
                             System.out.println("You pressed 'r'.");  
                             System.out.println("Lets start.");           
     
     
                 do{  
                     roll();	
     
     
                     if(dice == 1)  
                     {  
                         System.out.println("You got 1 and you lost your turn.");
                         System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                         humanTurnZero();            
                         computerTurn();  
     
                     }  
                     else if(dice != 1) 
                     {
     
                         humanTotalPoints += dice; 	
                         	if(humanTotalPoints >= 100)    
                     	 	{  
                     	 		System.out.println("You threw: " + dice);
                     	 		System.out.println("Your GRAND TOTAL score is: " + humanTotalPoints);
                       	 		System.out.println("Congratulations, you win!");  
                       	 		System.exit(0);  
                     	 	} 
                         humanTurnScore();	
                         System.out.println("Your GRAND TOTAL score is: " + humanTotalPoints);  
                         System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                         System.out.println("You can hold or roll again.");  
                         System.out.println("To roll again press 'r' or 'h' to hold.");  
                         keyboard = new Scanner(System.in);  
                         String choice = keyboard.nextLine();  
     
                         if(choice.equalsIgnoreCase("R"))                                  
                             {  
                               System.out.println("You pressed 'r'.");  
                               System.out.println("Lets roll again.");   
                               roll();           
     
    			                   	 if(!choice.equalsIgnoreCase("R"))
    			                   	 	{
    			                   	 		System.out.println("You didn't press 'r'. To make sure the program is running correctly please press 'r' to roll or 'h' to hold.");
    			                   	 	    humanGame();
    			                   	 	}
                       	 	}
     
                         if(choice.equalsIgnoreCase("h"))  
                             {                         
                             System.out.println("You pressed 'h' and loose your turn.");
                             System.out.println("Your Grand total is: " + humanTotalPoints);
                             humanTurnZero();  
                             computerTurn();  
                             }     
     
                     	}  
     
                }while(humanTurn);     
     
             }return dice;	
         }  
     public int computerTurn()  
     {  
         System.out.println("Now it's computer turn.");  
     
         do {  
             roll();  
     
             if(dice != 1)  
             {  
             	 computerTotalPoints += dice; 
             	 	if(computerTotalPoints >=100)  
                     	{  
                     		System.out.println("Computer threw: " + dice);
                     		System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
             				System.out.println("Game Over! the computer wins");  
             				System.exit(0);  
             			} 	
                 System.out.println("Computer threw: " + dice);
                 System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                 System.out.println("Your Grand total is: " + humanTotalPoints);
                 computerTurnScore();  
                 roll();  
             }  
     
             if(dice == 1)   
             {  
                 System.out.println("Computer thrown 1 therefore it's your turn now.");  
                 computerTurnZero();  
                 humanGame();  
             }  
     
             if(computerTurnPoints >= 20)  
             {  
                 System.out.println("Computer scored already " + computerTurnPoints + " you'd better start to focus.");  
                 System.out.println("Please play again");  
                 humanGame();  
             }   
     
         }while (computerTurn);  
         return dice;  
     
     }     
     
     
     public static void main(String[] args)  
     {  
         new Assignment();  
     }  
     }

    I commented out (up close to the top of the program) where I am asking the user at the beginning of the program what their name is. I actually need in all the System.out.println where it says 'You' I need it to say the usersName.

    Would someone please help me with this program. I know someone is kind enough to help me out here.

    Thank you in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    I commented out (up close to the top of the program) where I am asking the user at the beginning of the program what their name is.
    Can you explain why you did that and what the problem was with the code before the changes were made?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    I commented it out because I think I need these statements in the code but I am not sure where I need to put them. I need to be able to ask the user their name at the beginning of the program and not asked again.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    need to be able to ask the user their name at the beginning of the program
    Constructors are often only called once. Is that true for your game? If so, the code could be placed in the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    Yes, Only once.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    Did that work when you tried it?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    Norm, because I am a beginner I am not sure how to do that. I tried to put something like
    private String usersName = ' ';
    but not sure how to ask the question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    I don't really understand how the person and computer take turns in this program. If the two players were to take multiple turns in a single game, the flow might be something like:

    Ask user's name, store in a variable

    Start loop:
    - user's turn (use name)
    - computer's turn
    - present results
    End loop, continue looping until current game ends

    Ask user (use name) if another game is desired
    - if another game is desired, start another game (either another loop or a new game entirely)
    - otherwise, exit

    You looping does not follow this design, and maybe it shouldn't. If you were to explain how the game is supposed to work, we could help you fix the looping.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    The code to get the user's name was commented out. What happens when that code is uncommented and moved to the constructor?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pig Dice Game with the Computer / User Input

    Ok, I have worked the program out a little bit more. But when I run it it is not printing out the users name.

    My New Program is this:

     import java.util.Scanner;  
     
     public class Assignment  
     {  
    	 String usersName="";
    	 Boolean humanTurn = true;
    	 Boolean computerTurn = true;
    	 int dice;
    	 int humanTurnPoints, computerTurnPoints;
    	 int humanTotalPoints = 0;
    	 int computerTotalPoints = 0;
    	 private Scanner keyboard;
    	 private Scanner key;	 
     
         public void roll()  
             {  
             dice = (int)(Math.random()*6) + 1;                   
             }  	
     
         public int humanTurnScore()  
             {  
                 {  
                	 humanTurnPoints = dice + humanTurnPoints;
                	 System.out.println(usersName + "threw: " + dice);
                	 System.out.println(usersName + "have scored: " + humanTurnPoints + " in your turn.");  
                 } return humanTurnPoints;  
             } 
         public void humanTurnZero()
         {
         	humanTurnPoints = 0;
         } 
     
         public int computerTurnScore()  
             {  
                 {  
                     computerTurnPoints = dice + computerTurnPoints;  
                     System.out.println("Computer has scored: " + computerTurnPoints + " in its turn.");  
                 } return computerTurnPoints;  
             }     
          public void computerTurnZero()
        	 {
         		computerTurnPoints = 0;
        	 } 
     
         public Assignment(String usersName)  
         {  
     
        	 humanGame();  
             if(!humanTurn)  
             {  
                 computerTurn();  
             }  
         }  
         public int humanGame()
         	{  	          
             System.out.println("To start the game please press 'r'.");  
             key = new Scanner(System.in);  
             String start = key.nextLine();  
             if(!start.equalsIgnoreCase("R")) 
             {
             		System.out.println("Make sure you are pressing 'r'.");
             		humanGame();
             }
     
             if(start.equalsIgnoreCase("R"))
             	{                          
            	 System.out.println(usersName + "pressed + 'r'.");
            	 System.out.println("Lets start.");           
     
     
                 do{  
                     roll();	   
                     if(dice == 1)  
                     {  
                    	 System.out.println(usersName + " got 1 and you lost your turn.");
                    	 System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                    	 humanTurnZero();
                    	 computerTurn();  
     
                     }  
                     else if(dice != 1) 
                     {
     
                         humanTotalPoints += dice; 	
                         	if(humanTotalPoints >= 100)    
                     	 	{  
                         		System.out.println(usersName + " threw: " + dice);
                         		System.out.println(usersName +" r GRAND TOTAL score is: " + humanTotalPoints);
                         		System.out.println("Congratulations, you win!");
                         		System.exit(0);  
                     	 	} 
                         	humanTurnScore();
                         	System.out.println(usersName +" r GRAND TOTAL score is: " + humanTotalPoints);
                         	System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                         	System.out.println(usersName + " can hold or roll again.");
                         	System.out.println("To roll again press 'r' or 'h' to hold.");
                         	keyboard = new Scanner(System.in);
                         	String choice = keyboard.nextLine();
     
                         	if (choice.equalsIgnoreCase("R"))                                 
                             {  
                         		System.out.println(usersName + " pressed 'r'.");  
                               System.out.println("Lets roll again.");   
                               roll();           
     
    			                   	 if(!choice.equalsIgnoreCase("R"))
    			                   	 	{
    			                   		System.out.println(usersName + " you didn't press 'r'. To make sure the program is running correctly please press 'r' to roll or 'h' to hold.");
    			                   	 	    humanGame();
    			                   	 	}
                       	 	}
     
                         if(choice.equalsIgnoreCase("h"))  
                             {                         
                        	 System.out.println(usersName + " pressed 'h' and loose your turn.");
                        	 System.out.println(usersName + "'s Grand total is: " + humanTotalPoints);
                             humanTurnZero();  
                             computerTurn();  
                             }     
     
                     	}  
     
                }while(humanTurn);     
     
             }return dice;	
         }  
     public int computerTurn()  
     {  
         System.out.println("Now it's computer turn.");  
     
         do {  
             roll();  
     
             if(dice != 1)  
             {  
             	 computerTotalPoints += dice; 
             	 	if(computerTotalPoints >=100)  
                     	{  
                     		System.out.println("Computer threw: " + dice);
                     		System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
             				System.out.println("Game Over! the computer wins");  
             				System.exit(0);  
             			} 	
                 System.out.println("Computer threw: " + dice);
                 System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                 System.out.println(usersName + " Grand total is: " + humanTotalPoints);
                 computerTurnScore();  
                 roll();  
             }  
     
             if(dice == 1)   
             {  
                 System.out.println("Computer thrown 1 therefore it's your turn now.");  
                 computerTurnZero();  
                 humanGame();  
             }  
     
             if(computerTurnPoints >= 20)  
             {  
                 System.out.println("Computer scored already " + computerTurnPoints + " you'd better start to focus.");  
                 System.out.println("Please play again");  
                 humanGame();  
             }   
     
         }while (computerTurn);  
         return dice;  
     
     }     
     
     
     public static void main(String[] args)  
     {  
    	 System.out.println("Please enter your name:");
    	 Scanner vScanner = new Scanner(System.in);
    	 String vUserName=vScanner.nextLine();
    	 new Assignment(vUserName);
    	 vScanner.close();  
     }  
     }

    Please enter your name:
    Diana
    To start the game please press 'r'.
    r
    pressed + 'r'.
    Lets start.
    threw: 3
    have scored: 3 in your turn.
     r GRAND TOTAL score is: 3
    Computer's GRAND TOTAL score is: 0
     can hold or roll again.
    To roll again press 'r' or 'h' to hold.
    r
     pressed 'r'.
    Lets roll again.
    threw: 2
    have scored: 5 in your turn.
     r GRAND TOTAL score is: 5
    Computer's GRAND TOTAL score is: 0
     can hold or roll again.
    To roll again press 'r' or 'h' to hold.
    r

    This is what it is printing out. Not sure of what I did wrong.

    --- Update ---

    I actually got it to work. I saw where I needed to add two more lines. And now all I need to do is fix some spacing errors. Thank you for everyone's help. It is nice to be able to have a third eye in programming. I am still a beginner but I am determined to work this out. I am sure I will be back. Thanks again

Similar Threads

  1. Dice Game! PLEASE HELP!
    By mhaxton in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2013, 12:47 PM
  2. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  3. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  4. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM
  5. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM