-
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!
-
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.
-
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 !");
}
}
}
}
With Regards
Anand
-
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 !");
}
}
}
}
please in the code above,
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.
Similar Threads
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By zfqjava in forum java.announcements
Replies: 2
Last Post: 01-08-2002, 04:26 AM
-
By Dharmesh in forum .NET
Replies: 4
Last Post: 10-01-2001, 03:47 PM
-
Replies: 0
Last Post: 03-21-2001, 04:01 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks