2016-12-17 28 views
1

我正在設計一個模擬骰子游戲的程序。該代碼計算每輪獲得的總分數以及每輪獲勝的玩家。如何使用for循環來計數變量?

我想獲得的總體勝利和積分。我嘗試在主類中使用for循環,但我不確定如何在此問題中實現它。

第1輪:

播放機1 3 1 5分:9

播放器2 2 6 6分:14

優勝者是播放器2

第2輪:

玩家1 3 6 5分數:14

播放器2 2 3 2分:7

優勝者是播放器1

第3輪:

播放機1 3 3 6分:12

播放器2 5 4 6分:15

獲勝者是玩家2

總勝:播放器1 - > 1 /播放器2 - > 2個

總分:播放器1 - > 35 /播放器2 - > 36

主類

import java.util.Scanner; 

public class Game { 
    // ------------------- FIELDS ------------------------  
     // Create instance of Scanner class 
     public static Scanner input = new Scanner(System.in); 
     // variables 
     public static ThreeDiceScorer thrdiesc; 

     public static int diceArray []; 

    // ------------------ METHODS ------------------------ 
     public static void main(String[] args) { 

     int rounds; // input by user 
     int players; // input by user 

     System.out.print("Please input number of rounds (grater or equal than 0) --> "); 
     rounds = input.nextInt(); 
     System.out.print("\n"); 

     System.out.print("Please input number of players (grater or equal than 2) --> "); 
     players = input.nextInt(); 
     System.out.print("\n"); 


     for (int r = 0; r < rounds; r++) { // loop for number of rounds 
     int max = 0; 
     int max_p = 0; 
     System.out.println("Round " + (r+1) + ": "); 
     for (int p = 0; p < players; p++) { //loop for players 
      int diceArray[] = new int[3]; 
     for (int i = 0; i < diceArray.length; i++) { // loop for dice Array (data of Array) 
     diceArray [i] = 1 + (int)(6 * Math.random()); 
     } 
     // Create new ThreeDice and calculator instances 
     thrdiesc = new ThreeDiceScorer(diceArray [0], diceArray [1], diceArray [2]); 

     //Calculate 
     thrdiesc.calcTotalPoints(); 
     thrdiesc.printResult(p, r); 
      if (thrdiesc.total > max) { 
       max = thrdiesc.total; 
       max_p = p; 
      } 
      } 
     System.out.println("Winner is player " + (max_p + 1) + "\n"); 
      } 
     System.out.println("Total wins: "); 
     System.out.println("Total points: "); 

    }//end Main Method 
} // end Class 

計算類

public class ThreeDiceScorer { 
    public static int total; 
    public int die1; 
    public int die2; 
    public int die3; 

    public ThreeDiceScorer(int s1, int s2, int s3) { 
      die1 = s1; 
      die2 = s2; 
      die3 = s3; 
    } 
public void calcTotalPoints() { 
    int sumOfDice = die1 + die2 + die3; 
     total= sumOfDice; 
    } 

     public void printResult(int p, int r) { 
     System.out.println("player " + (p + 1) + " " + die1 + " " + die2 + " " + die3 + " " + "points: " + total); 
    } 
} 

回答

0

我用在主類環試過,但我不知道如何實現它在這個問題。

我會說,一次做一件事。測試你的實現,一旦你確認它的工作,然後繼續前進。順序步驟,以達到你的目標應該是:

步驟

  1. 實施骰子首批推出的一個球員。 (可以用一種方法來做)
  2. 調用上面實現的方法爲第二位玩家擲骰子。
  3. 決定優勝者

  4. 一旦步驟1-3被正確執行,一個循環(對於該特定情況下,優選一for-loop

    //Example: 
    int numOfRounds = 3; //can receive this from user input 
    
    for(int x=0; x<numOfRounds; x++){ 
        rollDice(playerOne); 
        rollDice(playerTwo); 
        decideWinner(playerOne, playerTwo); 
    } 
    
  5. 一旦步驟1-4內封閉步驟1-3經過測試,工作正常。落實總成績顯示:

    //Example: 
    int numOfRounds = 3; //can receive this from user input 
    for(int x=0; x<numOfRounds; x++){ 
        rollDice(playerOne); 
        rollDice(playerTwo); 
        decideWinner(playerOne, playerTwo);    
    } 
    displayFinalScore(); 
    

總分和總勝場可以存儲在其中看起來這是一個非常簡單的播放器類:

public class Player{ 
    private String name; 
    private int totalScore; 
    private int totalWins; 
} 

動態多播放器

我試圖讓解決方案儘可能短而簡單爲了你的理解但是如果你想讓這個節目動態吸納n的玩家數量。同樣的程序流程仍然適用。

在步驟4中,你可以做到這一點爲:

int numOfPlayers = 2; //can receive this from user input 

ArrayList<Player> players = new ArrayList<Player>(); 
for(int x=0; x<numOfPlayers; x++) 
    numOfPlayers.add(new Player("Player " + (x+1))); 

for(int x=0; x<numOfRounds; x++){ 
    for(int y=0; y<players.size(); y++) //iterate through all players 
     rollDice(players.get(y)); 
    decideWinner(players);    //decide the winner from all the players 
} 
+0

將這項工作超過兩個選手,作爲球員和輪數輸入用戶 – astrnm

+0

@astrnm我想過太多,但我擔心這可能會使解決方案複雜化。不是直接插入播放器對象,他可以插入播放器對象列表,它仍然可以工作。無論如何,我想我會更新它。 – user3437460

+0

謝謝你的幫助,我真的很感激。我一直在努力實現這個代碼一天,並且我越來越靠近你了。但是我對java很陌生,當你說_ ** 1時,我有點困惑。1,先執行一個玩家的骰子。 (可以用方法做) 2.調用上面實現的第二個玩家擲骰子的方法。 3.決定贏家** _我真的不知道該怎麼做。再一次你真的很有幫助,謝謝 – astrnm

0

有很多方法來解決這個問題,我想補充兩個計數器像int count = 0,並增加他們:一個有一個的勝利(對前:if(victory) { c++ })和其他通過表達每次count = count + points。 記住要實現這些整數開頭或for循環將重置COUNT1 COUNT2和0