Posted by: sadhu_sant July 28, 2008
Java help
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();
    }
}

Read Full Discussion Thread for this article