World Series of Slots - Java Pt. 1

Chris Chen
import java.util.*;
public class WSoS {
static Scanner in = new Scanner(System.in);
public static void main(String[] args){
playSlots();
}
public static void playSlots(){ //plays the entire game of slots
int numTokens = 0, bet = 0, moneyWon; // number of tokens, the amount of tokens use wants to bet, the amount of money that the player won from the slot roll
char play = 'n', again = 'n';
String letter = "";
System.out.print("The World Series of Slots Machine! \n\n");
do{
System.out.print("How many tokens are you buying? ");
numTokens = in.nextInt();
if(numTokens System.out.print("You must buy at least one Token to play in the World Series of Slots.\n\n");
}while(numTokens do{
in.nextLine();
do{
System.out.print("You have " + numTokens + " tokens. Would you like to play? (y/n): ");
letter = in.nextLine();
play = letter.charAt(0);
if(!(play == 'n' || play == 'y'))
System.out.print("Invalid Entry. Please try again. \n\n");
}while(!(play == 'n' || play == 'y'));
if(play == 'y'){
do{
System.out.print("How many tokens would you like to bet? ");
bet = in.nextInt();
if(bet > numTokens || bet System.out.print("Nice Try! You have " + numTokens + " tokens. You must bet something. \n");
}while(bet > numTokens || bet System.out.print("\nYou have bet " + bet + " tokens... Good Luck! \n\n");
moneyWon = spinSlots(bet);
if(moneyWon == 0)
numTokens -= bet;
else
numTokens += moneyWon;
}
if(numTokens == 0){
do{
System.out.print("You are out of tokens. Would you like to buy more? (y/n): ");
in.nextLine();
letter = in.nextLine();
again = letter.charAt(0);
if(!(again == 'n' || again == 'y'))
System.out.print("Invalid Entry. Please try again. \n\n");
}while(!(again == 'n' || again == 'y'));
}
}while(play == 'y');
System.out.print("Thanks for playing!");
}
public static int spinSlots(int bet){ //uses the randomizer to determine if the player won, then calulates how much money the slot produces
Random r = new Random();
int winnings = 0, slot[] = {0, 0, 0};
System.out.print("\tSPIN:");
for(int i = 0; i < 3; i++){
slot[i] = r.nextInt(9) + 1;
System.out.print(" [" + slot[i] + "]");
}
System.out.print("\n\n");
if(slot[1] == slot[2] && slot[1] == slot[3]){
if(slot[1] == 5){
winnings = bet;
System.out.print("You have won " + winnings + "tokens! Go You!\n\n");
}
else if(slot[1] == 2){
winnings = bet * 2;
System.out.print("You have won " + winnings + "tokens! Awsome!\n\n");
}
else if(slot[1] == 7){
winnings = bet * 3;
System.out.print("You have won " + winnings + "tokens! YOUR THE BEST SLOTTER EVER!\n\n");
}
else
System.out.print("Too bad.. you lost. Try again and you might win~\n\n");
}
else
System.out.print("Too bad.. you lost. Try again and you might win~\n\n");
return(winnings);
}
}

Published by Chris Chen

Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.