Scalable solution for Rock-Paper-Scissor

后端 未结 7 1527
悲&欢浪女
悲&欢浪女 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 01:47

    The shortest way:

    var n = 5; // Rock, Paper, Scissors, Lizard-Spock
    
    function calculate(x, y, n) {
      return 1 - ((n + x - y) % n) % 2;
    }
    
    function getWinner(p1Gestrure, p2Guesture) {
      if(p1Gestrure === p2Guesture) {
         return - 1; // tie
      }
    
      return this.calculate(p1Gestrure, p2Guesture); // 0: win for p1. 1: win for p2.
    }
    

    I've created a cli game, please feel free to take a look there. https://github.com/julianusti/rpc-es6

提交回复
热议问题