Skip to content

Instantly share code, notes, and snippets.

@taylorsmithgg
Created March 23, 2017 07:54

Revisions

  1. taylorsmithgg created this gist Mar 23, 2017.
    219 changes: 219 additions & 0 deletions Craps.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,219 @@
    import java.io.IOException;
    import java.util.Scanner;

    /**
    Write a description of class Game here.
    @author (your name)
    @version (a version number or a date)
    */
    public class Game
    {
    private static int wins = 0;
    private static int losses = 0;

    /**
    * Start the game loop
    * @return
    */
    public static int start(){
    Scanner scanner = new Scanner(System.in);
    return Integer.parseInt(scanner.next());
    }

    /**
    * Roll a single die
    * @return
    */
    public static int roll(){
    return (int)(6.0 * Math.random() + 1.0);
    }

    /**
    * ASCII Art Dice
    * @param num
    */
    public static void dice(int num){
    int width = 12;
    int height = 6;

    System.out.print(" ");

    for(int y = 0; y <= height; y++) {
    if(y > 0 && y < height){
    System.out.print("|");
    }

    for (int x = 0; x <= width; x++) {
    if (y == 0 || y == height) {
    if(x == 0 && y == height){
    System.out.print(" ");
    } else {
    System.out.print("-");
    }

    if(x == width){
    System.out.print("\n");
    }

    } else {
    switch(num) {
    case 1:
    if(y == 3 && x == 6){
    System.out.print("O");
    } else {
    System.out.print(" ");
    }
    break;
    case 2:
    if(y == 5 && x == 3){
    System.out.print("O");
    }
    else if(y == 1 && x == 10){
    System.out.print("O");
    } else {
    System.out.print(" ");
    }
    break;
    case 3:
    if(y == 3 && x == 6){
    System.out.print("O");
    }
    else if(y == 5 && x == 3){
    System.out.print("O");
    }
    else if(y == 1 && x == 10){
    System.out.print("O");
    } else {
    System.out.print(" ");
    }
    break;
    case 4:
    if(y == 1 && x == 3){
    System.out.print("O");
    }
    else if(y == 5 && x == 3){
    System.out.print("O");
    }
    else if(y == 1 && x == 10){
    System.out.print("O");
    }
    else if(y == 5 && x == 10){
    System.out.print("O");
    } else {
    System.out.print(" ");
    }
    break;
    case 5:
    if(y == 3 && x == 6){
    System.out.print("O");
    }
    else if(y == 1 && x == 3){
    System.out.print("O");
    }
    else if(y == 5 && x == 3){
    System.out.print("O");
    }
    else if(y == 1 && x == 10){
    System.out.print("O");
    }
    else if(y == 5 && x == 10){
    System.out.print("O");
    } else {
    System.out.print(" ");
    }
    break;
    case 6:
    if(y == 3 && x == 3){
    System.out.print("O");
    }
    else if(y == 3 && x == 10){
    System.out.print("O");
    }
    else if(y == 1 && x == 3){
    System.out.print("O");
    }
    else if(y == 5 && x == 3){
    System.out.print("O");
    }
    else if(y == 1 && x == 10){
    System.out.print("O");
    }
    else if(y == 5 && x == 10){
    System.out.print("O");
    } else {
    System.out.print(" ");
    }
    break;
    }
    }
    }

    if(y > 0 && y < height){
    System.out.println("|");
    }
    }

    }

    /**
    * Evaluate result
    * @param num
    */
    public static void eval(int num){
    boolean won = false;
    final String win = "You win";
    final String lose = "You lose";
    String result = "";

    switch(num){
    case 2:
    losses++;
    result = result.concat("Snake Eyes! ");
    break;
    case 3:
    losses++;
    break;
    case 7:
    wins++;
    won = true;
    break;
    case 11:
    wins++;
    won = true;
    break;
    case 12:
    losses++;
    break;
    default:
    if(roll() + roll() == 7){
    losses++;
    } else {
    wins++;
    won = true;
    }
    break;
    }

    result = result.concat(won ? win : lose).concat(" | " + "W: " + wins + "/" + "L: " + losses);
    System.out.println(result);
    }

    public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println("To roll, enter 1.");
    System.out.println("To stop, enter 0.");
    while(start() != 0){
    System.out.println("\n");
    int i = roll();
    dice(i);
    System.out.println("Roll 1: " + i);
    int j = roll();
    dice(j);
    System.out.println("Roll 2: " + j);
    System.out.println("Total:\t" + i + " + " + j + " = " + (i + j));
    eval(i + j);
    System.out.println("To play again, enter 1. To exit, enter 0.");
    }

    }

    }