package aceybuggy; /************************************************************************** * Acey Ducey Card Game * We want a computer program to simulate the card game of Acey Ducey. * The computer is to play the role of the dealer. You are dealt face * cards face up. The player has the option to bet or not to bet depending * on whether you feel the next card dealt will have a value between the * first two. If you bet, the third card is dealt and the computer determines * if you won or lost, adding or subtracting your bet accordingly. ***************************************************************************/ import java.io.*; public class Acey { static final int Ace = 1; static final int King =13; // The cards to be dealt int[] deck; int dealcount=0; // count of cards dealt final int kBankLimit = 1000; // set bank limit: exceed this to break bank BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); /* setDeck assigns a deck of cards for the game. * This is to facilitate testing of the application by providing a * predetermined sequence of cards. Don't include this method * in your cyclomatic complexity calculations. */ public void setDeck (int[] newDeck) { deck = newDeck; } /* * play the game (the main game logic) */ public void play() { int bankRoll; //how much money to play with int bankLimit; //the limit of win int bet; //amount of user's bet int firstCard; //first card dealt during hand int secondCard; //second card dealt during hand int thirdCard; //third card dealth during hand boolean winFlag; //true if player won the hand System.out.println("Welcome to Ace Ducey. \n"); //welcome message bankRoll=100; //initialize player's bankroll do //do- while loop { System.out.println("You now have $" + bankRoll + ". "); System.out.print("You are dealt: "); firstCard = dealCard(); //call dealcard to get first ard System.out.print(" "); secondCard = dealCard(); //call dealcard to get secondcard System.out.println(); bet = obtainValidBet(bankRoll); //call obtain valid bet // Did player chicken out? if (bet==0) { System.out.println("Chicken! "); } // Did player enter a legal bet? if (bet>0) { System.out.print("Third card is: "); thirdCard = dealCard(); //call dealcard to get thirdcard System.out.println(); winFlag = Determine_Outcome(firstCard, secondCard, thirdCard); bankRoll = updateBankRoll (bankRoll, winFlag, bet); } } while (!gameOver(bet,bankRoll)); { /* Determine end game reason */ if (bet < 0) { // player quit System.out.println("You have $ " + bankRoll); System.out.println(" Goodbye!!"); } else if (bankRoll < 0) //player busted System.out.println("Sorry you are busted!!!"); else System.out.println("You broke the bank. Goodbye!!"); } } /** ******************************************************************* * Deal a random card value * @pre 0 <= dealcount < deck.length * @post ace<=Function Value<=king */ private int dealCard () { int theCard; //The_Card = int (rand() % ((King - Ace) + Ace)); //convert random value to card value theCard = deck[dealcount++]; if (dealcount >= deck.length) dealcount = 0; displayCard (theCard); return theCard; } /********************************************************************* * @pre 1 <= theCard <= 13 * @post theCard name is displayed */ private void displayCard(int theCard) //a card to display { final String[] cardnames = {"ace","two","three","four","five","six","seven","eight","nine", "ten","jack","queen","king"}; System.out.print(cardnames[theCard]); } /********************************************************************** * Get bet amount from player * @pre 0 < bankRoll < Bank_Limit * @return player's bet */ private int obtainValidBet (int bankRoll) //player's current bankroll { int bet=0; try { System.out.print("Your bet? "); String reply = keyboard.readLn(); bet = Integer.parseInt(reply); while (bet>bankRoll); { System.out.println("Sorry your bet is too big "); //too big message System.out.print("Your bet? "); //bet prompt reply = keyboard.readLine(); bet = Integer.parseInt(reply); } } catch (NumberFormatException ex) { ex.printStackTrace(); } catch (java.io.IOException ex) { ex.printStackTrace(); } return bet; //a valid bet amount } /********************************************************************** * Determine if hand was won or lost * @post Function Value==TRUE, if (player won the hand) * ==FALSE, if (player lost the hand) */ private boolean Determine_Outcome (int First_Card, int Second_Card, int Third_Card) { return (((First_Card < Third_Card) && (Third_Card < Second_Card))|| ((Second_Card < Third_Card) && (Third_Card < First_Card))); } /********************************************************************** * @pre bet > 0, * @post * A win or lose message is shown * bankRoll has been updated */ private int updateBankRoll ( int bet, /*amount of bet*/ boolean Win_Flag, /*true if player won hand*/ int bankRoll) /*current bankroll*/ { if (Win_Flag) { System.out.println(" You won!!!\n"); //you won message bankRoll = bankRoll + bet; //increment bankroll by bet } else { System.out.println(" You lose!!\n"); //you lose message bankRoll = bankRoll + bet; //decrement bankroll by bet } return bankRoll; } /********************************************************************* * See if game is over * @return * TRUE if bet is negative or * bankRoll is negative or * bankRoll > Bank_Limit * FALSE, otherwise */ private boolean gameOver ( int bet, //amount of bet int bankRoll) //current bankroll { return ((bet < 0) || (bankRoll < 0) || (bankRoll > kBankLimit); } /** Test Driver */ public static void main(String[] args) { int[] cards = {1,11,2,1,13,7,1,12,6,1,13,5,1,11,4}; Acey app = new Acey(); app.setDeck(cards); app.play(); } }