Results 1 to 3 of 3

Thread: Pig dice game

  1. #1
    dangyounoobs is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default Pig dice game

    Hello. I am new here and to java. Been learning for about a month now. This code works but I wanted to know if I could shorten it. Like use any shortcuts? This is based on the dice game, Pig. I changed it up a little to make it easier to code. So if you have any tips post. thanks.

    Java Code:
    import java.util.*;
    
    
    public class Pig {
    	
    	public static void main(String[] args){
    		
    		Random rand = new Random();
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.println("Welcome to the game of PIG!");
    		System.out.println("Both players start off with zero points");
    		System.out.println("First player to 100 points or more wins");
    		System.out.println("How many rounds?");
    		int numOfRounds = scan.nextInt();
    		
    		int round = 1;
    		int compScore = 0;
    		int playerScore = 0;
    		int compToss = 0;
    		int playerToss = 0;
    		int pRound = 0;
    		int cRound = 0;
    		
    		while(round <= numOfRounds){
    			compScore = 0;
    			playerScore = 0;
    			compToss = 0;
    			playerToss = 0;
    			
    			while(!(compScore >= 100 || playerScore >= 100)){
    				System.out.println("Computers turn");
    			
    				int roll1 = rand.nextInt(6)+1;
    				int roll2 = rand.nextInt(6)+1;
    				int roll3 = rand.nextInt(6)+1;
    				compToss = roll1 + roll2 + roll3;
    				compScore = compScore + compToss;
    			
    				System.out.println("Computer rolled a " +roll1+ ", " +roll2+ ", and " + roll3);
    				System.out.println("Computer score is now: " + compScore);
    				
    				
    				System.out.println("Players turn");
    				System.out.print("Press any button to roll: ");
    				String x = scan.next();
    				
    				if(x.equals("x")){
    					playerToss = rand.nextInt(6)+1;
    				}
    				System.out.println("Player rolled a " + playerToss);
    				
    				while(playerToss != 1){
    					playerScore = playerScore + playerToss;
    					System.out.println("Player score is now: " + playerScore);
    					
    					System.out.print("Press any button to roll or \'end\' to end turn: ");
    					x = scan.next();
    					if(x.equals("end")){
    						break;
    					}else{
    						playerToss = rand.nextInt(6)+1;
    					}
    					System.out.println("Player rolled a " + playerToss);
    				}
    				
    				if(playerToss == 1){
    					playerScore = 0;
    					System.out.println("Player got a 1. Score reset to 0");
    				}
    				
    				System.out.println("Players turn is over. Computers turn");
    			}
    			
    			if(compScore > playerScore){
    				System.out.println("Computer is the winner!");
    				cRound++;
    			}else{
    				System.out.println("Player is the winner!");
    				pRound++;
    			}
    			round++;
    		}
    		System.out.println("---- Final Score ----");
    		System.out.println("Computer:  " + cRound + "   Player:  " + pRound);
    	}
    }

  2. #2
    ripstar14 is offline Member
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Default Re: Pig dice game

    Hello, I have read your code and I too am new to the java language. Now, when I got to do this program, I found this an yeah... A way you can shorten your code is by doing a Die class, or that you have another Class to call in while running your game

    Taken from the book that is used in my class is this
    Java Code:
    import java.util.Random;
    
    public class Die
    {
       private final int MIN_Faces = 4;
       
       private static Random GEN = new Random();
       private int numFaces;
       private int faceVaule;
       
       public Die ()
       {
           numFaces = 6;
           faceVaule = 1;
       }
       
       public Die (int faces)
       {
           if( faces < MIN_Faces)
           numFaces = 6;
           else
           numFaces = faces;
           faceVaule = 1;
       }
       public int roll ()
       {
           faceVaule = GEN.nextInt(numFaces) + 1;
           return faceVaule;
       }
       public int getFaceValue ()
       {
           return faceVaule;
       }
    }
    What you do is have your games code in one form an this in the other, the game will call this class to roll the dice
    Last edited by JosAH; 12-07-2013 at 10:55 AM. Reason: added [code] ... [/code] tags

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,946
    Rep Power
    4

    Default Re: Pig dice game

    This thread is over two years old. I doubt the OP cares anymore.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Need help with AI in roll dice game
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 11:49 AM
  2. Dice game help and arrays
    By SnarkKnuckle in forum New To Java
    Replies: 4
    Last Post: 03-10-2011, 01:57 AM
  3. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 11:50 AM
  4. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 01:01 AM
  5. help debugging a dice game
    By Windoze in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •