“三射手算法”

时间:2015-03-17 23:00:35

标签: java algorithm probability

就像标题所说,你有3个'射手':射手 A B C
规则如下:

每个人都有不同的命中率:
射击 A 将击中 100%次。
射击 B 将击中 80%次。
射手 C 将击中 50%的次数。

每一名射手在被击中后都会死亡,并会被移除。

每个射手都会优先击中仍然活着的射手,并且击中的几率最高。如果所有射手都还活着: C 射击 A B 射击 A A 射击 B

每个射手在轮到他时只能射一颗子弹。

每个开始的新游戏都会让三个射手中的一个随机开始。

一旦射手轮到他(如果那个射手不是唯一一个站立的射手),那么下一个将要射击的射手将被随机挑选,而那个刚刚轮到他的射手就无法挑选再次(意味着刚刚出场的射手不能连续两次打,但是在下一次转弯之后他可能会再次打球)。

一场比赛只有在最后一名射手站立的比赛中获胜才能结束。
_________________________________________________________________________

现在,我的任务是找到每个射手的平均胜率。让我们说,2,000,000场比赛。
我的结果如下:
A:0.2344525(23.4%),B:0.3187745(31.8%),C:0.446773(44.6%)。 我们班上没有给出正式答案,而且我检查过的每个人都有自己不同的答案。

我的解决此方案的代码是否正确(以向我提供正确答案的形式)?

public static char shooters(){
        char winner = 'x';//'x' means that there's no winner yet
        char a = 'v', b = 'v', c = 'v';//'v' means that they're alive
        double turn = Math.random()*3, shoot;
        /*turn:
          0 to 1 will always mean it's a's turn.
          1 to 2 will always mean it's b's turn.
          2 to 3 will always mean it's c's turn.
        */
        while(winner == 'x'){//winner will change to 'a', 'b' or 'c' upon ending
            if(turn < 1){//A starts (1.0)
                b = 'x';//b' is dead.
                turn = 2.5;//c's turn now.
            }else if(turn < 2){//B starts (0.8)
                shoot = Math.random();
                if(shoot <= 0.8){//b hits
                    if(a == 'v' && c == 'v'){//both opponents alive, a goes down
                        a = 'x';//a' is dead.
                        turn = 2.5;//c's turn now.
                    }else{
                        winner = 'b';//whatever the case, b will hit the last competitor left.
                    }
                }else{//b misses
                    if(a == 'v' && c == 'v'){
                        turn = Math.random()*2;
                        if(turn >= 1){//since we can't let b' play the next turn consecutively:
                            turn++;//adjust to represent c's turn instead of b's.
                        }
                    }else{
                        turn = 2.5;//c's turn, since a is dead.
                    }
                }
            }else{//C starts (0.5)
                shoot = Math.random();
                if(shoot <= 0.5){//c hits
                    if(a == 'v' && b == 'v'){//both opponents alive, a goes down
                        a = 'x';//a' is dead.
                        turn = 1.5;//b's turn now.
                    }else{
                        winner = 'c';//whatever the case, c will hit the last competitor left.
                    }
                }else{//c misses
                    if(a == 'v' && b == 'v'){
                        turn = Math.random()*2;
                    }else if(a == 'v'){
                        winner = 'a';//a's turn since b is dead which leads to c dying.
                    }else{
                        turn = 1.5;//b's turn, since a' is dead.
                    }
                }
            }
        }
        return winner;
    }

0 个答案:

没有答案