Results 1 to 7 of 7
- 12-04-2011, 07:13 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Pig Game code gone wrong! Help please.
First ever attempt at anything like this:
How far wrong is that?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; } }
- 12-04-2011, 07:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-04-2011, 07:55 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
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,
- 12-04-2011, 08:01 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
- 12-04-2011, 08:10 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
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???
- 12-04-2011, 09:32 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: Pig Game code gone wrong! Help please.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-05-2011, 01:24 AM #7
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
What's wrong with this game loop?
By Jacob_ in forum Java GamingReplies: 1Last Post: 06-02-2011, 12:27 AM -
Simple number guessing game but something's wrong???
By JohnPringle83 in forum New To JavaReplies: 11Last Post: 05-29-2011, 08:43 PM -
What is wrong with this code?
By Mythreadings in forum New To JavaReplies: 38Last Post: 11-19-2010, 12:43 AM -
what is wrong in dis code?
By jitun2004 in forum New To JavaReplies: 8Last Post: 04-15-2009, 09:30 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks