Write an application that models the gunfight at the OK Corral.
In 1881, Deputy Town Marshall Wyatt Earp of Tombstone, Arizona led a group of police officers and others in a gunfight with members of a local gang called the Cowboys. The shootout occurred near a business called the OK Corral. There are many movies about this encounter in the western genre.
Here is sample output from a run of my solution to this problem:
Cowboy # 1 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 1 and draws!
Cowboy # 1 is hit and receives 0.9234579390622397 damage; current health is 0.07654206093776028
Cowboy # 2 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 2 and draws!
The shot misses!
Cowboy # 3 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 3 and draws!
Cowboy # 3 is hit and receives 0.49276554008457263 damage; current health is 0.5072344599154274
Cowboy # 4 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
[many lines omitted]
Wyatt Earp steadfastly stands up to Cowboy # 2 and draws!
Cowboy # 2 is killed!
Wyatt Earp steadfastly stands up to Cowboy # 3 and draws!
Cowboy # 3 is killed!
Cowboy # 4 attempts to shoot Wyatt Earp in the back like a coward
Wyatt Earp is hit and receives 0.32322057006351534 damage; current health is 0.19384304012357878
Wyatt Earp steadfastly stands up to Cowboy # 4 and draws!
Cowboy # 4 is killed!
Earp won!
• Use this Character class without changes other than imports and a package declaration. Notice that accuracy and health are doubles that should be between 0 and 1, and that health is initialized to 1. Make sure you understand how the receiveInjury() method works before you do any coding .
public abstract class Character {
protected Random r = new Random();
protected boolean alive = true;
protected String name;
protected double accuracy;
protected double health = 1d;
public boolean isAlive() {
return alive;
}
public double getAccuracy() {
return accuracy;
}
public String getName() {
return name;
}
public void receiveInjury(double damageDone) {
if (damageDone < health) {
health -= damageDone;
System.out.println(name + \" is hit and receives \" + damageDone + \" damage; current health is \" + health);
} else {
health = 0;
alive = false;
System.out.println(name + \" is killed!\");
}
}
public abstract void shoot(Character c);
}
• Extend Character in two subclasses, each with a constructor and an attack() method:
The Character subclass Marshall has a constructor that takes the Marshall\'s name and accuracy as arguments and sets these variables accordingly. Marshall\'s shoot() method prints the name of the Marshall and says that he steadfastly stands up to his opponent, using the opponent\'s name, and draws. It then gets a value by calling nextDouble() on an instance of Random. If the random value is less than the Marshall\'s accuracy, the opponent suffers an injury (call the opponent\'s receiveInjury() method) whose severity is the result of a new call to nextDouble(). If the random value is greater than the Marshall\'s accuracy, print a message that says that the shot missed.
The Character subclass Cowboy maintains a static int that represents the number of Cowboys created. The constructor increments this value and names the new Cowboy using the static int, e.g. \"Cowboy # 3.\" The constructor takes only one value, the Cowboy\'s accuracy, as an argument. Cowboy\'s shoot() method works like Marshall\'s, except that it says that the Cowboy attempts to shoot the Marshall in the back like a coward.
• Write the ShootOut class. Shootout has a list of Cowboys, an instance of Random, and a Marshall. Shootout has a instance, not static, battle() method as well as a main(). main() instantiates a ShootOut and calls its battle() method.
battle() instantiates a Marshall named Wyatt Earp with some high (at least 0.9) accuracy. It then instantiates 5 Cowboys, giving each one a random accuracy using the instance of Random and adding each one to the list. The fight will usually go on longer if you divide the random double by 2 before using it as a Cowboy\'s accuracy.
After the Cowboys are instantiated, the fight begins. As long as Wyatt Earp is alive and at least one Cowboy is also alive, Earp faces each surviving Cowboy one at a time. The Cowboys shoot first in each case, with Earp then shooting if and only if he is still alive. Use conditionals carefully to be sure no one who is already dead every shoots and that no shots are fired at anyone who is already dead.
At the end of the shootout, print a message indicating which side won.
Write an application that models the gunfight at the OK Corral.
In 1881, Deputy Town Marshall Wyatt Earp of Tombstone, Arizona led a group of police officers and others in a gunfight with members of a local gang called the Cowboys. The shootout occurred near a business called the OK Corral. There are many movies about this encounter in the western genre.
Here is sample output from a run of my solution to this problem:
Cowboy # 1 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 1 and draws!
Cowboy # 1 is hit and receives 0.9234579390622397 damage; current health is 0.07654206093776028
Cowboy # 2 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 2 and draws!
The shot misses!
Cowboy # 3 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 3 and draws!
Cowboy # 3 is hit and receives 0.49276554008457263 damage; current health is 0.5072344599154274
Cowboy # 4 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
[many lines omitted]
Wyatt Earp steadfastly stands up to Cowboy # 2 and draws!
Cowboy # 2 is killed!
Wyatt Earp steadfastly stands up to Cowboy # 3 and draws!
Cowboy # 3 is killed!
Cowboy # 4 attempts to shoot Wyatt Earp in the back like a coward
Wyatt Earp is hit and receives 0.32322057006351534 damage; current health is 0.19384304012357878
Wyatt Earp steadfastly stands up to Cowboy # 4 and draws!
Cowboy # 4 is killed!
Earp won!
• Use this Character class without changes other than imports and a package declaration. Notice that accuracy and health are doubles that should be between 0 and 1, and that health is initialized to 1. Make sure you understand how the receiveInjury() method works before you do any coding .
public abstract class Character {
protected Random r = new Random();
protected boolean alive = true;
protected String name;
protected double accuracy;
protected double health = 1d;
public boolean isAlive() {
return alive;
}
public double getAccuracy() {
return accuracy;
}
public String getName() {
return name;
}
public void receiveInjury(double damageDone) {
if (damageDone < health) {
health -= damageDone;
System.out.println(name + \" is hit and receives \" + damageDone + \" damage; current health is \" + health);
} else {
health = 0;
alive = false;
System.out.println(name + \" is killed!\");
}
}
public abstract void shoot(Character c);
}
• Extend Character in two subclasses, each with a constructor and an attack() method:
The Character subclass Marshall has a constructor that takes the Marshall\'s name and accuracy as arguments and sets these variables accordingly. Marshall\'s shoot() method prints the name of the Marshall and says that he steadfastly stands up to his opponent, using the opponent\'s name, and draws. It then gets a value by calling nextDouble() on an instance of Random. If the random value is less than the Marshall\'s accuracy, the opponent suffers an injury (call the opponent\'s receiveInjury() method) whose severity is the result of a new call to nextDouble(). If the random value is greater than the Marshall\'s accuracy, print a message that says that the shot missed.
The Character subclass Cowboy maintains a static int that represents the number of Cowboys created. The constructor increments this value and names the new Cowboy using the static int, e.g. \"Cowboy # 3.\" The constructor takes only one value, the Cowboy\'s accuracy, as an argument. Cowboy\'s shoot() method works like Marshall\'s, except that it says that the Cowboy attempts to shoot the Marshall in the back like a coward.
• Write the ShootOut class. Shootout has a list of Cowboys, an instance of Random, and a Marshall. Shootout has a instance, not static, battle() method as well as a main(). main() instantiates a ShootOut and calls its battle() method.
battle() instantiates a Marshall named Wyatt Earp with some high (at least 0.9) accuracy. It then instantiates 5 Cowboys, giving each one a random accuracy using the instance of Random and adding each one to the list. The fight will usually go on longer if you divide the random double by 2 before using it as a Cowboy\'s accuracy.
After the Cowboys are instantiated, the fight begins. As long as Wyatt Earp is alive and at least one Cowboy is also alive, Earp faces each surviving Cowboy one at a time. The Cowboys shoot first in each case, with Earp then shooting if and only if he is still alive. Use conditionals carefully to be sure no one who is already dead every shoots and that no shots are fired at anyone who is already dead.
At the end of the shootout, print a message indicating which side won.
Write an application that models the gunfight at the OK Corral.
In 1881, Deputy Town Marshall Wyatt Earp of Tombstone, Arizona led a group of police officers and others in a gunfight with members of a local gang called the Cowboys. The shootout occurred near a business called the OK Corral. There are many movies about this encounter in the western genre.
Here is sample output from a run of my solution to this problem:
Cowboy # 1 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 1 and draws!
Cowboy # 1 is hit and receives 0.9234579390622397 damage; current health is 0.07654206093776028
Cowboy # 2 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 2 and draws!
The shot misses!
Cowboy # 3 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
Wyatt Earp steadfastly stands up to Cowboy # 3 and draws!
Cowboy # 3 is hit and receives 0.49276554008457263 damage; current health is 0.5072344599154274
Cowboy # 4 attempts to shoot Wyatt Earp in the back like a coward
The shot misses!
[many lines omitted]
Wyatt Earp steadfastly stands up to Cowboy # 2 and draws!
Cowboy # 2 is killed!
Wyatt Earp steadfastly stands up to Cowboy # 3 and draws!
Cowboy # 3 is killed!
Cowboy # 4 attempts to shoot Wyatt Earp in the back like a coward
Wyatt Earp is hit and receives 0.32322057006351534 damage; current health is 0.19384304012357878
Wyatt Earp steadfastly stands up to Cowboy # 4 and draws!
Cowboy # 4 is killed!
Earp won!
• Use this Character class without changes other than imports and a package declaration. Notice that accuracy and health are doubles that should be between 0 and 1, and that health is initialized to 1. Make sure you understand how the receiveInjury() method works before you do any coding .
public abstract class Character {
protected Random r = new Random();
protected boolean alive = true;
protected String name;
protected double accuracy;
protected double health = 1d;
public boolean isAlive() {
return alive;
}
public double getAccuracy() {
return accuracy;
}
public String getName() {
return name;
}
public void receiveInjury(double damageDone) {
if (damageDone < health) {
health -= damageDone;
System.out.println(name + \" is hit and receives \" + damageDone + \" damage; current health is \" + health);
} else {
health = 0;
alive = false;
System.out.println(name + \" is killed!\");
}
}
public abstract void shoot(Character c);
}
• Extend Character in two subclasses, each with a constructor and an attack() method:
The Character subclass Marshall has a constructor that takes the Marshall\'s name and accuracy as arguments and sets these variables accordingly. Marshall\'s shoot() method prints the name of the Marshall and says that he steadfastly stands up to his opponent, using the opponent\'s name, and draws. It then gets a value by calling nextDouble() on an instance of Random. If the random value is less than the Marshall\'s accuracy, the opponent suffers an injury (call the opponent\'s receiveInjury() method) whose severity is the result of a new call to nextDouble(). If the random value is greater than the Marshall\'s accuracy, print a message that says that the shot missed.
The Character subclass Cowboy maintains a static int that represents the number of Cowboys created. The constructor increments this value and names the new Cowboy using the static int, e.g. \"Cowboy # 3.\" The constructor takes only one value, the Cowboy\'s accuracy, as an argument. Cowboy\'s shoot() method works like Marshall\'s, except that it says that the Cowboy attempts to shoot the Marshall in the back like a coward.
• Write the ShootOut class. Shootout has a list of Cowboys, an instance of Random, and a Marshall. Shootout has a instance, not static, battle() method as well as a main(). main() instantiates a ShootOut and calls its battle() method.
battle() instantiates a Marshall named Wyatt Earp with some high (at least 0.9) accuracy. It then instantiates 5 Cowboys, giving each one a random accuracy using the instance of Random and adding each one to the list. The fight will usually go on longer if you divide the random double by 2 before using it as a Cowboy\'s accuracy.
After the Cowboys are instantiated, the fight begins. As long as Wyatt Earp is alive and at least one Cowboy is also alive, Earp faces each surviving Cowboy one at a time. The Cowboys shoot first in each case, with Earp then shooting if and only if he is still alive. Use conditionals carefully to be sure no one who is already dead every shoots and that no shots are fired at anyone who is already dead.
At the end of the shootout, print a message indicating which side won.
Marshall.java
public class Marshall extends Character{
public Marshall(String name, double accuracy) {
this.name = name;
this.accuracy = accuracy;
}
@Override
public void shoot(Character c) {
System.out.println(name+\" steadfastly stands up to \"+c.getName()+\" and draws!\");
double shotAccuracy = r.nextDouble();
if(shotAccuracy < accuracy){
c.receiveInjury(r.nextDouble());
} else{
System.out.println(\"The shot misses!\");
}
}
}
Cowboy.java
public class Cowboy extends Character{
public static int cowboyCount = 0;
public Cowboy(double accuracy) {
cowboyCount++;
this.name = \"Cowboy # \"+ cowboyCount;
this.accuracy = accuracy;
}
@Override
public void shoot(Character c) {
System.out.println(name+\" attempts to shoot \"+c.getName()+\" in the back like a coward\");
double shotAccuracy = r.nextDouble();
if(shotAccuracy < accuracy){
c.receiveInjury(r.nextDouble());
} else{
System.out.println(\"The shot misses!\");
}
}
}
ShootOut.java
import java.util.ArrayList;
import java.util.Random;
class ShootOut{
Random r = new Random();
Marshall marshall;
ArrayList<Cowboy> cowboyList;
private void battle(){
marshall = new Marshall(\"Wyatt Earp\", .95);
cowboyList = new ArrayList<>();
int cowboyCount = 5;
for(int index = 0 ;index<cowboyCount; index++)
cowboyList.add(new Cowboy(r.nextDouble()/2));
int index=0;
int maxIndex = cowboyCount-1;
while(cowboyList.size() > 0){
if(index >= maxIndex) index = 0;
Cowboy cowboy = cowboyList.get(index);
cowboy.shoot(marshall);
if(!marshall.isAlive()) {
System.out.println(\"Cowboys won!\");
break;
}
marshall.shoot(cowboy);
if(!cowboy.isAlive()) cowboyList.remove(index);
}
if(cowboyList.size() == 0) System.out.println(marshall.getName()+\" won!\");
}
public static void main(String args[]){
new ShootOut().battle();
}
}