//Dice game created by Kemal 'kemkoi' Kojic 2015-08-29
import java.util.Scanner;
import java.util.Random;
public class cantSleep {
//----------How the program should work-------------------//
//Create a dice program which lets the user choose between h/l (High/Low)
//The dice is then rolled and depending on which number it lands on there are different
//actions.
// l = 1-3, h = 4-6
//The user should start with a bank value of a 1000 marks.
//For each win the amount of the bet should be doubled.
//After a win/loss the user should be prompted again, asking if they want to play again.
//----------How to create the program --------------------//
//Create a boolean variable for the loop so the user can be prompted after one run.
//Create one integer for the bank value and one for the dice.
//Create a char and set make it so the Scanner takes in values for either 'h' or 'l'
//If the user gives any value other than the ones above prompt them to do it over again.
//Create a random method which random's out the dice number.
//Create an switch/case for the different outcomes on the dice number.
//------Problems encountered----//
//Everything works until the last step, whenever you win/lose
//the bankValue doesn't change.
//Random number generator method.
public static int randomGenerator(int startValue, int lastValue)
{
int randomNum = rand.nextInt((lastValue-startValue) + 1) + startValue;
return randomNum;
}
public static void diceWin(int bankValue, int bet, int dice, char choice, boolean runAgain)
{
Scanner userInput
= new Scanner
(System.
in);
bankValue += bet;
System.
out.
println("Congratulations, the dice shows " + dice
+ " and you just won yourself " + bet
+ " $."); System.
out.
print("Would you like to bet again? (y/n): "); choice = userInput.nextLine().charAt(0);
while(choice != 'y' && choice != 'n')
{
System.
out.
println("Please use the characters 'y' or 'n' next time."); System.
out.
print("Would you like to bet " + bet
+ "? (y/n): "); choice = userInput.next().charAt(0);
if(choice == 'n')
{
runAgain = false;
}
}
}
public static void diceLoss(int bankValue, int bet, int dice, char choice, boolean runAgain)
{
Scanner userInput
= new Scanner
(System.
in);
bankValue -= bet;
System.
out.
println("Sorry, the dice shows " + dice
+ " and you just lost " + bet
+ " $."); System.
out.
print("Would you like to bet again? (y/n): "); choice = userInput.nextLine().charAt(0);
while(choice != 'y' && choice != 'n')
{
System.
out.
println("Please use the characters 'y' or 'n' next time."); System.
out.
print("Would you like to bet " + bet
+ "? (y/n): "); choice = userInput.next().charAt(0);
if(choice == 'n')
{
runAgain = false;
}
}
}
public static void main
(String[] args
) {
Scanner userInput
= new Scanner
(System.
in);
//Variable declaration
boolean runAgain = true, low = true;
int bankValue = 1000, dice = 0, bet = 0;
char choice;
//Start of the program
System.
out.
println("Hello and welcome to the dice machine."); while(runAgain == true)
{
System.
out.
println("Your current balance is: " + bankValue
+ " $."); //User input, if the bet amount is bigger than the balance it gives an error message out.
System.
out.
print("How much would you like to bet?: "); bet = userInput.nextInt();
while(bet > bankValue)
{
System.
out.
print("You do not have that much money on your bank account, please try again: "); bet = userInput.nextInt();
}
//Prompts the user for a verification of the bet.
//If anything but 'y' and 'n' is typed in it gives an error message out.
//If the user types a 'y' in the program continues to run, if they type an 'n' in the program closes.
System.
out.
print("Do you really want to bet " + bet
+ "? (y/n): "); choice = userInput.next().charAt(0);
while(choice != 'y' && choice != 'n')
{
System.
out.
println("Please use the characters 'y' or 'n' next time."); System.
out.
print("Would you like to bet " + bet
+ "? (y/n): "); choice = userInput.next().charAt(0);
if(choice == 'n')
{
}
}
//Asks the user what he/she would like to put the bet on. (h/l)
System.
out.
print("What would you like to put " + bet
+ " on, 'h' or 'l'? (l = 1-3, h = 4-6): "); choice = userInput.next().charAt(0);
while(choice != 'h' && choice != 'l')
{
System.
out.
println("Please use the characters 'h' or 'l' next time."); System.
out.
print("What would you like to put " + bet
+ " on, 'h' or 'l'? (l = 1-3, h = 4-6): "); choice = userInput.next().charAt(0);
}
//Executing the randomGenerator method.
dice = randomGenerator(1, 6);
//Giving the dice numbers boolean values.
switch(dice)
{
case 1:
low = true;
break;
case 2:
low = true;
break;
case 3:
low = true;
break;
case 4:
low = false;
break;
case 5:
low = false;
break;
case 6:
low = false;
break;
default:
System.
out.
println("Error, dice gave the number " + dice
); }
if((low == true && choice == 'l') || (low == false && choice == 'h'))
{
diceWin(bankValue, bet, dice, choice, runAgain);
} else if((low == true && choice == 'h') || (low == false && choice == 'l')) {
diceLoss(bankValue, bet, dice, choice, runAgain);
} else {
System.
out.
println("Something went horribly wrong, please check the source code."); }
}
userInput.close();
}
}