本文介绍了对石头纸剪刀可扩展的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚经历了本场比赛的一个变种:石头纸剪刀蜥蜴,斯波克

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

我已经写了一个Java code传统RPS的问题,但是当我试图延长我的code的游戏(RPSLS)的新版本。我觉得我的code是非常坏。这里是一个片段:

I have written a Java code for traditional R-P-S problem, but when I tried extending my code for the newer version of the game (R-P-S-L-S)..I felt my code is terribly bad. Here is a snippet :

 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;
    }

我实现了code不能很容易地扩展到新的版本 - 以及超过2名球员。这是多个if / else或开关/件,主要是因为。我需要一些帮助的重新设计我的$ C $下实现2目标:

I realized the code cant be extended easily for the newer version - as well as for more than 2 players. This is mainly because of multiple if/else or switch/cases. I need some help re-designing my code for achieving the 2 objectives :

  1. 进一步修改按照RPCLS问题。

  1. Further modification as per R-P-C-L-S problem.

支持超过2名球员。

我不需要code,只是一些指导方针应该有所帮助。

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

谢谢!

编辑:好像我错在想,这个游戏可以超过2播放器上播放。我对不起这个错误,请忽略第二个要求。

推荐答案

在,石头纸剪刀脚的游戏,很容易决定是否将一个胜利反对此举b。使用其指数在一个周期。所以,你不必在C每个组合的结果,你的$ C $手动定为其他的答案在这里建议。

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:

让的号码指定给每个移动(0,1,2,3,4)。

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

请注意,一举一动击败两个动作:

Notice that every move beats two moves:

  1. 此举previous给它的周期(提前,四例)
  2. 在周期
  3. 此举2箱子领先

因此​​,让 D =(5 + A - B)5%。然后:

  1. D = 1或D = 3 =>一胜
  2. D = 2或D = 4 =>乙胜
  3. 在D = 0 =>领带

有关的石头纸剪的版本:


For the Rock-Paper-Scissor version:

D =(3 + A - B)%3 。然后:

  1. D = 1 =>一胜
  2. D = 2 =>乙胜
  3. 在D = 0 =>领带

泛化对于n> = 3和n奇:


Generalization For n >= 3 and n odd:

D =(N + A - B)%N 。然后:

  1. 如果D = 0 =>领带
  2. 如果D%2 = 1 =>一胜
  3. 如果D%2 = 0 =>乙胜

这篇关于对石头纸剪刀可扩展的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!