很久以前,在阿斯加德的土地上住着3个巫师甘道夫,梅林和邓布利多。这三个巫师经常争论谁是有史以来最伟大的软件开发人员。为了一劳永逸地结束这场争论,他们同意一场殊死搏斗。甘道夫是一个糟糕的射手,只有1/3的概率击中目标。梅林稍微好一点,命中目标的概率是1/2。邓布利多是一个专业的射手,命中目标的概率是95%。击中意味着杀戮,被击中的人退出决斗。
为了弥补射击技巧上的不平等,三人决定轮流开火,先是甘道夫,然后是梅林,最后是邓布利多。这个循环会一直重复,直到有一个人或生物站着,而这个人或生物将是有史以来最伟大的软件开发人员。
一个明显而合理的策略是让每个巫师射向仍然活着的最准确的射手,理由是这个射手是最致命的,并且有最好的反击机会。
用这个策略编写一个程序来模拟决斗。你的程序应该使用随机数和问题中给出的概率来确定射手是否击中目标。
java:
package ClassObjectThree;
public class ThreeFighter {
//Gandalf, Merlin and Dumbledore
public static void main(String[] args) {
int survivors = 3;//存活人数
int f1WinningProbability = 0;//f1胜利的次数
int f2WinningProbability = 0;//f2胜利的次数
int f3WinningProbability = 0;//f3胜利的次数
int[] pre1 = {1,3};//枪手1 Gandalf 的精度
int[] pre2 = {1,2};//枪手2 Merlin的精度
int[] pre3 = {95,100};//枪手3 Dumbledore的精度
Fighter fighter1 = new Fighter("Gandalf",pre1,true);
Fighter fighter2 = new Fighter("Merlin",pre2,true);
Fighter fighter3 = new Fighter("Dumbledore",pre3,true);
int temp = 1;//若temp = 1则f1开枪
boolean success = false;//是否命中 false是没有命中
int number = 10000;
for(int i = 0 ; i < number; ++i) {
//success = false;//是否命中
survivors=3;
ThreeFighter.resurrection(fighter1,fighter2,fighter3);
while(survivors >1) {
success = false;
//System.out.println("temp: " + temp);
if(temp == 1) {//temp == 1, 枪手1开枪
if(fighter1.live == true) {//枪手1是否还活着
//System.out.println(fighter1.name + " is " + fighter1.live);
if(fighter3.live == true) {//枪手3是否还活着
success = fighter1.shootAttarget(fighter3);
}else {
success = fighter1.shootAttarget(fighter2);
}
}
temp = 2;//枪手2开枪
if(success == true) {
survivors-=1;
}
}else if(temp == 2) {//temp == 2 枪手2开枪
//System.out.println(fighter2.name + " is " + fighter2.live);
if(fighter2.live == true) {//枪手2是否还活着
if(fighter3.live == true) {//枪手3是否还活着
success = fighter2.shootAttarget(fighter3);
}else {
success = fighter2.shootAttarget(fighter1);
}
}
temp = 3;//枪手3开枪
if(success == true) {
survivors-=1;
}
}else if(temp == 3) {//temp == 3 枪手3开枪
//System.out.println(fighter3.name + " is " + fighter3.live);
if(fighter3.live == true) {//枪手3是否还活着
if(fighter2.live == true) {//枪手2是否还活着
success = fighter3.shootAttarget(fighter2);
}else {
success = fighter3.shootAttarget(fighter1);
}
}
temp = 1;//枪手1开枪
if(success == true) {
survivors-=1;
}
}
//System.out.println(fighter1.live +" " + fighter2.live + " "+fighter3.live + "幸存者: "+survivors);
}
if(fighter1.live == true) {
//System.out.println("fighter1 winner");
f1WinningProbability+=1;
}
else if(fighter2.live == true) {
//System.out.println("fighter2 winner");
f2WinningProbability+=1;
}
else if(fighter3.live == true) {
//System.out.println("fighter3 winner");
f3WinningProbability+=1;
}
}
System.out.println("排行榜"+number+"场决斗后");
System.out.println("Contestant Number of Wins Winning Percentag");
System.out.printf("%-10s%6d%23f",fighter1.name,f1WinningProbability,(double)(f1WinningProbability / (double)number));
System.out.println();
System.out.printf("%-10s%6d%23f",fighter2.name,f2WinningProbability,(double)(f2WinningProbability / (double)number));
System.out.println();
System.out.printf("%-10s%6d%23f",fighter3.name,f3WinningProbability,(double)(f3WinningProbability / (double)number));
System.out.println();
//System.out.println(fighter2.name + " " + f2WinningProbability + " "+(double)(f2WinningProbability / 10.0));
//System.out.println(fighter3.name + " " + f3WinningProbability + " "+(double)(f3WinningProbability / 10.0));
}
public static void resurrection(Fighter f1,Fighter f2,Fighter f3) {
f1.live=true;
f2.live=true;
f3.live=true;
}
/**
*
* public Fighter battle(Fighter f1,Fighter f2,Fighter f3) {
int liver = 3;//存活人数
while(liver > 1) {
}
return f1;
}
* */
}
class Fighter{
String name;//射击手的名字
int []pre = new int[2];//枪手精度
boolean live;//枪手是否活着
public Fighter(String name, int[] pre, boolean live) {//构造函数
this.name = name;
this.pre = pre;
this.live = live;
}
public boolean shootAttarget(Fighter target) {//射击目标
int temp = 1 + (int)(Math.random() * this.pre[1]);
boolean kill=false;
if(temp <= this.pre[0]) {
target.live = false;
//System.out.println(this.name + " kill " + target.name);
kill=true;
}else {
//System.out.println(this.name + " don't kill " + target.name);
}
return kill;
}
}