Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: From the Ground Up

Similar presentations


Presentation on theme: "Java Programming: From the Ground Up"— Presentation transcript:

1 Java Programming: From the Ground Up
Chapter 20 A Case Study: Video Poker, Revisited

2 Objective The objective of this chapter is an understanding of the design principle that entails the separation of the data model from the interface, or more simply, the model from the view. He we replace the text-based user interface developed in Chapter 11 with a more visual GUI that utilizes buttons, labels, and pictures. The separation of model from view that was underscored in the case study of Chapter 11 enables us to plug in a new graphical interface with minimal effort.

3 A Quick Review The poker application of Chapter 11 consists of seven interacting classes: Player PokerGame Bet Deck Card Hand Bankroll

4 A Quick Review Class Actions Player PokerGame Bet Deck Attributes
Bankroll bankroll; PokerGame pg; Bet bet; Player player Bet bet Bankroll bankroll Hand hand int bet Card deck[] Actions Initialize the bankroll. Add coins. Bet and Play. Discard. Display a Hand. Quit. Display final results. Present a menu. View initial hand. Discard or hold cards. Update bankroll. Get the bet. Set the bet. Shuffle the deck. Deal a card. Card Hand Bankroll int suit int value Card [] hand Deck deck int bankroll Get the suit. Get the value, i.e., rank. Get the name of a card. Evaluate the hand. Deal a new hand. Update a hand. Give the hand. Get the bankroll. Set the bankroll. Change the bankroll.

5 A Quick Review The Player class provides a text-based user interface.
It is the Player class that we re-implement here, replacing text-based input and output with a GUI of buttons, labels, and pictures. Replacing the text-based UI of Chapter 11 with a GUI does not require knowledge of the implementation details of the other classes.

6 A Quick Review To replace the old user interface with a GUI, all that is needed is information about the objectives and methods of some of the classes. The next figures show those classes and methods that we use in creating a new GUI based poker game. Except for the Player class, which handles all input and output, no previously developed class needs alteration.

7 A Quick Review Class Purpose Constructor Method method Bankroll
manages the number of coins in the machine Bankroll(); sets initial coin number to 0 void alterBankroll(int n); adds n coins to the number of coins in the machine int getBankRoll(); returns the number of coins currently in the machine

8 A Quick Review Class Purpose Constructor Method method Hand
maintains a hand of five cards Hand(); creates an empty hand String[] getHand(); returns an array of five String references that describes a hand, e.g., {“Ace of Hearts”, “2 of Spades”, “3 of Diamonds’,…}

9 A Quick Review Class Purpose Constructor Method method Bet
manages the current bet or wager Bet( int n); sets the bet to n coins int getBet(); returns the current bet void setBet(int n);

10 A Quick Review Class Purpose Constructor Method method PokerGame
plays the game: deals and updates the hands, maintains the list of discarded cards PokerGame(Bet bet, Bankroll bankroll, Player player ); initializes the bet and bankroll for a player void viewInitialHand( ); requests a hand of five cards via hand.getHand() asks the player to display the hand via the message player.displayHand(hand) void discardOrHoldCards(); queries the player for the list of discarded cards: player.getDiscard(…); updates the hand; requests that the player display the new hand: player,displayHand() evaluates the hand; determines the winnings/losses; updates the bankroll; asks the player to display the results: player.displayResults()

11 A Visual Poker Game The GUI consists not of a text menu, but of buttons, labels, and images.

12 A Visual Poker Game A video poker GUI

13 A Visual Poker Game Before playing a hand of poker, a player must insert coins into the machine. This action is simulated by clicking the Add 1 or the Add 5 button. These buttons can be clicked repeatedly. Each time a player clicks one of these buttons, either 1 or 5 coins are “inserted” into the machine. The bottom panel of the GUI displays the current number of coins, that is, the bankroll.

14 A player inserts three coins
A Visual Poker Game A player inserts three coins

15 A Visual Poker Game Once a player inserts coins into the machine, he clicks one of the five Bet buttons A bet from one to five coins is placed when clicked, but not more than the number of coins in the machine. A hand of five cards is then dealt.

16 A player bets two coins and a hand is dealt
A Visual Poker Game A player bets two coins and a hand is dealt

17 A Visual Poker Game After the initial hand is dealt, a player has the option of keeping or discarding any of those five cards. To “hold” or keep a card, a player clicks the number that is displayed below the card and that number is replaced by the word Hold.

18 Two cards are marked Hold
A Visual Poker Game Two cards are marked Hold

19 A Visual Poker Game A player then clicks the Deal button and those cards that the player chooses to discard are replaced with different cards. The hand is scored and the number of coins updated.

20 A Visual Poker Game The player keeps the two aces. The other three cards are replaced resulting in a hand containing two pair, which pays 2 to 1.

21 Laying Out the Frame First create a Player class that extends JFrame and includes the buttons and labels of the GUI. The following code builds a non-functioning GUI, i.e., a GUI with no listeners.

22 Laying Out the Frame /////////////// Player class, a GUI for video poker /////////////// public class Player extends JFrame { private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // add 1 coin private JButton add5Button // clicking adds 5 coins; private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking any of these buttons makes a bet and begins play

23 Laying Out the Frame public Player() // default constructor, places all components { super("Video Poker"); setBounds(0,0,400,500); // the label at the top of the frame resultLabel = new JLabel(); resultLabel.setFont(new Font("Arial", Font.BOLD, 18)); resultLabel.setText("Video poker"); //The five card images , the initial image is "Back.gif," which is a dummy card cardLabel = new JLabel[5]; for (int i = 0; i < 5; i++) cardLabel[i] = new JLabel(new ImageIcon("Back.gif"));  // the five hold/discard buttons holdButton = new JButton[5];

24 Laying Out the Frame for (int i = 0; i <5; i++) {
holdButton[i] = new JButton(""+(i+1)); // initially these have numbers holdButton[i].setFont(new Font("Arial",Font.BOLD, 18)); holdButton[i].setEnabled(false); // initially turned off } // the five bet and play buttons betAndPlayButton = new JButton[5]; for ( int i = 0; i < 5; i++) betAndPlayButton[i] = new JButton("Bet "+(i+1)); betAndPlayButton[i].setEnabled(false); // initially turned off betAndPlayButton[i].setFont(new Font("Arial", Font.BOLD, 15));

25 Laying Out the Frame // the deal button, initially turned off
dealButton = (new JButton("Deal")); dealButton.setFont(new Font("Arial", Font.BOLD, 18)); dealButton.setEnabled(false); // the quit button quitButton = new JButton("Quit"); quitButton.setFont(new Font("Arial", Font.BOLD, 15)); // label that displays current number of coins, the bankroll bankrollLabel = new JLabel(); bankrollLabel.setFont(new Font("Arial", Font.BOLD, 24)); bankrollLabel.setText("Coins remaining: " + 0 ); // initially no coins // two buttons that add 1 0r 5 coins to the machine add1Button = new JButton("Add 1"); add5Button = new JButton("Add 5"); add1Button.setFont(new Font("Arial", Font.BOLD, 15)); add5Button.setFont(new Font("Arial", Font.BOLD, 15));

26 Laying Out the Frame // panel for the play buttons, card labels, hold buttons, deposit buttons, deal and quit JPanel centerPanel = new JPanel(new GridLayout(4,5)); // add the five bet-and -play buttons for (int i = 0; i < 5; i++) centerPanel.add(betAndPlayButton[i]);  //add the five labels that displays the card images centerPanel.add(cardLabel[i]); // add the five hold buttons centerPanel.add(holdButton[i]);

27 Laying Out the Frame // add the two deposit buttons, the a blank button, the deal and quit buttons centerPanel.add(add1Button); centerPanel.add(add5Button); centerPanel.add(new JButton()); // a blank button as a separator centerPanel.add(dealButton); centerPanel.add(quitButton); //add the label that displays the results to the NORTH section of the frame add(resultLabel, BorderLayout.NORTH); // add the label that displays the coin count to the SOUTH section of the frame add(bankrollLabel, BorderLayout.SOUTH); // add the panel with the buttons and card labels to the CENTER of the frame add(centerPanel, BorderLayout.CENTER); setResizable(false); setVisible(true); }

28 Adding Coins Before a hand of poker can be played, a player must deposit coins into the machine. This is accomplished by clicking the Add 1 button or Add 5 button. Each click increases the bankroll by either one or five coins. Once coins have been added, the appropriate Bet buttons are enabled. For example, if a player deposits three coins, the buttons labeled Bet 1, Bet 2, and Bet 3 are enabled but Bet 4 and Bet 5 are not. The Bet 4 and Bet 5 buttons are disabled because you cannot bet four or more coins when there are just three coins in the machine! If a player deposits seven coins, then all five buttons are enabled. A Bankroll object manages the number of coins deposited into the machine.

29 Adding Coins Clicking Add 1 or Add 5 generates an action event that we handle with an inner class called ButtonHandler. This listener handles all events from either button. The following code: declares and initializes a Bankroll reference, bankroll, and implements ButtonListener, an inner class that responds to events generated by add1Button and add5Button. The response of ButtonListener necessitates: incrementing the bankroll, displaying the number of coins in the machine on the label referenced by bankrollLabel, and enabling the appropriate Bet and Play buttons.

30 Adding Coins public class Player extends JFrame {
private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // clicking adds 1 coin private JButton add5Button; // clicking adds 5 coins private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking buttons makes a bet and //begins play private Bankroll bankroll; // manages the number of coins in the machine

31 Adding Coins public Player() // constructor, places all components, registers listeners { // as above bankroll = newBankroll(); add1Button.addActionListener(new ButtonListener()); //register listener add5Button.addActionListenet( new Button Listener()); // register listener } private class ButtonListener implements ActionListener// responds to button events public void actionPerformed( ActionEvent e) if ((e.getSource() == add1Button) || (e.getSource() == add5Button))

32 Adding Coins { if (e.getSource() == add1Button)
bankroll.alterBankroll(1); // add one coin to the bankroll else bankroll.alterBankroll(5); // add 5 coins int br = bankroll.getBankroll(); // total number of coins deposited bankrollLabel. setText("Coins remaining: "+ br ); // display total coins // enable the appropriate bet buttons for ( int i = 0; i < 5; i++) if (br >= (i+1)) betAndPlayButton[i].setEnabled(true); return; }

33 The First Hand After a player inserts coins, he/she is ready to play a hand of poker. Now, the player clicks one of the buttons labeled Bet 1, Bet 2,…, Bet 5. Clicking one of these buttons determines the current bet and deals the initial poker hand. To the Player class we add code that: registers the ButtonListener class with each of the five Bet buttons, and responds to the Bet button events: instantiates and sets the bet, displays the bet on the label referenced by resultLabel, instantiates a new PokerGame passing bet, bankroll, and player as parameters, displays images of the cards that make up the hand, enables the Hold and Deal buttons, and disables the Bet buttons, the Add 1 and Add 5 buttons, and the Quit button.

34 The First Hand public class Player extends JFrame {
private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // add 1 coin private JButton add5Button // clicking adds 5 coins; private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking any of these buttons makes a bet and // begins play private Bankroll bankroll; // maintains number of coins in the machine private PokerGame pokerGame; private Bet bet; privateHand hand;

35 The First Hand public Player() // default constructor, lays out components, registers listeners { // as previously coded for (int i = 0; i< 5; i++) // register ButtonListener with each button betAndPlayButton[i].addActionListener(new ButtonListener()); } private class ButtonListener implements ActionListener public void actionPerformed( ActionEvent e) if ((e.getSource() == add1Button) || (e.getSource() == add5Button)) { // as previously coded }

36 The First Hand for (int i = 0; i < 5; i++) // respond to betAndPlayButton[i] if (e.getSource() == betAndPlayButton[i]) { bet = new Bet(); bet.setBet(i+1); // set the bet for this hand resultLabel.setText("Bet is "+(i+1)); // display the bet on the label pokerGame = new PokerGame(bet, bankroll,Player.this ); // instantiate PokerGame pokerGame.viewInitialHand(); // ask pokerGame to deal the first hand for (int j = 0; j < 5; j++) // for each hold button holdButton[j].setText(""+(j+1)); // display the card number holdButton[j].setEnabled(true); // enable the button }

37 The First Hand dealButton.setEnabled(true); // enable the deal button
add1Button.setEnabled(false); // disable add1Button.. add5Button.setEnabled(false); // disable add5Button quitButton.setEnabled(false); // disable quitButton for (int j = 0; j <5; j++) // disable all betAndPlayButtons betAndPlayButton[j].setEnabled(false); return; }

38 The First Hand Notice that the response to a betAndPlayButton event includes sending a message to pokerGame (line 36): pokerGame.viewInitialHand() The viewInitialhand() method of PokerGame consists of two method calls: public void viewInitialHand() { hand.newHand(); player.displayHand(hand); } The call to newHand() creates a new hand of five cards. This method works correctly regardless of the interface. However, the second call is a Player method, displayHand(hand).

39 The First Hand The text-based version of Player implements displayHand(Hand hand) as: public void displayHand(Hand hand) { String [] handString = hand.getHand(); for(int i = 0; i < 5; i++) System.out.println((i+1) +". "+handString[i]); } That is, a hand is displayed on the screen as a list of strings: Ace of Hearts Queen of Clubs Queen of Hearts 3 of Spades 4 of Hearts

40 The First Hand To accomplish visual we use a collection of 52 card images, conveniently named Ace of Hearts.gif, Ace of Spades.gif ,…, 10 of Hearts.gif, 10 of Spades.gif, etc. Moreover, the Hand method: String[] getHand() returns an array of five strings, e.g. , {“Ace of Spades ”, “Queen of Clubs” , “Queen of Hearts”, “3 of Spades”, “4 of Hearts”}.

41 The First Hand A revised displayHand() for a revised Player class can be written as: public void displayHand(Hand hand) { String[] handString = hand.getHand(); for (int i = 0; i < 5; i++) String name = handString[i]+".gif"; // name is an image file name cardLabel[i].setIcon(new ImageIcon(name)); // display images on labels } Thus, in addition to the constructor and the inner class ButtonListener, the GUI version of Player, like the text version, implements the displayHand(Hand hand) method.

42 Holding Cards A player has the option of holding or discarding any or all of his/her five cards. To retain a card, a player presses the numbered button shown directly below the card. The response to pressing any one of these buttons changes the button’s text from a number to the string “Hold” and disables the button. To register a listener with each such button we add the following statement to the constructor: for (int i = 0; i < 5; i++) holdButton[i].addActionListener(new ButtonListener());

43 Holding Cards To respond to events generated by these buttons, we add code to ButtonListener that changes a button’s text to “Hold” and disables the button: for (int i = 0; i < 5; i++) if (e.getSource() == holdButton[i]) // source is button[i] { holdButton[i].setText("Hold"); holdButton[i].setEnabled(false); return; } Once a player clicks a Hold button, the button is disabled and the decision cannot be reversed. One could certainly add a mechanism that allows a player to change his/her mind, but we opt for simplicity.

44 The New Hand After a player decides which cards to hold and which to discard, he/she clicks the Deal button. This action generates an event. The response to this event: invokes pokerGame.discardOrHoldCards(), disables the Deal and Hold buttons, and enables the other buttons.

45 The New Hand In addition to registering ButtonHandler as a listener for dealButton, we add the following if statement to the ButtonHandler class to handle a Deal button event: if (e.getSource() == dealButton) { pokerGame.discardOrHoldCards(); // discardOrHoldCards() does the work // enable and disable the appropriate buttons dealButton.setEnabled(false); for(int j = 0; j < 5; j++) holdButton[j].setEnabled(false); for ( int i = 0; i < 5; i++) if (bankroll.getBankroll() >= (i+1)) betAndPlayButton[i].setEnabled(true); add1Button.setEnabled(true); add5Button.setEnabled(true); quitButton.setEnabled(true); }

46 The New Hand The PokerGame method discardOrHoldCards() defined in Chapter 11 manages the updated hand: public void discardOrHoldCards() { player.getDiscard(holdCards); hand.updateHand(holdCards); player.displayHand(hand); int payoff = hand.evaluateHand(); int winnings = updateBankroll(payoff); player.displayResults(payoff, winnings); }

47 The New Hand Notice that discardOrHoldCards() invokes three Player methods: void getDiscard(boolean[] holdCards), void displayHand(Hand hand), and void displayResults( int payoff, int winnings).

48 The New Hand The getDiscard(boolean[] holdCards) method of the text-based Player class sets holdCards[i] to true if the player opts to keep the ith card and false otherwise. That is, the Player method getDiscard(…) tells the caller which cards to keep and which to discard. The new GUI Player class must do likewise. When a player retains a card, the corresponding Hold button is disabled.

49 The New Hand getDiscard(boolean[] holdCards) can be implemented by checking whether or not a Hold button is enabled: public void getDiscard(boolean[] holdCards) { for ( int i = 0; i < 5; i++) // check whether or not the Hold button is enabled if (holdButton[i].isEnabled()) // button was not clicked holdCards[i] =false; else // button was clicked and enabled holdCards[i] = true; } The displayResults( int payoff, int winnings) remains to be implemented.

50 The New Hand The text-based version of Player implements this method as: public void displayResults(int payoff, int winnings) { String nameOfHand = "Lose"; if (payoff == 250) nameOfHand = "Royal Flush"; else if (payoff == 50) nameOfHand = "Straight Flush"; else if (payoff == 25) nameOfHand = "Four of a Kind"; else if (payoff == 9) nameOfHand = "Full House"; else if (payoff == 6) nameOfHand = " Flush"; else if (payoff == 4) nameOfHand = "Straight "; else if (payoff == 3) nameOfHand = "Three of a Kind"; else if (payoff == 2) nameOfHand = "Two Pair"; else if (payoff == 1) nameOfHand = " Pair of Jacks or Better";

51 The New Hand { System.out.println("Winner: "+ nameOfHand);
if (winnings >0 ) { System.out.println("Winner: "+ nameOfHand); System.out.println("Payoff is "+ winnings + " coins."); } else System.out.println("You lost your bet of "+ bet.getBet()); System.out.println("Current Bankroll is " + bankroll.getBankroll()); System.out.println(); This method can be incorporated into the new Player class with minimal change. The game’s outcome is displayed on two labels rather than in a text-based window using System.out.println().

52 The New Hand The only code that must be altered is the final if-else statement: // use a label rather than println() for output if (winnings >0 ) resultLabel.setText("Winner: "+ nameOfHand+" - pays "+ winnings); else resultLabel.setText("You lost your bet of "+ bet.getBet()); bankrollLabel.setText("Coins remaining: " + bankroll.getBankroll());

53 The Complete Player Class
The new Player class has the following skeletal form that includes a constructor, three methods, and a private inner class: public class Player extends JFrame { // The Constructor public Player() sets up the components of the GUI registers listener with buttons } // Three Methods public void displayHand(Hand hand) displays images of the five cards in hand

54 The Complete Player Class
public void getDiscard(boolean[] holdCards) { holdCards[i] == true // if the ith card is retained } public void displayResults(int payoff, int winnings) { displays the outcome of a hand // Listener – an Inner Class private class ButtonListener implements ActionListener public void ActionPerformed(ActionEvent e) responds to events generated by GUI buttons

55 The Complete Player Class
The complete class, although rather lengthy, is direct and uncomplicated. The card images are assumed to be in a folder, Cards, which is in the same directory as the Player class.

56 The Complete Player Class
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Player extends JFrame { private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // add 1 coin private JButton add5Button; // clicking adds 5 coins; private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking makes a bet and begins play private Bankroll bankroll; private PokerGame pokerGame; private Bet bet; private Hand hand;

57 The Complete Player Class
public Player() // constructor { super("Video Poker"); bet = new Bet(); bankroll = new Bankroll(); setBounds(0,0,400,500); // the label places at the NORTH area of the frame resultLabel = new JLabel(); resultLabel.setFont(new Font("Arial", Font.BOLD, 18)); resultLabel.setText("Video poker"); // Display five card images , the initial image is "Back.gif" – a dummy card cardLabel = new JLabel[5]; for (int i = 0; i < 5; i++) cardLabel[i] = new JLabel(new ImageIcon("Cards/Back.gif"));

58 The Complete Player Class
// the five hold/discard buttons holdButton = new JButton[5]; for (int i = 0; i <5; i++) { holdButton[i] = new JButton(""+(i+1)); // initially display numbers 1 - 5 holdButton[i].setFont(new Font("Arial",Font.BOLD, 18)); holdButton[i].setEnabled(false); // initially turned off } // the five "bet and play" buttons betAndPlayButton = new JButton[5]; for ( int i = 0; i < 5; i++) betAndPlayButton[i] = new JButton("Bet "+(i+1)); // display Bet 1, Bet 2,…Bet 5 betAndPlayButton[i].setEnabled(false); // initially turned off betAndPlayButton[i].setFont(new Font("Arial", Font.BOLD, 15));

59 The Complete Player Class
// the deal button, initially turned off dealButton = (new JButton("Deal")); dealButton.setFont(new Font("Arial", Font.BOLD, 18)); dealButton.setEnabled(false); // the quit button quitButton = new JButton("Quit"); quitButton.setFont(new Font("Arial", Font.BOLD, 15)); // label that displays current number of coins, i.e. the "bankroll" bankrollLabel = new JLabel(); bankrollLabel.setFont(new Font("Arial", Font.BOLD, 24)); bankrollLabel.setText("Coins remaining: " + 0 ); // initially no coins // two buttons that either add 1 or 5 coins to the machine add1Button = new JButton("Add 1"); // displays "Add1" add5Button = new JButton("Add 5"); add1Button.setFont(new Font("Arial", Font.BOLD, 15)); add5Button.setFont(new Font("Arial", Font.BOLD, 15));

60 The Complete Player Class
// panel holds bet-and-play buttons, card labels, hold buttons, deposit buttons, // deal and quit JPanel centerPanel = new JPanel(new GridLayout(4,5)); // add the five be t-and - play buttons for (int i = 0; i < 5; i++) centerPanel.add(betAndPlayButton[i]); //add the five labels that display the card images centerPanel.add(cardLabel[i]); // add the five hold buttons centerPanel.add(holdButton[i]); // add the two deposit buttons, the a blank button, the deal and quit buttons centerPanel.add(add1Button); centerPanel.add(add5Button); centerPanel.add(new JButton()); // a blank button as a separator centerPanel.add(dealButton); centerPanel.add(quitButton);

61 The Complete Player Class
// add the label that displays the game results to the NORTH section of the frame add(resultLabel, BorderLayout.NORTH); // add the label that displays the coin count to the SOUTH section of the frame add(bankrollLabel, BorderLayout.SOUTH); // add the panel that holds the buttons and card labels to the CENTER of the frame add(centerPanel, BorderLayout.CENTER); //register listeners, one inner class does all listening add1Button.addActionListener(new ButtonListener()); add5Button.addActionListener(new ButtonListener()); dealButton.addActionListener(new ButtonListener()); quitButton.addActionListener(new ButtonListener()); for (int i = 0; i< 5; i++) betAndPlayButton[i].addActionListener(new ButtonListener());

62 The Complete Player Class
for (int i = 0; i < 5; i++) holdButton[i].addActionListener(new ButtonListener()); setResizable(false); setVisible(true); } public void displayHand(Hand hand) // displays images of five cards { String[] handString = hand.getHand(); String name = "Cards/"+handString[i]+".gif"; // name is a file name. cardLabel[i].setIcon(new ImageIcon(name));

63 The Complete Player Class
public void getDiscard(boolean[] holdCards) // maintains hold/discard information { for ( int i = 0; i < 5; i++) if (holdButton[i].isEnabled()) // card is discarded holdCards[i] =false; else // card is retained holdCards[i] = true; } public void displayResults(int payoff, int winnings) // displays the final outcome l String nameOfHand = "Lose"; if (payoff == 250) nameOfHand = "Royal Flush"; else if (payoff == 50) nameOfHand = "Straight Flush"; else if (payoff == 25) nameOfHand = "Four of a Kind";

64 The Complete Player Class
else if (payoff == 9) nameOfHand = "Full House"; else if (payoff == 6) nameOfHand = " Flush"; else if (payoff == 4) nameOfHand = "Straight "; else if (payoff == 3) nameOfHand = "Three of a Kind"; else if (payoff == 2) nameOfHand = "Two Pair"; else if (payoff == 1) nameOfHand = " Pair of Jacks or Better"; if (winnings >0 ) // display outcome on resultLabel resultLabel.setText("Winner: "+ nameOfHand+" - pays "+ winnings); else resultLabel.setText("You lost your bet of "+ bet.getBet()); bankrollLabel.setText("Coins remaining: " + bankroll.getBankroll()); }

65 The Complete Player Class
private class ButtonListener implements ActionListener // respond to button events { public void actionPerformed( ActionEvent e) if ((e.getSource() == add1Button) || (e.getSource() == add5Button)) // click Add 1/ Add 5 if (e.getSource() == add1Button) bankroll.alterBankroll(1); else bankroll.alterBankroll(5); int br = bankroll.getBankroll(); bankrollLabel. setText("Coins remaining: "+ br ); for ( int i = 0; i < 5; i++) if (br >= (i+1)) betAndPlayButton[i].setEnabled(true); return; }

66 The Complete Player Class
if (e.getSource() == quitButton) // click the Quit button System.exit(0); for (int i = 0; i < 5; i++) // click one of the five bet-and-play buttons if (e.getSource() == betAndPlayButton[i]) { bet = new Bet(); bet.setBet(i+1); resultLabel.setText("Bet is "+(i+1)); pokerGame = new PokerGame(bet, bankroll ,Player.this ); pokerGame.viewInitialHand(); for(int j = 0; j < 5; j++) // enable the hold buttons holdButton[j].setText(""+(j+1)); holdButton[j].setEnabled(true); }

67 The Complete Player Class
// enable and disable other buttons add1Button.setEnabled(false); add5Button.setEnabled(false); quitButton.setEnabled(false); dealButton.setEnabled(true); for (int j = 0; j <5; j++) betAndPlayButton[j].setEnabled(false); return; } for (int i = 0; i < 5; i++) // respond to a Hold button event if (e.getSource() == holdButton[i]) { holdButton[i].setText("Hold"); holdButton[i].setEnabled(false);

68 The Complete Player Class
if (e.getSource() == dealButton) // respond to a Deal button event { pokerGame.discardOrHoldCards(); dealButton.setEnabled(false); for(int j = 0; j < 5; j++) holdButton[j].setEnabled(false); for ( int i = 0; i < 5; i++) if (bankroll.getBankroll() >= (i+1)) // enough coins ? betAndPlayButton[i].setEnabled(true); add1Button.setEnabled(true); add5Button.setEnabled(true); quitButton.setEnabled(true); } public static void main(String[] args) Player pm = new Player();


Download ppt "Java Programming: From the Ground Up"

Similar presentations


Ads by Google