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 3 of 3

Thread: Dice game help, a little cleanup

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Dice game help, a little cleanup

    I've programmed a dice game for pig which utilizes a single die. The points are suppose to accumulate during your turn unless you hit a 1 and they are zeroed out and nothing added to your total or you can hold your turn points before a 1 is rolled and just add them to the total passing it to the next player. My turn total points and the accumlated points do not seem to hold up correctly. When I hit hold, it's only taking points from current turn. If I hit a 1 it adds it along with the rest of the turn and doesn't 0 out for that current turn. And the turn and total are remaining the same now. Everytime I change something I get a new problem. So I thought I would go ahead and post here before to much happened. Also for giggles, I need to use an array or arraylist somewhere, maybe to keep points? Is this possible, what would I need to do. Sorry so long winded, following is all my code.

    import java.util.Scanner;  //Imports scanner to be used for rolling / holding actions
     
    public class Pig  // Begin Class Pig
    {  
     
     // Declaring memory used in game
     int player1Total, player2Total; 
     int dice;  
     int player1TurnPoints, player2TurnPoints;    
     Boolean player1Turn = true;  
     Boolean player2Turn = true;  
     
     // Sets roll method and face value for dice, calls random
     public void roll()  
    	 {  
    	 dice = (int)(Math.random()*6) + 1;  
    	 }  
     
     // Keeps track of points during current turn, dice value, plus turn points		   
     public int player1TurnScore()  
    	 {  
    		 {  
    			 player1TurnPoints = dice + player1TurnPoints;  
    			 System.out.println("Player 1 scored: " + player1TurnPoints + " in their turn.");  
    		 } return player1TurnPoints;  
    	 } 
     
    //Keeps track of points during current turn, dice value, plus turn points	   
     public int player2TurnScore()  
    	 {  
    		 {  
    			 player2TurnPoints = dice + player2TurnPoints;  
    			 System.out.println("Player 2 scored: " + player2TurnPoints + " in their turn.");  
    		 } return player2TurnPoints;  
    	 }
     
     // Declares if points are "0" you lose turn
     public void player1TurnZero()
     {
     	//player1TurnPoints = 0;
     	System.out.println("Player 1 loses turn.");
     } 
     
    //Declares if points are "0" you lose turn
     public void player2TurnZero()
     {
     	//player2TurnPoints = 0;
     	System.out.println("Player 2 loses turn.");
     } 
     
     
     
     // Declares whose turn it should be 
     public Pig()  
     {  
    	player1();  
    	 //if(!player1Turn)  
    	 //{  
    		// player2();  
    	// }  
     }  
     
     // Starts the game and rolls die for player 1
     public int player1(){  
     
    	 System.out.println("Player 1 hit 'r' to roll your die.");  
    	 Scanner key = new Scanner(System.in);  
    	 String start = key.nextLine();  
    	 if(start.equalsIgnoreCase("r")){  
    					 {  
    					 System.out.println("The die has been rolled.");  
    					 roll();  
    					 }  
    	 }  
    	 do{  
    		  roll();
     
     
    			 if(dice == 1)  // Lose your turn when a 1 is rolled
    			 {  
    				 System.out.println("1 has been rolled, you lose your turn.");
    				 player1TurnZero();			
    				 player2();  
     
    			 }  
    			 else if(dice != 1)  // Shows what you rolled on your turn
    			 {  
    				 System.out.println(dice + " Has been rolled");
    				 player1TurnScore();
    				 System.out.println("Player 1 total score so far is: " + player1TurnPoints);    
    				 System.out.println("Player 1 press 'r' to roll or 'h' to hold.");
     
    				 Scanner keyboard = new Scanner(System.in);  
    				 String choice = keyboard.nextLine();  
     
    				 // What happens when you press r or h
    				 if(choice.equalsIgnoreCase("r"))  
    				 {					 
    					 {    
    					 System.out.println("Rolling again.");   
    					 roll();  
    					 }  
    				 }  
    				// What happens when you press r or h
    				 if(choice.equalsIgnoreCase("h"))  
    					 {						 
    					 System.out.println("Player 1, your points thus far is " + player1Total);
    					 player2();  // Switches to Player 2 Turn
    					 }	 
     
    			 }  
     
    			 player1Total += player1TurnPoints; // Adds turn points to Total
    			 if(player1Total >= 100)	 // You win once you beat 100 points
    			 {  
    				 System.out.println("Player 1 wins, way to go champ!");  
    				 System.exit(0);  
    			 }  
     
    		}while(player1Turn);	 
    	 return dice;  
     
     }  
     
     
    //Starts the game and rolls die for player 2
     public int player2(){  
     
    	 System.out.println("Player 2 hit 'r' to roll your die.");  
    	 Scanner key = new Scanner(System.in);  
    	 String start = key.nextLine();  
    	 if(start.equalsIgnoreCase("r")){  
    					 {  
    					 System.out.println("The die has been rolled.");  
    					 roll();  
    					 }  
    	 }  
    	 do{  
    		  roll();
     
     
    		     if(dice == 1)  // Lose your turn when a 1 is rolled
    			 {  
    				 System.out.println("1 has been rolled, you lose your turn.");
    				 player2TurnZero();			
    				 player1();  
     
    			 }  
    			 else if(dice != 1)  // Shows what you rolled on your turn
    			 {  
    				 System.out.println(dice + " Has been rolled");
    				 player2TurnScore();
    				 System.out.println("Player 2 total score so far is: " + player2TurnPoints);   
    				 System.out.println("Player 2 press 'r' to roll or 'h' to hold.");  
    				 Scanner keyboard = new Scanner(System.in);  
    				 String choice = keyboard.nextLine();  
     
    				 // What happens when you press r or h
    				 if(choice.equalsIgnoreCase("r"))  
    				 {					 
    					 {    
    					 System.out.println("Rolling again.");   
    					 roll();  
    					 }  
    				 }  
    				// What happens when you press r or h
    				 if(choice.equalsIgnoreCase("h"))  
    					 {						 
    					 System.out.println("Holding, your points thus far is " + player2Total);
    					 player1();  // Switches to Player 1 Turn
    					 }	 
     
    			 }  
     
    		     player2Total += player2TurnPoints; // Adds turn points to Total
    			 if(player2Total >= 100)	 // You win once you beat 100 points
    			 {  
    				 System.out.println("Player 2 wins, way to go champ!");  
    				 System.exit(0);  
    			 }  
     
    		}while(player2Turn);	 
    	 return dice;  
     
     }  	 
     
    public static void main(String[] args)  
    {  
     new Pig();  
    }  
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Dice game help, a little cleanup

    An array holds a bunch of objects, either primitives(int, float, double, etc) or objects(Object, String, Integer, Character, etc)

    It has a fixed size

    An int array of size 5 looks like this

    int[] anArray = new int[5];

    Note, indexing starts at 0 and ends at size -1.

    You must set all values in array as they start out null.

    anArray[0] = 5;
    You cannot go beyond fixed size.

    ArrayList is generic. It can keep expanding.

    ArrayList<Integer> anArrayList = new ArrayList<Integer>();

    anArrayList.add(5);

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    SnarkKnuckle (February 28th, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Dice game help, a little cleanup

    Thanks for the array info, I think I see how I"ll utilize those. However before I do any ideas on how to correct how the points are being passed?

Similar Threads

  1. [SOLVED] Help printing random dice
    By vanDarg in forum Java Theory & Questions
    Replies: 12
    Last Post: February 1st, 2011, 03:09 PM
  2. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 11:49 AM
  3. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM
  4. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM