Java dice game

By | 17/11/2014

The program asks how much money you want to bet (amount of rounds) and throws three dice. If you get six once you win 1 euro, if twice 20 euros and if all three dice give a six you win 40 euros.

DiceGame.java:

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

public class DiceGame {
 Scanner reader=new Scanner(System.in);
 Random randomize=new Random();

 private void run() {
  int money=getBet();
  System.out.println("Die 1\tDie 2\tDie 3\tWin");
  int totalWins=0;
  for(int i=0; i< money; i++) {
   int dieValue1=throwDie();
   int dieValue2=throwDie();
   int dieValue3=throwDie();
   int roundWins=countWinnings(dieValue1, dieValue2, dieValue3);
   totalWins+=roundWins;
   printRow(dieValue1, dieValue2, dieValue3, roundWins);
  }
  System.out.println("\nYou won "+totalWins+"€");
 }
 private int getBet() {
  int input=0;
  do {
   System.out.println("How much do you want to bet? (3-1000)");
   input=reader.nextInt();
  }while(input<3 || input>1000);
  return input;
 }
 private int throwDie() {
  int die=randomize.nextInt(6)+1;
  return die;
 }
 private int countWinnings(int first, int second, int third) {
  int winnings;
		
  if(first==6 && second==6 && third==6) {
   winnings=40;
  }
  else if(first==6 && second==6 || first==6 && third==6 || second==6 && third==6) {
   winnings=20;
  }
  else if(first==6 || second==6 || third==6) {
   winnings=1;
  }
  else {
   winnings=0;
  }
  return winnings;
 }
 private void printRow(int first,int second,int third, int roundWins)
 {	
  System.out.println(first+"\t"+second+"\t"+third+"\t"+roundWins+"\t");
 }
 public static void main(String[] args) {
  DiceGame program = new DiceGame();
  program.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *