2016-04-21 27 views
0

ゲームの目標は、コンソールからテキストメニューを使用して2人のプレーヤーのサイコロを投げるゲームを作成することです。私は3つのクラス、ゲーム、プレイヤー、ダイスを持っています。プレーヤークラスにはストリングプレイヤー名フィールドとintプレーヤースコアフィールドがあります。ダイスクラスにはRNGコードがありますが、ゲームのほとんどはゲームクラスにあります。オブジェクトフィールドへの追加

ゲームを開始するとき、私は2人のプレイヤー(ユーザーから取られたテキスト)に名前をつけ、2人のプレーヤーオブジェクトを作成します。私はゲームの1ラウンドからスコアを返す "ロールダイス"メソッドを呼び出すことができますが、プレーヤーオブジェクトに既に定義されているフィールドにそのスコアをどのように追加しますか?

プレーヤーごとに1つのオブジェクトがあるはずですが、個々のオブジェクトを参照することはできないようです。これらのオブジェクトを別の方法で作成しても、プレーヤーのスコアを増やそうとしているメソッドと同じクラスのものに問題がありますか?

あなたはオブジェクトがもはや参照/表示されている、とされるために利用可能になるとすぐに去るよう Players' outside of the method. Because you declareプレーヤープレイヤー inside the namePlayers() `メソッド、メソッド(メソッドのスコープ)への参照を持っていないする必要が
import java.util.Scanner; 

    public class Game 
    { 
     // instance variables - replace the example below with your own 
     private int x; 

     /** 
     * Constructor for objects of class Game 
     */ 
     public Game() 

     { 
      // initialise instance variables 
      x = 0; 

     } 

     public void menu() 
     { 

     Scanner scanner = new Scanner(System.in); 


      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("(6) Choose an option:");  

     int selection = scanner.nextInt(); 

     switch (selection) { 
      case 1: 
       // Perform "original number" case. 
         nameplayers(); 
         playround(); 
       break; 
      case 2: 
       // Perform "encrypt number" case. 
         System.out.println("(2) "); 
       break; 
      case 3: 
       // Perform "decrypt number" case. 
            System.out.println("(3) "); 
       break; 
      case 4: 
       // Perform "quit" case. 
            System.out.println("(4) "); 
       break; 
      default: 
       // The user input an unexpected choice. 



     } 


    } 

    public void nameplayers() 
    { 
    String namep1; 
    String namep2; 
    Scanner scanner = new Scanner(System.in); 

    System.out.print("Enter the name of player 1: "); 
    namep1 = scanner.next(); 
    Player P1 = new Player(); 
    P1.setName(namep1); 
    System.out.print("Enter the name of player 2: "); 
    namep2 = scanner.next(); 
    Player P2 = new Player(); 
    P2.setName(namep2); 


    } 

     public void rolldice() 
     { 
      int dice1; 
      int dice2; 
      int rollTotal; 

      // put your code here 
      dice1 = (int)(Math.random()*6) + 1; 
      dice2 = (int)(Math.random()*6) + 1; 
      if(dice1 == dice2){ 
       rollTotal = ((dice1 + dice2) *2); 
      } 
      else { 
       rollTotal = (dice1 + dice2); 
       } 



      System.out.println(" has rolled a " + dice1 + " and " + dice2 + " which totals " + rollTotal + " points"); 
      P1.addScore(rollTotal); 

    } 

    public void playround() 
    { 
    String namep1; 
    String namep2; 
    Scanner scanner = new Scanner(System.in); 

    System.out.println("The dice are thrown!"); 
    try{ 
     Thread.sleep(2000);//2000ms = 2s 
    }catch(InterruptedException ex){ 
    } 
    System.out.println(".."); 
    try{ 
     Thread.sleep(2000);//2000ms = 2s 
    }catch(InterruptedException ex){ 
    } 
    System.out.println(" .."); 
    try{ 
     Thread.sleep(2000);//2000ms = 2s 
    }catch(InterruptedException ex){ 
    } 
    System.out.println(" :"); 
    try{ 
     Thread.sleep(2000);//2000ms = 2s 
    }catch(InterruptedException ex){ 
    } 
    System.out.println(" .."); 
    try{ 
     Thread.sleep(2000);//2000ms = 2s 
    }catch(InterruptedException ex){ 
    } 


    rolldice(); 

    } 
    } 

     import java.util.Scanner; 


public class Player 
{ 
    // the name of the player 
    private String playerName; 

    // the current score of the player 
    private int playerScore; 

    /** 
    * Constructor for objects of class Player 
    */ 
    public Player() 
    { 

    String playerName = ""; 

    int playerScore = 0; 

    } 


    public void setName(String newName) 
    { 
     // put your code here 
     playerName = newName; 
        System.out.println("Player name set as " + 
           newName); 

    } 
    public void addScore(int addScore) 
    { 
     // put your code here 
     playerScore = playerScore + addScore; 

    } 

} 

答えて

0

ガベージコレクション。

あなたGameクラスへのローカル変数としてプレイヤーを定義します。

public class Game 
{ 
    Player player1; 
    Player player2; 
} 

次に、あなたのnamePlayers()方法では、単にプレイヤーを作成して設定します。

P1 = new Player(); 
P1.setName(namep1); 
System.out.print("Enter the name of player 2: "); 
namep2 = scanner.next(); 
P2 = new Player(); 
P2.setName(namep2); 
0

それを行うには、ほとんどのオブジェクト指向の方法プライベート変数を更新するメソッドを記述することです。以下のような何か:

public void updateScore(int score) { 
    this.x = x + score; 
{ 

あなたが好きなプレーヤーオブジェクトでこのメソッドを呼び出します。 player1.updateScore(resultPlayer1)。 player2.updateScore(resultPlayer2);

関連する問題