[Show all top banners]

sadhu_sant
Replies to this thread:

More by sadhu_sant
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java help
[VIEWED 10274 TIMES]
SAVE! for ease of future access.
Posted on 07-28-08 8:27 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

In the land of puzzlevania , Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzle-solver of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron was a poor shot and only hit his target with a probability of 1/3. Bob was a bit better and hit his target with a probability of 1/2. Charlie was an excellent marksman and had a 9/10 probability A hit means a kill and the person hit drops out of the duel.
To compensate for the inequities in their marksmanship skills, the three decided that they would fire in turns, start with Aaron, followed by Bob, and then by Charlie. The would repeat until there was one man standing .That man would be remembered for all time as the Greatest Puzzle-Solver of All Time.
An Obvious and reasonable strategy is for each man to shoot at the most accurate shooter shill alive, one the grounds that this shoot is the deadliest and has the best chance of hitting back.
the way to tell if the shot hit is by setting the boolean value nameAlive equal to false. so basically they take turns shooting at the best shooter.

ok guys that the question . i wrote a class duelist, which compiles fine but doesnot give me results,,,,so can u please help me find my error.....I have been  spending hrs to find the error,,,,,

public class Duelist
{
   private String charlie;
 private String bob;
 private String aaron;
 private int numberOfDuels = 10000;
 int aaron_wins=0, bob_wins=0, charlie_wins=0;
 boolean aaronAlive = true;
 boolean bobAlive = true;
 boolean charlieAlive = true;
 
public void shoot(boolean targetAlive, double accuracy, int number_alive)
{
 double random_num = (double) (Math.random()*1);

 if (random_num <= accuracy)
 {
  targetAlive = false;
        number_alive--;
     }
      
}
 public  String startDuel()
{
 
 int num_alive = 3;

 do
 {
  if (aaronAlive)
  {
   if (charlieAlive)
    shoot(charlieAlive, 1/3.0, num_alive);
   else if (bobAlive)
    shoot(bobAlive, 1/3.0, num_alive);
  
  }


  if (bobAlive)
  {
   if (charlieAlive)
    shoot(charlieAlive, 0.5, num_alive);
   else if (aaronAlive)
    shoot(aaronAlive, 0.5, num_alive);
    else
    ;
   
  }
  if(charlieAlive)
  {
   if (bobAlive)
    shoot(bobAlive, 1.0, num_alive);
   else if  (aaronAlive)
    shoot(aaronAlive, 1.0, num_alive);
   else
   ;
  }


 }while(num_alive > 1);

 if (aaronAlive)
  return "Aaron";
 else if(bobAlive)
  return "Bob";
 else
  return "Charlie";

}
public int strategy1()
{
 String winner="";

 do
 {
 winner = startDuel();

  if(winner == "Aaron")
   aaron_wins++;
  else if (winner == "Bob")
   bob_wins++;
  else
   charlie_wins++;

  numberOfDuels--;
 }while (numberOfDuels >0);
return 0 ;
}
public void printOutput()
{
System.out.println("number of wins of charlie: " +charlie_wins);
System.out.println("number of wins of Aaron: " +aaron_wins);
System.out.println("number of wins of bob: " +bob_wins);
}

}
//Tester method//

public class Duelist_Test
{
     public static void main (String[] args) {
        Duelist Duelist_Test = new Duelist();
        Duelist_Test.startDuel();
        Duelist_Test.strategy1();
        Duelist_Test.printOutput();
    }
}


 
Posted on 07-28-08 10:24 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

quickly skimming your code, it looks alright to me... i am not too sure about your shoot algorithm though (everything else looks like its written well to fit the problem description), specifically this part:

double random_num = (double) (Math.random()*1);

 if (random_num <= accuracy)
 {
  targetAlive = false;
        number_alive--;
     }

So, this would mean you choose a random number between 0 and 1 and if it is equal to or less than the probability of the person's shooting odds, the target dies? I might be overlooking something here, but I can not quite understand that part...  Should it be random_num >=accuracy? Thats the only place where i can see yout code going wrong - and that would be more of an algorithm flaw than a coding mistake. What is the output youre getting?


 
Posted on 07-28-08 10:31 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thanks for the comment reverse_engineer ,, my code compiles but wont execute.
 
Posted on 07-28-08 11:43 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

come on guys,,,,, help me out.............
 
Posted on 07-28-08 11:59 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

first the question is too elongated to read....i didn't read it infact....

anyway,
well just went briefly through your code.... and i am wondering what you are doing here..


public void shoot(boolean targetAlive, double accuracy, int number_alive)
{
//  whatever .......
  

     number_alive--;
     }
      
}
 public  String startDuel()
{
 
 int num_alive = 3;

 do
 {
  if (aaronAlive)
  {
   if (charlieAlive)
    shoot(charlieAlive, 1/3.0, num_alive);
   else if (bobAlive)
    shoot(bobAlive, 1/3.0, num_alive);
  
  }


The variable num_alive are local in both cases ...how do you expect the value of that variable to be changed ???









 
Posted on 07-28-08 12:11 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I didnt quite went thru the algo but this is where u were doing wrong

while passing paremeters, it never changes the original. So your code never came out of the loop.

This explains more http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

I've done some modification to ur code.I'm not sure how correct is the changes..but u might want to do similar. Good Luck.

 

class Person{

public boolean alive;

public int number_alive;

public int wins;

public Person(boolean pAlive, int pWins)

{

alive=pAlive;

wins=pWins;

}

}

public class Duellist {

private String charlie;

private String bob;

private String aaron;

private int numberOfDuels = 10000;

int aaron_wins=0, bob_wins=0, charlie_wins=0;

boolean aaronAlive = true;

boolean bobAlive = true;

boolean charlieAlive = true;

int num_alive=3;

public void shoot(Person p, double accuracy)

{

double random_num = (double) (Math.random()*1);

if (random_num <= accuracy)

{

p.alive = false;

num_alive--;

}

}

public String startDuel()

{

Person aaron=new Person(true,0);

Person charlie=new Person(true,0);

Person bob = new Person(true,0);

do

{

if (aaron.alive)

{

if (charlie.alive)

shoot(charlie, 1/3.0);

else if (bob.alive)

shoot(bob, 1/3.0);

}

 

if (bob.alive)

{

if (charlie.alive)

shoot(charlie, 0.5);

else if (aaron.alive)

shoot(aaron, 0.5);

else

;

}

if(charlie.alive)

{

if (bob.alive)

shoot(bob, 1.0);

else if (aaron.alive)

shoot(aaron, 1.0);

else

;

}

 

}while(num_alive > 1);

if (aaron.alive)

return "Aaron";

else if(bob.alive)

return "Bob";

else

return "Charlie";

}

public int strategy1()

{

String winner="";

do

{

winner = startDuel();

if(winner == "Aaron")

aaron_wins++;

else if (winner == "Bob")

bob_wins++;

else

charlie_wins++;

numberOfDuels--;

}while (numberOfDuels >0);

return 0 ;

}

public void printOutput()

{

System.out.println("number of wins of charlie: " +charlie_wins);

System.out.println("number of wins of Aaron: " +aaron_wins);

System.out.println("number of wins of bob: " +bob_wins);

}

public static void main (String[] args) {

Duellist Duelist_Test = new Duellist();

Duelist_Test.startDuel();

Duelist_Test.strategy1();

Duelist_Test.printOutput();

}

 

}

 

I got following o/p

number of wins of charlie: 0

number of wins of Aaron: 8329

number of wins of bob: 1671

Not sure how correct the math is..

and also you might want to remove the variables that's not used on the modified one...:)

Last edited: 28-Jul-08 12:17 PM

 
Posted on 07-28-08 12:11 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

also why you are having this "else" statement ?? (when it has no use at all)


if (bobAlive)
  {
   if (charlieAlive)
    shoot(charlieAlive, 0.5, num_alive);
   else if (aaronAlive)
    shoot(aaronAlive, 0.5, num_alive);
    else
    ;
  



 
Posted on 07-28-08 12:14 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

One more thing..use ECLIPSE.. and you can step through your code in debug mode..it makes life easier.
 
Posted on 07-28-08 12:56 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 


First of all, there are lots of prepostions missing in the question and it has lots of typos to understand clearly.

Second : WHy are you guys taking 10000 fights as the maximum? I didnt see any reference in the que :s
Third, what is meant by "one the grounds that this shoot is the deadliest and has the best chance of hitting back."?

 
Last edited: 28-Jul-08 01:03 PM

 
Posted on 07-28-08 1:30 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thanks guys for all the help.
 
Posted on 07-28-08 2:01 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Wonderful solution.. But i feel something is wrong with the probability part. Since random numbers are being generated it might not reveal the true sense of probability. I think, it would make much sense if you could count the number of shots made by each person example conside 10 as the base. So Charlie makes 9 perfect shots , bob makes 5 perfect shots, and aron 3.33 (or 4). Reset the count when one man standing. Random pick the shots and remove them.

just a little logic. Might or Might not work. Hope that helps. wont be able to help out with the codes been a while.


 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Travel Document for TPS (approved)
All the Qatar ailines from Nepal canceled to USA
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters