类中的Java-Player数组
我需要做的是能够调用视图类中的玩家数组,打印轮到谁和他们的分数。当轮到他们时,转到下一名球员并打印该球员的得分。当轮到第1名球员时,他们的总得分会恢复,并增加 最终目标: 玩家和死亡职业是一个数组。我的骰子和得分方法都很好。这是贪婪/法克尔的游戏。1名玩家掷6个骰子,并根据这些骰子得分。然后轧制未使用/未着色的模具。在球员回合结束时,我们必须打印出他们的回合分数和总分。转到下一个玩家,然后重新开始。当某个玩家达到某个分数时,游戏结束,该玩家就是赢家 现在我只有一个if语句,它循环4个玩家。这不是目标。我需要将玩家类放入一个数组中,并使用该类循环遍历玩家,并可能使用它报告他们的游戏分数类中的Java-Player数组,java,arrays,class,Java,Arrays,Class,我需要做的是能够调用视图类中的玩家数组,打印轮到谁和他们的分数。当轮到他们时,转到下一名球员并打印该球员的得分。当轮到第1名球员时,他们的总得分会恢复,并增加 最终目标: 玩家和死亡职业是一个数组。我的骰子和得分方法都很好。这是贪婪/法克尔的游戏。1名玩家掷6个骰子,并根据这些骰子得分。然后轧制未使用/未着色的模具。在球员回合结束时,我们必须打印出他们的回合分数和总分。转到下一个玩家,然后重新开始。当某个玩家达到某个分数时,游戏结束,该玩家就是赢家 现在我只有一个if语句,它循环4个玩家。这不是
public class Player {
/** player id */
private int id;
/** player name */
private String name;
/** player's score in the game */
private int gameScore;
/**
* Constructs a new Player with the specified id.
*
* @param id player's id
*/
public Player(int id) {
this.id = id;
}
/**
* Returns the id of player.
*
* @return player's id
*/
public int getId() {
return id;
}
/**
* Returns the name of player.
*
* @return player's name
*/
public String getName() {
return name;
}
/**
* Sets the name of player using the given parameter value.
*
* @param name value used to set the name of player
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the player's score in the game.
*
* @return player's score in the game
*/
public int getGameScore() {
return gameScore;
}
/**
* Sets the game score of a player.
*
* @param score value used to set the game score of player
*/
public void setGameScore(int score) {
this.gameScore = score;
}
/**
* Returns a String representing a player.
*
* @return string form of this player
*/
public String toString() {
return id + "";
}
}
公共类死亡{
/**最大面值*/
专用静态最终整数最大值=6;
/**显示在模具上的当前值*/
私人价值;
/**
*构造一个面值为1的模具实例。
*/
公众死亡(){
faceValue=1;
}
/**
*计算此模具的新面值并返回结果。
*
*@返回模具的面值
*/
公共int roll(){
faceValue=(int)(Math.random()*MAX)+1;
返回faceValue;
}
/**
*设置模具的面值。
*
*@param value表示模具表面值的整数
*/
公共void setFaceValue(int值){
如果(值>0&&value){
System.out.println(“错误-参数外的值”);
}否则{
打破
}
}捕获(输入不匹配异常){
scan.next();
System.out.println(“错误-输入必须是整数值”);
}
}
返回who;
}
私有整型getPlayerTotal(字符串输入){
int playerTotal;
while(true){
试一试{
系统输出打印(输入+“”);
playerTotal=Integer.parseInt(scan.nextLine());
如果(播放器总<最小值| |播放器总>最大值){
System.out.println(“错误-参数外的值”);
}否则{
打破
}
}捕获(输入不匹配异常){
scan.next();
System.out.println(“错误-输入必须是整数值”);
}
}
返回玩家总数;
}
private int getScoreTotal(字符串输入){
总积分;
while(true){
试一试{
系统输出打印(输入+“”);
scoreTotal=Integer.parseInt(scan.nextLine());
如果(scoreTotalmaxScore){
System.out.println(“错误-参数外的值”);
}否则{
打破
}
}捕获(输入不匹配异常){
scan.next();
System.out.println(“错误-输入必须是整数值”);
}
}
返回总分;
}
//公共整数总计(){
//int playerScore=playerClass.getGameScore()+farkleScore;
//返回playerScore;
// }
私有void gameOn(){
布尔覆盖=假;
布尔值endTurn=false;
字符串回答;
char answerChar='Y';
线卷;
System.out.println(“Player”+farkle.getPlayers();
当(!结束){
而(!endTurn){
roll=farkle.toString();
farkleScore=farkle.score();
System.out.println(“Player”+farkle.getPlayers()+“rolls”
+掷骰+价值+分数);
如果(farkleScore<1){
系统输出
.println(“对不起,你掷了一个0。转到下一个玩家!”);
endTurn=true;
}
if(farkle.availableDie()<1){
系统输出
.println(“对不起,您的骰子用完了。请转到下一个玩家!”);
endTurn=true;
}
如果(farkle.availableDie()>1&&farkleScore>1){
系统输出
.print(“你想继续吗?你有”
+farkle.availableDie()
+“剩余模具(Y或N):”;
答案=scan.nextLine().trim().toUpperCase();
answerChar=answer.charAt(0);
如果(answerChar=='N'){
endTurn=true;
}
}
}
while(endTurn){
System.out.println(“\n现在轮到下一个玩家了。”);
法克尔·帕斯迪();
farkle.nextPlayer();
endTurn=false;
}
}
}
公共静态void main(字符串[]args){
新GreedCLI();
}
}
包模型;
导入java.util.*;
公共类GreedGame{
/**总剩余模具的整数*/
私有整数剩余模=6;
/**计数数字出现的次数*/
私有int[]numFreq;
/**玩家阵法*/
私人整数玩家=1;
/**呼叫玩家类*/
私人游戏者自由职业;
/**模具阵列*/
私家车[死亡];
私人玩家[]谁;
私人int whoInt;
/**全部玩家*/
私人球员;
/**首发球员*/
私人球员;
/**所需总点数*/
私人积分赢取点;
/**调用玩家方法来获得回合*/
私人球员转身;
/**本回合得分*/
私人int得分=0;
/**本回合得分*/
个人积分总分;
/**toString的滚动结果的字符串*/
私有字符串滚动结果;
/**调用类来掷骰子*/
模具辊=新模具();
/*****************************************************************
*默认构造函数,设置实例变量的值
*
*@param玩家
*和胜利点
public class Die {
/** maximum face value */
private static final int MAX = 6;
/** current value showing on the die */
private int faceValue;
/**
* Constructs a Die instance with a face value of 1.
*/
public Die() {
faceValue = 1;
}
/**
* Computes a new face value for this die and returns the result.
*
* @return face value of die
*/
public int roll() {
faceValue = (int) (Math.random() * MAX) + 1;
return faceValue;
}
/**
* Sets the face value of the die.
*
* @param value an int indicating the face value of the die
*/
public void setFaceValue(int value) {
if (value > 0 && value <= MAX) {
faceValue = value;
}
}
/**
* Returns the face value of the die.
*
* @return the face value
*/
public int getFaceValue() {
return faceValue;
}
import java.util.*;
import model.Die;
import model.Player;
import model.GreedGame;
public class GreedCLI {
private int whoInt;
private int farkleScore;
private Player playerClass;
private GreedGame farkle;
private Scanner scan = new Scanner(System.in);
private final int max = 4;
private final int min = 2;
private final int minStarter = 1;
private final int minScore = 1000;
private final int maxScore = 10000;
int toWin, totalPlayers, player;
public GreedCLI() {
startUp();
gameOn();
}
public void startUp() {
System.out.println("Welcome to Farkle! \n \n");
totalPlayers = getPlayerTotal("Please enter total number of player (2-4): ");
toWin = getScoreTotal("Please enter total points needed to win: ");
player = getStartPlayer();
System.out.println("\nGood Luck!\n");
farkle = new GreedGame(totalPlayers, player, toWin);
}
private int getStartPlayer() {
int who;
while (true) {
try {
System.out.print("Which player will start the game(1-"
+ totalPlayers + ")?: ");
who = Integer.parseInt(scan.nextLine());
if (who < minStarter || who > totalPlayers) {
System.out.println("Error - values outside parameter.");
} else {
break;
}
} catch (InputMismatchException ex) {
scan.next();
System.out.println("Error - input must be an integer value.");
}
}
return who;
}
private int getPlayerTotal(String enter) {
int playerTotal;
while (true) {
try {
System.out.print(enter + "");
playerTotal = Integer.parseInt(scan.nextLine());
if (playerTotal < min || playerTotal > max) {
System.out.println("Error - values outside parameter.");
} else {
break;
}
} catch (InputMismatchException ex) {
scan.next();
System.out.println("Error - input must be an integer value.");
}
}
return playerTotal;
}
private int getScoreTotal(String enter) {
int scoreTotal;
while (true) {
try {
System.out.print(enter + "");
scoreTotal = Integer.parseInt(scan.nextLine());
if (scoreTotal < minScore || scoreTotal > maxScore) {
System.out.println("Error - values outside parameter.");
} else {
break;
}
} catch (InputMismatchException ex) {
scan.next();
System.out.println("Error - input must be an integer value.");
}
}
return scoreTotal;
}
// public int scoreTotal() {
// int playerScore = playerClass.getGameScore() + farkleScore;
// return playerScore;
// }
private void gameOn() {
boolean over = false;
boolean endTurn = false;
String answer;
char answerChar = 'Y';
String roll;
System.out.println("Player " + farkle.getPlayers() + "'s turn!");
while (!over) {
while (!endTurn) {
roll = farkle.toString();
farkleScore = farkle.score();
System.out.println("Player " + farkle.getPlayers() + " rolls "
+ roll + " worth " + farkleScore);
if (farkleScore < 1) {
System.out
.println("Sorry, you rolled a 0. Moving on to the next Player!");
endTurn = true;
}
if (farkle.availableDie() < 1) {
System.out
.println("Sorry, you are out of dice. Moving on to the next Player!");
endTurn = true;
}
if (farkle.availableDie() > 1 && farkleScore > 1) {
System.out
.print("Would you like to keep rolling? You have "
+ farkle.availableDie()
+ " die remaining (Y or N): ");
answer = scan.nextLine().trim().toUpperCase();
answerChar = answer.charAt(0);
if (answerChar == 'N') {
endTurn = true;
}
}
}
while (endTurn) {
System.out.println("\nNow it is the next player's turn.");
farkle.passDie();
farkle.nextPlayer();
endTurn = false;
}
}
}
public static void main(String[] args) {
new GreedCLI();
}
package model;
import java.util.*;
public class GreedGame {
/** int for total die remaining */
private int remainingDie = 6;
/** counts number of times number appears */
private int[] numFreq;
/** array for players */
private int players = 1;
/** call player class */
private Player playerFromClass;
/** array for die */
private int[] die;
private Player[] who;
private int whoInt;
/** total players */
private int totalPlayers;
/** starting player */
private int currentPlayer;
/** total number of points needed */
private int winningPoints;
/** calls player method to get turn */
private Player turn;
/** score for the turn */
private int turnScore = 0;
/** score for the turn */
private int totalScore;
/** string for the roll result for the toString */
private String rollResult;
/** calls class to roll the die */
Die dieRoll = new Die();
/*****************************************************************
* Default constructor, sets the values of the instance variables
*
* @param players
* and winning points pulled from CLI
*****************************************************************/
public GreedGame(int totalPlayers, int firstPlayer, int winningPoints) {
super();
this.totalPlayers = totalPlayers;
this.currentPlayer = firstPlayer;
this.winningPoints = winningPoints;
}
public Player playerClass() {
return playerFromClass;
}
public int getPlayers() {
return players;
}
/* private Player[] getStartPlayerClass() {
for(int i = 0; i < totalPlayers; i++){
i = this.currentPlayer++;
}
return who;
}*/
public void nextPlayer(){
if(players < 1){
players = 1;
}
else if(players < 4){
players++;
}
else{
players = 1;
}
}
/*private int getStartPlayerInt(){
whoInt = who.getId();
return whoInt;
}*/
public Player getTurn(){
return turn;
}
public void setPlayers(int players) {
this.players = players;
}
/*****************************************************************
* calculates remaining die
*****************************************************************/
public int availableDie() {
return this.remainingDie;
}
/*****************************************************************
* boolean to passDie
*****************************************************************/
public void passDie() {
this.remainingDie = 6;
}
/*****************************************************************
* array to roll the remaining dice
*****************************************************************/
public int[] rollDie() {
this.die = new int[this.remainingDie];
for (int i = 0; i < this.die.length; i++) {
this.die[i] = dieRoll.roll();
}
return this.die;
}
/*****************************************************************
* toString for the cli to call, puts roll in string.
*****************************************************************/
public String toString() {
rollResult = Arrays.toString(rollDie());
return rollResult;
}
/*****************************************************************
* score method to add up total points and can be called elsewhere
*****************************************************************/
public int score() {
rollCheck();
turnScore = 0;
return straight() + threePairs() + sixOfAKind() + fiveOfAKind()
+ fourOfAKind() + threeOfAKind() + eachFive() + eachOne();
}
/*****************************************************************
* array to roll the remaining dice
*****************************************************************/
public int[] rollCheck() {
availableDie();
this.numFreq = new int[6];
for (int i = 0; i < 6; i++) { // set to zero
this.numFreq[i] = 0;
}
for (int i = 0; i < this.remainingDie; i++) {
if (die[i] == 1) {
numFreq[0] += 1;
}
if (die[i] == 2) {
numFreq[1] += 1;
}
if (die[i] == 3) {
numFreq[2] += 1;
}
if (die[i] == 4) {
numFreq[3] += 1;
}
if (die[i] == 5) {
numFreq[4] += 1;
}
if (die[i] == 6) {
numFreq[5] += 1;
}
}
return this.numFreq;
}
/*****************************************************************
* scoring method for rolling a single or two 1's
*****************************************************************/
private int eachOne() {
if (straight() == 0 && sixOfAKind() == 0 && threePairs() == 0
&& this.numFreq[0] < 3) {
if (this.numFreq[0] == 1) {
turnScore = 100;
this.remainingDie--;
return turnScore;
}else if (this.numFreq[0] == 2) {
turnScore = 200;
this.remainingDie -= 2;
return turnScore;
} else {
return 0;
}
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling a single or two 5's
*****************************************************************/
private int eachFive() {
if (straight() == 0 && sixOfAKind() == 0 && threePairs() == 0
&& this.numFreq[4] < 3) {
if (this.numFreq[4] == 1) {
turnScore = 50;
this.remainingDie--;
return turnScore;
}else if (this.numFreq[4] == 2) {
turnScore = 100;
this.remainingDie -= 2;
return turnScore;
} else {
return 0;
}
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling 3 of a kind
*****************************************************************/
private int threeOfAKind() {
if (sixOfAKind() == 0 && fiveOfAKind() == 0 && fourOfAKind() == 0
&& straight() == 0) {
if (this.numFreq[0] == 3) {
turnScore += 1000;
this.remainingDie -= 3;
return turnScore;
}
if (this.numFreq[1] == 3) {
turnScore += 200;
this.remainingDie -= 3;
return turnScore;
}
if (this.numFreq[2] == 3) {
turnScore += 300;
this.remainingDie -= 3;
return turnScore;
}
if (this.numFreq[3] == 3) {
turnScore += 400;
this.remainingDie -= 3;
return turnScore;
}
if (this.numFreq[4] == 3) {
turnScore += 500;
this.remainingDie -= 3;
return turnScore;
}
if (this.numFreq[5] == 3) {
turnScore += 600;
this.remainingDie -= 3;
return turnScore;
} else {
return 0;
}
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling four of a kind
*****************************************************************/
private int fourOfAKind() {
if (sixOfAKind() == 0 && fiveOfAKind() == 0 && straight() == 0) {
if (this.numFreq[0] == 4) {
turnScore += 2000;
this.remainingDie -= 4;
return turnScore;
}
if (this.numFreq[1] == 4) {
turnScore += 400;
this.remainingDie -= 4;
return turnScore;
}
if (this.numFreq[2] == 4) {
turnScore += 600;
this.remainingDie -= 4;
return turnScore;
}
if (this.numFreq[3] == 4) {
turnScore += 800;
this.remainingDie -= 4;
return turnScore;
}
if (this.numFreq[4] == 4) {
turnScore += 1000;
this.remainingDie -= 4;
return turnScore;
}
if (this.numFreq[5] == 4) {
turnScore += 1200;
this.remainingDie -= 4;
return turnScore;
} else {
return 0;
}
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling 5 of a kind
*****************************************************************/
private int fiveOfAKind() {
if (sixOfAKind() == 0 && straight() == 0) {
if (this.numFreq[0] == 5) {
turnScore += 4000;
this.remainingDie -= 5;
return turnScore;
}
if (this.numFreq[1] == 5) {
turnScore += 800;
this.remainingDie -= 5;
return turnScore;
}
if (this.numFreq[2] == 5) {
turnScore += 1200;
this.remainingDie -= 5;
return turnScore;
}
if (this.numFreq[3] == 5) {
turnScore += 1600;
this.remainingDie -= 5;
return turnScore;
}
if (this.numFreq[4] == 5) {
turnScore += 2000;
this.remainingDie -= 5;
return turnScore;
}
if (this.numFreq[5] == 5) {
turnScore += 2400;
this.remainingDie -= 5;
return turnScore;
} else {
return 0;
}
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling 6 of a kind
*****************************************************************/
private int sixOfAKind() {
if (this.numFreq[0] == 6) {
turnScore += 8000;
this.remainingDie -= 6;
return turnScore;
}
if (this.numFreq[1] == 6) {
turnScore += 1600;
this.remainingDie -= 6;
return turnScore;
}
if (this.numFreq[2] == 6) {
turnScore += 2400;
this.remainingDie -= 6;
return turnScore;
}
if (this.numFreq[3] == 6) {
turnScore += 3200;
this.remainingDie -= 6;
return turnScore;
}
if (this.numFreq[4] == 6) {
turnScore += 4000;
this.remainingDie -= 6;
return turnScore;
}
if (this.numFreq[5] == 6) {
turnScore += 4800;
this.remainingDie -= 6;
return turnScore;
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling 3 pairs
*****************************************************************/
private int threePairs() {
int pairs = 0;
if (this.numFreq[0] == 2) {
pairs++;
}
if (this.numFreq[1] == 2) {
pairs++;
}
if (this.numFreq[2] == 2) {
pairs++;
}
if (this.numFreq[3] == 2) {
pairs++;
}
if (this.numFreq[4] == 2) {
pairs++;
}
if (this.numFreq[5] == 2) {
pairs++;
}
if (pairs == 3) {
turnScore += 800;
this.remainingDie -= 6;
return turnScore;
} else {
return 0;
}
}
/*****************************************************************
* scoring method for rolling a straight
*****************************************************************/
private int straight() {
if (this.numFreq[0] == 1 && this.numFreq[1] == 1
&& this.numFreq[2] == 1 && this.numFreq[3] == 1
&& this.numFreq[4] == 1 && this.numFreq[5] == 1) {
turnScore += 1200;
this.remainingDie -= 6;
return turnScore;
} else {
return 0;
}
}
Player[] playerArray = new Player[n];
for(int i=0; i<playerArray.length; i++)
{
// If you need your player id to be different than i just say so in the comments
playerArray[i] = new Player(i);
}