Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: DiceGame - Saving Values Of Rolled Die In Int Array

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy DiceGame - Saving Values Of Rolled Die In Int Array

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: DiceGame - Saving Values Of Rolled Die In Int Array

    The set method should have an argument that is the value to be used in the set.
    To save a value in an array, use an assignment statement:
      theArray[theIndex] = theValue;
    Change the value of theIndex after each save to move to the next slot in the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: DiceGame - Saving Values Of Rolled Die In Int Array

    First, is it necessary to use an int array? I could imagine a throwDice method in the Player class which basically does what your Wuerfel class does. Each throw would return an instance of a Dice class and could be stored in either Dice array or an ArrayList<Dice> in each player class. So each Player would record their own throw history. The show history method would simply iterate over the Dice array or List.

    Regards,
    Jim

  4. #4
    Junior Member
    Join Date
    Nov 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DiceGame - Saving Values Of Rolled Die In Int Array

    Unfortunately I have to use an int array.

    I have changed my get method, it gets called with a counter which is incrementet everytime a die is thrown.

    The dieHistorie array now gets filled as its supposed and when I try to print it out by itself it works.

    But to print it out I have to call the method int[] getResults(); in the Player class.

    I struggle to get all the values from the dieHistorie arrays in the different Dice objects into one array in the Player object.

    This sounds like so much nonsene when trying to type it out.

    When I call following method from the Player object it prints out all the correct values:

    Player.java
     
    public void printhistorie() {
    		for (int i = 0; i < dice.length; i++) {
    			for(int j = 0; j < dice.length; j++) {
    			System.out.println("Throw: " + dice[i].getHistorie(j));
    		}
    		}
    	}

    Now instead of printing it out directly I have to store the values in an array.

    I have tried every variety of for loops that exists, but holy moly, feels impossible

    Thank you guys for your answers!

    /edit

    I might have expressed myself wrong in the inital post, I dont want to store all values in the array in the Dice(Wuerfel) class, in this array are just the values for this.dice object.

    Where I need to get all values in a single array is in getResults();, like explained above.
    Last edited by breezy; November 27th, 2018 at 06:54 AM. Reason: Clearify inital post

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: DiceGame - Saving Values Of Rolled Die In Int Array

    Does the history record the sum of the rolls or the individual dice values? I assumed the later.

    Regards,
    Jim

  6. #6
    Junior Member
    Join Date
    Nov 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DiceGame - Saving Values Of Rolled Die In Int Array

    Your assumption is right.

    Right now I am trying my luck with an ArrayList since I dont need to worry about the index so much.

    I'll keep you updated!

  7. #7
    Junior Member
    Join Date
    Nov 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DiceGame - Saving Values Of Rolled Die In Int Array

    Alright, I got it working! I put the elements into an array list first and then i was able to get all values into an array.

    Thanks for the help guys!

Similar Threads

  1. Saving from csv file and copying contents to a 2D array problems
    By vysero in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 4th, 2018, 12:36 PM
  2. How to use an array that has int values "keyed" to a String name.
    By craigjlner in forum Collections and Generics
    Replies: 8
    Last Post: December 27th, 2013, 11:02 PM
  3. Saving an array to a buffer.
    By Tyluur in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 12th, 2012, 11:00 PM
  4. Getting pixels from an image, and saving them in an array?
    By jtvd78 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 14th, 2011, 08:48 PM

Tags for this Thread