If you don't know the rules of craps, I'll explain it here.
1.First you need to set a bankroll.
2.From the bankroll, you bet a certain amount of money. If you win, that bet is doubled and added to your bankroll, if you loose, then that bet is subtracted from your bank roll.
3. You play the game by rolling two dice.
4. The total of a dice must be 7 or 11 to win.
5. If the total of the dice is 2, 3, or 12, then you loose.
6. There is another way of winning or loosing.
7.If you roll any other number other than those numbers above
8.For example, you roll a 5, you MUST roll a 5 again in order to win or you loose if you roll ANY OTHER number.
When I was coding this game, I forgot rule number 8 completely. But I did manage to put in half or rule# 8. I put made it so that if you roll the same number twice, you win. But I can't make it so that if you don't roll that same number twice, you will loose.
I tried, but I ran into errors and it became too complicated for me.
My code is below, I will post two different version of it. One version is where I am right now with only half of step eight done and the other half not. And the other version is my failed attempt of adding. Maybe you guys can look at my attempt and get an idea.
The dice class:
public class PairOfDice {
private int die1; // Number showing on the first die.
private int die2; // Number showing on the second die.
public PairOfDice() {
// Constructor. Rolls the dice, so that they initially
// show some random values.
roll(); // Call the roll() method to roll the dice.
}
public void roll() {
// Roll the dice by setting each of the dice to be
// a random number between 1 and 6.
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public int getDie1() {
// Return the number showing on the first die.
return die1;
}
public int getDie2() {
// Return the number showing on the second die.
return die2;
}
public int getTotal() {
// Return the total showing on the two dice.
return die1 + die2;
}
public void resetTotal() {
//Reset dice and total
die1 = 0;
die2 = 0;
}
}
Version 1 of my code that is stable and with only half of step 8:
import java.util.Scanner;
public class CrapsFA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PairOfDice dice;
int bankroll = 0;
int bet = 0;
String input;
int rollCount = 0; // Number of times the dice have been rolled
int bankrolltemp = 0;
int repeat = 0;
dice = new PairOfDice(); // Create the PairOfDice object.
rollCount = 0;
//start getting bankroll and bet amount:
do{ //get bankroll and make sure it's greater than 20.
System.out.print("How much money is in your bank roll" +
"(Must be more than 20)?: ");
bankroll = scanner.nextInt();
} while(bankroll < 20);
do{ //get amount of bet and not less than 10 and greater than bank roll
System.out.print("how much would you like to bet" +
"(Cannot be greater than bankroll or less than 10): ");
bet = scanner.nextInt();
} while(bet < 10 || bet > bankroll);
while (bankroll > 0) {
do {
System.out.println(" ");
System.out.println("Press R to roll dice");
System.out.print("Or enter X to exit: ");
input = scanner.next();
if (input.equalsIgnoreCase("R")){
repeat = dice.getTotal();
dice.roll();
System.out.println("The dice come up " + dice.getDie1()
+ " and " + dice.getDie2() + " = " + dice.getTotal());
rollCount++;}
}
while (dice.getTotal() != 2 && dice.getTotal() != 3 &&
dice.getTotal() != 12 && dice.getTotal() != 7 &&
dice.getTotal() != 11);
if (input.equalsIgnoreCase("X")){
dice.resetTotal();
System.out.print("A small fee is charged for quitting " +
"Bankroll -$2.00 ");
bankrolltemp = bankroll;
bankroll = 0;
}
if (dice.getTotal() == 7 || dice.getTotal() == 11)
{
System.out.print("You win!" +
" You rolled:" + dice.getTotal());
System.out.println(". Your bet:" + bet + " has double" +
" and been added to your bankroll.");
bankroll += (bet * 2);
System.out.println(" Your bankroll is now:" + bankroll);
}
if (dice.getTotal() == 2 || dice.getTotal() == 3 || dice.getTotal() == 12){
System.out.print("You lost! " +
"You rolled:" + dice.getTotal());
System.out.println(". Your bet:" + bet + " is lost" +
" and been subtracted from your bankroll.");
bankroll -= bet;
System.out.println(" Your bankroll is now:" + bankroll);
}
else if (repeat == dice.getTotal()){
System.out.print("You win!" +
" You rolled:" + dice.getTotal() + " twice");
System.out.println(". Your bet:" + bet + " has double" +
" and been added to your bankroll.");
bankroll += (bet * 2);
System.out.println(" Your bankroll is now:" + bankroll);
}
}
bankroll = bankrolltemp;
bankroll -= 2;
System.out.print("Your final bankroll is:" + bankroll);
}
}
My failed attempt (you guys don't have to look it if you guys already have an idea):
import java.util.Scanner;
public class CrapsFA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PairOfDice dice;
int bankroll = 0;
int bet = 0;
String input;
int rollCount = 0; // Number of times the dice have been rolled
int bankrolltemp = 0;
int repeat = 0;
boolean other = false;
dice = new PairOfDice(); // Create the PairOfDice object.
rollCount = 0;
//start getting bankroll and bet amount:
do{ //get bankroll and make sure it's greater than 20.
System.out.print("How much money is in your bank roll" +
"(Must be more than 20)?: ");
bankroll = scanner.nextInt();
} while(bankroll < 20);
do{ //get amount of bet and not less than 10 and greater than bank roll
System.out.print("how much would you like to bet" +
"(Cannot be greater than bankroll or less than 10): ");
bet = scanner.nextInt();
} while(bet < 10 || bet > bankroll);
while (bankroll > 0) {
do {
System.out.println(" ");
System.out.println("Press R to roll dice");
System.out.print("Or enter X to exit: ");
input = scanner.next();
if (input.equalsIgnoreCase("R")){
dice.resetTotal();
repeat = dice.getTotal();
dice.roll();
System.out.println("The dice come up " + dice.getDie1()
+ " and " + dice.getDie2() + " = " + dice.getTotal());
rollCount++;}
}
while (dice.getTotal() != 2 && dice.getTotal() != 3 &&
dice.getTotal() != 12 && dice.getTotal() != 7 &&
dice.getTotal() != 11);
if (input.equalsIgnoreCase("X")){
dice.resetTotal();
System.out.print("A small fee is charged for quitting " +
"Bankroll -$2.00 ");
bankrolltemp = bankroll;
bankroll = 0;
}
if (other == true || repeat != dice.getTotal()){
System.out.print("You lost! " +
"You rolled:" + dice.getTotal() + " and not");
System.out.println(". Your bet:" + bet + " is lost" +
" and been subtracted from your bankroll.");
bankroll -= bet;
System.out.println(" Your bankroll is now:" + bankroll);
}
if (dice.getTotal() == 7 || dice.getTotal() == 11)
{
System.out.print("You win!" +
" You rolled:" + dice.getTotal());
System.out.println(". Your bet:" + bet + " has double" +
" and been added to your bankroll.");
bankroll += (bet * 2);
System.out.println(" Your bankroll is now:" + bankroll);
}
if (dice.getTotal() == 2 || dice.getTotal() == 3 || dice.getTotal() == 12){
System.out.print("You lost! " +
"You rolled:" + dice.getTotal());
System.out.println(". Your bet:" + bet + " is lost" +
" and been subtracted from your bankroll.");
bankroll -= bet;
System.out.println(" Your bankroll is now:" + bankroll);
}
else if (dice.getTotal() == 4 && dice.getTotal() == 5 &&
dice.getTotal() == 6 && dice.getTotal() == 8 &&
dice.getTotal() == 9 && dice.getTotal() == 10){
other = true;
}
else if (repeat == dice.getTotal()){
System.out.print("You win!" +
" You rolled:" + dice.getTotal() + " twice");
System.out.println(". Your bet:" + bet + " has double" +
" and added to your bankroll.");
bankroll += (bet * 2);
System.out.println(" Your bankroll is now:" + bankroll);
}
}
bankroll = bankrolltemp;
bankroll -= 2;
System.out.print("Your final bankroll is:" + bankroll);
}
}
Edited by An Alien, 11 February 2011 - 11:36 AM.


Sign In
Create Account


Back to top









