Skip to content

Instantly share code, notes, and snippets.

@funk29
Forked from anonymous/gist:8372760
Created January 11, 2014 17:15
Show Gist options
  • Save funk29/8373703 to your computer and use it in GitHub Desktop.
Save funk29/8373703 to your computer and use it in GitHub Desktop.
//killerabbit37, ITCS 1215 Section 4
import java.util.Random;
public class Craps
{
public static int wins;
public static int losses;
public static int point;
public static int roll = 1;
public static int round = 1;
public static int die1;
public static int die2;
public static int roundTotal = 100000;
public static void main(String args[])
{
while (round <= roundTotal)
{
roll();
round++;
}
System.out.println(wins + " win(s), " + losses + " loss(es)");
}
public static void roll()
{
Random rand = new Random();
die1 = rand.nextInt(6) + 1;
die2 = rand.nextInt(6) + 1;
print();
if (isLoss(die1 + die2))
{
losses++;
roll = 1;
}
else
{
if (isWin(die1 + die2))
{
wins++;
roll = 1;
}
else
{
roll++;
roll();
}
}
}
/*
public static boolean isWin(int total)
{
if (roll == 1)
{
if (total == 7 || total == 11)
return true;
else
{
point = total;
return false;
}
}
else
{
if (total == point)
return true;
else
return false;
}
}
*/
public static boolean isWin(int total)
{
if (roll == 1)
{
switch(total)
{
case 7:
case 11:
return true;
default:
point = total;
return false;
}
}
else
{
if (total == point)
return true;
else
return false;
}
}
/*
public static boolean isLoss(int total)
{
if (roll == 1)
{
if (total == 2 || total == 3 || total == 12)
return true;
}
else
{
if (total == 7)
return true;
}
return false;
}
*/
public static boolean isLoss(int total)
{
if (roll == 1)
{
switch(total)
{
case 2:
case 3:
case 12:
return true;
default:
return false;
}
}
else
{
switch(total)
{
case 7:
return true;
default:
return false;
}
}
}
public static void print()
{
if (round <= 10)
{
System.out.println("Round " + round + ", Roll " + roll + ", Die1: " + die1 + ", Die2: " + die2 + " -- Total: " + (die1 + die2));
if (isWin(die1 + die2))
{
System.out.println("WIN!");
System.out.println((wins + 1) + " win(s), " + losses + " loss(es)" + "\n");
}
if (isLoss(die1 + die2))
{
System.out.println("LOSE!");
System.out.println(wins + " win(s), " + (losses + 1) + " loss(es)" + "\n");
}
if (isWin(die1 + die2) == false && roll == 1)
System.out.println("Point is " + point);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment