Algorithm for scissor paper stone

前端 未结 8 613
日久生厌
日久生厌 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:44

    This is how I would do it:

    public class Program
    {
    
        public enum RPSPlay { Rock, Scissors, Paper }
        public enum RPSPlayResult { Win, Draw, Loose }
    
        public static readonly int SIZE = Enum.GetValues(typeof(RPSPlay)).Length;
    
        static RPSPlayResult Beats(RPSPlay play, RPSPlay otherPlay)
        {
            if (play == otherPlay) return RPSPlayResult.Draw;
            return ((int)play + 1) % SIZE == (int)otherPlay 
                ? RPSPlayResult.Win 
                : RPSPlayResult.Loose;
        }
    
        static void Main(string[] args)
        {
            Random rand = new Random();
            while (true)
            {
                Console.Write("Your play ({0}) (q to exit) : ", string.Join(",", Enum.GetNames(typeof(RPSPlay))));
                var line = Console.ReadLine();
                if (line.Equals("q", StringComparison.OrdinalIgnoreCase))
                    return;
                RPSPlay play;
                if (!Enum.TryParse(line, true, out play))
                {
                    Console.WriteLine("Invalid Input");
                    continue;
                }
                RPSPlay computerPlay = (RPSPlay)rand.Next(SIZE);
                Console.WriteLine("Computer Played {0}", computerPlay);
                Console.WriteLine(Beats(play, computerPlay));
                Console.WriteLine();
            }
        }
    }
    
    0 讨论(0)
  • 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
      }
    }
    
    0 讨论(0)
提交回复
热议问题