RockPaperScissors有多种方法

时间:2017-04-27 23:18:58

标签: java

我正在尝试使用多种方法编写一个rockpaperscissors游戏,但我遇到了一些麻烦。我已经完成了主要方法的所有工作,但我似乎在创建输入验证循环时遇到了一些麻烦。

以下是代码:

我认为让我感到悲伤的方法就是这个

  while (isValid(x) == false)
  {
     System.out.print("Invalid Input: Please try again\n");
     x = getUserChoice();
     isValid(x);
  }

以下是完整资料来源:

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors
{
   public static String getComputerChoice()
   {
   Random r = new Random();
   String[] compInput = new String[3];
   compInput[0] = "Rock";
   compInput[1] = "Paper";
   compInput[2] = "Scissors";
   int random = r.nextInt(2) + 1;

   return compInput[random];
   }

   public static String getUserChoice()
   {
      Scanner kb = new Scanner(System.in);

      String input;
      System.out.print("Enter Rock, Paper, or Scissors: ");
      input = kb.nextLine();

      return input;
   }

   public static boolean isValid(String userChoice)
   {
      if (!userChoice.equalsIgnoreCase("Rock"))
         return false;
      else if (!userChoice.equalsIgnoreCase("Paper"))
         return false;
      else if (!userChoice.equalsIgnoreCase("Scissors"))
         return false;
      else
      return true;
   }

   public static void displayOutcome(String choice1, String choice2)
   {
      if (choice1.length() - 1 > choice2.length() -1)
      {
         if (choice1.equalsIgnoreCase("Paper"))
            System.out.print("\nPlayer Wins: paper beats rock.");
         else if (choice2.equalsIgnoreCase("Paper"))
            System.out.print("\nPlayer Wins: scissors beat paper");
         else if (choice1.equalsIgnoreCase("Scissors") && (choice2.equalsIgnoreCase("Rock")))
            System.out.print("\nComputer Wins: Rock Beats Scissors");
      }

      if (choice2.length() - 1 > choice1.length() -1)
      {
         if (choice2.equalsIgnoreCase("Paper"))
            System.out.print("\nComputer Wins: paper beats rock.");
         else if (choice1.equalsIgnoreCase("Paper"))
            System.out.print("\nComputer Wins: scissors beat paper");
         else if (choice2.equalsIgnoreCase("Scissors") && (choice1.equalsIgnoreCase("Rock")))
            System.out.print("\nPlayer Wins: Rock Beats Scissors");
      }
   }

   public static void main(String[] args)
   {
      String x = getUserChoice();
      String y = getComputerChoice();

      while (isValid(x) == false)
      {
         System.out.print("Invalid Input: Please try again\n");
         x = getUserChoice();
         isValid(x);
      }

      displayOutcome(x, y);
   }
}

1 个答案:

答案 0 :(得分:1)

鉴于您没有告诉我们您期望的可观察结果,您实际观察到的可观察结果,以及您的问题是什么,我会尽我所能。

您的代码存在很多问题。

while循环体的最后一行是isValid(x);,它只是确定truefalse的值,然后忽略它。您可以删除该行而不影响程序逻辑。

每次拨打Random r = new Random();时都会指定getComputerChoice。这使得结果不是随机的。相反,在方法外部初始化final Random实例,在您的情况下作为静态成员。

在你的逻辑if (choice1.length() - 1 > choice2.length() -1)中,不仅比较双方的-1都是多余的,而且它不是比较结果的好方法。逻辑并不明显,如果你要以任何方式更改字符串,让我们说将它们翻译成另一种语言,字符串长度的意外将没有用。如果选择是enum常量:

,可以使用直接逻辑,也许这样
public enum RpsChoices {
  ROCK, PAPER, SCISSORS, ;

  public static Boolean beats(RpsChoices left, RpsChoices right) {
    if (left == null || right == null || left == right ) {
      return null;
    }
    switch (left) {
    case ROCK:
      return right == SCISSORS;
    case PAPER:
      return right == ROCK;
    case SCISSORS:
      return right == PAPER;
    }
  }
}

然后你会有

public static void displayOutcome(RpsChoices player, RpsChoices computer) {
  Boolean outcome = RpsChoices.beats(player, computer);
  if (outcome == null) {
    System.out.println("Tie: Player threw " + player
        + ", computer threw " + computer);
  }
  else if (outcome) {
    System.out.println("Player wins: Player threw " + player
        + ", computer threw " + computer);
  }
  else {
    System.out.println("Computer wins: Player threw " + player
        + ", computer threw " + computer);
  }
}

然后你的isValid循环归结为:

for (RpsChoices choice = RpsChoices.valueOf(getUserChoice().toUpperCase());
     choice != null;
     choice = RpsChoices.valueOf(getUserChoice().toUpperCase())) {
  displayOutcome(choice, getComputerChoice());
}

通过完全不使用isValid方法进行优化。

对于维护人员来说,这一点要清楚得多,更容易理解,更容易受到拼写错误和高性能等问题的影响(尽管性能在这么小的事情上没有实际意义)。