package AceyDucey; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class AceyDucey { private Random rnd = new Random(); private Boolean Running = true; private Boolean GameOver = false; private Integer Wallet = 0; private Integer[] Cards = {0,0,0}; private Boolean isAnInt(String input) { try { Integer.parseInt(input); return true; }catch(Exception ex) { return false; } } private String askAQuestion(String prompt) { while(true) { System.out.print(prompt + "?"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String ans = br.readLine(); if(ans == null || ans == "") { System.out.println("PLEASE ANSWER THE QUESTION."); } else { return ans.toUpperCase(); } }catch(Exception ex) { } } } private Integer getIntInput(String prompt) { while(true) { System.out.print(prompt + " "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String ans = br.readLine(); if(isAnInt(ans)) { return Integer.parseInt(ans); }else { System.out.println("YOU DID NOT ENTER AN INTEGER."); } }catch(Exception ex) { } } } private Integer askForBet() { Integer bet = 0; while(true) { bet = getIntInput("WHAT IS YOU BET?"); if(bet.intValue() < 0) { System.out.println("PLEASE BET A NON-NEGATIVE AMOUNT."); }else if(bet.intValue() > Wallet.intValue()) { System.out.println("SORRY MY FRIEND, BUT YOU BET TOO MUCH."); System.out.println("YOU ONLY HAVE " + Wallet + " DOLLARS."); }else { return bet; } } } private Boolean askToPlayAgain() { while(true) { String ans = askAQuestion("DO YOU WANT TO PLAY AGAIN"); if(ans.equalsIgnoreCase("YES") || ans.equalsIgnoreCase("Y")) return true; if(ans.equalsIgnoreCase("NO") || ans.equalsIgnoreCase("N")) return false; System.out.println("PLEASE ANSWER \"YES\" OR \"NO\""); } } private Boolean askToSeeTheRules() { while(true) { String ans = askAQuestion("DO YOU WANT TO SEE THE RULES"); if(ans.equalsIgnoreCase("YES") || ans.equalsIgnoreCase("Y")) return true; if(ans.equalsIgnoreCase("NO") || ans.equalsIgnoreCase("N")) return false; System.out.println("PLEASE ANSWER \"YES\" OR \"NO\""); } } private void displayCard(Integer card) { if(card.intValue() == 11) System.out.println("JACK"); else if(card.intValue() == 12) System.out.println("QUEEN"); else if(card.intValue() == 13) System.out.println("KING"); else if(card.intValue() == 14) System.out.println("ACE"); else System.out.println(card); } private void displayRules() { System.out.println("ACEY DUCEY IS PLAYED IN THE FOLLOWING MANNER:"); System.out.println("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP"); System.out.println("YOU HAVE THE OPTION TO BET OR NOT BET DEPENDING"); System.out.println("ON WHETHER OR NOT YOU FEEL THE NEXT CARD WILL "); System.out.println("HAVE A VALUE BETWEEN THE FIRST TWO."); System.out.println(); System.out.println("IF YOU DO NOT WANT TO BET, INPUT A 0 (ZERO)."); } private Integer generateRandomNumber(Integer minimum, Integer maximum, Boolean show) { Integer value = rnd.nextInt(maximum)+minimum; if(show)System.out.println(value); return value; } private Integer selectCard() { while(true) { Integer card = generateRandomNumber(2,13,false); if(!Cards[0].equals(card) && !Cards[1].equals(card) && !Cards[2].equals(card)) { displayCard(card); return card; } } } public void run() { System.out.print("*** ACEY DUCEY : JAVA ***"); Running = true; if(askToSeeTheRules())displayRules(); while(Running) { Wallet = 100; Cards[0] = Cards[1] = Cards[2] = 0; GameOver = false; while(!GameOver) { System.out.println("YOU NOW HAVE " + Wallet + " DOLLARS."); Cards[0] = selectCard(); Cards[1] = selectCard(); Integer bet = askForBet(); if(bet > 0) { Cards[2] = selectCard(); if( (Cards[2] > Cards[0] && Cards[2] < Cards[1]) || (Cards[2] > Cards[1] && Cards[2] < Cards[0])) { System.out.println("YOU WIN!!!"); Wallet += bet; } else { System.out.println("SORRY, YOU LOSE."); Wallet -= bet; } if(Wallet < 1) { System.out.println("SORRY FRIEND, YOU ARE BROKE."); GameOver = true; } } else { GameOver = true; } } System.out.println("*** GAME OVER ***"); Running = askToPlayAgain(); } System.out.println("OK, HOPE YOU HAD FUN."); System.out.println("THANKS FOR PLAYING!"); } public static void main(String[] args) { AceyDucey game = new AceyDucey(); game.run(); } }