Algorithm for scissor paper stone

前端 未结 8 617
日久生厌
日久生厌 2021-02-06 20:03

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

8条回答
  •  一整个雨季
    2021-02-06 20:46

    Here's one of many possible solutions. This will print Win.

    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Input userInput = Input.Rock;
          Result result = Play(userInput);
          Console.WriteLine(Enum.GetName(result.GetType(), result));
          Console.ReadKey();
        }
    
        static Result Play(Input userInput)
        {
          Input computer = Input.Scissors;
    
          switch (userInput)
          {
            case Input.Paper:
              switch (computer)
              {
                case Input.Paper: return Result.Draw;
                case Input.Rock: return Result.Win;
                case Input.Scissors: return Result.Lose;
                default: throw new Exception("Logic fail.");
              }
            case Input.Rock:
              switch (computer)
              {
                case Input.Paper: return Result.Lose;
                case Input.Rock: return Result.Draw;
                case Input.Scissors: return Result.Win;
                default: throw new Exception("Logic fail.");
              }
            case Input.Scissors:
              switch (computer)
              {
                case Input.Paper: return Result.Win;
                case Input.Rock: return Result.Lose;
                case Input.Scissors: return Result.Draw;
                default: throw new Exception("Logic fail.");
              }
            default: throw new Exception("Logic fail.");
          }
        }
      }
      enum Input
      {
        Rock,
        Paper,
        Scissors
      }
      enum Result
      {
        Lose,
        Draw,
        Win
      }
    }
    

提交回复
热议问题