• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with a dicegame

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic