Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help on Yahtzee

 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even without loops, the code could still benefit by breaking up that monster main method into a few smaller methods.
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so are u guys gonna help me, or just criticize my code?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I change it, sry about that
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey I'm sorry man, i forgot to create the dice on the loop random so I include this line

 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be easier to play with real dices :roll:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok guys this is what i have so far
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic