Algorithm for scissor paper stone

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

    Observation: The user wins if userInput is only one ahead of computerInput (case of (1,2), (2,3)) or lag two (case of (3,1)).

    Conversely, if userInput lags one behind computerInput or two ahead, the user loses.

    In the modulo 3, the lag one is the same as the advance two. (-1 mod 3 == 2 mod 3 == 2)

    int decision = (userInput - computerInput + 3) % 3;
    String[] answer = {"Draw", "Win", "Lose"};
    
    return answer[decision];
    

提交回复
热议问题