Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Craps game

$
0
0
I have to make craps program in which the program asks the user how many iterations/time he/she wants to play this game, and what is the maximum amount of much money he/she wants to bet each time.

For example in the sample run given below the user wants to play the game 1000 times and wants to bet a maximum of $50 in each iteration. The actual amount will be randomly generated for each iteration such that it will not exceed 50, so e.g in first iteration the amount maybe 22, and in the second it may be 48 etc. Based on this information 1000 rounds of the game are played and results are printed. The RESULTS section consist of 5 lines


Line #1 tell that out of total iteration how many were won by gambler and how many were won by the casino.

Line #2 tells how many times the gambler won on the first round

Line #3 tells how many times the casino won on the first round

Line#4 tells how much money gambler won at the end

Line#5 tells how much casino won at the end


Sample Run:

D:\Fal2012\OOP> java Craps

Enter the number of time you want to play this game: 1000
Enter maximum money that you want to bet in each iteration: 50

RESULTS
1)Gambler win 488, Casino wins 512
2)First roll wins for gambler 238
3)First roll wins for casino 105
4)Gambler has $-1,982
5)Casino has $1,982

----------------------------------------------------------------

My program so far, its pretty much basic Craps game, what i need help in is how to make logic of getting how many times each one wins, wins on first round, and how much the can win, etc. Thanks

import java.util.Random;
import java.util.Scanner;

public class Craps
{
	//creates a random number generator for use in method rollDice. you cant make another variable refer to random class object.
	private static final Random randomNumbers = new Random();
	
	//enumeration with constants that represent the game status
	private enum Status { CONTINUE, WON, LOST };
	
	//constants that will represnet the specific rolls of the dice
	private static final int SNAKE_EYES = 2;
	private static final int TREY = 3;
	private static final int SEVEN = 7;
	private static final int YO_LEVEN = 11;
	private static final int BOX_CARS = 12;
	
	//main method containing game of craps
	public static void main(String[] args)
	{ 
		int myPoint = 0;	// the initial points a players has at the start of the game
		Status gameStatus;	// gameStatus determines the status if it is "CONTINUE", "WON", "LOST"
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter the number of time you want to play this game:");
		int numberOfIterations = input.nextInt();

		System.out.print("Enter the maximum money that you want to bet in each iteration:");
		int maxMoney = input.nextInt();
	
		for( int roll = 1; roll <= numberOfIterations; roll++)
		{
			int sumOfDice = rollDice();	//declaring the sum of the roll and calling rollDice function
		
			//determine the game status and point base don first roll
			switch( sumOfDice )
			{
				case SEVEN:
				case YO_LEVEN:		//if the player roll 7 or 11, they win on the first roll.
					gameStatus = Status.WON;
					System.out.println("Player Wins");
					break;
				case SNAKE_EYES:
				case TREY:		//if the player rolls 2, 3, or 12 on the first roll; they lose.
				case BOX_CARS:
					gameStatus = Status.LOST;
					System.out.println("Casino Wins");
					break;
				default:	// sumOfDice equals to 4,5,6,8,9, or 10 then remember the point and Continue the game
					gameStatus = Status.CONTINUE;
					myPoint = sumOfDice;	//remember the point
					System.out.printf("My Point if %d\n", myPoint);
					break;
			}

			while( gameStatus == Status.CONTINUE )
			{
				sumOfDice = rollDice(); //roll dice again
			
				//determine the game status
				if( sumOfDice == myPoint )
				{
					gameStatus = Status.WON;  // sum of dice is same as your last point, so you win
				}
			
				else if( sumOfDice == SEVEN )
				{
					gameStatus = Status.LOST; // you rolled a seven, therefore you lose
				}
			
				//display Win of Lose message
				if( gameStatus == Status.WON )
				{
					System.out.println("Player Wins");
				}
			
				else if( gameStatus == Status.LOST)
				{
					System.out.println("Player Loses");
				}
					
			}
		}
	}// end of main

	public static int rollDice()
	{
		//picks random die values
		int die1 = 1 + randomNumbers.nextInt(6);	//first die roll scaling from 0-5 and initial value 1
		int die2 = 1 + randomNumbers.nextInt(6);	//second die roll scaling from 0-5 and initial value at 1
		
		int sum = die1 + die2; // sum of the random values generated by die 1 and 2

		//displays result of this roll
		System.out.printf("Player rolled %d + %d = %d\n", die1 , die2, sum);
		return sum;
	}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>