Scalable solution for Rock-Paper-Scissor

后端 未结 7 1522
悲&欢浪女
悲&欢浪女 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条回答
  •  Happy的楠姐
    2020-12-05 02:09

    In, Rock-Paper-Scissor games, it is easy to decide if move a wins against move b using their index at a cycle. So you don't have to manually decide in your code the result of every combination as other answers here suggest.


    For the Rock-Paper-Scissor-Spock-Lizard version:

    Let's assign a number to each move (0, 1, 2, 3, 4).

    Notice that every move beats two moves:

    1. The move previous to it in the cycle (or four cases ahead)
    2. The move two cases ahead in the cycle

    So let d = (5 + a - b) % 5. Then:

    1. d = 1 or d = 3 => a wins
    2. d = 2 or d = 4 => b wins
    3. d = 0 => tie

    For the Rock-Paper-Scissor version:

    let d = (3 + a - b) % 3. Then:

    1. d = 1 => a wins
    2. d = 2 => b wins
    3. d = 0 => tie

    Generalization For n >= 3 and n odd:

    Let d = (n + a - b) % n. Then:

    1. If d = 0 => tie
    2. If d % 2 = 1 => a wins
    3. If d % 2 = 0 => b wins

    enter image description here

提交回复
热议问题