Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Duelist {
- private String name;
- private double probabilityOfHitting;
- private boolean alive = true;
- //Only declared instance variables. Must created setters and getters
- public void setName(String newName){
- name = newName;
- }
- //name setter created
- public void setProbabilityOfHitting( double newProbabilityOfHitting){
- probabilityOfHitting = newProbabilityOfHitting;
- }
- //probability of hitting setter created
- public void setAlive(boolean newAlive){
- alive = newAlive;
- }
- //name setter created
- //now must create getters
- public String getName(){
- return name;
- }
- //created the name getter
- public double getProbabilityOfHitting(){
- return probabilityOfHitting;
- }
- //created the probability of hitting getter
- public boolean getAlive(){
- return alive;
- }
- //created the alive getter
- //no constructors created before
- public Duelist(String tempName, double tempProbability){
- name = tempName;
- probabilityOfHitting = tempProbability;
- }
- //constructor is now created
- //need to create a method for the duelists to shoot at each other
- public void shootAtTarget(Duelist target){
- double randomNum = Math.random();
- if (this.probabilityOfHitting ==1){
- target.setAlive(false);
- target.getAlive();
- }
- else if (randomNum <= this.probabilityOfHitting){
- target.setAlive(false);
- target.getAlive();
- }
- else {
- target.getAlive();
- }
- }
- }
- public class Tester {
- public static void main(String[] args) {
- int winsA = 0;
- int winsB = 0;
- int winsC = 0;
- Duelist aaron = new Duelist("Aaron",(1/3));
- Duelist bob = new Duelist("Bob", (1/2));
- Duelist charlie = new Duelist("Charlie", 1);
- if(aaron.getAlive() == true){
- if(charlie.getAlive()== true){
- aaron.shootAtTarget(charlie);
- }
- else if(bob.getAlive() == true){
- aaron.shootAtTarget(bob);
- }
- else{
- winsA++;
- }
- }
- else if(bob.getAlive() == true){
- if(charlie.getAlive() == true){
- bob.shootAtTarget(charlie);
- }
- else if(aaron.getAlive() == true){
- bob.shootAtTarget(aaron);
- }
- else{
- winsB++;
- }
- }
- else{
- if (bob.getAlive() == true){
- charlie.shootAtTarget(bob);
- }
- else if(aaron.getAlive() == true){
- charlie.shootAtTarget(aaron);
- }
- else{
- winsC++;
- }
- }
- System.out.println(winsA);
- System.out.println(winsB);
- System.out.println(winsC);
- }
- }
- Duelist aaron = new Duelist("Aaron",(1/3));
- Duelist aaron = new Duelist("Aaron",(1.0/3.0));
- private static Random random = new Random(0);
- //need to create a method for the duelists to shoot at each other
- public void shootAtTarget(Duelist target)
- {
- double randomNum = random.nextDouble();
- if (randomNum <= this.probabilityOfHitting)
- {
- target.setAlive(false);
- }
- }
- int alive = 3;
- while (alive > 1)
- {
- System.out.println("Status: A:"+aaron.getAlive()+" B:"+bob.getAlive()+" C:"+charlie.getAlive());
- if (aaron.getAlive())
- {
- if(charlie.getAlive())
- {
- System.out.println("A shoots at C");
- aaron.shootAtTarget(charlie);
- if (!charlie.getAlive())
- {
- System.out.println("A killed C");
- alive--;
- }
- }
- else if(bob.getAlive())
- {
- System.out.println("A shoots at B");
- aaron.shootAtTarget(bob);
- if (!bob.getAlive())
- {
- System.out.println("A killed B");
- alive--;
- }
- }
- }
- // Other cases ...
- }
- if (aaron.getAlive())
- {
- winsA++;
- }
- if (bob.getAlive())
- {
- winsB++;
- }
- if (charlie.getAlive())
- {
- winsC++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement