算法剪刀石头纸 [英] Algorithm for scissor paper stone

查看:284
本文介绍了算法剪刀石头纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用它的工作原理下面的方法,但不知道是否有更好的算法进行测试。有没有更好的办法做到这一点?这样做在C#中,但把语法放在一边,认为该算法将是整个OOP语言一样。谢谢你。

 公共字符串戏(INT userInput)
        {// ComputerIn是1-3之间的随机产生的数
            ComputerIn = computerInput();

            如果(ComputerIn == userInput)
                返回画;

            否则,如果(ComputerIn == 1&安培;&安培; userInput == 2)
                返回赢;

            否则,如果(ComputerIn == 2&安培;&安培; userInput == 3)
                返回赢;

            否则,如果(ComputerIn == 3&安培;&安培; userInput == 1)
                返回赢;

            否则,如果(ComputerIn == 1&安培;&安培; userInput == 3)
                返回输;

            否则,如果(ComputerIn == 2&安培;&安培; userInput == 1)
                返回输;

            其他
                返回输;
        }
 

解决方案

  IF((ComputerIn)%3 + 1 == userInput)
    返回赢;
否则,如果((userInput)%3 + 1 == ComputerIn)
    返回输
其他
    返回画
 

如果你换3绕到1(使用%),那么获胜者总是比失败者更大的1。

这方法是更自然,当你使用0-2,在这种情况下,我们会使用(ComputerIn + 1)%3 。我想到了我的答案通过底层 ComputerIn ComputerIn-1 UserInput UserInput-1 并简化了前pression。

修改,看后很长一段时间这个问题。由于写的,如果 ComputerIn 不使用其他任何地方,并仅用于确定赢/输/平局,那么这种方法实际上相当于:

 如果(ComputerIn == 1)
    返回赢;
否则,如果(ComputerIn == 2)
    返回输
其他
    返回画
 

这是这样做的结果是完全没有区别。除非随机产生的数被暴露于该方法的外部。无论您输入的是什么,总有1/3的机会所有的可能性。

I am using the following method which works but wondering if there is a better algorithm to perform the test. Is there a better way to do it? Doing this in C# but putting syntax aside, believe the algorithm is going to be the same across OOP languages. Thank you.

public String play(int userInput)
        {   //ComputerIn is a randomly generated number between 1-3
            ComputerIn = computerInput();

            if (ComputerIn == userInput)
                return "Draw";

            else if (ComputerIn == 1 && userInput == 2)
                return "Win";

            else if (ComputerIn == 2 && userInput == 3)
                return "Win";

            else if (ComputerIn == 3 && userInput == 1)
                return "Win";

            else if (ComputerIn == 1 && userInput == 3)
                return "Lose";

            else if (ComputerIn == 2 && userInput == 1)
                return "Lose";

            else
                return "Lose";
        }

解决方案

if ((ComputerIn) % 3 + 1 == userInput)
    return "Win";
else if ((userInput) % 3 + 1 == ComputerIn)
    return "Lose"
else
    return "Draw"

If you wrap 3 around to 1 (using %) then the winner is always 1 greater than the loser.

This approach is more natural when you use 0-2, in which case we would use (ComputerIn+1)%3. I came up with my answer by subbing ComputerIn with ComputerIn-1 and UserInput with UserInput-1 and simplifying the expression.

Edit, looking at this question after a long time. As written, if the ComputerIn is not used anywhere else, and is only used to determine win/lose/draw, then this method is actually equivalent to:

if (ComputerIn == 1)
    return "Win";
else if (ComputerIn == 2)
    return "Lose"
else
    return "Draw"

The results from this are entirely indistinguishable. Unless the randomly generated number is exposed to outside of this method. No matter what your input is, there's always 1/3 chance of all possibilities.

这篇关于算法剪刀石头纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆