/************************************************************** ** ** Program : hilo.java ** ** Author : Ryan J. Stradling ** ** Created : July 10, 1996 ** ** Revisions : August 5, 1996 - changed the design ** ** Purpose : This will play a hilo card game that is text based. ** ***************************************************************/ import java.awt.*; import java.util.Random; import java.lang.Math; import java.lang.String; /********************************************************************* ** ** Class : hilo ** ** Inherits : From the applet class ** ** This class will handle the window interactions and the game interface ** **********************************************************************/ public class hilo extends Frame { // Instance variables Label cash_label, bet_label; /* The player's cash and bet labels */ Label msg_label; /* The player's message label */ Label first_label, second_label, third_label; // Text Field variables TextField first_text, second_text, third_text; TextField cash_text; TextField msg_text; TextField bet_text; // Button variables Button deal_button, quit_button; Player player; /* the player playing */ Card card_one, card_two, card_three; /* the cards drawn */ Game game; /* The game class variable */ String message; // Messages to output // CONSTRUCTOR METHOD hilo () { setBackground(Color.blue); // Set background color to blue setForeground(Color.white); // Set foreground color to white /* Initialize variables */ resize(250, 400); player = new Player(); game = new Game(); setLayout(new FlowLayout()); // Set layout Style // ADDING LABELS AND THE TEXT FIELDS cash_label = new Label("Your cash"); cash_text = new TextField(4); add(cash_label); add(cash_text); cash_text.setText("$0"); cash_text.setEditable(false); deal_button = new Button("DEAL"); add(deal_button); // ADDING THE DEAL BUTTON first_label = new Label("First Card"); first_text = new TextField(13); third_label = new Label("Middle Card"); third_text = new TextField(13); second_label = new Label("Second Card"); second_text = new TextField(13); msg_label = new Label("Message Text"); msg_text = new TextField(25); bet_label = new Label("Your Bet: $"); bet_text = new TextField(5); bet_text.setText("0"); add(first_label); add(first_text); add(third_label); add(third_text); add(second_label); add(second_text); add(bet_label); add(bet_text); add(msg_label); add(msg_text); quit_button = new Button("QUIT"); add(quit_button); // ADDING THE QUIT BUTTON show(); first_text.setText(""); second_text.setText(""); third_text.setText(""); // Set all text windows to start with not editable first_text.setEditable(false); second_text.setEditable(false); third_text.setEditable(false); msg_text.setEditable(false); bet_text.setEditable(true); bet_text.disable(); } public boolean action (Event evt, Object arg) { /* Deal with the actions of the window */ // Deal button hit if (evt.target instanceof Button) { if (evt.target == deal_button) { deal_button.disable(); deal(); return true; } else if (evt.target == quit_button) { bet_text.disable(); dispose(); System.exit(0); return true; } return false; } // Text field had data entered into it else if (evt.target instanceof TextField) { if (evt.target == bet_text) { bet_text.disable(); bet(); return true; } return false; } return false; } private boolean bet() { /* This function handles the bet textfield event */ int amount_bet = 0; // Get the amount bet and convert from a string to an integer try { amount_bet = Integer.parseInt(bet_text.getText()); } catch (NumberFormatException e ) { bet_text.enable(); bet_text.requestFocus(); msg_text.setText("INVALID ENTRY IN BET FIELD"); return(false); } // Make sure bet is between 10 and 100 if (amount_bet >= 10 && amount_bet <= 100) { game.draw(); // Draw the third card // Get the game instance messages card_three = game.card_three(); game.WonLost(player, amount_bet); message = game.message(); // Set the text fields third_text.setText(card_three.getName()); msg_text.setText(message); cash_text.setText("$"+player.earned()); // Set the bet text field uneditable and deal button enabled bet_text.disable(); deal_button.enable(); } else { // Get the bet again and display error message bet_text.enable(); msg_text.setText("BET MUST BE 10-100"); deal_button.disable(); } return (true); } private void deal () { /* This will handle the event for the pressing of the deal button */ // If the cards are not equal if (game.deal()) { third_text.setText(""); // get the game instance messages card_one = game.card_one(); card_two = game.card_two(); message = game.message(); // set the textfields msg_text.setText(message); first_text.setText(card_one.getName()); second_text.setText(card_two.getName()); // Set the state of the bet_text and deal_button widgets bet_text.enable(); deal_button.disable(); bet_text.setText(""); bet_text.requestFocus(); // bet_text is focused on } else { third_text.setText(""); // get the game instance messages card_one = game.card_one(); card_two = game.card_two(); message = game.message(); // set the textfields msg_text.setText(message); first_text.setText(card_one.getName()); second_text.setText(card_two.getName()); bet_text.setText(""); // Set the state of the bet_text and deal_button widgets bet_text.disable(); deal_button.enable(); } } public static void main ( String args[] ) { hilo hilo_game = new hilo(); } } /********************************************************************** ** ** class Player ** ** This class is the player class for the game ** ***********************************************************************/ class Player { // Instance variables private protected int earnings; // Total earnings Player () /* CONSTRUCTOR METHOD */ { earnings = 0; } int earned() /************************************************************* ** ** int earned() ** ** returns : amount player has earned so far ** **************************************************************/ { return earnings; } void add_earnings(int amount) /************************************************************** ** ** add_earnings(int amount) ** ** returns : nothing ** ** adds to earnings the "amount" passed in ** ***************************************************************/ { earnings+=amount; } } /********************************************************************* ** ** class : Game ** ** This class will handle the actual playing and rules of the hi-lo ** game. ** **********************************************************************/ class Game { static final int NUMSHUFFLE = 200; private protected Deck deck; private protected Card card_one, card_two, card_three; private protected String message; Game() /***************************************************************** ** ** constructor method ** ** sets card_place to 0 ** ******************************************************************/ { deck = new Deck(); deck.shuffle(NUMSHUFFLE); } Card card_one() { /* card_one instance variable access method */ return card_one; } Card card_two() { /* card_two instance variable access method */ return card_two; } Card card_three() { /* card_three instance variable access method */ return card_three; } String message() { /* message instance variable access method */ return message; } boolean deal() { /* This method will deal the first two cards */ // Reshuffle the deck if low on cards if (deck.remaining() <= 3) { deck.shuffle(NUMSHUFFLE); message = new String("SHUFFLING THE CARDS"); } else message = new String(""); // Deal and set the first two cards deck.deal(1); card_one = deck.card(); deck.deal(2); card_two = deck.card(); // outer cards are different if (card_one.getValue() != card_two.getValue()) { return true; } // outer cards are the same, so redeal more cards else { message = new String("REDEAL: CARDS ARE THE SAME"); return false; } } boolean draw() { /* This method will draw the third (middle) card */ deck.deal(3); card_three = deck.card(); return true; } void WonLost( Player player, int bet ) { /* this method will tell whether the player has one or lost */ // The integer value of the cards int card_one_value = card_one.getValue(); int card_two_value = card_two.getValue(); int card_three_value = card_three.getValue(); // POSTED: PAY DOUBLE if (card_three_value == card_one_value || card_three_value == card_two_value) { message = new String("PAY DOUBLE"); player.add_earnings(-1*bet*2); } else if (card_two_value > card_one_value) { if (card_three_value > card_one_value && card_three_value < card_two_value) { /* WON */ message = new String("YOU WON"); player.add_earnings(bet); } else { /* LOST */ message = new String("YOU LOST"); player.add_earnings(-1*bet); } } else if (card_two_value < card_one_value) { if (card_three_value < card_one_value && card_three_value > card_two_value) { /* WON */ message = new String("YOU WON"); player.add_earnings(bet); } else { /* LOST */ message = new String("YOU LOST"); player.add_earnings(-1*bet); } } } } /********************************************************************* ** ** class : Deck ** ** This class will handle shuffling and making the cards. ** **********************************************************************/ class Deck { // INSTANCE VARIABLES private protected Card cards[] = new Card[52]; private protected int index_array[] = new int[52]; private protected String cards_type[] = new String[13]; private protected String suits[] = new String[4]; private protected int cards_remaining; private protected Card card; Deck() /* Constructor */ { cards_type[0] = "2"; cards_type[1] = "3"; cards_type[2] = "4"; cards_type[3] = "5"; cards_type[4] = "6"; cards_type[5] = "7"; cards_type[6] = "8"; cards_type[7] = "9"; cards_type[8] = "10"; cards_type[9] = "J"; cards_type[10] = "Q"; cards_type[11] = "K"; cards_type[12] = "A"; suits[0] = "Spades"; suits[1] = "Hearts"; suits[2] = "Diamonds"; suits[3] = "Clubs"; for (int i = 0; i < 52; i++) cards[i] = new Card(); // Initialize the deck to be in order for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { cards[j+i*13].setCard(suits[i], cards_type[j]); } } for (int i = 0; i < 52; i++) { index_array[i] = i; } } void shuffle(int num_times) { /* This method will shuffle the deck */ double first_rand, second_rand; Random rand_num = new Random(); int first_number; int second_number; int temp; cards_remaining = 52; for (int i = 0; i < num_times; i++) { first_rand = rand_num.nextDouble(); second_rand = rand_num.nextDouble(); first_number = (int) Math.round((51*first_rand)); second_number = (int) Math.round((51*second_rand)); temp = index_array[first_number]; index_array[first_number] = index_array[second_number]; index_array[second_number] = temp; } } void deal(int which_card) { /* This method will deal a card */ int index_val = 52 - cards_remaining; index_val = index_array[index_val]; String suit_type, card_type; cards[index_val].setCardInt(which_card); card = cards[index_val]; cards_remaining--; } int remaining() { /* accessor method for cards_remaining instance variable */ return cards_remaining; } Card card() { /* accessor method for card instance variable */ return card; } } class Card { /* this class will simulate a card */ protected private int int_value; // Integer value of the card protected private String name; // The full name of the card String suit; String card_type; Card() { int_value = 0; name = ""; suit = ""; card_type = ""; } void setCard ( String suit, String card_type ) // The constructor creates the integer value of the card and // the cards full name. { this.suit = new String(suit); this.card_type = new String(card_type); name = new String(card_type + " of " + suit); } void setCardInt( int which_card ) { int_value = GetIntCardValue(card_type, which_card); } String getName () { /* Accessor method for name instance variable */ return name; } int getValue() { /* Accessor method for int_value instance variable */ return int_value; } private int GetIntCardValue(String card, int card_num) { /************************************************************** ** ** GetIntCardValue(String card, int card_num) ** ** returns : integer value of the card ** ** This method will return the integer value of the "card" given ** its value. NOTE: The card_num is just used for the ace card ** and will set Ace = 1 if it is the first card dealt or 14 ** otherwise. ** ***************************************************************/ if (card.equals("A") && card_num == 1) return 1; else if (card.equals("A") && (card_num == 2 || card_num == 3)) return 14; else if (card.equals("2")) return 2; else if (card.equals("3")) return 3; else if (card.equals("4")) return 4; else if (card.equals("5")) return 5; else if (card.equals("6")) return 6; else if (card.equals("7")) return 7; else if (card.equals("8")) return 8; else if (card.equals("9")) return 9; else if (card.equals("10")) return 10; else if (card.equals("J")) return 11; else if (card.equals("Q")) return 12; else if (card.equals("K")) return 13; else return 0; } }