File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes help on Yahtzee Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help on Yahtzee " Watch "help on Yahtzee " New topic
Author

help on Yahtzee

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
I'm doing a game in Java called Yahtzee, i'm sure some of u may heard about it. Well if you haven't this program is a game that rolls 5 dice 3 times, random generated dice, and it ask the user which di(c)e to keep after every roll. You only have 3 chances to roll the dice and once you decide which dice keep, you can re-roll the others left. So where is the point in this game, well depending on the value of dice you come up with, will be the total score you will make. Here a brief explanation of the scoring:

three of a kind (3 dice of the same kind)score: sum of all the dice
four of a kind(4 dice of the same kind)score: sum of all the dice
full house(2 of one kind, 3 of another)score: 25 points
small straight(4 consecutive numbers) score: 30 points
straight(5 consecutive numbers) score: 40 points
Yahtzee (5 dice of the same kind)score: 50 points
Chance (any combination) score: sum of all the dice

This is what i have so far:




AS u may notice, I haven't being able to come up with the scoring method, and thats what i need the most, if you guys can give me a hand on this, that'll be very helpful.


Note: I love programming.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could implement methods that establish an Arbitrary amount of points bbased upon checking the final result using boolean methods. ie:


As a sidebar, your code would be a lot more readable and maitainable if you break up your main method into smaller methods describing one particular task i.e.

public static void promptForReroll()
{
//Add logic here
}
that way you could test each method individually to test the logic, making it much easier to debug, and it would reduce the amount of code you have to rewrite (in the current example you ask the player which dice to reroll three times, with a series of if-else statements to process each request).

Incidentally, why the the number of characters in choice ever be more than one?
[ January 24, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Before I help you get into scoring, I think you need to learn about arrays and for loops. If you understand how to use these correctly, you can write at most 10 lines of code to do everything that you have here. I think at this point a Yatzhee game might be a little too complex. Perhaps you can start with something simpler. How about a guessing game where the computer picks a random number and you ask the user to enter a number. Give the user 5 tries to guess the number. If you can write this program, you should be able to gain a basic understanding of loops.

I don't want to discourage you from this Yahtzee game. I just think that you need to learn some more tools that will make it easier for you to write it.

Layne


Java API Documentation
The Java Tutorial
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
this is my teacher's code, he gave it to me, becasue I was doing a gui, but he wanted me to stop doing it because of it's complicity, in that way he decided to give the whole class a non-gui code. he want us to generate the methods by using the "primitive" and long way. Without for statements, and while and so on.So the thing is it does have to be that long, cuz we are not allow to use the for loops i wanted to use.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Even without loops, the code could still benefit by breaking up that monster main method into a few smaller methods.
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Ok, so are u guys gonna help me, or just criticize my code?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

"MrManual"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by MrManuel:
Ok, so are u guys gonna help me, or just criticize my code?


Okay, so how do you score this game by hand? I (basically) know the rules myself, but it would be best if you think about it. (Besides, apparently the version I would come up with wouldn't be acceptable since I would almost certainly use loops of some sort.) So after the player rolls the dice and is ready to score that roll, what happens?

Layne
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
After each roll, The program asks the user for which di(c)e to keep, let's say, if there were 2 dice equals, the player should want to keep them, to get a better score once the total amount of rolls finished (3). So the program only re-roll the di(c)e left over. Once you decide which one to keep.

Hope this helps!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Right, but I think you have written the code for that. I was trying to get you to think about what happens once the player is done rolling. If I recall correctly, the player gets to roll up to 3 times. So after he/she rolls the third time, then what happens? How will you implement this in code?

Also, I'm looking ahead a little bit. Once the first person finishes his turn, you will need to go to the next person and then the next. After everyone is finished you will need to start over and let each person take another turn. It continues to repeat until the game is over. How the hell are you supposed to write all of this without using loops??? You would have to copy and paste thousands of code!!! This seems like a very silly requirement to me and a HUGE waste of time. I apologize that I can't let this go, but I think it isn't worth your time if you can't use for loops. If this is a college class, you might consider dropping this class since the professor doesn't seem to know what he's doing, in my opinion.

Layne
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
OK,lol, this is not a college class, it's an grade 12 class. the point is that if I use loops, i have to explain why did i chose loops, and for. Which will make my documentation longer anyways. but ok, I'm gonna do that, but let's start by replacing my code, and once again, I'm stuck in that, cuz if a change my code again, and start using loops, I'll still have the same problem doing the scoring method. plzz give me a hand on this, I don't know how to replace my rolling dice method with a loop, and none of my code.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First of all, you should probably create an array for your dice. This will help because you can have ONE variable that holds all six dice rather than SIX separate variables. I think you should start over. Can you write a small program that creates an array for the six dice and uses a loop to "roll" each dice and print out the values of each dice?

I think this might be good place to start.

Layne
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Ok, there you go Hope this helps

int dice [];
dice = new int [6];
for (int i = 0 ; i < 6 ; i++)
{
dice [i] = i + 1;
c.println(dice[i]);
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That's a good start. However...

"MrManuel", please read Mark Sprizler's earlier post and comply. We need a first and last name (with a space between). Mr is not a name. Thank you.
[ January 24, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Ok I change it, sry about that
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Thanks.

[Manuel]: the point is that if I use loops, i have to explain why did i chose loops, and for.

Well, you could say "because my scrolling finger was getting really tired as I tried to look at the file without any loops." Also, the more lines of code you have, the more chances you have of accidentally creating bugs. That's not an absolute rule: sometimes ten lines of code are clearer and easier to understand than three. But I think it's frequently true. I'm pretty sure that using loops to cut down the length of your code (currently 800 lines) will make it clearer and easier to find bugs in.
[ January 24, 2006: Message edited by: Jim Yingst ]
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Lol, yes man, But I'm having a hard time , don't know how to create a loop for replacing my bunch of if statements
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Understood. It's going to be hard if you try to replace the whole method at once, so I think you'll probably be better of sticking with the new short bit of code you just wrote, and "growing" it to gradually do all the things you wanted the original code to do.

So, what does this method (the short one you just wrote) print out? Does it print the same thing every time? Is there a way to can midify the code so that the results are random, like you're "rolling" the dice?
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
hey I'm sorry man, i forgot to create the dice on the loop random so I include this line

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by Jim Yingst:
Understood. It's going to be hard if you try to replace the whole method at once, so I think you'll probably be better of sticking with the new short bit of code you just wrote, and "growing" it to gradually do all the things you wanted the original code to do.

So, what does this method (the short one you just wrote) print out? Does it print the same thing every time? Is there a way to can midify the code so that the results are random, like you're "rolling" the dice?


here's the thing Now that i use an array to generate 5 random dice, in a short form. But It's gettin a little bit harder for me trying to ask the user which dice to keep and which ones to re roll. And once the user says ok, I'm going to re roll 1 2 3, how can i re-roll, I'm stuck on that thing, this is what i have so far:


that's it, if you look to my first code, you'll se that the longest part of my code relays on the if statements used during re roll depending on user answer on which dice to re roll. Can you please tell me a shorter form to replace all those if statements
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
That's a great start. One comment: you can combine the first two lines into a single one. Also, you can put these few lines of code into a single method, say rollDice(). What will its return value and parameters be? Next, write a method that asks the user to enter which dice to keep. You can write yet another method to actually reroll the other dice, but worry about that later. When you get that far, post the whole class and we can keep helping you from there.

Layne
[ January 24, 2006: Message edited by: Layne Lund ]
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by Layne Lund:
That's a great start. One comment: you can combine the first two lines into a single one. Also, you can put these few lines of code into a single method, say rollDice(). What will its return value and parameters be? Next, write a method that asks the user to enter which dice to keep. You can write yet another method to actually reroll the other dice, but worry about that later. When you get that far, post the whole class and we can keep helping you from there.

Layne

[ January 24, 2006: Message edited by: Layne Lund ]


OK so i added a reRoll method


How can I get the values of the dice random generated, cuz I need to ask the user if he want me to, which die to re-roll , plzz, can someone make that method for me, or explain me a bit more. I also need u guys to help me with how divide the random numbers into separate dies, for example, first number generated == die1, and so on, so in that way when the user asks me to re-roll let's say die number 4 and 5,how do only change those dice, plzz give me hand
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
We are getting there, so be a little patient. I first have a few comments on your new method. Here's some more code that uses your method:

What will the output be? You might be surprised that it prints -1 both times! This is because when you pass a variable to a method, a COPY of the value is passed in. Assigning a value to a parameter inside a method does NOT change the value of the variable in the caller. To fix this we should return the random value instead. This means we don't even need a parameter. With this in mind, we should write the reRoll() method as

Now, I think you should write a method that asks the user which dice to keep. We will worry about how to re-roll the other dice after you get that method finished.

Layne
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79

Now, I think you should write a method that asks the user which dice to keep. We will worry about how to re-roll the other dice after you get that method finished.

Layne


Yes man, but thats the point, I can't get it done
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Manuel Diaz:


Yes man, but thats the point, I can't get it done


Sure you can. It just takes time and effort. In fact, you have already written the code in your original program that asks the user for the dice they want to keep. I'm just asking you to place this code in its own method in the new version we are working on here.

Layne
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

It would be easier to play with real dices :roll:


[My Blog]
All roads lead to JavaRanch
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Layne Lund:


Sure you can. It just takes time and effort. In fact, you have already written the code in your original program that asks the user for the dice they want to keep. I'm just asking you to place this code in its own method in the new version we are working on here.

Layne


I'm emphasizing this method to get input because it will determine how we do the next step. What I mean is that the format of the input from the user will determine how we go about rerolling the dice that the player doesn't want to keep. Hopefully this will be more clear when we go on to the next part.

Layne
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
ok guys this is what i have so far
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Oh sry I forgot to mention, that when I input the die to re roll, my program re rolls everysingle dice, Why this is happening??
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Manuel Diaz:
Oh sry I forgot to mention, that when I input the die to re roll, my program re rolls everysingle dice, Why this is happening??


This is because reRoll() calls rollDice() which then rolls every die. So first of all, you need to remove the call to rollDice() that is inside the reRoll() method. Then you need to figure out how to make reRoll() roll only the dice specified. So how do you know which die to reroll? Is this stored in a variable? If so, which one? How can you use that variable to reroll ONLY the specified die?

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help on Yahtzee