【三個槍手問題】誰是最偉大的軟件開發工程師?

Once upon a time in the land of Asgard there lived 3 wizards Gandalf, Merlin and Dumbledore. These 3 wizards often had an argument over which one of them was thegreatest software developer of all time. To end the argument once and for all, they agreed on a fight to the death. Gandalf was a poor shooter and only hit his target with a probability of 1/3. Merlin was a bit better and hit his target with a probability of 1/2. Dumbledore was an expert marksman andnever missed. A hit means a kill and the person hit drops out of the duel.
To compensate for the inequities in their marksmanship skills, the three decided that they would fire in turns, starting with Gandalf, followed by Merlin, and then by Dumbledore. The cycle would repeat until there was one man or creature standing, and that man or creature would be the greatest software developer of All Time.
An obvious and reasonable strategy is for each wizard 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 Fighter that contains the wizard’s name
and shooting accuracy, a Boolean indicating whether the fighter is still alive, and a method shootAtTarget(Fighter target) that sets the target to dead if the fighter hits his target (using a random number and the shooting accuracy) and does nothing otherwise.
Once you can simulate a single fight, add a loop to your program that simulates 10,000 fights. Count the number of times that each contestant wins and print the probability of winning for each contestant in a tabular format. For example:

 

 

 

Fighter.java

package com.zx.bean;

import com.zx.util.Util;

/**
 * @author 作者
 * @version 創建時間:2019年10月17日 下午9:50:55
 * @email [email protected] 類說明
 */
public class Fighter {
	String name;
	int accuracy;
	boolean weatherAlive;

	public Fighter() {
		super();
	}

	public Fighter(String name, int accuracy, boolean weatherAlive) {
		super();
		this.name = name;
		this.accuracy = accuracy;
		this.weatherAlive = weatherAlive;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(int accuracy) {
		this.accuracy = accuracy;
	}

	public boolean isWeatherAlive() {
		return weatherAlive;
	}

	public void setWeatherAlive(boolean weatherAlive) {
		this.weatherAlive = weatherAlive;
	}

	public void shootAtTarget(Fighter fighter) {
		boolean flag = Util.getTargetProbability(this.getAccuracy());
		if (flag) {//如果射中
				fighter.setWeatherAlive(false);
		} 
	}
}

 

Main.java

package com.zx.main;
/**
* @author 作者
* @version 創建時間:2019年10月18日 下午3:48:34
* @email [email protected]
* 類說明
*/

import java.text.DecimalFormat;

import com.zx.bean.Fighter;
import com.zx.util.Util;

public class Main {
	public static void main(String[] args) {
		Fighter[] f = new Fighter[3];
		for (int i = 0; i < 3; i++) {
			f[i] = new Fighter();
			f[i].setWeatherAlive(true);
		}
		int gameTime = 10000;
		int winNumOfGandalf = 0;
		int winNumOfMerlin = 0;
		int winNumOfDumbledore = 0;
		float winRateOfGandalf = 0.0f;
		float winRateOfMerlin = 0.0f;
		float winRateOfDumbledore = 0.0f;
		f[0].setAccuracy(33);
		f[1].setAccuracy(50);
		f[2].setAccuracy(100);
		for (int i = 0; i < gameTime; i++) {
			while (!Util.wheatherOnlyOne(f)) {//當比賽未結束時
				if (f[0].isWeatherAlive()) {//如果Gandalf存活,選擇M或者D進行射擊,如果D存活,射擊D,如果D不存活,則射擊M
					f[0].shootAtTarget(Util.getCurrentBestTarget(f, 0));
				}
				if (f[1].isWeatherAlive()) {
					if(!Util.wheatherOnlyOne(f)) {
						f[1].shootAtTarget(Util.getCurrentBestTarget(f, 1));
					}
					else {
						break;
					}
				}
				if (f[2].isWeatherAlive()&& !Util.wheatherOnlyOne(f)) {
						f[2].shootAtTarget(Util.getCurrentBestTarget(f, 2));
				}
			}
			if (Util.getWinnerNum(f) == 0) {
				winNumOfGandalf++;
			}else
			if (Util.getWinnerNum(f) == 1) {
				winNumOfMerlin++;
			}else
			if (Util.getWinnerNum(f) == 2) {
				winNumOfDumbledore++;
			}else {
				System.out.println("dada");
			}
			Util.setAllAlive(f);
		}

		DecimalFormat df = new DecimalFormat("0.00%");
		winRateOfGandalf = (float) winNumOfGandalf / gameTime;
		winRateOfMerlin = (float) winNumOfMerlin / gameTime;
		winRateOfDumbledore = (float) winNumOfDumbledore / gameTime;
		System.out.println("LEADERBOARD - AFTER " + gameTime + " DUELS");
		System.out.println("Contestant\t" + "Number of Wins" + "Winning Percentag");
		System.out.println("Gandalf\t" + "\t" + winNumOfGandalf + "\t" + df.format(winRateOfGandalf));
		System.out.println("Merlin\t" + "\t" + winNumOfMerlin + "\t" + df.format(winRateOfMerlin));
		System.out.println("Dumbledore\t" + winNumOfDumbledore + "\t" + df.format(winRateOfDumbledore));
	}

}

Util.java

package com.zx.main;
/**
* @author 作者
* @version 創建時間:2019年10月18日 下午3:48:34
* @email [email protected]
* 類說明
*/

import java.text.DecimalFormat;

import com.zx.bean.Fighter;
import com.zx.util.Util;

public class Main {
	public static void main(String[] args) {
		Fighter[] f = new Fighter[3];
		for (int i = 0; i < 3; i++) {
			f[i] = new Fighter();
			f[i].setWeatherAlive(true);
		}
		int gameTime = 10000;
		int winNumOfGandalf = 0;
		int winNumOfMerlin = 0;
		int winNumOfDumbledore = 0;
		float winRateOfGandalf = 0.0f;
		float winRateOfMerlin = 0.0f;
		float winRateOfDumbledore = 0.0f;
		f[0].setAccuracy(33);
		f[1].setAccuracy(50);
		f[2].setAccuracy(100);
		for (int i = 0; i < gameTime; i++) {
			while (!Util.wheatherOnlyOne(f)) {//當比賽未結束時
				if (f[0].isWeatherAlive()) {//如果Gandalf存活,選擇M或者D進行射擊,如果D存活,射擊D,如果D不存活,則射擊M
					f[0].shootAtTarget(Util.getCurrentBestTarget(f, 0));
				}
				if (f[1].isWeatherAlive()) {
					if(!Util.wheatherOnlyOne(f)) {
						f[1].shootAtTarget(Util.getCurrentBestTarget(f, 1));
					}
					else {
						break;
					}
				}
				if (f[2].isWeatherAlive()&& !Util.wheatherOnlyOne(f)) {
						f[2].shootAtTarget(Util.getCurrentBestTarget(f, 2));
				}
			}
			if (Util.getWinnerNum(f) == 0) {
				winNumOfGandalf++;
			}else
			if (Util.getWinnerNum(f) == 1) {
				winNumOfMerlin++;
			}else
			if (Util.getWinnerNum(f) == 2) {
				winNumOfDumbledore++;
			}else {
				System.out.println("dada");
			}
			Util.setAllAlive(f);
		}

		DecimalFormat df = new DecimalFormat("0.00%");
		winRateOfGandalf = (float) winNumOfGandalf / gameTime;
		winRateOfMerlin = (float) winNumOfMerlin / gameTime;
		winRateOfDumbledore = (float) winNumOfDumbledore / gameTime;
		System.out.println("LEADERBOARD - AFTER " + gameTime + " DUELS");
		System.out.println("Contestant\t" + "Number of Wins" + "Winning Percentag");
		System.out.println("Gandalf\t" + "\t" + winNumOfGandalf + "\t" + df.format(winRateOfGandalf));
		System.out.println("Merlin\t" + "\t" + winNumOfMerlin + "\t" + df.format(winRateOfMerlin));
		System.out.println("Dumbledore\t" + winNumOfDumbledore + "\t" + df.format(winRateOfDumbledore));
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章