Results 1 to 4 of 4
  1. #1
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Critique my first Java Program!

    Our first assignment in my computer science was to make the game Pig. It was the very first thing we did in the class and i was hoping to get some feedback on the program i wrote for it. For those of you who dont know how the game works:
    The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions:

    roll - If the player rolls a
    1: the player scores nothing and it becomes the opponent's turn.
    2 - 6: the number is added to the player's turn total and the player's turn continues.
    hold - The turn total is added to the player's score and it becomes the opponent's turn.
    Also, i should note our professor has wrote a quick shortcut code library, so you may notice that in there, specifically for printLine etc..
    Java Code:
    public class Pig extends ljing.Program {
    
    public static void main(String[] args) {
    		new Pig().run();
    }
    
    public void run() {
    		int PlayerOne = 0;
    		int PlayerTwo = 0;
    		boolean player1turn = true;
    		java.util.Random die = new java.util.Random();
    		int turnPoints = 0;
    		boolean player2turn = false;
    
    		// need to modify keep going game condition
    		while (PlayerOne <=100 && PlayerTwo <= 100){
    		while (player1turn) {
    			int roll = die.nextInt(6) + 1;
    
    			if (roll == 1) {
    				System.out.println("Sorry P0, you rolled a " + roll);
    				System.out.println("Score: " + PlayerOne + "- " + PlayerTwo);
    				turnPoints = 0;
    				player2turn = true;
    				player1turn = false;
    				break;
    
    			} else {
    				System.out.println("P0,You rolled a " + roll);
    				turnPoints += roll;
    				printLine("You have " + turnPoints + " points this turn");
    				if (readBoolean("keep going?"))
    				{
    					continue;
    				}
    				
    					 else {
    						PlayerOne += turnPoints;
    						printLine("Score: " + PlayerOne + "- " + PlayerTwo);
    						player1turn = false;
    						player2turn = true;
    						turnPoints = 0;
    						if (PlayerOne >= 100){
    							printLine("Congratulations P0! You win!");
    							System.exit(0);
    							
    							}
    						break;
    						
    						}
    					}
    
    				}
    			
    		
    
    		while (player2turn) {
    			int roll = die.nextInt(6) + 1;
    
    			if (roll == 1) {
    				System.out.println("Sorry P1, you rolled a " + roll);
    				System.out.println("Score: " + PlayerOne + "- " + PlayerTwo);
    				turnPoints = 0;
    				player1turn = true;
    				player2turn = false;
    				break;
    
    			} else {
    				System.out.println("P1,You rolled a " + roll);
    				turnPoints += roll;
    				printLine("You have " + turnPoints + " points this turn");
    				if (readBoolean("Keep Going?"))
    				{
    					continue;
    				}
    				{
    					if (PlayerTwo >= 100) {
    						printLine("Congratulations, player1! You Win! Exiting...");
    						System.exit(0);
    					} else {
    						// if no roll
    						PlayerTwo += turnPoints;
    						printLine("Score: " + PlayerOne + "- " + PlayerTwo);
    						player1turn = true;
    						player2turn = false;
    						turnPoints = 0;
    						if (PlayerTwo >= 100) {
    							printLine("Congratulations P1! You win!");
    							System.exit(0);
    							
    							}
    						break;
    						
    					}
    
    				}
    			}
    
    		}
    
    }
    }
    }
    Thank you for all your feedback!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Critique my first Java Program!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Critique my first Java Program!

    Wait what is db? Sorry newb here

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Critique my first Java Program!

    My initials

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 05-17-2011, 05:48 PM
  2. Please critique
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 09-24-2010, 04:43 AM
  3. Critique Java Game: Help Me Improve
    By gretty in forum New To Java
    Replies: 1
    Last Post: 07-15-2010, 05:30 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •