我正在设计一个模拟骰子游戏的程序。该代码计算每个回合的总积分以及赢得每个回合的玩家。

我试图获得总的胜利数和总分。我尝试在主类中使用for循环,但不确定如何在此问题中实现它。


  第1轮:
  
  玩家1 3 1 5点:9
  
  玩家2 2 6 6点:14
  
  获胜者是玩家2
  
  第二回合
  
  玩家1 3 6 5点:14
  
  玩家2 2 3 2分:7
  
  获胜者是玩家1
  
  第三回合
  
  玩家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);
    }
}

最佳答案

我尝试在主类中使用for循环,但不确定如何在此问题中实现它。


我会说,一次做一件事。测试您的实现,一旦确认它可以正常工作,就继续进行。达到目标的顺序步骤是:

脚步


首先为一个玩家实施掷骰子。 (可以用一种方法做到)
为第二名玩家的骰子掷骰调用上述实现的方法。
决定获胜者
一旦正确执行了第1-3步,就将第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);
}

一旦步骤1-4经过测试,即可正常工作。实施总分显示:

//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();



总得分和总胜利可以存储在一个非常简单的Player类中,如下所示:

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
}

08-06 12:21