Ejemplo n.º 1
0
  // ------------------------------------------------------------------------------------------------------------
  // Creates a PairOfDice object, rolls 1000 times while simultaneously checking for pairs of 6
  // ------------------------------------------------------------------------------------------------------------
  public static void main(String[] args) {
    PairOfDice diePair =
        new PairOfDice(); // Creates a PairOfDice object, which creates two Die objects
    int numBoxCars = 0; // Pairs of 6
    final int MAX_ROLLS = 1000;
    NumberFormat fmt = NumberFormat.getPercentInstance();

    for (int i = 0; i <= MAX_ROLLS; i++) { // Rolls (and checks) 1000 times
      diePair.roll();

      // If the pair is equal, and the first die is equal to 6; If the first die is 6, the second
      // must be 6
      if (diePair.areEqual() && diePair.getSingleValue(1) == 6) {
        numBoxCars++;
      }
    }
    System.out.println("Out of 1000 total rolls, there were " + numBoxCars + " BoxCars.");
    System.out.println(
        "That makes the chance of a BoxCar about " + fmt.format((double) numBoxCars / MAX_ROLLS));
  }
Ejemplo n.º 2
0
  /** This method represents a game */
  private void game() {
    Scanner scan = new Scanner(System.in);
    String userInput;

    do {
      // read user input
      // by default it is y
      // if user input nothing consider that he wants default and use default
      System.out.print("Do you want to roll? (Y/n/q): ");
      String input = scan.nextLine();
      System.out.println();
      if (input.equals("")) {
        userInput = "y";
      } else {
        userInput = input;
      }

      // while user input value is y
      // it is user's turn
      // if user input value is n it means that it's machine's turn
      while (userInput.equalsIgnoreCase("y")) {
        // roll dices
        dices.roll();

        // add both values from dices to point of a current turn
        user.addCurrentTurnPoints(dices.getValue1() + dices.getValue2());

        // if we get '1' or two '1' then drop points for player
        // and change a hand to machine
        if (dices.getValue1() == 1 || dices.getValue2() == 1) {
          if (dices.getValue1() == dices.getValue2()) {
            System.out.println("You rolled two 1s. You lose all of your points and your hand");
            user.dropPoints(true);
            userInput = "n";
          } else {
            System.out.println("You rolled 1. You lose current points and your hand");
            user.dropPoints(false);
            userInput = "n";
          }
        }

        // if hand is still user's then show result and ask for a next roll of dices
        if (userInput.equalsIgnoreCase("y")) {
          System.out.println(
              "So far you have all points: "
                  + user.getAllPoints()
                  + " and points at current turn: "
                  + user.getPointsCurrentTurn());
          System.out.println("So far your enemy has all points: " + machine.getAllPoints());
          System.out.print("Do you want to roll? (Y/n/q): ");
          input = scan.nextLine();
          System.out.println();
          if (input.equals("")) {
            userInput = "y";
          } else {
            userInput = input;
          }
        }
      }
      // if user asks to stop rolling then adding points from current turn to all points
      // but if we dropped point of user we will add "0"
      user.addPoints(user.getPointsCurrentTurn());

      // while turn is 'n' (machine's) and points of machine for current turn less than 20
      while (userInput.equalsIgnoreCase("n") && machine.getPointsCurrentTurn() < 20) {
        // roll dices
        dices.roll();

        // add dices' values to current points of machine
        machine.addCurrentTurnPoints(dices.getValue1() + dices.getValue2());

        // check if there is not '1' value on any dice
        // if there is then drop points and change turn
        if (dices.getValue1() == 1 || dices.getValue2() == 1) {
          if (dices.getValue1() == dices.getValue2()) {
            System.out.println("Machine rolled two 1s. It loses all its points and its hand");
            machine.dropPoints(true);
            userInput = "y";
          } else {
            System.out.println("Machine rolled 1. It loses current points and its hand");
            machine.dropPoints(false);
            userInput = "y";
          }
        }
      }
      // if machine got points for current turn more than 20
      // then it stores these points
      machine.addPoints(machine.getPointsCurrentTurn());

      // if no one won we print results again
      if (user.getAllPoints() < 100 && machine.getAllPoints() < 100) {
        System.out.println(
            "So far you have all points: "
                + user.getAllPoints()
                + " and points at current turn: "
                + user.getPointsCurrentTurn());
        System.out.println("So far your enemy has all points: " + machine.getAllPoints());
      }
    } while (!userInput.equals("q") && user.getAllPoints() < 100 && machine.getAllPoints() < 100);

    System.out.println();

    // if someone won then print results and points
    if (user.getAllPoints() >= 100) {
      System.out.println("You win with " + user.getAllPoints() + " points");
      System.out.println("Your enemy has " + machine.getAllPoints() + " points");
    } else if (machine.getAllPoints() >= 100) {
      System.out.println("You lose");
      System.out.println("You have " + user.getAllPoints() + " points");
      System.out.println("Your enemy has " + machine.getAllPoints() + " points");
    }
  }
Ejemplo n.º 3
0
  public static void main(String[] args) throws IOException {

    PairOfDice you = new PairOfDice();
    PairOfDice me = new PairOfDice();

    int metotal;
    int youtotal;

    youtotal = 0;
    metotal = 0;

    BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));

    // Round 1
    System.out.println("Round number 1, hit enter to roll the dice:");
    bufReader.readLine();

    you.roll();

    System.out.println(
        "Your roll:"
            + "\t"
            + "DICE 1 = "
            + you.getDice1()
            + "\t"
            + "and DICE 2 = "
            + you.getDice2());
    System.out.println("Your total:" + "\t" + you.getSum());

    me.roll();

    System.out.println(
        "My roll:" + "\t" + "DICE 1 = " + me.getDice1() + "\t" + "and DICE 2 = " + me.getDice2());
    System.out.println("My total:" + "\t" + me.getSum());

    if (you.getSum() > me.getSum()) {
      System.out.println("...Human Wins The Round!" + "\n");
      youtotal += 1;
    } else if (you.getSum() < me.getSum()) {
      System.out.println("...Computer Wins The Round!" + "\n");
      metotal += 1;
    } else System.out.println("...No Winner! It's A TIE!" + "\n");

    // Round 2
    System.out.println("Round number 2, hit enter to roll the dice:");
    bufReader.readLine();

    you.roll();

    System.out.println(
        "Your roll:"
            + "\t"
            + "DICE 1 = "
            + you.getDice1()
            + "\t"
            + "and DICE 2 = "
            + you.getDice2());
    System.out.println("Your total:" + "\t" + you.getSum());

    me.roll();

    System.out.println(
        "My roll:" + "\t" + "DICE 1 = " + me.getDice1() + "\t" + "and DICE 2 = " + me.getDice2());
    System.out.println("My total:" + "\t" + me.getSum());

    if (you.getSum() > me.getSum()) {
      System.out.println("...Human Wins The Round!" + "\n");
      youtotal += 1;
    } else if (you.getSum() < me.getSum()) {
      System.out.println("...Computer Wins The Round!" + "\n");
      metotal += 1;
    } else System.out.println("...No Winner! It's A TIE!" + "\n");

    // Round 3
    System.out.println("Round number 3, hit enter to roll the dice:");
    bufReader.readLine();

    you.roll();

    System.out.println(
        "Your roll:"
            + "\t"
            + "DICE 1 = "
            + you.getDice1()
            + "\t"
            + "and DICE 2 = "
            + you.getDice2());
    System.out.println("Your total:" + "\t" + you.getSum());

    me.roll();

    System.out.println(
        "My roll:" + "\t" + "DICE 1 = " + me.getDice1() + "\t" + "and DICE 2 = " + me.getDice2());
    System.out.println("My total:" + "\t" + me.getSum());

    if (you.getSum() > me.getSum()) {
      System.out.println("...Human Wins The Round!" + "\n");
      youtotal += 1;
    } else if (you.getSum() < me.getSum()) {
      System.out.println("...Computer Wins The Round!" + "\n");
      metotal += 1;
    } else System.out.println("...No Winner! It's A TIE!" + "\n");

    // Round 4
    System.out.println("Round number 4, hit enter to roll the dice:");
    bufReader.readLine();

    you.roll();

    System.out.println(
        "Your roll:"
            + "\t"
            + "DICE 1 = "
            + you.getDice1()
            + "\t"
            + "and DICE 2 = "
            + you.getDice2());
    System.out.println("Your total:" + "\t" + you.getSum());

    me.roll();

    System.out.println(
        "My roll:" + "\t" + "DICE 1 = " + me.getDice1() + "\t" + "and DICE 2 = " + me.getDice2());
    System.out.println("My total:" + "\t" + me.getSum());

    if (you.getSum() > me.getSum()) {
      System.out.println("...Human Wins The Round!" + "\n");
      youtotal += 1;
    } else if (you.getSum() < me.getSum()) {
      System.out.println("...Computer Wins The Round!" + "\n");
      metotal += 1;
    } else System.out.println("...No Winner! It's A TIE!" + "\n");

    // Round 5
    System.out.println("Round number 5, hit enter to roll the dice:");
    bufReader.readLine();

    you.roll();

    System.out.println(
        "Your roll:"
            + "\t"
            + "DICE 1 = "
            + you.getDice1()
            + "\t"
            + "and DICE 2 = "
            + you.getDice2());
    System.out.println("Your total:" + "\t" + you.getSum());

    me.roll();

    System.out.println(
        "My roll:" + "\t" + "DICE 1 = " + me.getDice1() + "\t" + "and DICE 2 = " + me.getDice2());
    System.out.println("My total:" + "\t" + me.getSum());

    if (you.getSum() > me.getSum()) {
      System.out.println("...Human Wins The Round!" + "\n");
      youtotal += 1;
    } else if (you.getSum() < me.getSum()) {
      System.out.println("...Computer Wins The Round!" + "\n");
      metotal += 1;
    } else System.out.println("...No Winner! It's A TIE!" + "\n");

    // Round 6
    System.out.println("Round number 6, hit enter to roll the dice:");
    bufReader.readLine();

    you.roll();

    System.out.println(
        "Your roll:"
            + "\t"
            + "DICE 1 = "
            + you.getDice1()
            + "\t"
            + "and DICE 2 = "
            + you.getDice2());
    System.out.println("Your total:" + "\t" + you.getSum());

    me.roll();

    System.out.println(
        "My roll:" + "\t" + "DICE 1 = " + me.getDice1() + "\t" + "and DICE 2 = " + me.getDice2());
    System.out.println("My total:" + "\t" + me.getSum());

    if (you.getSum() > me.getSum()) {
      System.out.println("...Human Wins The Round!" + "\n");
      youtotal += 1;
    } else if (you.getSum() < me.getSum()) {
      System.out.println("...Computer Wins The Round!" + "\n");
      metotal += 1;
    } else System.out.println("...No Winner! It's A TIE!" + "\n");

    // Final Results

    System.out.println("Game over ... final results:");
    System.out.println("\t" + "Human total:" + "\t" + youtotal);
    System.out.println("\t" + "Computer total:" + "\t" + metotal);

    if (youtotal > metotal) System.out.println("Human Wins!!!" + "\n");
    else if (youtotal < metotal) System.out.println("Computer Wins!!!" + "\n");
    else System.out.println("No Winner! This game is A TIE!" + "\n");
  }