Results 1 to 7 of 7
  1. #1
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Pig Game code gone wrong! Help please.

    First ever attempt at anything like this:

    Java Code:
    import java.util.Scanner;
    public class PigGame {
    
    	/**
    	 * @param args
    	 * @return 
    	 */
    	public static int main(String[] args) {
    		// TODO Auto-generated method stub
    		boolean keepgoing = true;
    		int Roll = 0, turnPoints1 = 0, turnPoints2 = 0;
    		Scanner keyboard = new Scanner(System.in);
    		String Player1, Player2;
    		boolean Player1Go = true, PlayGame = true;	
    		System.out.print("Player 1:\n\tName: ");
    		Player1 = keyboard.nextLine();
    		System.out.print("Player 2:\n\tName: ");
    		Player2 = keyboard.nextLine();
    		System.out.println("Welcome to the Pig Game " + Player1 + " and " + Player2 + ". \n\tYe are in a race to 100. \n\t\tGood luck!\n");
    
    		System.out.println("Start game?(Y/N)");
    		if(keyboard.nextLine().equalsIgnoreCase("y")) {
    			while(PlayGame) {
    				while(turnPoints1 >= 100 && turnPoints1 > turnPoints2) {
    					System.out.println("Player 2's score is " + turnPoints2 + " and player 1's score is " + turnPoints1 + "."); 
    					System.out.println("Welldone " + Player1 + ", You have won the game!");
    					System.out.println("Would you like to play agian?(Y/N)");
    					if(keyboard.nextLine().equalsIgnoreCase("y")) {
    						turnPoints2 = 0;
    						turnPoints1 = 0;
    						System.out.print("Player 1:\n\tName: ");
    						Player1 = keyboard.nextLine();
    						System.out.print("Player 2:\n\tName: ");
    						Player2 = keyboard.nextLine();
    						System.out.println("Welcome to the Pig Game " + Player1 + " and " + Player2 + ". \n\tYe are in a race to 100. \n\t\tGood luck!\n");
    					}
    					else {
    						System.exit(0);
    					}
    				}
    				while(turnPoints2 >= 100 && turnPoints2 > turnPoints1) {
    					System.out.println("Player 1's score is " + turnPoints1 + " and player 2's score is " + turnPoints2 + ".");
    					System.out.println("Sorry you didn't win...");
    					System.out.println("Would you like to play agian?(Yes or No)");
    					if(keyboard.nextLine().equalsIgnoreCase("yes")) {
    						turnPoints2 = 0;
    						turnPoints1 = 0;
    						System.out.print("Player 1:\n\tName: ");
    						Player1 = keyboard.nextLine();
    						System.out.print("Player 2:\n\tName: ");
    						Player2 = keyboard.nextLine();
    						System.out.println("Welcome to the Pig Game " + Player1 + " and " + Player2 + ". \n\tYe are in a race to 100. \n\t\tGood luck!\n");
    					} else {
    						System.exit(0);
    					}
    				}
    				while(Player1Go) {
    					turnPoints1 += Roll;
    				}
    				while(Player1Go != true) {
    					turnPoints2 += Roll;
    				}
    			}
    		} else {
    			System.exit(0);
    		}
    		
    	
    
    if(Player1Go) {
    	while(keepgoing) {
    		Roll = (int)(Math.random() * 6) + 1;
    		if(Roll == 1 ) {
    			System.out.println("Sorry you rolled: " + Roll);
    			System.out.println("It is Player 2's turn:");
    			turnPoints1= 0;
    			Player1Go = false;
    			keepgoing = false;
    		} else {
    			System.out.println("You rolled: " + Roll); 
    			turnPoints1+=Roll;
    			System.out.println("Do you wish to roll(r) or hold(h)?");
    			if (keyboard .nextLine().equalsIgnoreCase("h")) {
    				System.out.println("It is the Player 2's turn:"); 
    				Player1Go = false;
    				Player1Go = false;
    				keepgoing = false;
    			}
    		}
    	}
    
    
    return turnPoints1;
    
    
    } else if (Player1Go != true) {
    	while(keepgoing) {
    		Roll = (int)(Math.random() * 6) + 1;
    		if(Roll == 1 ) {
    			System.out.println("Sorry you rolled: " + Roll);
    			System.out.println("It is Playe 1's turn:");
    			turnPoints2= 0;
    			Player1Go = false;
    			keepgoing = false;
    		} else {
    			System.out.println("You rolled: " + Roll); 
    			turnPoints2+=Roll;
    			System.out.println("Do you wish to roll(r) or hold(h)?");
    			if (keyboard.nextLine().equalsIgnoreCase("h")) {
    				System.out.println("It is the Player 1's turn:"); 
    				Player1Go = true;
    				Player1Go = true;
    				keepgoing = true;
    			}
    		}
    	}
    }
    return turnPoints2;
    }
    }
    How far wrong is that?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Pig Game code gone wrong! Help please.

    What does 'gone wrong' mean? Did your program crash? Did it harm your computer beyond repair? Did it corrupt your hard disk? Made your monitor explode or your keyboard melt?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Pig Game code gone wrong! Help please.

    It just wont run when I try to execute the code. Sometimes it comes up asking "Please enter your name" and i havent got that written up anywhere,

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Pig Game code gone wrong! Help please.

    Quote Originally Posted by GoodThing007 View Post
    It just wont run when I try to execute the code. Sometimes it comes up asking "Please enter your name" and i havent got that written up anywhere,
    You are accidentally running the wrong program. Check for a file named: PigGame.class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Pig Game code gone wrong! Help please.

    Thanks for the fast reply..
    Yes you are right it was a different programme, but now that I know that I tried it again making 100% sure of which programme i'm running and every other programmes I checked work no problem. This programme just wont run for me???

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Pig Game code gone wrong! Help please.

    Quote Originally Posted by GoodThing007 View Post
    Thanks for the fast reply..
    Yes you are right it was a different programme, but now that I know that I tried it again making 100% sure of which programme i'm running and every other programmes I checked work no problem. This programme just wont run for me???
    Did your program text compile without errors? If so, it should run (and possibly behave with errors); if it doesn't correctly compile it can't possibly run.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Pig Game code gone wrong! Help please.

    thats the thing tho, it doesnt come up saying there's an error or even any warnings or anything along those lines? its a strange one.

    And again, Thanks.

Similar Threads

  1. What's wrong with this game loop?
    By Jacob_ in forum Java Gaming
    Replies: 1
    Last Post: 06-02-2011, 12:27 AM
  2. Simple number guessing game but something's wrong???
    By JohnPringle83 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:43 PM
  3. What is wrong with this code?
    By Mythreadings in forum New To Java
    Replies: 38
    Last Post: 11-19-2010, 12:43 AM
  4. what is wrong in dis code?
    By jitun2004 in forum New To Java
    Replies: 8
    Last Post: 04-15-2009, 09:30 AM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 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
  •