java - Threeway Duel to the Death -
working on project intro java class, heres gist of needs --
in land of puzzlevania, aaron, bob, , charlie had anargument on 1 of them greatest puzzler of time. endthe argumentonce , all, agreed on duel death. aaronis poor shooter , hits target probability of 1/3. bob isa bit better , hits target probability of 1/2. charlie expertmarksman , never misses. hit means kill , person hit drops outof duel. compensate inequities in marksmanship skills, itis decided contestants fire in turns starting aaron,followed bob, , charlie. cycle repeat until there oneman standing. , man remembered greatest puzzlerof time.
a. write function simulate single shot. should usethe following declaration: voidshoot(bool& targetalive, double accuracy, int&num_alive); simulate shooting @ targetalive thegiven accuracy generating random number between 0 , 1. if therandom number less accuracy, target hit andtargetalive should set false. appendix 4 illustrates how generate randomnumbers. example, if bob shooting @ charlie, invokedas: shoot(charliealive,0.5, num_alive); here, charliealive boolean variable indicates if charlieis alive. test function using driver program before moving on stepb.
b. obvious strategy each man shoot @ accurate shooter still alive on grounds shooter deadliest , has best chance of hitting back. write second function named start duel uses shoot function simulate entire duel using strategy. should loop until 1 contestant left, invoking shoot function proper target , probability of hitting target according shooting. function should return variable indicates won duel.
c. in main function, invoke startduel function 1,000 timesin loop, keeping track of how many times each contestant wins. output probability each contestant win wheneveryone uses strategy of shooting @ accurate shooter left alive.
d. counter intuitive strategy aaron intentionally misson first shot. thereafter, uses strategy of shooting atthe accurate shooter left alive. strategy means aaron guaranteed live past first round, since bob , charlie fire @ each other. modify program accommodate new strategy , output probability of winning each contestant.
so here code i'm working with... far goes through once, i'm not sure should put loop? other pointers in code appreciated.
import java.util.random; public class duelist { random rnd = new random(); static int aaron_wins,bob_wins,charlie_wins; class shooter { public static final int startinghits = 1; private string name; private double accuracy; private int hitsleft = startinghits; public shooter(string name, double accuracy) { this.name = name; this.accuracy = accuracy; } public string getname() { return this.name; } public double getaccuracy() { return this.accuracy; } public boolean isalive() { return this.hitsleft > 0; } public void takehit() { this.hitsleft--; } public void shoot(shooter target) { if (rnd.nextdouble() <= this.getaccuracy()) { system.out.println(this.getname() + " hits " + target.getname()); target.takehit(); if (!target.isalive()) { system.out.println(target.getname() + " dies."); } else { system.out.println(this.getname() + " misses " + target.getname()); } } } } private shooter [] shooters; public duelist() { this.shooters = new shooter [] { new shooter("aaron", 0.33), new shooter("bob", 0.5), new shooter("charlie", 1) }; } public shooter picktarget(shooter shooter) { shooter victim = null; for(shooter possiblevictim : this.shooters) { if (!possiblevictim.isalive()) { continue; } if (shooter==possiblevictim) { continue; } if (victim == null || possiblevictim.getaccuracy() > victim.getaccuracy()) { victim = possiblevictim; } } return victim; } public void fireaway() { int currentshooter = 0; int maxshooter = this.shooters.length; while(true) { shooter shooter = this.shooters[currentshooter++]; if (shooter.isalive()) { shooter victim = picktarget(shooter); if (victim!=null) { shooter.shoot(victim); } else { system.out.println(shooter.getname() + " wins."); if(shooter.getname().equals("aaron")) aaron_wins++; else if(shooter.getname().equals("bob")) bob_wins++; else if(shooter.getname().equals("charlie")) charlie_wins++; break; } } if (!(currentshooter<maxshooter)) { currentshooter=0; } } } public static string beginduel_alternative_strategy() { boolean aaronalive = true; boolean bobalive = true; boolean charliealive = true; int num_alive = 3; aaron_wins=bob_wins=charlie_wins=0; string winner = ""; int round = 1; { if (aaronalive) { if (round == 1) { 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); } if(charliealive) { if (bobalive) shoot(bobalive, 1.0, num_alive); else if (aaronalive) shoot(aaronalive, 1.0, num_alive); } round++; num_alive--; }while(num_alive > 1); if (aaronalive) { winner = "aaron"; aaron_wins++; } else if(bobalive) { winner = "bob"; bob_wins++; } else { winner = "charlie"; charlie_wins++; } return winner; } public static void shoot(boolean targetalive, double accuracy, int number_alive) { random rnd2 = new random(); if (rnd2.nextdouble()< accuracy) { targetalive = false; number_alive--; } } public static void main(string[] args) { duelist duel = new duelist(); duel.fireaway(); system.out.println("using first strategy: \n"); system.out.println("aaron won " + aaron_wins + " duels or " + aaron_wins * 100 + "%\n"); system.out.println("bob has " + bob_wins + " duels or " + bob_wins * 100 + "%\n"); system.out.println("charlie has " + charlie_wins + " duels or " + charlie_wins * 100 + "%\n"); system.out.println(); system.out.println("using alternate strategy: \n"); system.out.println("winner :" + beginduel_alternative_strategy()); system.out.println(); system.out.println("aaron has " + aaron_wins + " duels or " + aaron_wins * 100 + "%\n"); system.out.println("bob won " + bob_wins + " duels or " + bob_wins * 100 + "%\n"); system.out.println("charlie won " + charlie_wins + " duels or " + charlie_wins * 100 + "%\n"); } }
the answer of question written in requirements problem:
c. in main function, invoke startduel function 1,000 timesin loop, keeping track of how many times each contestant wins. output probability each contestant win when uses strategy of shooting @ accurate shooter left alive.
// main() pseudocode: shooter[] shooters = new shooter[3](); // or java syntax ... // set strategy, name each of 3 shooters... shooters[0].setname(".."); shooters[0].setstrategy(...); // ... // storage count player wins round... int[] wincounters = new int[3]; for( int = 0; < 1000; i++ ) { int winner = startduel(shooters); // returns index of winner... wincounters[winner]++; } // ... output statistics ....
Comments
Post a Comment