2017-11-02 18 views
-1

こんにちは私は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; 
} 
} 
+0

'getRandomChoice()'は文字列を返します。どのようにそれを格納していますか?それでも、コンピュータの選択肢を手に入れようとしていませんか?なぜあなたは 'getRandomChoice()'を直接呼びますか? –

+0

真理値表上に広告を表示するか、またはurコードを紙に書いている状態で実行するだけで助けになるかもしれません – tgkprog

答えて

1

それは一部のように見えるプログラムは、RPSクラス

メインクラス内playRound()メソッドでコンピュータ入力し、ユーザ入力との間の関係を分析しますあなたがゲームをプレイするために使用しているものとは対照的に、あなたが表示しているものの中に問題がある可能性があります。 game.playround()を呼び出すと、コンピュータがゲームをプレイするために使用している「オブジェクト」をランダムに選択するgetRandomChoice()を呼び出しています。しかし、メインクラスの次の行では、getRandomChoice()を再度呼び出すgame.getComputerChoice()を呼び出します。何を実現する必要があるのは、getRandomChoice()メソッドを呼び出すたびに(潜在的に)別の答えを得るということです。その答えは、コンピュータの「選択肢」を変数に隠し、その変数をgetComputerChoice()メソッドで返すだけです。これはgetRandomChoice()をもう一度呼び出すのではありません。

0

"getComputerChoice()"メソッドに問題があるようです。

"getRandomChoice()"の新しいインスタンスを返すのではなく、コンピュータ選択が既に "getRandomChoice()"によって設定されているため、 "computerChoice"を返す必要があります。作られています。

public String getComputerChoice() { 
    return computerChoice; 
} 
0

あなたは物を保管していません。ランダムに1回、ラウンドごとにどこかに保管してください。そして、この方法では、あなたは物事をリセットせず、多くのコードを繰り返します。だから、勝ったのはint、0 == tie、1 == player、2 == computerです。

コンピュータが持っているものをコンピュータの選択肢に保存し、1回に1回だけランダムに設定します。

public void playRound(String playerChoice) { 
resetRound(); 
getRandomChoice(); 
playerWins = false;//better to use an int for who won /round status 
computerWins = false; 
    rounds++;//do not need to repeat this, its common so put it here 
if ((playerChoice.equals("R") && computerChoice.equals("S")) 
     || (playerChoice.equals("P") && computerChoice.equals("R")) 
     || (playerChoice.equals("S") && computerChoice.equals("P"))) { 

    playerWins = true;//this needs to be reset 
    playerScore++; 
} else if ((playerChoice.equals("R") && computerChoice.equals("P")) 
     || (playerChoice.equals("P") && computerChoice.equals("S")) 
     || (playerChoice.equals("S") && computerChoice.equals("R"))) { 

    computerWins = true; 
    computerScore++; 
} else { 
    isTie = true; 
} 
関連する問題