2016-04-20 7 views
0

私はうまく動作するDiceThrowingGameを実装しようとしています。私は現在、「プレイヤー」「ゲーム」の2つのクラスを持っています。 Gameクラスのコードは次のとおりです。別のクラスのメソッドを呼び出すことについて混乱しました

パッケージdice.throwing.game; import java.util.Scanner;

public class Game { 
    private int winningPoints; 
    private Player player1, player2; 
    private boolean gameSetup; 

    private final String defaultWinningScore = "200"; 

    public Game() { 
     this.setWinningPoints(200); 
     this.gameSetup = false; 
    } 

    private int readScore(Scanner scanner) { 
     String score = ""; 
     try { 
      score = scanner.nextLine(); 
      if (score.isEmpty()) { 
       score = this.defaultWinningScore; 
      } 
     } catch (Exception e) { 
     } 
     return Integer.parseInt(score); 
    } 

    private Player createPlayer(Scanner scanner) { 
     String name = null; 
     do { 
      try { 
       name = scanner.nextLine(); 
       if (name.isEmpty()) { 
        System.err.println("Name cannot be empty. Try again: "); 
       } 
      } catch (Exception e) { 
      } 
     } while (name.isEmpty()); 

     return new Player(name); 
    } 

    private void startGame(Scanner scanner) { 
     System.out.println("Enter first player's name: "); 
     player1 = createPlayer(scanner); 
     System.out.println("Enter second player's name: "); 
     player2 = createPlayer(scanner); 
     System.out.println("Maximum score to win (<Enter> to use default 200): "); 
     this.setWinningPoints(this.readScore(scanner)); 
     this.gameSetup = true; 
    } 

    private void playOneRound() { 
     int p1r1, p1r2, p2r1, p2r2; 
     p1r1 = (int) (1 + ((Math.random() * 10) % 6)); 
     p1r2 = (int) (1 + ((Math.random() * 10) % 6)); 
     p2r1 = (int) (1 + ((Math.random() * 10) % 6)); 
     p2r2 = (int) (1 + ((Math.random() * 10) % 6)); 

     int p1Points, p2Points; 
     boolean p1Bonus = false, p2Bonus = false; 
     p1Points = p1r1 + p1r2; 
     p2Points = p2r1 + p2r2; 
     if (p1r1 == p1r2) { 
      p1Points *= 2; 
      p1Bonus = true; 
     } 
     if (p2r1 == p2r2) { 
      p2Points *= 2; 
      p2Bonus = true; 
     } 

     player1.setTotalPoints(player1.getTotalPoints() + p1Points); 
     player2.setTotalPoints(player2.getTotalPoints() + p2Points); 
     System.out.print(player1.getName() + " rolled " + p1r1 + " + " + p1r2 + ", and scored " + p1Points + " points"); 
     if (p1Bonus) 
      System.out.println(" (BONUS!)"); 
     else 
      System.out.println(); 
     System.out.print(player2.getName() + " rolled " + p2r1 + " + " + p2r2 + ", and scored " + p2Points + " points"); 
     if (p2Bonus) 
      System.out.println(" (BONUS!)"); 
     else 
      System.out.println(); 
    } 

    private void leaderBoard() { 
     int p1Points = player1.getTotalPoints(); 
     int p2Points = player2.getTotalPoints(); 

     if (p1Points == p2Points) 
      System.out.println("Both players have the same score (" + p1Points + ") at the moment"); 
     else { 
      System.out.print(player1.getName() + "'s current score is " + p1Points); 
      if (p1Points > p2Points) 
       System.out.println(" <--- CURRENT LEADER!"); 
      else 
       System.out.println(); 
      System.out.print(player2.getName() + "'s current score is " + p2Points); 
      if (p1Points < p2Points) 
       System.out.println(" <--- CURRENT LEADER!"); 
      else 
       System.out.println(); 
     } 
    } 

    private void gameHelp() { 
     System.out.println("<ENTER SOME HELP TEXT HERE>"); 
    } 

    private boolean isGameOver() { 
     int player1Points = player1.getTotalPoints(); 
     int player2Points = player2.getTotalPoints(); 

     if(player1Points == player2Points && 
       player1Points > this.winningPoints) { 
      System.out.println("Game Over! It's a draw!"); 
      return true; 
     } else if(player1Points > this.winningPoints && player1Points > player2Points) { 
      System.out.println("Game Over! " + player1.getName() + " is the winner!"); 
      return true; 
     } else if(player2Points > this.winningPoints && player2Points > player1Points) { 
      System.out.println("Game Over! " + player2.getName() + " is the winner!"); 
      return true; 
     } 
     return false; 
    } 

    private void eventLoop() { 
     Scanner scanner = new Scanner(System.in); 
     int choice = 0; 
     boolean exit = false; 
     while (!exit) { 
      System.out.println("Welcome to my Dice-and-Roll Game!"); 
      System.out.println("=============================="); 
      System.out.println("(1) Start a new game"); 
      System.out.println("(2) Play one round"); 
      System.out.println("(3) Who is leading now?"); 
      System.out.println("(4) Display Game Help"); 
      System.out.println("(5) Exit Game"); 
      System.out.println("Choose an option: "); 

      try { 
       choice = Integer.parseInt(scanner.nextLine()); 
       if (choice < 1 || choice > 5) { 
        System.err.println("Error : Choose an option between 1 and 5"); 
        choice = 0; 
       } 
      } catch (NumberFormatException e) { 
       System.err.println("Error : Choose an option between 1 and 5"); 
       choice = 0; 
      } 

      if (!this.gameSetup && (choice == 2 || choice == 3)) { 
       System.err.println("Error : Players have not been setup"); 
       choice = 0; 
      } 

      switch (choice) { 
      case 1: 
       this.startGame(scanner); 
       break; 
      case 2: 
       this.playOneRound(); 
       break; 
      case 3: 
       this.leaderBoard(); 
       break; 
      case 4: 
       this.gameHelp(); 
       break; 
      case 5: 
       exit = true; 
      } 

      if(this.gameSetup && this.isGameOver()) { 
       System.out.println("Exiting now.. See you later!"); 
       exit = true; 
      } 
     } 
     scanner.close(); 
    } 

    public static void main(String[] args) { 
     Game game = new Game(); 
     game.eventLoop(); 

    } 

    public int getWinningPoints() { 
     return winningPoints; 
    } 

    public void setWinningPoints(int winningPoints) { 
     this.winningPoints = winningPoints; 
    } 

} 

しかし、私はこの部分である「ダイス」と呼ばれる別のクラスへのコードのDiceThrowingの一部を移動したい:全体playOneRound()メソッドを置くかどうかにか、単に混乱

private void playOneRound() { 
     int p1r1, p1r2, p2r1, p2r2; 
     p1r1 = (int) (1 + ((Math.random() * 10) % 6)); 
     p1r2 = (int) (1 + ((Math.random() * 10) % 6)); 
     p2r1 = (int) (1 + ((Math.random() * 10) % 6)); 
     p2r2 = (int) (1 + ((Math.random() * 10) % 6)); 

Math.Random行ですか?責任と抽象化を維持するために

答えて

1

を分離: を、私はあなたがまた、サイコロの配列がnbOfThrowsパラメータで投げ返す第二の方法を持っている可能性が (int) (1 + ((Math.random() * 10) % 6)); の結果を返します方法Dice.getNumber()を持っているでしょう。

次に、playOneRound()は、ゲームルールで許される回数だけサイコロの投げを行います。

+0

ありがとうございます。私はDiceThrowという新しいクラスをコードで作成しました: 'public class DiceThrow { public int generate(){ ランダムランダム=新しいランダム(); return(int)(1 +((Math.random()* 10)%6)); } } ゲームクラスの元のコードをgenerate()メソッドに置き換えました。今はうまく動作します。 –

+0

また、Random変数をDiceのインスタンス変数にしてgenerate()で再利用するだけでいいです – Blitzkr1eg

関連する問題