[The Project Question] [JAVA] In the land of Puzzlevania, Aaron , Bob , and Char
ID: 3552024 • Letter: #
Question
[The Project Question] [JAVA]
In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time.
To end the argument once and for all, they agreed on a duel to the death.
Aaron was a poor shooter 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 expert marksman and never missed.
A hit means a kill and the person hit dropx out of the duel.
To compensate for the inequities in their marksmanship skills,
the three decided that they would fire in turns, starting with Aaron, followed by Bob, and then by Charlie.
The cycle would repeat until there was one man stadning, and that man would be the Greatest Puzzler of All Time.
An obvious and reasonable startegy is for each man to shoot at the most accurate shooter still alive,
on the grounds that this shooter is the deadliest and has the best chance of hitting back.
Write a program to simulate the duel using this strategy.
Your program should....
- Use random numbers and the probabilities given in the problem to determine whether a shooter hits the target.
- Create a class named Duelist that contains the dueler's name and shooting accuracy,
a Boolean indicating whether the dueler is still alive,
and a method ShootAtTarget(Duelist target) that sets the target to dead if the dueler hits his target
(using a random number and the shooting accuracy) and does nothing otherwise.
Once you can simulate a aingle duel, add a loop to your program that simulates 10,000 duels.
Count the number of times that each contestant wins and print the probability of winning for each contestant
(e.g., for Aaron your program might output "Aaron won 3,595 / 10,000 duels or 39.95%").
+
An alternate strategy is for Aaron to intentionally miss on his first shot.
- Modify the program to accomodate this new startegy and output the probability of winning for each contestant.
- Which strategy is better for Aaron: to intentionally miss on the first shot or to try and hit the best shooter?
- Who has the best chance of winning, the best shooter or the worst shooter?
=====================================================================================
=> Please help me code the program.
I should consider whether all the things mentioned above are included in the program.
=====================================================================================
Explanation / Answer
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.