こんにちは私は2つのクラスでロックペーパーはさみのゲームを作るための課題を与えられていますが、私は正しい勝者を得ていないゲームは誰でも私がなぜ理解するのを助けることができます再生されますか?ロックペーパーはさみ複数のクラスとメソッドを持つJavaプロジェクト
import java.util.Scanner;
import java.util.Random;
public class GameMain {
public static void main(String[] args) {
System.out.println("Welcome to Rock, Paper, Scissors.\nYou will play against the computer ;-)\n");
Scanner scan = new Scanner(System.in);
boolean keepPlaying = true;
String playerStr = "", computerStr = "";
// using a seed means the same sequence of numbers will be generated.
int seed = 123456;
Random rnd = new Random(seed);
RPSGame game = new RPSGame(rnd);
// The looping section
while (keepPlaying) {
System.out.println("Enter R for rock, P for paper, or S for scissors. Enter X to quit.");
playerStr = scan.nextLine();
System.out.println("You entered: " + playerStr);
if (playerStr.equalsIgnoreCase("X"))
keepPlaying = false;
else if (game.isValidInput(playerStr)) {
game.playRound(playerStr);
computerStr = game.getComputerChoice();
System.out.println("The computer chose: " + computerStr);
if (game.playerWins())
System.out.println("You win!");
else if (game.computerWins())
System.out.println("Computer wins!");
else
System.out.println("It's a tie!");
System.out.println(game.getScoreReportStr());
} else // invalid input
System.out.println("Your input should be R, P, or S. Please enter again.");
}
System.out.println("Bye for now.");
}
}
RPSクラス::
import java.util.Random;
public class RPSGame {
private Random rand;
private int playerScore;
private int computerScore;
private int rounds;
private String computerChoice;
private boolean playerWins;
private boolean computerWins;
private boolean isTie;
/*
* The constructor assigns the member Random variable, rand, to the instance
* passed in (either a seeded or non-seeded instance). It also initializes the
* member variables to their default values: rounds, player and computer scores
* are 0, the playerWins and computerWins variables are set to false. The
* computer choice is initialized to the empty string.
*/
public RPSGame(Random r) {
rand = new Random();
playerScore = 0;
computerScore = 0;
rounds = 0;
computerChoice = "";
playerWins = false;
computerWins = false;
isTie = false;
}
/*
* This method returns true if the inputStr passed in is either "R", "P", or
* "S", false otherwise.
*/
public boolean isValidInput(String inputStr) {
if ((inputStr.equals("R")) || (inputStr.equals("P")) || (inputStr.equals("S"))) {
return true;
} else {
return false;
}
}
/*
* This method carries out a round of play of the RPS game. First, it calls
* resetRound so that all variables from the last round are reset. Then, it gets
* the computer's choice (by calling the getRandomChoice method), compares it to
* the player's choice and determines who wins a round of play according to the
* rules of the game. The player and computer scores are updated accordingly.
* The computerWins and playerWins variables are updated accordingly. The number
* of rounds of play is incremented.
*/
public void playRound(String playerChoice) {
resetRound();
getRandomChoice();
if ((playerChoice.equals("R") && computerChoice.equals("S"))
|| (playerChoice.equals("P") && computerChoice.equals("R"))
|| (playerChoice.equals("S") && computerChoice.equals("P"))) {
rounds++;
playerWins = true;
playerScore++;
} else if ((playerChoice.equals("R") && computerChoice.equals("P"))
|| (playerChoice.equals("P") && computerChoice.equals("S"))
|| (playerChoice.equals("S") && computerChoice.equals("R"))) {
rounds++;
computerWins = true;
computerScore++;
} else {
rounds++;
isTie = true;
}
}
// Returns the choice made by the computer for the current round of play.
public String getComputerChoice() {
return getRandomChoice();
}
// Returns true if the player has won the current round, false otherwise.
public boolean playerWins() {
return this.playerWins;
}
// Returns true if the computer has won the current round, false otherwise.
public boolean computerWins() {
return this.computerWins;
}
// Returns true if neither player nor computer has won the current round, false
// otherwise.
public boolean isTie() {
return this.isTie;
}
// Returns the number of rounds, the player's current score, and the computer's
// current score in a String format.
public String getScoreReportStr() {
String a = Integer.toString(rounds);
String b = Integer.toString(playerScore);
String c = Integer.toString(computerScore);
return ("Total plays: " + a + '\n' + "Your Score: " + b + ", computer score: " + c);
}
/*
* This "helper" method uses the instance of Random to generate an integer which
* it then maps to a String: "R", "P", "S", which is returned. This method is
* called by the playRound method.
*/
private String getRandomChoice() {
int choice = rand.nextInt((3 - 1) + 1) + 1;
if (choice == 1) {
computerChoice = "R";
}
if (choice == 2) {
computerChoice = "P";
}
if (choice == 3) {
computerChoice = "S";
}
return computerChoice;
}
/*
* Resets the variables that record state about each round: computerChoice to
* the empty string and the playerWins and computerWins to false. This method is
* called by the playRound method.
*/
private void resetRound() {
computerChoice = "";
playerWins = false;
computerWins = false;
isTie = false;
}
}
'getRandomChoice()'は文字列を返します。どのようにそれを格納していますか?それでも、コンピュータの選択肢を手に入れようとしていませんか?なぜあなたは 'getRandomChoice()'を直接呼びますか? –
真理値表上に広告を表示するか、またはurコードを紙に書いている状態で実行するだけで助けになるかもしれません – tgkprog