/************************************************************** ** ** Program : hilo.java ** ** Author : Ryan J. Stradling ** ** Created : July 10, 1996 ** ** Revisions : August 5, 1996 ** ** Purpose : This will play a hilo card game that is graphics based. ** ***************************************************************/ import java.awt.*; import java.util.Random; import java.lang.Math; import java.lang.String; import java.awt.image.*; /********************************************************************* ** ** Class : hilo ** ** Inherits : From the applet class ** ** This class will handle the window interactions and the game interface ** **********************************************************************/ public class hilo extends java.applet.Applet { // 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 cash_text; TextField msg_text; TextField bet_text; // Button variables Button deal_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 private Image offScrImg, card1_image, card2_image, card3_image; final int iwidth = 116; final int iheight = 169; boolean paint_third, painting; public void update ( Graphics g ) { // Creating offscreen buffer image if ( offScrImg == null ) offScrImg = createImage( size().width, size().height ); // Creating offscreen graphics context Graphics og = offScrImg.getGraphics(); if (painting) { // Set up clipping region og.clipRect(5, 175, (5+(iwidth*2)+10+iwidth), (iheight)); g.clipRect(5, 175, (5+(iwidth*2)+10+iwidth), (iheight)); // Clear the screen to blue og.setColor(Color.blue); og.fillRect(0, 0, size().width, size().height); GetImages(); // Get the images for the cards // if you have an image for card1 draw it if (card1_image != null) og.drawImage(card1_image, 5, 175, iwidth, iheight, this); // if you have an image for card2 draw it if (card2_image != null) og.drawImage(card2_image, (5+((iwidth *2)+10)), 175, iwidth, iheight, this); // Only draw the third card if you are supposed to if (paint_third) { if (og.drawImage(card3_image, (5+iwidth+5), 175, iwidth, iheight, this)) { paint_third = false; } } // Copy offscreen buffer to display g.drawImage(offScrImg, 0, 0, this); } } void GetImages () { /* gets the images for the cards */ card1_image = getImage(getCodeBase(), "cards/"+card_one.card_file); card2_image = getImage(getCodeBase(), "cards/"+card_two.card_file); if (card_three != null) // Don't get third card unless you have it card3_image = getImage(getCodeBase(), "cards/"+card_three.card_file); } public void paint( Graphics g ) { update(g); } // OVERIDDEN INIT METHOD public void init () { setBackground(Color.blue); // Set background color to blue setForeground(Color.white); // Set foreground color to white /* Initialize variables */ player = new Player(); game = new Game(); painting = false; paint_third = false; 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 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(bet_label); add(bet_text); add(msg_label); add(msg_text); // Set all text windows to start with not editable msg_text.setEditable(false); 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(); painting = true; paint_third = false; repaint(); 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(); if (bet()) { paint_third = true; repaint(); } 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 msg_text.setText(message); cash_text.setText("$"+player.earned()); // Set the bet text field uneditable and deal button enabled deal_button.enable(); bet_text.disable(); } else { // Get the bet again and display error message msg_text.setText("BET MUST BE 10-100"); deal_button.disable(); bet_text.enable(); } return(true); } private void deal () { /* This will handle the event for the pressing of the deal button */ // If the cards are not equal msg_text.setText(""); if (game.deal()) { // get the game instance messages card_one = game.card_one(); card_two = game.card_two(); message = game.message(); msg_text.setText(message); // Set the state of the bet_text and deal_button widgets deal_button.disable(); bet_text.enable(); bet_text.setText(""); bet_text.requestFocus(); // bet_text is focused on } else { // get the game instance messages card_one = game.card_one(); card_two = game.card_two(); message = game.message(); msg_text.setText(message); // Set the state of the bet_text and deal_button widgets bet_text.disable(); deal_button.enable(); } } } /********************************************************************** ** ** 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; String card_file; 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); card_file = suit + "_"+ card_type + ".gif"; } 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; } }