Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Absolute Java chapter 5 project 3 - puzzle mania duel

  1. #1
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default Absolute Java chapter 5 project 3 - puzzle mania duel

    I have to write a program which loops a duel 10000 times. Somehow one of the three duelist is winning every time and it must be an error in the code. Was hoping someone could point me in the right direction.
    edit: forgot to include the duelist class. It is below the 10000duels class.
    Here is my 10000duels class:

    Java Code:
    public class OneThousandDuels
     {
    	
    	public static void main (String[] args) 
    	{
    		Duelist aaron, bob, charlie;
    		aaron = new Duelist("Aaron", 1.0/3.0);
    		bob = new Duelist("Bob", .5);
    		charlie = new Duelist("Charlie", 1.0);
    		
    		int aaronWins = 0;
    		int bobWins =0;
    		int charlieWins = 0;
    		
    		int shot = 1;
    		
    		for (int i = 0; i < 10000; i++)
    		{
    			
    					
    			while(Duelist.getDeaths() < 2)
    			{
    				if (Duelist.getDeaths() < 2 && (aaron.getAlive() && shot != 1))
    				{
    					if (charlie.getAlive())
    						aaron.shootAtTarget(charlie);
    					else
    						aaron.shootAtTarget(bob);
    				}
    				if (Duelist.getDeaths() < 2 && bob.getAlive())
    				{
    					if (charlie.getAlive())
    						bob.shootAtTarget(charlie);
    					else
    						bob.shootAtTarget(aaron);
    				}
    				if (Duelist.getDeaths() < 2 && (charlie.getAlive()))
    				{
    					if (bob.getAlive())
    						charlie.shootAtTarget(bob);
    					else
    					charlie.shootAtTarget(aaron);
    				}
    				shot++;
    				
    				
    			}
    					bob.resurrectTheDead();
    					charlie.resurrectTheDead();
    					aaron.resurrectTheDead();
    			
    			if(aaron.getAlive())
    				{
    					aaronWins++;
    					
    				}
    				else if(bob.getAlive())
    				
    					bobWins++;
    					
    				
    				else
    					charlieWins++;
    					
    				
    				
    			}
    			double aaronRate = (aaronWins / 10000);
    			double bobRate = (bobWins / 10000);
    			double charlieRate = (charlieWins / 10000);
    		System.out.println("Aaron wins with a rate of %" + aaronRate + " in 10000 games.");
    
        System.out.println("Bob wins with a rate of %" + bobRate + " in 10000 games.");
    
        System.out.println("Charlie wins with a rate of %" + charlieRate + " in 10000 games.");
    		
    	}
    }
    import java.util.Random;
    public class Duelist
     {
    	private String duelistName;
    	private double shootingAccuracy;
    	private boolean alive;
    	private static int deaths;
    	public Duelist()
    	{
    		duelistName = "none";
    		shootingAccuracy = 0.0;
    		alive = true;
    	}
    		//conustructors
    	public Duelist(String name, double accuracy)
    	{
    		duelistName = name;
    		shootingAccuracy = accuracy;
    		alive = true;
    	}
    	public Duelist(Duelist other)
    	{
    		duelistName = other.duelistName;
    		shootingAccuracy = other.getShootingAccuracy();
    		alive = other.getAlive();
    	}
    	public String getName()
    	{
    		return duelistName;
    	}
    	public double getShootingAccuracy()
    	{
    		return shootingAccuracy;
    	}
    	public boolean getAlive()
    	{
    		return alive;
    	}
    	public void setName (String nextName)
    	{
    		duelistName = nextName;
    	}
    	public void setShootingAccuracy(double nextAccuracy)
    	{
    		shootingAccuracy = nextAccuracy;
    	}
    	public void setDuelistNameAndAccuracy(String theName, double theAccuracy)
    	{
    		duelistName = theName;
    		shootingAccuracy = theAccuracy;
    	}
    	public static int getDeaths()
    	{
    		return deaths;
    	}
    	public void shootAtTarget(Duelist target)
    	{
    		if ((double)Math.random() < shootingAccuracy)
    		{
    			target.dead();
    		}
    		
    	}
    	private void dead()
    	{
    		alive = false;
    		deaths++;
    	}
    	public boolean equals(Duelist otherDuelist)
    	{
    		return ((duelistName.equals(otherDuelist.duelistName)) && (shootingAccuracy == otherDuelist.getShootingAccuracy()));
    	}
    	public static void resetDeaths()
    	{
    		deaths = 0;
    	}
    	public void resurrectTheDead()
    	{
    		alive = true;
    	}
    	public String toString()
    	{
    			if(alive)
    		{
    			return (duelistName + ", " + (shootingAccuracy * 100) + "% accuracy, is alive");
    		}
    			else 
    			return (duelistName + ", " + (shootingAccuracy * 100) + "% accuracy, is dead");
    	}
    }
    Last edited by javanewbie82; 11-08-2016 at 10:23 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Absolute Java chapter 5 project 3 - puzzle mania duel

    Lines 68 thru 70 are problematic. The divisions are integer so any wins less that 10000 will be 0.

    You also didn't include the other classes or methods, or explain the Duelist constructor values.

    Regards,
    Jim
    javanewbie82 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Absolute Java chapter 5 project 3 - puzzle mania duel

    How are you debugging the code to see what it is doing?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default Re: Absolute Java chapter 5 project 3 - puzzle mania duel

    I use geany to code with and compile line by line. Very new to java here...is that considered debugging or would I use a different program?

Similar Threads

  1. new to Java , then my blog is the solution for absolute beginners . Thank you :-)
    By santhosh reddy WILLY in forum Reviews / Advertising
    Replies: 2
    Last Post: 07-07-2012, 08:12 AM
  2. Absolute path for a file in Java?
    By 260191894 in forum New To Java
    Replies: 7
    Last Post: 06-13-2012, 08:28 PM

Tags for this Thread

Posting Permissions

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