Java – Rock Paper scissors for foldable solutions

Just experienced a variant of the game: Rock Paper scissor Lizard Spock

I have written java code for a traditional r-p-s problem, but when I try to extend the code of my game version (r-p-s-l-s), I think my code is very bad This is a fragment:

if (player1.equals("ROCK") && 
         player2.equals("SCISSORS")) {
        winner = 1;
    }
    // Paper covers rock...
    else if (player1.equals("PAPER") &&
         player2.equals("ROCK")) {
        winner = 1;
    }
    // Scissors cut paper...
    else if (player1.equals("SCISSORS") &&
         player2.equals("PAPER")) {
        winner = 1;
    }
    else {
        winner = 2;
    }

I realized that the code could not easily be extended to newer versions - and more than 2 players This is mainly due to multiple if / else or switch / case I need some help redesigning my code to achieve two goals:

>Further modification according to r-p-c-l-s problem. > Support more than 2 players

I don't need code, just some guidelines that should help

thank you!!

Editor: it seems that I'm wrong. I think this game can be played by more than 2 players I'm sorry for this error, please ignore the second request

Solution

In the rock paper scissor game, it is easy to decide whether to use their index to move a victory in a loop So you don't need to manually determine the result of each combination in your code. Other answers are suggested here

For rock paper scissor Spock lizard version:

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

Note that each step jumps two steps:

>The action before this cycle (or four ahead) > this action is two ahead of this cycle

So let d = (5 a – b)% 5 then:

>D = 1 or D = 3 = > one win > d = 2 or D = 4 = > b win > d = 0 = > tie

For rock paper scissor version:

Let d = (3 a – b)% 3 then:

>D = 1 = > one win > d = 2 = > b win > d = 0 = > tie

Generalization of n = 3 and n odd numbers:

Let d = (n a – b)% n. then:

>If d = 0 = > tie > if d% 2 = 1 = > one win > if d% 2 = 0 = > b win

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>