Please please help me, I am having alot of difficulty doing a program in Java, I'm new to this programming language and would much appreciate your help! This is the program, I need to make, could you please reply, with how I should do it?
Write a program that simulates a game of dice. In this game, players take alternate turns rolling two dice. On each turn, they record the sum of the two dice and add this to their total. If a player rolls a doublet (both dice have the same value), then the player gets to roll again. However, if the doublet is "snake eyes" (both dice have a value of 1) or "box cars" (both dice have a value of 6), then the player loses their next turn instead. The first player to reach a total of 75 will win.
For example, if games went to 20 (instead of 75), the output should be as follow (note: no user input is required):
I almost forgot to answer your question! oops:) yes the rolls do need to be random for every roll of the two dies, so, I think you need to use the Math.Random command or something for every roll:)
hi i wonder if some one can solve this program i wanna know how can you solve this program with java i am interested to learn java and i wonder of you can post the answer here so we can learn thanx
public int rollDice(){ dieOne = 1 + Math.abs(rand.nextInt() % 6); dieTwo = 1 + Math.abs(rand.nextInt() % 6);
dieSum = dieOne + dieTwo;
return dieSum;
}// End rollDice()
public boolean checkForSnakeEyes(){ boolean snakeEyes = false;
// Only one way to get get a 2 and that is if both dice values are one. if(dieSum == 2){ snakeEyes = true; rollMessage = "a " + dieOne + " and a " + dieTwo + " SNAKE EYES ! And loses a turn."; } return snakeEyes;
}// End checkForSnakeEyes()
public boolean checkForBoxCars(){ boolean boxCars = false;
// Only one way to get get a 12 and that is if both dice values are six. if(dieSum == 12){ boxCars = true; rollMessage = "a " + dieOne + " and a " + dieTwo + " BOX CARS ! And loses a turn."; } return boxCars;
}// End checkForBoxCars()
public boolean checkForValidDoublet(){ boolean doublet = false;
if(dieOne == dieTwo && (!checkForSnakeEyes() && !checkForBoxCars())){ doublet = true; rollMessage = "a " + dieOne + " and a " + dieTwo + " A VALID DOUBLET ! And gets to roll again."; } return doublet;
}// End checkForValidDoublet()
public boolean getGameWon(){ boolean gameWon = false;
if(playerOneSum > 75){ gameWon = true; System.out.println("Player One is the winner."); printFinalScore();
} else if(playerTwoSum > 75){ gameWon = true; System.out.println("Player Two is the winner."); printFinalScore();
} return gameWon;
}// End gameWon()
public void printFinalScore(){ System.out.println(""); System.out.println("Player One score = " + playerOneSum); System.out.println("Player Two score = " + playerTwoSum);
}// End printFinalScore()
public static void main(String[] args) { GodMain godMain = new GodMain(); godMain.playGame();
}// End main(String[])
}// End class GodMain()
Quick explanation. I'd have prefered to do this in a more object oriented way ie Player would be in its own class and would use setters and getters for the private variables. I'm not commenting these variables because their names are self explanatory. In this example .. God refers to Game Of Dice not GOD. If you have a question .. send me an email.
The constructor initializes a few variables for us. You could set them as you declare them, but I choose to do this in the constructor.
The main method makes a call to playGame(). This is our 'engine'. The (while) loop drives it.
rollDice() simulates rolling dice. We could also use the following to get a "random" number. dieOne = 1 + (int)(Math.random() * 6); This will get you a "random" number while the application is running, but the simulation will repeat itself everytime you run it. Try it and see. The Random class doesn't suffer this drawback. If you need more help understanding the % operator or the Math.abs() method .. email me.
Next, we determine whose turn it is to roll. You may have to modify this logic because I don't believe you're supposed to award points when the roll comes up snake eyes or box cars and I believe that lose 'next' turn means this turn ends and you forfiet your next turn unless the other player happens to lose their turn. Simply modify the logic tests to accomodate your need. Email me if you still have problems.
The only real tricky part is the rollAgainCount stuff. It's my solution and I'm sure there's a better way. Again, if we had a Player.class (Object) this would be easier and cleaner. Email your questions on this and I will be glad to help.