• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a dicegame  RSS feed

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
Trying to make a "simpel" little dicegame. First choose number of players, number of dice, number of sides on the dice(maxValue) and lastly give name to the players.
The game is about to guess the value of the total dices before throwing them. If guess is right add 1 point to he player. Five rounds then finito and the score for every player prints.

My problem is that when I try to initialize the Player class. Give every player there names and dices. It only returns the adress and not the names I put in like this: [Player@29edc073, Player@37f5d386, Player@6c121f1d, Player@19b8e059, Player@38910040]. And the for-lop to give every player dices just crashes(indexoutofbounds). So how do I have to approach this to get it right?

the code:



 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see the word static before your List. Why?

If you get output like that, look at the documentation for the Object class. Read what it says about the toString method.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote: So how do I have to approach this to get it right?

You have written ENTIRELY too much code before finding out it doesn't work. I never write more than 2-3 lines before I compile and test the heck out of it.

So the answer to the above question is "write less code before you test".
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another aspect of what Fred said is starting smaller and simpler. Rome wasn't built in a day. Likewise, you can start with a small, working program then slowly build it up by incrementally adding new functionality. Start with one pair of dice. Simulate a bunch of rolls and add up the results of all the rolls as you go. That's simpler to code and easier to test. When you get that working, add another player to the mix. When you get that working, add prompts and user input. And so on.

The advantage to this approach is that with each increment of functionality, you will likely find places in your code that make it difficult to work in the new functionality. These are signs of either wanting to make too big of a change at one go and/or poor design choices that need to be corrected. Software should be soft and easy to change. When it's hard to change, then something is not quite right.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Die class has a bug in it. You are using Random.nextInt(n), so the values produced will range from 0 inclusive to maxValue exclusive rather than 1 to maxValue inclusive.

You can see this for yourself by running this code:

This code will always print "true" with the Die code that you have now.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
It only returns the adress and not the names I put in like this: [Player@29edc073, Player@37f5d386, Player@6c121f1d, Player@19b8e059, Player@38910040].


I don't see any code that prints out the array list -- but based on the output, you probably just did a print() of the list object, which of course, will traverse the elements and use the toString() method. Did you correctly override the toString() method?

Daniel Ungerfält wrote:
And the for-lop to give every player dices just crashes(indexoutofbounds). So how do I have to approach this to get it right?


You didn't actually create any dice. You created an array list to hold the dice objects, and then you traversed the empty list trying to configure the non-existent dice.

Henry
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also wasn't mentioned yet, that you have public fields, which suggests that your class is not well encapsulated.
Check class "Player" and "Die" and make fields "private". That is why you have "setter" and "getter" methods.
In case you wouldn't have - these fields could be accessed and changed without help of above mentioned methods.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!