The Artima Developer Community
Sponsored Link

Java Answers Forum
Lonely Female, who needs Java program help!:)

7 replies on 1 page. Most recent reply: Oct 30, 2002 10:59 AM by natasha

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
natasha

Posts: 4
Nickname: flygirl
Registered: Oct, 2002

Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 8:54 AM
Reply to this message Reply
Advertisement
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):


Example 1:


Player 1 rolls a 3 and a 3

Player 1 now has 6

Player 1 gets to roll again

Player 1 rolls a 5 and a 1

Player 1 now has 12

Player 2 rolls a 5 and a 1

Player 2 now has 6

Player 1 rolls a 5 and a 6

Player 1 now has 23

Player 1 wins with a total of 23


Example 2:


Player 1 rolls a 4 and a 6

Player 1 now has 10

Player 2 rolls a 4 and a 1

Player 2 now has 5

Player 1 rolls a 2 and a 5

Player 1 now has 17

Player 2 rolls a 6 and a 3

Player 2 now has 14

Player 1 rolls a 1 and a 1

Player 1 now has 19

Player 1 loses a turn

Player 2 rolls a 1 and a 2

Player 2 now has 17

Player 2 rolls a 3 and a 5

Player 2 now has 25

Player 2 wins with a total of 25


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 11:59 AM
Reply to this message Reply
Do you still need help with this? This is a real simple task to do and uderstand once you've seen it done.

Send me an email dray@inphact.com and let me know if you need this to be separate classes and objects or if one class is okay.

No payment necessary. Also, do the die rolls need to be random with each start of the application?

natasha

Posts: 4
Nickname: flygirl
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 12:14 PM
Reply to this message Reply
Thank you Daniel, so much, I really appreciate, your help:) I will email you right now:) By the way, my email address is nastashak@2die4.com

bye:)

natasha

Posts: 4
Nickname: flygirl
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 12:16 PM
Reply to this message Reply
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:)

Thank you:) Bye:)

hyderman

Posts: 11
Nickname: salem99
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 5:18 PM
Reply to this message Reply
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

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 11:24 PM
Reply to this message Reply
import java.util.Random;

/**
* Title: Game Of Dice
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author Daniel Ray
* @version 1.0
*/

public class GodMain {
private String rollMessage;

private int dieOne;
private int dieTwo;
private int dieSum;
private int rollAgainCount;
private int playerOneSum;
private int playerTwoSum;

private boolean playerOnesTurn;
private boolean playerOneLostTurn;
private boolean playerTwoLostTurn;

private Random rand;

public GodMain() {
rand = new Random();

playerOnesTurn = true;
playerOneLostTurn = false;
playerTwoLostTurn = false;

}// End GodMain()

public void playGame(){
while(!getGameWon()){
int total = rollDice();

if(playerOnesTurn && !playerOneLostTurn){
if(checkForValidDoublet()){
playerOneSum += total;
playerOnesTurn = true;

} else if(checkForSnakeEyes() || checkForBoxCars()){
playerOnesTurn = false;
playerOneLostTurn = true;
playerTwoLostTurn = false;

} else {
rollMessage = "a " + dieOne + " and a " + dieTwo;
playerOneSum += total;

if(playerTwoLostTurn){
rollAgainCount++;

}

if(rollAgainCount > 0){
rollAgainCount = 0;
playerTwoLostTurn = false;

} else {
playerOnesTurn = false;

}

}
System.out.println("Player One rolled " + rollMessage + " Score = " + playerOneSum);

} else {
if(checkForValidDoublet()){
playerTwoSum += total;

} else if(checkForSnakeEyes() || checkForBoxCars()){
playerOnesTurn = true;
playerOneLostTurn = false;
playerTwoLostTurn = true;

} else {
rollMessage = "a " + dieOne + " and a " + dieTwo;
playerTwoSum += total;

if(playerOneLostTurn){
rollAgainCount++;

}

if(rollAgainCount > 0){
rollAgainCount = 0;
playerOneLostTurn = false;

} else {
playerOnesTurn = true;

}

}
System.out.println("Player Two rolled " + rollMessage + " Score = " + playerTwoSum);

}

}// End while(!getGameWon())

}// End playGame()

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.

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 29, 2002 11:27 PM
Reply to this message Reply
No, my code is not all left aligned ! <grin> That's why the methods end with // End someMethod() to help you when you copy the code into your IDE.

natasha

Posts: 4
Nickname: flygirl
Registered: Oct, 2002

Re: Lonely Female, who needs Java program help!:) Posted: Oct 30, 2002 10:59 AM
Reply to this message Reply
hey shawn you reject, dont copy the program!

Flat View: This topic has 7 replies on 1 page
Topic: RMI Callback Previous Topic   Next Topic Topic: Image as backgrouns in a FRAME (so NOT in an Applet or 'swing')

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use