This is my first Java beginner course and I am having trouble with creating the code for my game.
The game is between two players goes as follows:
The goal is to be the first one to reach 100 points.... The players will alternate turns. However, during a turn, a player may roll as many times as she pleases, and each sum of the pair of dice will be added to her total.
However, if the player rolls doubles then the player loses all the points and begins all over and loses their turn and must pass the dice.
If the player chooses to pass the dice to her opponent before doubles are rolled, the points will be permanently added to her total.
So far i have done the following - any guidance would be appreciated... what i am really confused on is how do I alternative between the 2 players.... thanks
the biggest problem you have is that all your code is in the main method. That means none of it is re-usable.
You need to think about re-doing this code in a OO-way. You need to think up what classes you want to create, and what methods they should have.
At the VERY least, you want to break down your code into methods so you can call them as needed.
And really, before you do any of that, you should turn off your computer and use your brain. Coding is really 90% thinking, and 10% typing. You should really know (or at least have a good idea about) how to do it before you type a single line of java.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Please don't interpret Fred's advice as being nasty! He gave you valuable advice that applies to all programmers. He's not saying you're not using your brain, he's just highlighting the fact that programming starts by breaking up the problem in your mind.
From your story, it looks like you need a way to roll a pair of dice, and determine the sum, and whether doubles were rolled.
You also need a way to save the total of each player, and you need to be able to pass the turn.
All of this means you can probably have a DiceRoll class represents one roll of two dice, and you can have a Player class that makes DiceRolls until they roll doubles, or they win.
Finally a DiceGame class keeps track of which player's turn it is, and whether the game is finished.
You can print the output of these classes to the console. Don't even think of starting with a graphical user interface before you've implemented these classes.
It's a pleasure to see superheros taking such an interest in science. And this tiny ad: