Algorithm for scissor paper stone

前端 未结 8 627
日久生厌
日久生厌 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);
      }      
    }
    
    0 讨论(0)
  • 2021-02-06 20:21

    I would prefer to use a static 3x3 matrix to store the possible outcomes. But it is a question of taste, and I am a mathematician.

    0 讨论(0)
  • 2021-02-06 20:21

    \From A java beginner Perspective. User plays with the computer to infinity.

    import java.util.Scanner;
    
    public class AlgorithmDevelopmentRockPaperScissors{
        public static void main(String[] args){
    
            System.out.println("\n\nHello Eric today we are going to play a game.");
            System.out.println("Its called Rock Paper Scissors.");
            System.out.println("All you have to do is input the following");
            System.out.println("\n  1 For Rock");
            System.out.println("\n        2 For Paper");
            System.out.println("\n        3 For Scissors");
    
            int loop;
            loop = 0;
    
            while (loop == 0){
                System.out.println("\n\nWhat do you choose ?");
    
    
            int userInput;
            Scanner input = new Scanner(System.in);
            userInput = input.nextInt();
    
            while (userInput > 3 || userInput <= 0 ){ //ensure that the number input by the sure is within range 1-3. if else the loop trap.
    
                System.out.println("Your choice "+userInput+" is not among the choices that are given. Please enter again.");
                userInput = input.nextInt();
            }
    
    
    
            switch (userInput){
                case 1:
                System.out.println("You Chose Rock.");
                break;
    
                case 2:
                System.out.println("You Chose Paper.");
                break;
    
                case 3:
                System.out.println("You Chose Scissors");
                break;
    
                default:
                System.out.println("Please Choose either of the choices given");
                break;
    
            }
    
    
    
            int compInput;
            compInput = (int)(3*Math.random()+1);
    
            switch (compInput){
                case 1:
                System.out.println("\nComputer Chooses Rock.");
                break;
    
                case 2:
                System.out.println("\nComputer Chooses Paper.");
                break;
    
                case 3:
                System.out.println("\nComputer Chooses Scissors");
                break;
    
            }
    
            if (userInput == compInput){
    
                System.out.println(".........................................");
                System.out.println("\nYou Both chose the same thing, the game ends DRAW.");
                System.out.println(".........................................");
            }
    
            if (userInput == 1 && compInput == 2){
    
                System.out.println(".........................................");
                System.out.println("\nComputer wins because Paper wraps rock.");
                System.out.println(".........................................");
            }
    
            if (userInput == 1 && compInput == 3){
    
                System.out.println(".........................................");
                System.out.println("\nYou win because Rock breaks Scissors.");
                System.out.println(".........................................");
            }
    
            if (userInput == 2 && compInput == 1){
    
                System.out.println(".........................................");
                System.out.println("\nYou win Because Paper wraps Rock");
                System.out.println(".........................................");
            }
    
            if (userInput == 2 && compInput == 3){
    
                System.out.println(".........................................");
                System.out.println("\nComputer wins because Scissors cut the paper");
                System.out.println(".........................................");
            }
    
            if (userInput == 3 && compInput == 1){
    
                System.out.println(".........................................");
                System.out.println("\nComputer Wins because Rock Breaks Scissors.");
                System.out.println(".........................................");
            }
    
            if (userInput == 3 && compInput == 2){
    
                System.out.println(".........................................");
                System.out.println("\nYou win because scissors cut the paper");
                System.out.println(".........................................");
            }
    
            }
    
    
        }
    
    }
    
    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2021-02-06 20:33
    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"
    

    This can even be further simplified to

    return new String[]{"Win", "Lose", "Draw"}[ComputerIn-1];
    

    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. That is, what you're asking for, is just a complicated way of returning "Win", "Lose", or "Draw" with equal probability.

    0 讨论(0)
  • 2021-02-06 20:37

    A simple JavaScript implementation using sine wave function to calculate result:

    <script>
        var tab = ["Lose","Draw","Win"];
        var dict = ["Paper","Stone","Scissors"];
        var i,text = '';
        for (i = 0; i < dict.length; i++) {
            text += i + '-' + dict[i] + ' ';
        }
        var val1 = parseInt(prompt(text));
        var val2 = Math.floor(Math.random() * 3);
        alert('Computer chose: ' + dict[val2]);
        result = Math.sin((val1*180+1) - (val2*180+1));
        alert(tab[Math.round(result)+1]);
    </script>
    

    No if's required, just for fun...

    Check it

    0 讨论(0)
提交回复
热议问题