我必须设计一个程序来模拟玩家滚动三个骰子进行多轮比赛。每个掷骰子都给出了分数。我必须为每一轮掷骰子价值,以及每个玩家对这些值的点数和每一轮的获胜者(该轮中得分最高的玩家,或者如果没有他们是一样的。)
我已经实施了积分计算器,但我不知道如何展示每一轮的胜利者。此外,我应该在水平方向上垂直显示输出。
我想也许比较游戏类中循环内部的值可能有用。附:我是java的新手,如果有更好的解决方案,请提出更改代码的建议。
这是我的节目正在显示
第1轮 - >球员1:2 4 5分:11
第2轮 - >球员1:2 3 5分:10
第3轮 - >球员1:2 4 6分:12
第4轮 - >球员1:4 4 6分:34
第5轮 - >球员1:3 4 5分:52
第1轮 - >球员2:3 5 5分:33
第2轮 - >球员2:3 6 6分:35
第3轮 - >球员2:2 3 4分:49
第4轮 - >球员2:1 1 3分:25
第5轮 - >球员2:1 2 4分:7
这就是它应该显示的内容
第一轮1:1 3 3分:27球员2:1 4 5分:10轮获胜者是球员1 第二轮第1轮1:1 2 5分:8球员2:1 3 6分:10轮获胜者是球员2第3轮选手1:1 4 4分:29球员2:4 5 6分:55圆形冠军是选手2
第四轮第1轮1:1 3 5分:9球员2:1 5 5分:31轮获胜者是球员2第5轮第1轮:3 6 6分:35球员2:2 2 4分:28轮获胜者是球员1
总胜场:球员1:2 /球员2:3
总积分:玩家1:108 /玩家2:134
每轮平均得分:球员1:21.6 /球员2:26.8
总分得分者是球员2。
主要代码
self
ThreeDice class
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 rounds (grater or equal than 0) --> ");
players = input.nextInt();
System.out.print("\n");
for (int p = 0; p < players; p++) { //loop for players
for (int r = 0; r < rounds; r++) { // loop for number of rounds
int diceArray [] = new int [3];
for (int i = 0; i < diceArray.length; i++) { // loop for random 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.getDie1();
thrdiesc.getDie2();
thrdiesc.getDie3();
thrdiesc.threeSame();
thrdiesc.runOfThree();
thrdiesc.pair();
thrdiesc.allDifferent();
thrdiesc.calcTotalPoints();
thrdiesc.printResult(p,r);
}
System.out.print("\n");
}
}//end Main Method
}// end Class
ThreeDiceScorer(计算器)类
public class ThreeDice {
// ---------------------- ATTRIBUTES ---------------------
protected int die1;
protected int die2;
protected int die3;
// ------------------ CONSTRUCTOR -------------------
public ThreeDice(int s1, int s2, int s3) {
// This puts the three dice values in ascending order.
int tmp;
if (s2 < s1) {
tmp = s2;
s2 = s1;
s1 = tmp;
}
if (s3 < s2) {
tmp = s3;
s3 = s2;
s2 = tmp;
}
if (s2 < s1) {
tmp = s2;
s2 = s1;
s1 = tmp;
}
die1 = s1;
die2 = s2;
die3 = s3;
}
// --------------------- METHODS ---------------------
// Accessor methods
public int getDie1() {
return die1;
}
public int getDie2() {
return die2;
}
public int getDie3() {
return die3;
}
public boolean threeSame() {
return (die1 == die3);
}
public boolean runOfThree() {
return (( (die1 + 1) == die2) && ( (die2 + 1) == die3));
}
public boolean pair() {
return (((die1 == die2) || (die2 == die3)) && (die1 != die3));
}
public boolean allDifferent() {
return (!runOfThree() && (die1 != die2) && (die2 != die3));
}
public void printResult() {
if (threeSame())
System.out.println("The roll is all the same.");
else if (runOfThree())
System.out.println("The roll is a run.");
else if (pair())
System.out.println("The roll is a pair.");
else if (allDifferent())
System.out.println("The roll is all different.");
}
}
答案 0 :(得分:0)
循环和最大:
for (int r = 0; r < rounds; r++) { // loop for number of rounds
int max = 0;
int max_p = 0;
System.out.println("Round " + r + ": ");
for (int p = 0; p < players; p++) { //loop for players
int diceArray[] = new int[3];
//...
thrdiesc.printResult(p, r);
if (thrdiesc.total > max) {
max = thrdiesc.total;
max_p = p;
}
}
System.out.println("Winner is player " + (max_p + 1) + "\n");
}
PrintResult方法:
public void printResult(int p, int r) {
System.out.println("player " + (p + 1) + " " + die1 + " " + die2 + " " + die3 + " " + "points: " + total);
}
答案 1 :(得分:0)
在查看代码时,我感觉您可以通过创建一些简单的类来使自己更容易,准确地说是五或六。
首先,我会把一些部分分成几类。我想到的两个主要类是一个简单的Die
类,它只是一个不可变的Die
,在创建时将die值设置为1到6之间的随机数。一旦创建{{1对象无法更改。你的Die
类很窄,实际上是不必要的,因为三个骰子应该是ThreeDice
对象(下一个类)的一部分,作为一个简单的3 Player
个对象数组和一个数组Die
个对象我们可以将骰子从低到高排序。
“Die”类的样本如下:
Die
下一个帮助的课程是Player类。这个类的重要部分将是一个玩家名称,然后是一个Public final class Die implements Comparable<Die>
{
private int dieNumber;
// default constructor
public Die()
{
RollDie();
}
public int GetDieNumber()
{
return dieNumber;
}
public int compareTo(Die otherDie)
{
return this.dieNumber - otherDie.dieNumber;
}
private void RollDie()
{
dieNumber = 1 + (int)(6 * Math.random());
}
}
对象数组(在你的情况下大小为3),以保持玩家随机骰子。在这个类中,您还可以使用方法获取3个骰子的总值,以及方法/变量,以获得用户获得的额外点数,如果3个骰子是相同的数字,如果有一对,等等。当创建骰子数组时,我们可以利用骰子数组从低到高的排序。这样可以更轻松地检查直道。
下面是一个Player类示例。
Die
然后,由于你想要保留所有轮次的总数,我想你可能需要一个public class Player implements Comparable<Player>
{
private String playerName;
private Die[] diceArray;
private int diceTotal = 0;
private int extraPoints = 0;
private int overallTotal = 0;
private String extraPointsString = "";
public Player(String inName, Die[] inDiceArray)
{
playerName = inName;
diceArray = inDiceArray;
SetDiceTotals();
}
public String GetPlayerName()
{
return playerName;
}
public int GetExtraPoints()
{
return extraPoints;
}
public int GetDiceTotal()
{
return diceTotal;
}
public int GetOverallTotal()
{
return overallTotal;
}
public String GetExtraPointsString()
{
return extraPointsString;
}
public Die[] GetDiceArray()
{
return diceArray;
}
public String toString()
{
String playerString = playerName + " Dice values: ";
for (int i = 0; i < diceArray.length; i++)
{
if (i < (diceArray.length - 1))
playerString = playerString + diceArray[i].GetDieNumber() + ", ";
else
playerString = playerString + diceArray[i].GetDieNumber();
}
playerString = playerString + " Total: " + GetDiceTotal();
playerString = playerString + " - Special Points added: " + GetExtraPoints() + " for having " + GetExtraPointsString();
return playerString + " Total Points: " + GetOverallTotal();
}
public int compareTo(Player otherPlayer)
{
int thisTotal = this.GetDiceTotal() + this.GetExtraPoints();
int otherTotal = otherPlayer.GetDiceTotal() + otherPlayer.GetExtraPoints();
return otherTotal - thisTotal;
}
// private internal method to set dice totals, extra points and extra points string
private void SetDiceTotals()
{
int total = 0;
for (int i = 0; i < diceArray.length; i++)
{
total = total + diceArray[i].GetDieNumber();
}
diceTotal = total;
if (is3OfAKind())
{
extraPoints = 60;
extraPointsString = "Three of a Kind";
}
else
{
if (isPair())
{
extraPoints = 40;
extraPointsString = "Pair";
}
else
{
if (isStraight())
{
extraPoints = 20;
extraPointsString = "Straight";
}
else
{
extraPoints = 0;
extraPointsString = "All die are different";
}
}
}
overallTotal = extraPoints + diceTotal;
}
private boolean is3OfAKind()
{
if (diceArray[0].GetDieNumber() == diceArray[1].GetDieNumber() &&
diceArray[0].GetDieNumber() == diceArray[2].GetDieNumber())
return true;
return false;
}
private boolean isPair()
{
if (diceArray[0].GetDieNumber() == diceArray[1].GetDieNumber() ||
diceArray[0].GetDieNumber() == diceArray[2].GetDieNumber() ||
diceArray[1].GetDieNumber() == diceArray[2].GetDieNumber() )
return true;
return false;
}
// this method needs to have the diceArray sorted from low to high
private boolean isStraight()
{
if (diceArray[1].GetDieNumber() == (diceArray[0].GetDieNumber() + 1) &&
diceArray[2].GetDieNumber() == (diceArray[1].GetDieNumber() + 1) )
return true;
return false;
}
}
类。该类将包含一轮Round
个对象。还有一个圆形数字,所有玩家的回合总分数,该轮次的平均分数以及表示哪个玩家赢得该回合的字符串。
圆形课程示例如下。
Player
然后,由于你想保留所有玩家的总数,你可能想要制作一个小public class Round
{
private Player[] playerArray;
private int roundNumber = 0;
private int totalPointsForRound = 0;
private double roundAveragePoints = 0;
private String roundWinnerName = "";
public Round(int inRoundNumber, Player[] inPlayerArray)
{
playerArray = inPlayerArray;
roundNumber = inRoundNumber;
totalPointsForRound = SetAllPointsForRound();
roundAveragePoints = SetAveragePoints();
roundWinnerName = SetRoundWinnerName();
}
public int GetTotalPointsForRound()
{
return totalPointsForRound;
}
public double GetAveragePointsForRound()
{
return roundAveragePoints;
}
public String GetRoundWinnerName()
{
return roundWinnerName;
}
public Player[] GetPlayerArray()
{
return playerArray;
}
public int GetRoundNumber()
{
return roundNumber;
}
private String SetRoundWinnerName()
{
// sort the array from high to low - if the first two total are equal then its a tie
Player[] tempArray = playerArray;
Arrays.sort(tempArray);
if (tempArray[0].GetOverallTotal() == tempArray[1].GetOverallTotal())
return "Tie";
if (tempArray[0].GetOverallTotal() > tempArray[1].GetOverallTotal())
return tempArray[0].GetPlayerName();
return "Unknown Winner???";
}
private double SetAveragePoints()
{
double totalPoints = GetTotalPointsForRound();
double average = totalPoints/playerArray.length;
return Math.round(average*100.0)/100.0;
}
private int SetAllPointsForRound()
{
int allPoints = 0;
for (int i = 0; i < playerArray.length; i++)
{
allPoints = allPoints + playerArray[i].GetOverallTotal();
}
return allPoints;
}
}
课程。这个课程将简单地包括一个球员名称,所有轮次的总胜利和所有轮次的总分数。请注意,由于PlayerTotals
Player
中的每个Round's
对象都包含该特定回合的总计,因此这些轮次不是针对一轮的总数。
PlayerTotals类示例位于
之下playerArray
然后还有两个类,你可以实际组合成一个类。一个是静态public class PlayerTotals implements Comparable<PlayerTotals>
{
String playerName;
int totalWins = 0;
int totalPoints = 0;
public PlayerTotals(String inPlayerName)
{
playerName = inPlayerName;
}
public int GetTotalPoints()
{
return totalPoints;
}
public void SetTotalPoints(int inPoints)
{
totalPoints = inPoints;
}
public int GetTotalWins()
{
return totalWins;
}
public void SetTotalWins(int inWins)
{
totalWins = inWins;
}
public int compareTo(PlayerTotals otherPlayerTotals)
{
int thisTotalPoints = this.GetTotalPoints();
int otherTotalPoints = otherPlayerTotals.GetTotalPoints();
return otherTotalPoints - thisTotalPoints;
}
}
类,它有助于执行一些全局操作:GameUtils
,此方法获取GetPlayerArray
个对象的数组。每个Player
对象将包含每个玩家掷出的3个骰子的数组。这个骰子数组将从低到高排序。这是为每轮获得每个玩家的初始随机掷骰的方法。同样在这里我们可以Player
我们可以循环遍历所有回合并总计每个玩家获得多少胜利。一种名为GetPlayerOverallWins
的方法,用于获取所有回合的联系总数。并且方法GetTotalTies
可以获得所有玩家的所有玩家积分。在这里,我还提示用户输入玩家数量和回合数量,并检查以确保用户输入有效。
GameUtils示例如下:
GetPlayerOverallPoints
最后是一个public final class GameUtils
{
public static Player[] GetPlayerArray(int numOfPlayers, int numOfDice)
{
Player[] playerArray = new Player[numOfPlayers];
for (int i = 0; i < numOfPlayers; i++)
{
Die[] diceArray = new Die[numOfDice];
for (int j = 0; j < numOfDice; j++)
{
diceArray[j] = new Die();
}
Arrays.sort(diceArray);
playerArray[i] = new Player("Player " + (i + 1), diceArray);
}
return playerArray;
}
public static int GetNumberOfPlayers(Scanner input)
{
return GetValidInteger("Please input number of players (greater than 0) --> ", input);
}
public static int GetNumberOfRounds(Scanner input)
{
return GetValidInteger("Please input number of rounds (greater than 0) --> ", input);
}
private static int GetValidInteger(String prompt, Scanner input)
{
boolean done = false;
int validInt = -1;
String userInput = "";
while (!done)
{
System.out.print(prompt);
userInput = input.nextLine();
try
{
validInt = Integer.parseInt(userInput);
done = true;
}
catch (NumberFormatException e)
{
System.out.println("Invalid Input: " + userInput + " Try again!");
}
}
return validInt;
}
public static int GetPlayerOverallWins(String playerName, Round[] allRounds)
{
int totalWins = 0;
for (int i = 0; i < allRounds.length; i++)
{
Round curRound = allRounds[i];
String roundWinner = curRound.GetRoundWinnerName();
if (playerName.equals(roundWinner))
{
totalWins++;
}
}
return totalWins;
}
public static int GetTotalTies(Round[] allRounds)
{
int totalTies = 0;
for (int i = 0; i < allRounds.length; i++)
{
Round curRound = allRounds[i];
String roundWinner = curRound.GetRoundWinnerName();
if (roundWinner.equals("Tie"))
{
totalTies++;
}
}
return totalTies;
}
public static int GetPlayerOverallPoints(String player, Round[] allRounds)
{
int totalPoints = 0;
for (int i = 0; i < allRounds.length; i++)
{
Round curRound = allRounds[i];
for (int j = 0; j < curRound.GetPlayerArray().length; j++)
{
Player curPlayer = curRound.GetPlayerArray()[j];
if (player.equals(curPlayer.GetPlayerName()))
{
totalPoints = totalPoints + curPlayer.GetOverallTotal();
break;
}
}
}
return totalPoints;
}
}
类,主要条目将它们放在一起。骰子游戏类将由全局变量DiceGame
组成。每轮使用numberOfPlayers
,numberOfRounds
和numberOfDice
,然后使用playerArray
数组在所有轮次运行后保留所有轮次以进行总计。下面的示例首先设置循环次数的循环,在此循环中我们为它们创建所有玩家和骰子值,然后将轮次信息保存到新的Rounds
对象中,然后放置每个新的Round
将对象放入数组中。然后将当前轮次的结果输出给用户。一旦轮次数的循环结束,我们就应该有一个Round
个对象的数组。这是Round
类帮助的地方,因为我们可以为所有轮创建另一个PlayerTotals
对象数组。这使用了PlayerTotals
中的一些方法,这些方法可以很好地放入这个主类中。在所有轮次的所有玩家总数相加之后,结果将输出给用户。
主DiceGame类示例:
GameUtils
希望这些让事情变得更轻松。祝你好运!