You are browsing Java Forum.
SEARCH YOUR SOLUTION HERE  

Dice Game

esoone. I am a grad student in an accelerated Java class and so it is understandably a little bit hard for me. I've never used Java in my life. I am using a custom textbook from my school that has all of the examples in the book as source code snippet on a CD. Unfortunately I purchased a used book with out a CD so I am stuck and I can't complete my Program. The hardys are outlined given below:
Part 1:
* Write a Java class called Die (as in the singular of Dice) that represents one die with faces displaying values between 1 and 6. It should contain :
--one integer instance variable faceValue, that represents the current face value of the die, an integer constant (MAX) that represents the maximum face value of the die.
--a constructor
--and five regular methods: roll, setFaceValue, getFaceValue, toString and equals.

* The roll method should compute a random integer between 1 and 6 and set the current face value with that as a way of simulating the rolling of the die. (What Java class and method would you use to get a random integer find out..)
* Now, write a driver (i.e., program used for testing) called RollingDice, in order to test your Die class.
Part 2:
* Using the Die class, design and implement a class called PairOfDice, composed of two Die Objects. Include methods to set and get the individual die values, toString and equals methods, a method to roll the dice (call it roll), and a method that returns the current sum of the two die values (call it getDiceSum).
Part: 3
Properly using your PairOfDice class, write a Java program (a driver with a main function), called DiceGame.java, that allows the user to play the a game with the computer. Here are the rules of the game: A player rolls the dice repeatedly until she rolls at least one 1 or voluntarily gives up the dice. Each time she rolls the dice the total on the faces of the dice is added to her score, except when she rolls a 1. If she rolls one 1, she loses all the points she's collectd so far in her turn. If she rolls two 1's, she loses all of her points so far in the game. (This means you've to keep track of points in each turn and points since the beginning of the game). The first player to get a total of 100 wins. The computer follows the same rules, except that it turns over the dice as soon as its score for the current turn is at least 20.
Here's an excerpt from a working Program.
Welcome to the DiceGame. It's you against the computer.
You play by rolling the dice. The first player
to get 100 points wins. However, if you roll one 1
you lose all the points you've collectd in your
turn. If you roll two 1's, you lose all your
points. You can turn the dice over at any time.
However, if you roll one or two 1's, you lose your
turn. I (the computer) play by the same rules,
except I will always turn over the dice when I've
rolled 20 or more points in one turn.
Ready to begin (Type 'y' when you're ready)
y

You're rolling the dice . . .
You rolled 2 4
This gives you a turn total of
6
and a grand total of
6
The computer has a total of
0
Do you wana to continue rolling (Type 'y' or 'n')
y

You're rolling the dice . . .
You rolled 3 3
This gives you a turn total of
12
and a grand total of
12
The computer has a total of
0
Do you wana to continue rolling (Type 'y' or 'n')
y

You're rolling the dice . . .
You rolled 1 3
You got a 1
Continue (Type 'y' when you're ready to
turn the dice over to me)
y
The score is
You: 0
Computer: 0

I am rolling the dice . . .
I rolled 1 4
I got a 1
Continue (Type 'y' when you're ready)
y
The score is
You: 0
Computer: 0

You're rolling the dice . . .
You rolled 5 5
This gives you a turn total of
10
and a grand total of
10
The computer has a total of
0
Do you wana to continue rolling (Type 'y' or 'n')
n

I am rolling the dice . . .
I rolled 1 1
I got two 1's
Continue (Type 'y' when you're ready)
y
The score is
You: 10
Computer: 0

[Many turns deleted . . .]

You're rolling the dice . . .
You rolled 1 6
You got a 1
Continue (Type 'y' when you're ready to
turn the dice over to me)
y
The score is
You: 0
Computer: 93

I am rolling the dice . . .
I rolled 5 4
This gives me a turn total of
9
and a grand total of
102
The score is
You: 0
Computer: 102

Better luck next time
I've intended to do the part 3 of the program but I am still have hardys. For some reason the output always gives the user a 1 and ends the turn and the same goes for the computer. So applicationare notly I am not calling a method properly or some thing. Below is code snippet for all of the classes and the last one is the game itself. Be forewarned that it is so lengthy (in my opinion) and apologize in advance but I think I am so close and just wana finish this thing.import java.util.Random;
public class Die
{
private int faceValue;
private final int MAX=6;

public Die()
{
faceValue = 1;
}

Math.random Creates random integers from 1 - 6
public void roll()
{
faceValue = (int)(Math.random() * MAX) +1;
}

Sets the face value
public void setFaceValue(int value)
{
faceValue=value;
}

Gets the face value using the setFaceValue method
public int getFaceValue()
{
return faceValue;
}

Converts values into a String
public String toString()
{
String result = "You rolled a " + faceValue;
return result;
}

} end class Diepublic class RollingDice
{
------------------------------------------------------
Creates two Die Objects and rolls them several times
------------------------------------------------------
public static void main (String[] args )
{
Die d1, d2 ;
int sum ;
Creates two Die Objects
d1 = new Die() ;
d2 = new Die() ;
d1.roll() ;
d2.roll() ;
System.out.println ( " Die One: " + d1 + " , Die Two: " + d2 ) ;
sum = d1.getFaceValue() + d2.getFaceValue();
System.out.println ( " Sum: " + sum ) ;
} end main
} end class RollingDicepublic class PairOfDice
{
private Die d1, d2;
private int value1, value2, total;
-----------------------------------------------------------------
Creates two six-sided Die Objects, both with an initial
face value of one.
-----------------------------------------------------------------
public PairOfDice ()
{
d1 = new Die();
d2 = new Die();
value1 = 1;
value2 = 1;
total = value1 + value2;
}
-----------------------------------------------------------------
Rolls both dice and returns the integrated result.
-----------------------------------------------------------------
public void roll ()
{
d1.roll();
d2.roll();
}
-----------------------------------------------------------------
Returns the current integrated dice total.
-----------------------------------------------------------------
public int getDiceSum ()
{
total = getDie1() + getDie2();
return total;
}
-----------------------------------------------------------------
Returns the current value of the first die.
-----------------------------------------------------------------
public int getDie1 ()
{
return value1;
}
-----------------------------------------------------------------
Returns the current value of the second die.
-----------------------------------------------------------------
public int getDie2 ()
{
return value2;
}
-----------------------------------------------------------------
Sets the FaceValue of the first die.
-----------------------------------------------------------------
public void setDie1 (int value)
{
d1.setFaceValue(value);
}
-----------------------------------------------------------------
Sets the FaceValue of the second die.
-----------------------------------------------------------------
public void setDie2(int value)
{
d2.setFaceValue(value);
}
-----------------------------------------------------------------
Sets the FaceValue of the second die.
-----------------------------------------------------------------
public String toString()
{
String result = "You rolled a " + total;
return result;
}
}import java.util.Scanner;
public class DiceGame
{
public static void main( String [] args )
{
Rules of the Game
System.out.println("______________________________________");
System.out.println(" Rules of the Game ");
System.out.println(" ----------------- ");
System.out.println(" 1)It's you vs computer. ");
System.out.println(" 2)You play by rolling the dice. ");
System.out.println(" 3)The first player to reach 100 ");
System.out.println(" points wins. ");
System.out.println(" 4)When a player rolls a 1 ");
System.out.println(" the turn is over. ");
System.out.println(" 5)The computer's turn is over ");
System.out.println(" when turn total reach 20 points ");
System.out.println(" in one turn. ");
System.out.println("______________________________________");
PairOfDice d1 = new PairOfDice(); Creating PairOfDice Object
int turnTotal = 0;
int computerTotal = 0; your total
int playerTotal = 0; computer's total
int turnOver = 1; when to give up die
int winner = 100; amount to be reached before winning
Scanner in = new Scanner( System.in );
String answer; named of what will take answer from user
first do-while loop is for repeating the alter between user and computer
do{
if (playerTotal = winner)
System.out.println("You win ");
else
System.out.println("You lose ): ");
}
}Also, it is important to note that I am not getting any compiler error messages from any of the softwares just the hiccup from the game itself. If there are any cosmetic hardys pls let me know so that I can do improve next time.
Posted On: Thursday, November 29, 2012 Total Views: 27
View Complete with Replies


Hi,

This webpage contains content related to Java . If question related to "Dice Game" is not helpful or you didn't find the question or answer you were looking for then you can ask a new question through contact form. Our development Team will be happy to help you. This article was posted on Thursday, November 29, 2012 and having 27 views till Wednesday, September 17, 2014.

Thank You


Related Messages:

Some questions about developing a multiplayer game  (26 Views)
Guys
This is my first post on the forum. I am not sure if it belongs under "Game Development" or in "Java". Since, it is a game that's being written in Java.
I am not looking for anybody to write any code snippet for me, I just want some body to point me in the right direction.
I've been
View Replies !
TicTacToe game in Java  (19 Views)

I've tried set of things, nothing is working feels like world is falling apart.
I just want a basic tic tac toe game working in java.
I've tried this http: pctechtips.org tictactoe-game-java-code snippet
import java.util.*;
public class Tictactoe {
private static final int
View Replies !
want assist with class diagram for game  (27 Views)
so basically I've to make up a class program diagram for the game Cranium. It includes various various elements, and so far I've come up with 7 classes in relation to it, but I am not sure what data types are applicationlicable to them or what methods to applicationly to them. A basic understanding
View Replies !
Java Pong game - hardy with ball image  (27 Views)
esoone
I've a hardy inserting an image in the spot where my ball is suppose to be in my pong game. I when I load up the image and put it in it is spot the ball dissaperas. I will post 2 code snippets bellow, first 1 will be the model that works (Created using some built in mechanisms) and the oth
View Replies !
poker game in java  (17 Views)
i've copy pasted the code snippet from this tutorial here and i'm trying to run it but it shows me that error message.. i'm pretty new in java eclipse and i started to discovering before 1 week because of my college
if anybody can guide me what is neccessery to make this poker game to work or t
View Replies !
dice collection array that holds Objects  (37 Views)
I am some having hardys but firstly I explain what program does: the class explain collection of single die Objects.
Attributes= field with # of dice in collection, array holding the single die Objects.
Methods = alter # sides on die, display # sides on any single die, roll all dice and add
View Replies !
I want your assist guys on My game.  (18 Views)
Guys i have started developing my game and i want some few assists from you.
The game is some kind of scattered English Language vocabulary that wana to be rearrange. i wanaed to make it a Multi-player so i Created a server to be checking the players.
My request for assist are these.
1. I w
View Replies !
Bunco game  (15 Views)
Ok I am writing the GUI for this bunco game I already spell and I am executing into some really fusterating hardys. The game goes like this: the rounds are random and you should be able to select which player goes first. Three die are roled and if all three match the round its a bunco and you get 21
View Replies !
generating an online multiplayer board game  (17 Views)
i just wana to have an solution of what language s will i use if i wana to Create an online multiplayer board game..
give me some solutions..
View Replies !
Need assist with Hangman game  (2 Views)
I wana to make a hangman game some thing like this: http: www.geo.nl ki...geo-spel03.html
I am doing this with the components pallete from Netbeans we've not use custom creation code snippet to Create own panels frames, and more.
I wana to have 3 classes atleast (I am using 4)
1)Data cla
View Replies !
Unable to "smooth out" grid movement in game  (11 Views)
So in short, me and a partner are working on a game for our Computer Science final project, and we can't get the basic overworld engine to work like we want it to. The game we're making is a Pokemon clone, so the overworld screen is supposed to be a grid... plus it is a requirement to use a grid in
View Replies !
I've a java ssets game but want it to start with users points  (19 Views)
I think the answer to this is easy but I've no solution how can i do it.
The page has the application on it and theres a .settings file that I can alter the odds and the "starting points"


Now is there a way I can set this up so it searches the users credits and sets it in the game by
View Replies !
Creating a dice Object job.  (18 Views)
The question is to Write a program that Creates a Dice Object.
Your program will involve two various classes in various files:
One class for the main method
Another class describing the Dice Object
The Dice class should have fived methods (and one class variable for storing the
View Replies !
dice rolling class hardy  (23 Views)
Okay im confused on how can i Create a class and use it else where... here is my code snippet: the directions to my assignment are listed:Design and implement a class called Die (for a die in a pair of dice). Include a constructor for a die (which will set its value as null or -1) methods to set an
View Replies !
comparing indices of Int array and String array  (21 Views)
guys so I've a program that gives you options to add, save, delete, see, and fetch persons to and from a binary file.
I've gotten all the methods correctly (knock on wood) but this method is killing me: array where all Person Objects are added
private Person[] data;
** Method to print a
View Replies !
Getting some error messages in my completed dice program  (30 Views)
This is my so first Java program (excluding "World ").
I am almost done with the program but I am getting some error messages that I do not know how can i deerror. For reference I am using the Eclipse IDE.
What this program is supposed to do is Create a PairOfDice instance in main.
Then We'
View Replies !
i wana to inter student data according to their spadices major  (15 Views)

import javax.swing.JOptionPane;
public class assignment22{
public static double major( Students[] a1int n){

for(int i=0;i;


}

System.out.println("Student ID t t"+"Student Name t t"+"course1 t t"+"mark1 t t"+"course2 t t"+"mark2 t t"+"course3 t
View Replies !
Random numbers rolling dices, strange results  (14 Views)

I've got some strange results which i can't explain.
I roll a die for say 100.000.000 times, add all results and divide by the number of the rolls.
This is how i roll a die:
public static int rollDice(){
int result;
Random r = new Random();
int randint = r.nextInt(6);
res
View Replies !
Need dice to display  (14 Views)
I am trying to make a Yahtzee type game for my Java class. Esothing seems to be working fine except when I press the roll button I do not get the images to display. I did set the JLabels to images it just seems to be with the roll method or the actionPerformed method and I can't figure it out. I
View Replies !
javan easy dice game problem  (27 Views)
Hello
Thank you in advance for taking the time to assist me
My assignment is to write a dice game in java using the Die.java class.The Object of the game is to reach a total of 41 or more by rolling a pair of dice no more than 6 times. If you get 10, 20,30, or 40 as your total you automatica
View Replies !