Scalable solution for Rock-Paper-Scissor

后端 未结 7 1523
悲&欢浪女
悲&欢浪女 2020-12-05 01:28

Just went through a variant of the game : Rock-Paper-Scissor-Lizard-Spock

I have written a Java code for traditional R-P-S problem, but when I tried extending my code

7条回答
  •  再見小時候
    2020-12-05 02:08

    The nature of Rock-Paper-Scissors is such that you have to explicitly handle the case for every possible combination of states. So the number of cases you have to cover increases exponentially with the number of players, and polynomially (with the order of the polynomial being the number of players) with the number of options.

    Having said that, Java's enums are good for this kind of thing.

    Here's my stab at it:

    import java.util.Arrays;
    import java.util.List;
    
    enum Result {
        WIN, LOSE, DRAW;
    }
    
    enum Option {
    
        ROCK(SCISSORS),
        PAPER(ROCK),
        SCISSORS(PAPER);
    
        private List

    Adding more cases (Lizard and Spock) is consequently relatively simple. Adding more players would be more complicated; among other things, you'd have to determine what the rules of three-player Rock-Paper-Scissors even are, because I have no idea.

提交回复
热议问题