Algorithm for scissor paper stone

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

    Here is one-liner that we created at lunchtime.

    using System;
    
    public class Rps {
      public enum PlayerChoice { Rock, Paper, Scissors };
      public enum Result { Draw, FirstWin, FirstLose};
    
      public static Result Match(PlayerChoice player1, PlayerChoice player2) {
        return (Result)((player1 - player2 + 3) % 3);
      }
    
      public static void Main() {
        Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Rock), Result.Draw);
        Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Paper), Result.Draw);
        Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Scissors), Result.Draw);
    
        Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Scissors), Result.FirstWin);
        Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Paper), Result.FirstLose);
    
        Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Rock), Result.FirstWin);
        Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Scissors), Result.FirstLose);
    
        Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Paper), Result.FirstWin);
        Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Rock), Result.FirstLose);
      }
    
      public static void Test(Result sample, Result origin) {
        Console.WriteLine(sample == origin);
      }      
    }
    

提交回复
热议问题