有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇三方决斗

在我的intro java类中处理这个项目,下面是它需要做什么的要点--

In the land of Puzzlevania, Aaron, Bob, and Charlie had anargument over which one of them was the greatest puzzler of all time. To endthe argumentonce and for all, they agreed on a duel to the death. Aaronis a poor shooter and only hits his target with a probability of 1/3. Bob isa bit better and hits his target with a probability of 1/2. Charlie is an expertmarksman and never misses. A hit means a kill and the person hit drops outof the duel. To compensate for the inequities in their marksmanship skills, itis decided that the contestants would fire in turns starting with Aaron,followed by Bob, and then by Charlie. The cycle would repeat until there was oneman standing. And that man would be remembered as the greatest puzzlerof all time.

a. Write a function to simulate a single shot. It should usethe following declaration: voidshoot(bool& targetAlive, double accuracy, int&num_alive); This would simulate someone shooting at targetAlive with thegiven accuracy by generating a random number between 0 and 1. If therandom number is less than accuracy, then the target is hit andtargetAlive should be set to false. Appendix 4 illustrates how to generate randomnumbers. For example, if Bob is shooting at Charlie, this could be invokedas: shoot(charlieAlive,0.5, num_alive); Here, charlieAlive is a Boolean variable that indicates if Charlieis alive. Test your function using a driver program before moving on to stepb.

b. An obvious strategy 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 second function named start Duel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

c. In your main function, invoke the startDuel function 1,000 timesin a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win wheneveryone uses the strategy of shooting at the most accurate shooter left alive.

d. A counter intuitive strategy is for Aaron to intentionally misson his first shot. Thereafter, everyone uses the strategy of shooting atthe most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

这是我正在使用的代码。。。到目前为止,它只通过了一次,但我不确定我应该把循环放在哪里?此外,代码中的任何其他指针都将不胜感激

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; 

        do 
        { 
            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"); 
    } 
} 

共 (1) 个答案

  1. # 1 楼答案

    问题的答案写在问题的要求中:

    c. In your main function, invoke the startDuel function 1,000 timesin a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.

    // main() pseudocode:
    Shooter[] shooters = new Shooter[3](); // or however java syntax is ...
    // Set the strategy, name for each of the 3 shooters...
    shooters[0].setName("..");
    shooters[0].setStrategy(...);
    // ...
    
    // get some storage to count which player wins a round...
    int[] winCounters = new int[3];
    for( int i = 0; i < 1000; i++ )
    {
         int winner = startDuel(shooters); // returns index of winner...
         winCounters[winner]++;
    }
    // ... output the statistics ....