Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2009, 10:57 PM
Member
 
Join Date: Nov 2009
Posts: 2
Rep Power: 0
noturn10 is on a distinguished road
Default Unreachable statement Dice game
I need some help. For some reason whenever i run this program, it says that the statement on ln 123 can't be reached. Here is the program. Its a dice game.
Code:
 
import java. util.Scanner;

public class PigDiceGame
{
   public static void main ( String[] args )
   {
   	System.out.println("You are Going to Play the Pig Dice Game!");
   	

   	System.out.println("");
   	System.out.println("");
   	System.out.println("Do You Know the rules?...if not press 1...otherwise press any key to continue.");
   	Scanner sc = new Scanner (System.in);
   	int rules = sc.nextInt ();
   	if (rules == 1)
   		{
   		System.out.println("");
   		System.out.println("");	
   		System.out.println("Rules...");
   		System.out.println("");
   		System.out.println("");
   		System.out.println("1. In one turn the player rolls the dice until either");
   		System.out.println("	-They roll a one");
  	 	System.out.println("	-Or they choose to stop");
  	 	System.out.println("2. You add up the numbers on the dice and are trying to reach 100.");
   		System.out.println("3. Rolling a 1 will clear your points for that turn.");
   		System.out.println("4. If you choose to stop you can keep the points you have earned.");
   		System.out.println("5. Rolling a pair counts double points");
   		System.out.println("");
   		System.out.println("");
   		}
   	System.out.println("Lets begin...");
   	System.out.println("");
   	System.out.println("");
   	PairOfDice player1= new PairOfDice();
   	PairOfDice player2= new PairOfDice();
   	boolean game=true;
   	boolean round=true;	
   	int player1GameScore=0;
	int player2GameScore=0;
	int player1roundScore=0;
	int player2roundScore=0;
	int turnScore=0;
	int hold=1;   	
	System.out.print("If player1 is going to go first press 1, if player 2 is going to go press 2 ::");
	int turn = sc.nextInt ();    	
	while (game=true)
	   	{			
			while (true)
			{

				while(turn==1&&player1roundScore<=100-player1GameScore)
				{
					System.out.println("");
					System.out.print("Are you ready Player 1. Press 1 roll.(All other number are not allowed) :: ");
					int ready = sc.nextInt ();  
					player1.roll();
					player1.print();
			   		player1.totalofdice();
			   		if (player1.totalofdice()==0)
			   		{
			   			player1roundScore=player1.totalofdice();
			   			System.out.println("Player 1 have lost all your points");
			   			System.out.println("Player 1 have earned"+player1roundScore+"points");
			   			turn++;			   			
			   		}
			   		else
			   		{
			   			turnScore=player1.totalofdice();
			   			player1roundScore=player1roundScore+turnScore;
			   			System.out.println("Player 1 has earned"+player1roundScore+"points");
			   			System.out.print("To continue press 1 or press 2 if you wish to hold(bank) the points earned :: ");
						hold = sc.nextInt ();
						if (hold==2)
						{
							turn++;
						}
			   		}
	   			
				}
				player1GameScore=player1GameScore+player1roundScore;
				System.out.println("Player 1's new total score is"+player1GameScore+"points");
				while(turn==2&&player2roundScore<=100-player1GameScore)
				{
					System.out.println("");
					System.out.print("Are you ready Player 2. Press 1 roll. :: ");
					int ready = sc.nextInt(); 
					player2.roll();
					player2.print();
			   		player2.totalofdice();
			   		if (player2.totalofdice()==0)
			   		{
			   			player2roundScore=player2.totalofdice();
			   			System.out.println("Player 2 has lost all your points");
			   			System.out.println("Player 2 has earned "+player2roundScore+" points");
			   			turn--;			   			
			   		}
			   		else
			   		{
			   			turnScore=player2.totalofdice();
			   			player2roundScore=player2roundScore+turnScore;
			   			System.out.println("Player 2 has earned "+player2roundScore+" points");
			   			System.out.print("To continue press 1 or press 2 if you wish to hold(bank) the points earned :: ");
						hold = sc.nextInt();
						if (hold==2)
						{
							turn--;
						}
			   		}
			   		
				}				
				player2GameScore=player2GameScore+player2roundScore;
				player1roundScore=0;
				player2roundScore=0;
				System.out.println("Player 1's new total score is"+player1GameScore+"points");	
				System.out.println("Player2's new total score is"+player2GameScore+"points");
				if (player1GameScore>100)
					System.out.println("Player 1 wins");
				if (player2GameScore>100)
					System.out.println("Player 2 wins");	
			}
		
		System.out.println("do you want to play again Press 1 if you do");   This line
		int play= sc.nextInt ();
		if (play==1)
			game=true;
			else
		    game=false;	
		}
			

   }
}

Your help is greatly appreciated. By the way here the class file.
Code:
 
class PairOfDice
{
	private Die die1;
	private Die die2;
	private int total=0;
	
	
	public PairOfDice()
	{
		die1= new Die();
		die2= new Die();
	}
	
	public void roll()
	{
		die1.roll();
		die2.roll();
	}
	
	public int totalofdice()
	{
		if (die1.getFaceValue()==die2.getFaceValue ())
		{
			
			if (die1.getFaceValue ()== 1 && die2.getFaceValue ()== 1)
			{
				total=25;
			}
			if (die1.getFaceValue ()== 2 && die2.getFaceValue ()== 2)
			{
				total=9;
			}
			if (die1.getFaceValue ()== 3 && die2.getFaceValue ()== 3)
			{
				total=12;
			}
			if (die1.getFaceValue ()== 4 && die2.getFaceValue ()== 4)
			{
				total=16;
			}
			if (die1.getFaceValue ()== 5 && die2.getFaceValue ()== 5)
			{
				total=20;
			}
			if (die1.getFaceValue ()== 6 && die2.getFaceValue ()== 6)
			{
				total=24;
			}
		}
		
		else
			
		{
			if(die1.getFaceValue()==1||die2.getFaceValue()==1)
				total=0;
			else
				total=die1.getFaceValue()+die2.getFaceValue();
			
		}

		return total;
	}
	
	public void print()
	{
		System.out.println ("The first die rolled a:"+die1.getFaceValue ()+ "   The second die rolled a:"+ die2.getFaceValue ());
	}
}
Thanks

Last edited by noturn10; 11-22-2009 at 01:25 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-21-2009, 11:23 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,440
Rep Power: 8
Fubarable is on a distinguished road
Default
So in abbreviation, your code is structured something like this:
Code:
      while (game = true) {
         while (true) {
            //... bunch of code here
         }

         System.out.println("do you want to play again Press 1 if you do"); // This line
         //... more code here
So the compiler is helping you out here, because there in fact is no way for that line to be reached seeing as it follows a while (true) {} block.

Solution: fix your code so that it is reachable.

Much luck!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2009, 01:36 AM
Member
 
Join Date: Nov 2009
Posts: 2
Rep Power: 0
noturn10 is on a distinguished road
Default
Thanks, i was able to fix it
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a dice game. hero AWT / Swing 14 07-26-2009 12:50 PM
Unreachable Statement (accessors) - works as a main method! thomase New To Java 6 03-11-2009 05:38 PM
unreachable statement - Java calculator program V2001Gordon New To Java 3 12-13-2008 01:57 AM
Help debugging a dice game Windoze New To Java 7 11-22-2007 02:01 AM
help debugging a dice game Windoze Advanced Java 0 11-16-2007 11:28 PM


All times are GMT +2. The time now is 08:22 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org