DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

Thread: Creating a simple Craps game in Java

1. Registered User
Join Date
Jun 2007
Posts
1

Creating a simple Craps game in Java

Can you guys help me out? I'm trying to create a very simple game of craps but having some trouble. Below are the rules and requirements. Much appreciated!

Given the following rules for the game of Craps, simulate the play of a game, using the console to output the results of each roll and a WIN/LOSE message. Example output for a couple of runs is shown.

The player rolls two 6-sided dice (hint: use 1 + (int)(Math.random() * 6) ) to generate a random number between 1 and 6
A roll of 7 or 11 on the first try is a WIN
A roll of 2, 3 or 12 on the first try is a LOSE
Any other roll on the first try becomes the player's POINT
If a player rolled POINT, the player continues to roll until one of two things happens:
If a player in POINT rolls the POINT again, it is a WIN
If a player in POINT rolls 7, it is a LOSE
Example runs:
You rolled 7.
You win!

You rolled 12.
You lose!

You rolled 4. POINT is 4.
You rolled 3. POINT is 4.
You rolled 11. POINT is 4.
You rolled 4.
You win!

2. Registered User
Join Date
Dec 2005
Posts
97
well, what have you done so far? from what i can tell its going to be a while or do-while loop with some if statements inside.

3. Craps Game

Hi Mer

Here is the code that runs according to your expectations.
Have fun and visit the links given below for more.
Swing Basics
Java Small Projects
Code:
```/**
* @author Anand
* @version 1.0
*/
public class CrapsGame {

public static void main(String[] args) {
int dice1 = (int)(Math.random()* 6) + 1;
int dice2 = (int)(Math.random()* 6) + 1;
int roll = dice1 + dice2;
System.out.println();
System.out.print("You rolled "+roll+". ");
if(roll == 2 || roll == 3 || roll == 12){
System.out.println("You Lose !");
}else if(roll == 7 || roll == 11){
System.out.println("You Win !");
}else{
System.out.println("Point is "+roll+"\n");
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
int roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
while(roll2 != 7){
if(roll == roll2){
System.out.println("You Win !");
break;
}else{
System.out.println("Point is "+roll+"\n");
}
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
}
if(roll2 == 7){
System.out.println("You Lose !");
}
}
}
}```

4. Registered User
Join Date
Feb 2011
Posts
1

quesion

Originally Posted by ananthasivanvk
Hi Mer

Here is the code that runs according to your expectations.
Have fun and visit the links given below for more.
Swing Basics
Java Small Projects
Code:
```/**
* @author Anand
* @version 1.0
*/
public class CrapsGame {

public static void main(String[] args) {
int dice1 = (int)(Math.random()* 6) + 1;
int dice2 = (int)(Math.random()* 6) + 1;
int roll = dice1 + dice2;
System.out.println();
System.out.print("You rolled "+roll+". ");
if(roll == 2 || roll == 3 || roll == 12){
System.out.println("You Lose !");
}else if(roll == 7 || roll == 11){
System.out.println("You Win !");
}else{
System.out.println("Point is "+roll+"\n");
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
int roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
while(roll2 != 7){
if(roll == roll2){
System.out.println("You Win !");
break;
}else{
System.out.println("Point is "+roll+"\n");
}
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
}
if(roll2 == 7){
System.out.println("You Lose !");
}
}
}
}```
how do you maintain a record of the games won or lost,and display the number of wins or losses,and the individual game results on completion. that is store the results in an array,in order to display the summary after the last game has completed.
for example Summary of games completed
number of wins:5
number of losses:6
game 1: won
game 2:lost.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise