Hey,
im currently working an Dicegame which consists of the classes Wuerfel.java(translates to Dice), Player.java and the executable class DiceGame.java.
In the main method 3 Player-objects are createt which themselves create a specific number of Dice-objects.
In each round all dice are rolled for each player.
Now I want to save the number of eyes showing for each die rolled.
Means:
Player.one rolled 3 times with 3 dice each -> 1 4 5 6 3 3 1 4 2
Now I want those values saved in an int array in the Dice class.
At the end of the game I want to print the history of rolled die of the player with the highest score.
I have tried for hours now, just cant get it to work...I hope you understand this nonsense
Here is my Code:
public class DiceGame { public static void main(String[] args) { int runden = In.readInt("Wieviel Runden sollen gespielt werden?"); int[] arrayHistorie = new int[9]; Player one = new Player(runden); Player two = new Player(runden); Player three = new Player(runden); for(int i = 0; i < runden; i++) { one.play(); two.play(); three.play(); } System.out.println("Punkte Spieler 1: " + one.getScore()); System.out.println("Punkte Spieler 2: " + two.getScore()); System.out.println("Punkte Spieler 3: " + three.getScore()); } }
public class Player { private int score, counter; private int[] eyeHist; private Wuerfel dice[]; public Player(int nWuerfel) { dice = new Wuerfel[nWuerfel]; for (int i = 0; i < dice.length; i++) { dice[i] = new Wuerfel(); } } public void play() { for (int i = 0; i < dice.length; i++) { dice[i].wuerfeln(); } score += calcScore(); counter++; } public int calcScore() { int zwischenStand = 0; for (int i = 0; i < dice.length; i++) { if (istPassch(dice) == false) { zwischenStand += dice[i].getAugen(); } else { zwischenStand += (dice[i].getAugen() * 2); } } return zwischenStand; } public boolean istPassch(Wuerfel[] dice) { for (int i = 0; i < dice.length; i++) { if (dice[0].getAugen() != dice[i].getAugen()) { return false; } } return true; } public int getScore() { return score; } public int[] getResults() { eyeHist = new int[counter]; return eyeHist; } public void reset() { for (int i = 0; i < dice.length; i++) { dice[i] = new Wuerfel(); } } }
public class Wuerfel { private int augen, min, max, counter = 0; private int[] dieHistorie = new int[100]; // Default constructor which sets the default values public Wuerfel() { this(1, 6); } // Constructor that sets default values using parameters public Wuerfel(int min, int max) { this.min = min; this.max = max; } // Method that returns the shown eyes of the die public int getAugen() { return augen; } public void setAugen(int augen) { this.augen = augen; } // Method that rolls the die and returns a random number public int wuerfeln() { int range = (max - min) + 1; augen = (int) (Math.random() * range) + min; return augen; } public void setHistorie() { clueless } public int getHistorie(int v) { return dieHistorie[v]; } }
I have tried everything..the best I could get was the first value of every rolled die for each player.