Creating a simple Craps game in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Creating a simple Craps game in Java

  1. #1
    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. #2
    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. #3
    Join Date
    Jan 2007
    Location
    India
    Posts
    12

    Post 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

  4. #4
    Join Date
    Feb 2011
    Posts
    1

    quesion

    Quote Originally Posted by ananthasivanvk View Post
    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

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  2. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  3. Simple tool to help you create Java class
    By zfqjava in forum java.announcements
    Replies: 2
    Last Post: 01-08-2002, 04:26 AM
  4. JAVADOTNET !!!
    By Dharmesh in forum .NET
    Replies: 4
    Last Post: 10-01-2001, 03:47 PM
  5. Simple Java DOM question
    By Lenin in forum XML
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center