• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner's Yahtzee program  RSS feed

 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in the process of teaching myself how to program in Java, and I was wondering if you guys could look over my code and give me some help with how I am doing? I am really struggling with the methods aspect of programming. If you could point out any recommendations to make my program better and easier to follow it would be appreciated. This is what I have so far, I haven't got very far but before I get too far along, I would like to know if there is something I should be doing differently.


Thank you
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code above is better to use switch case statement instead of a lot of if statements.
 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sveto Koychev wrote:In the code above is better to use switch case statement instead of a lot of if statements.

Can I use a switch statement for choosing which dice to re-roll. I replaced the menu portion of the code with a switch statement, but I am unsure how to replace the code in the re-roll method because it deals with multiple variables. I also made the menu a method because I am trying to get more practice using methods. I really appreciate your feedback.

Below is the part I am unsure about how to use a switch statement.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an array of 5 dice? In that case you should use the number to calculate the index in the array:- numbers[i] = j;
You would not want a switch‑case at all. In the instance you showed, you would probably need i - 1 rather than i

Why do you have so many things marked static? That usually denotes non‑object‑oriented design.

And welcome to the Ranch
 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your feedback. I do have an array for the 5 dice, however I am not using it yet. As far as having so many things marked as static its because I kept getting error messages saying you can't access a non-static field in static main, so I changed them to static. I didn't know any other way to fix it. I know I should be utilizing methods more.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a serious mistake to make things static in order for them to compile. The correct response is to move things out of the main method. Have a look at this FAQ. You should have a Yahtzee class, Die class, Player class etc. Tthe main method can be even shorter than the FAQ suggestsYou may pass information to the main method or the constructor or startGame method as described in the Java Tutorials.
 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your feedback, I will check them out.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I am understanding correctly it should look a little more like what's below, I am really sorry I am having a hard time, and I really appreciate you all being patient with me and helping me out. Here is the code I have so far, I haven't transferred everything yet, but I am wanting to make sure I am closer to being on the right track.

 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot better, but startGame should not be static. Not Yahtzee.start... but new Yahtzee().start... Then you can if you wish run several games simultaneously.

Does anybody know whether the Random instance should be static? They used to say only use one instance per application otherwise you get repetitions of the same sequence. Is that still the case? Last time I asked, I was told no.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a println instruction at the end of the method which displays the five dice.
 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have so far. I think my next move is going to be to make a method that handles the 3 rolls per turn, that way I don't have to write the same bit of code 13 times. I also think for now I am going to skip scoring and leave it as single player until I get the rest of the code right.
This is my Die class

Yahtzee Class

The Class to run everything
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Davis wrote:This is what I have so far. I think my next move is going to be to make a method that handles the 3 rolls per turn, that way I don't have to write the same bit of code 13 times.

Right. But make sure you keep clear (and separate) what the business of "being a die" and "being a Yahtzee game" are.

It might also be worth mentioning that your class is called Die, which is singular, but it contains several Dice, which is plural.

I also think for now I am going to skip scoring and leave it as single player until I get the rest of the code right.

NOW you're getting the idea: Get one thing working, test it, make adjustments if needed, and then (and ONLY then), move on.

Programming is an incremental process.

Winston
 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have it to where you get 3 rolls per turn, and 13 turns per game. I have this working the way I want it (for now any way), I think the next order of business will be the scoring portion, because I am tired of keeping score myself on a piece of paper. I think I am starting to get the hang of it, it is definitely way different than programming on a TI-83 (the only previous programming experience).

 
Jon Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is what I have to check for small and large straights, and I was wondering if there was a better way to do this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!