Скачать презентацию Java Programming From the Ground Up Chapter 11 Скачать презентацию Java Programming From the Ground Up Chapter 11

308da11fa8ffd00221dc1884411af6c1.ppt

  • Количество слайдов: 73

Java Programming: From the Ground Up Chapter 11: Designing With Classes and Objects Java Programming: From the Ground Up Chapter 11: Designing With Classes and Objects

A Video Poker Game A Video Poker Machine A Video Poker Game A Video Poker Machine

Playing the Game • The poker machine simulates a single hand of five card Playing the Game • The poker machine simulates a single hand of five card stud poker. • To play the video poker game: – A player deposits an arbitrary number of coins or tokens into the machine. We call this amount the player’s bankroll. – The player makes a bet (one to five coins but not more than the bankroll). – A hand of five cards is dealt from a deck of 52 cards. The deck is reshuffled before each game. – After viewing his/her hand, the player decides which cards he/she wishes to keep and which he/she would like to replace. – New cards are dealt for those cards that the player chooses to discard. – The hand is evaluated and scored. – If the hand is a winner, a payout amount is added to the bankroll; otherwise, the bet is deducted from the bankroll. – The player can quit and cash out at any time. – The player can continue to play as long as the bankroll is not depleted. – The player can add coins to the bankroll before each game

Scoring the Game • A standard deck of cards consists of 52 different cards. Scoring the Game • A standard deck of cards consists of 52 different cards. • Each card has a rank or value as well as a suit. • The ordered ranks are: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, (Ace) • Note that, in rank, an Ace precedes 2 and also follows King. • The suits are Hearts, Diamonds, Spades, and Clubs.

Scoring the Game • The winning hands listed highest to lowest are: • Royal Scoring the Game • The winning hands listed highest to lowest are: • Royal Flush: Ten, Jack, Queen, King, Ace of the same suit. • For example, 10 , Jack , Queen, King, and Ace, all Clubs. Pays 250 to 1. That is, if a player bets one coin and is dealt a royal flush, then he/she wins 250 coins. Straight Flush: Five cards in rank sequence having the same suit but not a Royal Flush. For example, Ace of Hearts, 2 of Hearts, 3 of Hearts, 4 of Hearts, 5 of Hearts. Pays 50 to 1. • Four of a Kind: Four cards of the same rank. For example, 3 of Hearts, 3 of Diamonds, 3 of Clubs, 3 of Spades, 6 of Hearts. Pays 25 to 1. • Full House: Three cards of one rank and two of another. For example, 4 of Hearts, 4 of Spades, 4 of Clubs, 7 of Spades. Pays 9 to 1.

Scoring the Game • Flush: All five cards of the same suit but not Scoring the Game • Flush: All five cards of the same suit but not a straight flush. For example, 3 of Hearts, 6 of Hearts, 7 of Hearts, 10 of Hearts, Jack of Hearts. Pays 6 to 1. • • • Straight: Five cards in rank sequence but not a flush. For example, Ace of Hearts, 2 of Spades, 3 of Hearts, 4 of Clubs, 5 of Diamonds. Pays 4 to 1. Three of a Kind: Three cards of the same rank and two cards of two other ranks, that is, not a full house or four of a kind. For example, 5 of Hearts, 5 of Clubs, 5 of Spades, 7 of Clubs, 9 of Diamonds. Pays 3 to 1. Two Pair: Two cards of one rank, two of another, and one card of a third. For example, 6 of Hearts, 6 of Clubs, 9 of Spades, Ace of Hearts. Pays 2 to 1. • • Jacks or Better: Exactly one pair of Jacks, Queens, Kings, or Aces and nothing else of interest. For example, Jack of Hearts, Jack of Clubs, 2 of Spades, 3 of Clubs, 3 of Hearts. Pays 1 to 1.

Scoring the Game A Few Winning Poker Hands Scoring the Game A Few Winning Poker Hands

Problem Statement • Design and implement of an object based model for a video Problem Statement • Design and implement of an object based model for a video poker game. • A simple design process that incorporates the following three steps: 1. Determine the classes. 2. Determine the responsibilities of each class. 3. Determine the interactions and collaborations among the classes.

Determine the Classes • A common methodology for determining the classes and objects appropriate Determine the Classes • A common methodology for determining the classes and objects appropriate for an application entails noting and marking the nouns of the problem specification. • Although every noun does not necessarily give rise to a class, examining the nouns is a good starting point. • Implicit here is the assumption that the problem is clearly specified. • If the problem is unclear, vaguely stated, or poorly formulated, then you are on soft terrain.

Determine the Classes The problem specification, but with the nouns highlighted in boldface: • Determine the Classes The problem specification, but with the nouns highlighted in boldface: • To play the video poker game: • A player deposits an arbitrary number of coins into the machine. This amount is the bankroll. • To play the game • • The player makes a bet (one to five coins but not more than the bankroll). A hand of five cards is dealt from a deck of 52 cards to the player. The deck is reshuffled for each game. The player decides which cards he/she wishes to hold. New cards are dealt for those cards that the player wishes to discard. The hand is scored. If the hand is a winner, the winning amount is added to the bankroll. Otherwise, the bet is deducted from the bankroll. • The player can quit and cash out at any time. • The player can continue to play as long as the bankroll is not depleted. • The player can add to the bankroll before any individual game.

Determine the Classes The following nouns serve as “class candidates. ”: – – – Determine the Classes The following nouns serve as “class candidates. ”: – – – video poker game player coins machine amount bankroll game bet hand card deck

Determine the Classes • Some words from this list are redundant. • For example, Determine the Classes • Some words from this list are redundant. • For example, amount and bankroll refer to the same thing. • A coin probably does not warrant a class of itself. • video poker game and game are identical. • Not all nouns will necessarily correspond to classes, and not all classes will always have a corresponding noun. • So, for now, we settle on seven classes: – – – – Player Bankroll Bet Hand Card Deck Poker. Game

Determine Responsibilities of Each Class • What service does a class provide? • What Determine Responsibilities of Each Class • What service does a class provide? • What is each class’s responsibility? • What are the actions and behaviors of each class? • As the nouns indicate classes, the verbs of the problem statement help determine class responsibilities. • Just as every noun does not correspond to a class, every verb does not necessarily designate a class action or responsibility.

Determine Responsibilities of Each Class To play the video poker game: – A player Determine Responsibilities of Each Class To play the video poker game: – A player deposits an arbitrary number of coins into the machine. This amount is the bankroll. – The player makes a bet (one to five coins but not more than the bankroll). – A hand of five cards is dealt from a deck of 52 cards to the player. – The deck is reshuffled for each game. – The player decides which cards he/she wishes to hold. – New cards are dealt for those cards that the player wishes to discard. – The hand is scored. – If the hand is a winner the winning amount is added to the bankroll otherwise the bet is deducted from the bankroll.

Determine Responsibilities of Each Class • The player can quit and cash out at Determine Responsibilities of Each Class • The player can quit and cash out at any time. • The player can continue to play as long as the bankroll is not depleted. • The player can add to the bankroll before any individual game.

The Player Class • What can a poker player do? A player can: – The Player Class • What can a poker player do? A player can: – Deposit coins (add to the bankroll). – Play the game. – Make a bet. – Decide which cards to hold/discard. – Cash out – Quit. – Play another game.

The Player Class • The actions of a player correspond to the buttons on The Player Class • The actions of a player correspond to the buttons on the machine. • Player provides the user interface. • The buttons on the machine and the actions of Player are a good match. • Each machine action lends itself to a method of the Player class. • Because Player serves as the user interface, confine all IO to the Player class. • Player is responsible for displaying the cards as well as any other appropriate output.

The Player Class Bet: – – Give (return) its value ( a getter method). The Player Class Bet: – – Give (return) its value ( a getter method). Set a value (a setter method). – – Shuffle the cards. Deal a card, i. e. , return one card. – – Return its suit ( a getter method). Return its value (a getter method). Deck: Card: Hand: – – Deal and store a new hand. Update a hand after the player discards. Score a hand. Return the hand, i. e. , return the list of cards in the hand. Bankroll: – – – Update the current number of coins in the machine, i. e. increase or decrease the number of coins. Return the number of coins in the machine (a getter method). Change the number of coins in the machine its (a setter method).

Poker. Game • Every poker game has a dealer who distributes the cards and, Poker. Game • Every poker game has a dealer who distributes the cards and, for the most part, coordinates play. • A Poker. Game object coordinates the action of our game. • The Poker. Game object requests a new hand from the Hand object and “deals” that hand to the Player object. • Poker. Game is the “middleman” between Player and the other classes.

Poker. Game • The actions of Poker. Game might be listed as: – Get Poker. Game • The actions of Poker. Game might be listed as: – Get a new hand from Hand. – Tell Player to display the hand ( all IO is via Player). – Get the list of discard/hold cards from Player. – Update the hand, i. e. , tell Hand which cards to hold and which to displace. – Score the hand, i. e. , get the score from Hand. – Tell Player to display the final results. – Update Bankroll when the game is finished.

Design Issues – The Data Model and the View • Good OOP design demands Design Issues – The Data Model and the View • Good OOP design demands the separation of the user interface, or the view, from the underlying representation of the data, or the data model. • According to the current design, all output is via the Player object. • A Hand does not know how to print itself. • It is the Player class that handles the user interface or view. • The separation of the view from the data model is a flexible design methodology.

Iterative Refinement • The responsibilities of Player might be reduced to the following four Iterative Refinement • The responsibilities of Player might be reduced to the following four actions: – – Deposit coins. Make a bet (start the game). Decide which cards to hold/discard. Quit (cash out). • The responsibility of displaying the cards as well as the number of remaining coins also falls to Player. • Player should provide some type of a menu that corresponds to the buttons on the machine.

Iterative Refinement • The behaviors of the Poker. Game fit into three groups of Iterative Refinement • The behaviors of the Poker. Game fit into three groups of actions that mimic the progression of a single game: • Get the initial hand: • get a new hand, tell Player to display the hand – – • • – – get the discard/hold cards from Player, update the hand, ( replace some cards) score a hand, tell Player to display the results Discard and hold cards: Update the bankroll The actions of Poker. Game are messages or requests sent to other objects. For example, to obtain a new hand, a Poker. Game object sends a request to a Hand object, which returns a hand of five cards. Remember, Poker. Game is the coordinator, the casino dealer.

Iterative Refinement Player Deposit or accept coins. Poker. Game Get the initial hand. Bet Iterative Refinement Player Deposit or accept coins. Poker. Game Get the initial hand. Bet Get the bet. Make a bet (starts game). Discard and hold cards. Set the bet. Decide which cards to hold/discard. Display a hand. Deck Shuffle the deck. Deal a card. Update the bankroll. Cash out. Display a menu. Card Get the suit. Get the rank. Get the name of a card. Hand Score a hand. Deal a new hand. Update a hand. Give the hand. Bankroll Get the bankroll. Set the bankroll. Change the bankroll. Classes and actions for video poker

Determine the Interactions Among the Classes • Objects interact with other objects by sending Determine the Interactions Among the Classes • Objects interact with other objects by sending messages to each other. • A message sent by object A to object B is a request for B to provide some service or information to A. • A Poker. Game object sends messages to other objects. • Poker. Game coordinates.

Determine the Interactions Among the Classes • Messages that one object might send to Determine the Interactions Among the Classes • Messages that one object might send to another during a video poker session: • Poker. Game: sends a message to Hand requesting a new hand of five cards, sends a message to Player requesting that Player display the hand to the user, sends a message to Player requesting the user's list of discarded cards, sends a message to Hand requesting an updated hand, sends a message to Player requesting that the new hand be displayed, sends a message to Hand requesting a score for the hand, sends a message to Player requesting that Player display the results, and sends a message to Bankroll updating the current number of coins. – – – – • – – – – – instantiates an initial Bankroll object, sends a message to the Bankroll object when coins are added, instantiates and initializes a Bet, instantiates a Poker. Game, sends a message to the Poker. Game requesting the initial hand, sends a message to Poker. Game indicating which cards to discard and which to hold, and sends a message to Bankroll requesting the final coin count. A Player : • Hand asks Deck to deal a new hand. • Deck requests five Card objects.

Determine the Interactions Among the Classes Interacting objects Determine the Interactions Among the Classes Interacting objects

Some Attributes • Every class consists of both attributes and behaviors. • For example, Some Attributes • Every class consists of both attributes and behaviors. • For example, Bet must store the number of coins that comprise the bet, so a Bet object should have an instance variable int bet • Hand cannot function without Deck, so Hand must include a Deck object to get its job done. • A standard deck of cards consists of 52 cards. • Deck requires an array of 52 Card references. • Every Card object should include two integer fields, rank and suit.

Some Attributes • Poker. Game is a bit more involved. • Poker. Game collaborates Some Attributes • Poker. Game is a bit more involved. • Poker. Game collaborates with Player, Bankroll and Hand. • Furthermore, to update the bankroll, Poker. Game also needs to know the amount of the current bet. • The Poker. Game class includes the following instance variables: – – Player player; Bet bet; Bankroll bankroll; Hand hand; • Player collaborates with Bankroll, Bet, and Poker. Game. • Player class includes instance variables: – – – Bankroll bankroll; Bet bet; Poker. Game pg;

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

The Bet Class 1. public class Bet 2. { 3. private int bet; 4. The Bet Class 1. public class Bet 2. { 3. private int bet; 4. public Bet() //default constructor sets bet to 0 5. { 6. bet = 0; 7. } 8. 9. public Bet(int n) //one-argument constructor, sets bet to n 10. { 11. bet = n; 12. } 13. 14. public void set. Bet(int n) //setter 15. { 16. bet = n; 17. } 18. 19. public int get. Bet() //getter 20. { 21. return bet; 22. } 23. }

Some Attributes • It is good practice to test each class before moving on Some Attributes • It is good practice to test each class before moving on to the next. • This class is not complex; so testing is very simple. • To test and subsequently debug the class, include a main(. . ) method that tests the methods of Bet. 1. public static void main(String[] args) 2. { 3. Scanner input = new Scanner(System. in); 4. System. out. print (“Enter an integer: “); 5. int n = input. next. Int(); 6. Bet bet 1 = new Bet(); // default constructor 7. System. out. println (“ Getter “+ bet 1. get. Bet()); 8. bet 1. set. Bet(n); // test setter 9. System. out. println (“After Setter “+ bet 1. get. Bet()); 10. Bet bet 2 = new Bet(n); // one argument constructor 11. System. out. println (“Getter; “+ bet 2. get. Bet()); 12. bet 2. set. Bet(n+10); // setter uses an expression 13. System. out. println (“Getter; “+ bet 1. get. Bet()); 14. }

The Card Class • The attributes of a Card object are suit and value The Card Class • The attributes of a Card object are suit and value (rank of a card). • These are both integer fields. • Because a Card object should return its name, a third method get. Name() returns the name of a card as a string such as “ 2 of Spades” or “Queen of Hearts. ” • The two-argument constructor, public Card (int suit, int value) is normally used to create a new card. • There is also a default constructor that creates a Card object initialized as the “Ace of Hearts. ”

The Card Class 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. The Card Class 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. public class Card { private int suit; //1= Hearts, 2= Diamonds, 3=Clubs, 4 = Spades private int value; //1 = Ace… 11 = Jack, 12 = Queen, 13 = King public Card() //Ace of Hearts, by default { suit = 1; value = 1; } public Card(int s, int v) { suit = s; value = v; } public int get. Suit() { return suit; } public int get. Value() { return value; } public void set. Suit(int s) { suit = s; } 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. public void set. Value(int v) { value = v; } public String get. Name() // returns string, e. g. , "Ace of Hearts" { String name = ""; if (value == 1) name = "Ace of "; else if (value == 11) name = "Jack of "; else if ( value == 12) name = "Queen of "; else if (value == 13) name = "King of "; else // use the numerical value name = value + " of "; 50. // Add on the suit 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. if (suit == 1) name += "Hearts"; else if ( suit == 2) name += "Diamonds"; else if ( suit == 3) name += "Clubs"; else name += "Spades"; return name; } }

The Card Class • Testing the getter and setter methods is straightforward. To test The Card Class • Testing the getter and setter methods is straightforward. To test the get. Name() method include a loop that tests each card : 1. for (int s = 1; s <= 4; s++) // 4 suits 2. for (int val = 1; val <= 13; val++) // 13 cards per suit 3. { 4. Card cd = new Card(s, val); 5. System. out. println( s+ ", "+ val+ ": "+ cd. get. Name()); 6. }; • 1. 2. 3. 4. 5. 6. Alternatively, a segment that prompts for a suit and rank and displays the name of the corresponding card: System. out. print (“Suit: “); int s = input. next. Int(); System. out. print(“Value: “); int val = input. next. Int(); Card cd = new Card(s, val); System. out. println( s+ ", "+ val+ ": "+ cd. get. Name());

The Bankroll Class 1. 2. 3. public class Bankroll { private int bankroll; 4. The Bankroll Class 1. 2. 3. public class Bankroll { private int bankroll; 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. public Bankroll() // default constructor { bankroll = 0; } public Bankroll (int n) // one-argument constructor { bankroll = n; } public int get. Bankroll() { return bankroll; } public void set. Bankroll(int n) { bankroll = n; } public void alter. Bankroll(int n) // n can be negative { bankroll+= n; } }

The Deck Class • The only instance variable of the Deck class is an The Deck Class • The only instance variable of the Deck class is an array of 52 Card references. • The methods of the class are: – – deal a card, and shuffle the deck. • A skeletal version of Deck is: 1. public class Deck 2. { 3. Card[] deck; // array of 52 Card references 4. public Deck() 5. { 6. //instantiate and populate a deck 7. } 8. public void shuffle() 9. { 10. //rearrange deck 11. } 12. public Card deal() 13. { 14. //return the "next" card in deck 15. } 16. }

The Deck Class The Constructor: • The cards of a deck are numbered from The Deck Class The Constructor: • The cards of a deck are numbered from 1 to 52, so use an array of size 53, ignoring position 0. • To initialize deck, use a loop: 1. 2. 3. 4. 5. 6. 7. for (int rank =1; i <= 13; i++) // for each rank Ace. . . King { // place cards in order in deck[rank] = new Card(1, rank); // first suit; deck[rank+13] = new Card(2, rank); // second suit; deck[rank+26] = new Card(3, rank); // third suit; deck[rank+39] = new Card(4, rank); // fourth suit }

The Deck Class • The shuffle() method may not be as obvious as the The Deck Class • The shuffle() method may not be as obvious as the other methods: for card = 1 to 52 generate a random integer, rand, in the range 1 through 52. swap deck[card] with deck[rand]. • Written in Java: 1. 2. 3. 4. 5. 6. public void shuffle() { Random random. Number = new Random(); for (int card = 1; card<= 52; card++) { // find a random place in the deck int rand = random. Number. next. Int(52) + 1; // integer between 1 and 52, inclusive 7. 8. 9. 10. 11. } 12. } //swap deck[card] with deck[rand] Card temp = deck[card]; deck[card] = deck[rand]; deck[rand] = temp;

The Deck Class • deal(): Include another instance variable: int next; that holds the The Deck Class • deal(): Include another instance variable: int next; that holds the index of the next card. • This attribute should be initialized to 1. • The variable next must also be reset to 1 whenever the deck is shuffled. • So both the constructor and shuffle() must be adjusted.

The Deck Class • The entire class follows: 1. 2. 3. 4. 5. 6. The Deck Class • The entire class follows: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. public class Deck { private Card deck[]; private int next; // holds position of next card to be dealt public Deck() { deck =new Card[53]; // does not use position 0, uses 1… 52 for (int rank=1; rank <= 13; rank++) { // place cards in order in deck deck[rank] = new Card(1, rank); // "rank of first suit" e. g. 3 of hearts deck[rank+13] = new Card(2, rank); // rank of second suit e. g. 3 of diamonds deck[rank+26] = new Card(3, rank); // "rank of third suit" e. g 3 of clubs deck[rank+39] = new Card(4, rank); // "rank of fourth suit" e. g. 3 of spades } next = 1; // first card dealt is deck[next] } public void shuffle() { Random random. Number = new Random(); for (int card = 1; card <= 52; card++) { // find a random place in the deck int rand= random. Number. next. Int(52) + 1; //swap deck[card] with deck[rand] Card temp = deck[card]; deck[card] = deck[rand]; deck[rand] = temp; } next = 1; // top card of the deck } public Card deal() { if ( next > 52) // if deck is depleted shuffle(); Card c = deck[next]; next++; return c; } }

The Hand Class • Model a poker hand with an array of five Card The Hand Class • Model a poker hand with an array of five Card references. • Each time that Hand requires a new Card, Hand sends a request to Deck. • The instance variables of Hand are: – – Card[] hand; // holds 5 Card references Deck deck;

The Hand Class • The default constructor is simple and does no more than The Hand Class • The default constructor is simple and does no more than instantiate the instance variables: 1. public Hand() 2. { 3. hand = new Card[5]; 4. deck = new Deck(); 5. } // 5 cards per hand • The new. Hand() method creates deals a five-card hand. • The deck is first shuffled: 1. public void new. Hand() 2. { 3. deck. shuffle(); // a message to deck 4. for ( int i = 0; i < 5; i++) 5. hand[i] = deck. deal(); // request one card from deck 6. }

The Hand Class • Hand should also have a getter method that returns some The Hand Class • Hand should also have a getter method that returns some representation of a hand. • This implementation uses a String represenation: 1. public String[] get. Hand() 2. { 3. String[] cards. In. Hand = new String[5]; 4. for (int i = 0; i < 5; i++) 5. cards. In. Hand[i] = cards[i]. get. Name(); 6. return cards. In. Hand; 7. } • Hand does more than store an array of Card references. • Hand sends a message (get. Name()) to Card.

The Hand Class • Update. Hand(. . . ): • To update or revise The Hand Class • Update. Hand(. . . ): • To update or revise a poker hand, a Hand object must know those cards that the player wishes to discard and replace. • Poker. Game, in the role of game coordinator, queries Player for the discards and communicate this information to Hand. • We choose to send this data from Poker. Game to Hand as a boolean array parameter: boolean[] keep such that if keep[i] == false, the ith card of the hand must be replaced.

The Hand Class A player chooses to replace two cards and hold three. This The Hand Class A player chooses to replace two cards and hold three. This information is passed to Update. Hand() in the boolean array keep[].

The Hand Class • The code for update. Hand() follows: 1. 2. 3. 4. The Hand Class • The code for update. Hand() follows: 1. 2. 3. 4. 5. 6. public void update. Hand(boolean keep[]) { for( int i = 0; i < 5; i++) if (!keep[i]) hand[i] = deck. deal(); }

The Hand Class evaluate. Hand(): • Method determines whether or not a particular hand The Hand Class evaluate. Hand(): • Method determines whether or not a particular hand is a winner. • When a player is dealt a hand of cards, he/she usually arranges or sorts the cards. • Seeing the cards arranged in order makes it easier to recognize a winning hand. • Include a sort() method that orders a hand based on rank.

The Hand Class • One type winning hand is a flush. • A flush The Hand Class • One type winning hand is a flush. • A flush is a hand in which all five cards have the same suit. • We number the suits 1 to 4 and arbitrarily assign 1 to hearts, 2 to diamonds, 3 to clubs and 4 to spades. • Accordingly, we can keep track of the number of hearts, diamonds, clubs, and spades with an array suits[] such that: – – suits[1] holds the number of hearts, suits[2] holds the number of diamonds, suits[3] holds the number of clubs, and suits[4] holds the number of spades. • Since we have numbered the suits 1 to 4, we do not use suits[0]. • If for any i, suits[i] has the value 5, the hand is a flush.

The Hand Class (a) The suits[] array: 1 Heart, 2 Diamonds, 1 Club, and The Hand Class (a) The suits[] array: 1 Heart, 2 Diamonds, 1 Club, and 1 Spade (b) A flush: suits[1] == 5

The Hand Class • Several winning hands are comprised of two, three, or four The Hand Class • Several winning hands are comprised of two, three, or four cards of the same value or rank. • Use an integer array values[] such that values[i] holds the number of cards dealt with rank i. • For example: – – – – values[1] holds the number of Aces, values[2] holds the number of 2's, values[3] holds the number of 3's, … values[11] holds the number of Jacks, values[12] holds the number of Queens, and values[13] holds the number of Kings. • Do not use values[0] since no card has value 0.

The Hand Class The array values[] shows 1 two, 2 sevens, 1 ten, and The Hand Class The array values[] shows 1 two, 2 sevens, 1 ten, and 1 queen

The Hand Class • Using values[], it is easy to discern whether or not The Hand Class • Using values[], it is easy to discern whether or not a hand holds two pair, four of a kind, or a full house. • For example, if values[2] = 3 and values[7] = 2, then the hand is a full house consisting of 3 Twos and 2 Sevens. • Thus to implement evaluate. Hand() we need: • A helper function: void sort() that sorts a hand based on the ranks of the cards, and • two instance variables: int[] suits and int[] values that store information about a hand.

The Hand Class • The following revised implementation of Hand includes these arrays and The Hand Class • The following revised implementation of Hand includes these arrays and also a sort() method. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. class Hand { private Card[] cards; private Deck deck; private int suits[]; // holds the number of each suit in a hand private int values[]; // holds the number of each type card (A, 2, 3, 4, . . . K) public Hand() { cards = new Card[5]; suits = new int[5]; // uses indices 1. . 4 values = new int[14]; // uses indices 1. . 13 deck = new Deck(); } public void new. Hand() { deck. shuffle(); for (int i = 0; i < 5; i++) { cards[i] = deck. deal(); suits[cards[i]. get. Suit()]++ ; values[cards[i]. get. Value()]++; } sort(); } 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. public void update. Hand(boolean[] x) { for (int i = 0; i < 5; i++) if ( !x[i]) { // remove card data for card i suits[cards[i]. get. Suit()]-- ; values[cards[i]. get. Value()]--; // get a new card cards[i] = deck. deal(); // update data for card i suits[cards[i]. get. Suit()]++ ; values[cards[i]. get. Value()]++; } sort(); } public String[] get. Hand() { String[] cards. In. Hand = new String[5]; for (int i = 0; i < 5; i++) cards. In. Hand[i] = cards[i]. get. Name(); return cards. In. Hand; } private void sort() // orders cards by value field; a helper function { int max; // holds the position of the highest valued card for (int place = 4; place > 0; place--) { max = 0; // find the position of the highest valued card between 0 and place // the position of the high card is stored in max for (int i = 1; i <= place; i++) if ( cards[i]. get. Value() > cards[max]. get. Value()) max = i; // swap the highest valued card with the card in position place Card temp = cards[place]; cards[place] = cards[max]; cards[max] = temp; } } }

The Hand Class • The additions to the previous Hand class are: • Lines The Hand Class • The additions to the previous Hand class are: • Lines 5 and 6 contain declarations for the instance variable suits[] and values[]. • Lines 10 and 11 (in the constructor) instantiate suits[] and values[]. • Lines 20 and 21 update the arrays for each card dealt to a new hand. • Lines 31 and 32 update the arrays when a card is discarded. • Lines 36 and 37 update the arrays when a discarded card is replaced. • Lines 48 through 64 implement a standard sort method called selection sort. • The method arranges the array hand[] according to rank (retrieved by the get. Value() method on line 57). • The sort() method is a helper method that has private access. • Thus, sort() is not visible outside the Hand class; only the methods of Hand can invoke sort().

The Hand Class • Rather than create one gigantic method that checks each winning The Hand Class • Rather than create one gigantic method that checks each winning hand, implement nine smaller boolean methods: • boolean royal. Flush(); // returns true if a hand is a royal flush • boolean straight. Flush(); // returns true if a hand is a straight flush • boolean four. Of. AKind(); // returns true if a hand is four of a kind • etc.

The Hand Class • Each method checks for one particular type of hand so The Hand Class • Each method checks for one particular type of hand so that evaluate. Hand() has the following structure: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. Type. Of. Hand evaluate. Hand() { if (royal. Flush()) // if the hand is a royal flush return Royal Flush; else if (straight. Flush()) //else if the hand is a straight flush return Straight Flush; else if (four. Of. AKind()) //else if the hand is four of a kind return Four of A Kind; else if (full. House()) //else if the hand is a full house return Full House; else if (flush()) //else if the hand is a flush return Flush; else if (straight()) //else if the hand is a straight return Straight; else if (three. Of. AKind()) //else if the hand is three of a kind return Three of a Kind else if (two. Pair()) //else if the hand is two pair return Two Pair; else if (pair()) //else if the hand is a pair of Jacks or better return Pair of Jacks or Better; return Losing Hand; // otherwise, a losing hand }

The Hand Class • The return type of the previous algorithm is Type. Of. The Hand Class • The return type of the previous algorithm is Type. Of. Hand, which is not a defined type. • Define Type. Of. Hand as the payout associated with each hand. • If a hand is a royal flush, evaluate. Hand() returns 250, since a royal flush pays 250 to 1; . • If a hand is a straight flush, evaluate. Hand() returns 50, etc. • A losing hand returns -1.

The Hand Class • The payout uniquely identifies the hand can also be used The Hand Class • The payout uniquely identifies the hand can also be used to calculate a player’s winnings. evaluate. Hand() is implemented as: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. public int evaluate. Hand() // returns the payout for each hand { if (royal. Flush()) // royal flush pays 250 to 1 return 250; else if (straight. Flush()) // straight flush pays 50 to 1 return 50; else if (four. Of. AKind()) // four of a kind plays 25 to 1 return 25; else if (full. House()) // full house pays 9 to 1 return 9; else if (flush()) // flush pays 6 to 1 return 6; else if (straight()) // straight pays 4 to 1 return 4; else if (three. Of. AKind()) // three of a kind pays 3 to 1 return 3; else if (two. Pair()) // two pair pays 2 to 1 return 2; else if (pair()) // Jacks or better // pair of Jacks or better pays 1 to 1 return 1; return -1; // losing hand }

The Poker. Game Class • The attributes of Poker. Game are references to Bankroll, The Poker. Game Class • The attributes of Poker. Game are references to Bankroll, Bet, Hand, and Player. • Because Poker. Game passes the list of discarded cards to Hand, Poker. Game also maintains a boolean array indicating those cards that are to be discarded and those retained.

The Poker. Game Class • Below is a first iteration of Poker. Game that The Poker. Game Class • Below is a first iteration of Poker. Game that includes declarations and a constructor: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. public class Poker. Game { private Bankroll bankroll; private Bet bet; private Hand hand; private Player player; private boolean[] hold. Cards; public Poker. Game(Bet coins. Bet, Bankroll br, Player pl) { bankroll = br; bet = coins. Bet; player = pl; hand = new Hand(); hold. Cards = new boolean[5]; } public int update. Bankroll(int payout) { // alters the bankroll and returns the total winnings } public void view. Initial. Hand() { // deals the first hand } public void discard. Or. Hold. Cards() { //gets discards and a new hand }

The Poker. Game Class 1. 2. 3. 4. 5. 6. int update. Bankroll(int payout) The Poker. Game Class 1. 2. 3. 4. 5. 6. int update. Bankroll(int payout) { int winnings = payout * (bet. get. Bet()); // negative for a loss bankroll. alter. Bankroll(winnings); return winnings; } 1. 2. 3. 4. 5. public void view. Initial. Hand() { hand. new. Hand(); // send a message to hand, instantiate a hand player. display. Hand(hand. get. Hand()); // tell player to display the new hand } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. public void discard. Or. Hold. Cards() { player. get. Discard(hold. Cards); //ask player for the discard list hand. update. Hand(hold. Cards); // passes discards to hand updates itself player. display. Hand(hand. get. Hand()); // tell player to show the (revised) hand int payout = hand. evaluate. Hand(); // tell hand to evaluate itself and return the payout int winnings = update. Bankroll(payout); //update the bankroll, a Poker. Game method player. display. Results(payout, winnings); // tell player to display outcome of the game }

The Player Class 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. The Player Class 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. public class Player { private Scanner input; Bankroll bankroll; Poker. Game poker. Game; Bet bet; Hand hand; Player() { input = new Scanner(System. in); bankroll = new Bankroll(); bet = new Bet(); } 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. void get. Initial. Bankroll() // queries the user for the initial bankroll { int num. Coins; do { System. out. print("How many coins do you wish to insert into the machine: "); num. Coins = input. next. Int(); }while (num. Coins <= 0); System. out. println(); bankroll, set. Bankroll(num. Coins); } 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. void add. Coins() // adds more coins to the machine { int num. Coins; do { System. out. print("How many coins do you wish to insert into the machine: "); num. Coins = input. next. Int(); } while (num. Coins <= 0); bankroll. alter. Bankroll(num. Coins); System. out. println("Currently you have "+ bankroll. get. Bankroll()+ " coins"); System. out. println(); } public void bet. And. Play() // get the bet and play the game { int coins; do { System. out. print("Enter a bet: 1 to 5 coins: "); coins = input. next. Int(); } while (coins <=0 || coins > 5 || coins > bankroll. get. Bankroll()); bet. set. Bet(coins); poker. Game = new Poker. Game(bet, bankroll, this); poker. Game. view. Initial. Hand(); poker. Game. discard. Or. Hold. Cards(); }

The Player Class 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. The Player Class 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. public void display. Hand(String[] hand. String) { // five card hand is passed as a String[5] array System. out. println("***** Your Hand`1 *****"); for(int i = 0; i < 5; i++) System. out. println((i+1) +". "+hand. String[i]); System. out. println("****************"); System. out. println(); } public void get. Discard(boolean[] x) { String ans; System. out. println("Hold or discard? "); for (int i = 0; i < 5; i++) { do { System. out. print("Hold (h) or Discard (d) card number "+(i+1)+": "); ans = input. next(); if (ans. equals("h") ) x[i] = true; // hold else if (ans. equals("h") ) x[i] = false; // discard }while (!(ans. equals("h") || ans. equals("d"))); } System. out. println(); } 82. public void display. Results(int payout, int winnings) 83. { 84. String name. Of. Hand = "Lose"; 85. if (payout == 250) 86. name. Of. Hand = "Royal Flush"; 87. else if (payout == 50) 88. name. Of. Hand = "Straight Flush"; 89. else if (payout == 25) 90. name. Of. Hand = "Four of a Kind"; 91. else if (payout == 9) 92. name. Of. Hand = "Full House"; 93. else if (payout == 6) 94. name. Of. Hand = " Flush"; 95. else if (payout == 4) 96. name. Of. Hand = "Straight "; 97. else if (payout == 3) 98. name. Of. Hand = "Three of a Kind"; 99. else if (payout == 2) 100. name. Of. Hand = "Two Pair"; 101. else if (payout == 1) 102. name. Of. Hand = " Pair of Jacks or Better"; 103. if (winnings >0 ) 104. { 105. System. out. println("Winner: "+ name. Of. Hand); 106. System. out. println("Payout is "+ winnings + " coins. "); 107. } 108. else 109. System. out. println("You lost your bet of "+ bet. get. Bet()); 110. System. out. println("Current Bankroll is " + bankroll. get. Bankroll()); 111. System. out. println(); 112. }

The Player Class 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. The Player Class 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. public void quit() { int br = bankroll. get. Bankroll(); System. out. println("n******Game Over****** n"); if (br > 0) System. out. println("Returned: "+br+" coin(s)"); else System. out. println("No coins remain"); System. out. println("n***********"); } public void menu() { String choice; do { System. out. println("Choose"); System. out. println("1: Make a bet and play poker"); System. out. println("2: Add coins to the machine "); System. out. println("3: Cash out and quit"); System. out. print("Your choice: "); choice = input. next(); if (choice. equals("1")) bet. And. Play(); else if (choice. equals("2")) add. Coins(); }while ((!(choice. equals("3") ) && bankroll. get. Bankroll() >0)); } 141. 142. 143. 144. 145. 146. 147. 148. public static void main(String[] args) { Player player= new Player(); player. get. Initial. Bankroll(); player. menu(); player. quit(); } }

The Complete Application //////////Bet. java////////// 1. import java. util. *; 2. public class Bet The Complete Application //////////Bet. java////////// 1. import java. util. *; 2. public class Bet 3. { 4. private int bet; 5. public Bet() //default constructor sets bet to 0 6. { 7. bet = 0; 8. } 9. 10. public Bet(int n) //one-argument constructor, sets bet to n 11. { 12. bet = n; 13. } 14. 15. public void set. Bet(int n) //setter 16. { 17. bet = n; 18. } 19. 20. public int get. Bet() //getter 21. { 22. return bet; 23. } 24. } //////////Bankroll. java////////// 25. public class Bankroll 26. { 27. private int bankroll; 28. 29. public Bankroll() // default constructor 30. { 31. bankroll = 0; 32. } 33. 34. public Bankroll (int n) // one-argument constructor 35. { 36. bankroll = n; 37. } 38. 39. public int get. Bankroll() 40. { 41. return bankroll; 42. } 43. public void alter. Bankroll(int n) // n can be negative 44. { 45. bankroll+= n; 46. } 47. }

The Complete Application //////////Card. java////////// 48. 49. 50. 51. 52. 53. 54. 55. 56. The Complete Application //////////Card. java////////// 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. public class Card { private int suit; //1= Hearts, 2= Diamonds, 3=Clubs, 4 = Spades private int value; //1 = Ace… 11 = Jack, 12 = Queen, 13 = King public Card() //Ace of Hearts, by default { suit = 1; value = 1; } public Card(int s, int v) { suit = s; value = v; } public int get. Suit() { return suit; } public int get. Value() { return value; } public void set. Suit(int s) { suit = s; } public void set. Value(int v) { value = v; } 84. public String get. Name() // returns string, e. g. , "Ace of Hearts" 85. { 86. String name = ""; 87. if (value == 1) 88. name = "Ace of "; 89. else if (value == 11) 90. name = "Jack of "; 91. else if ( value == 12) 92. name = "Queen of "; 93. else if (value == 13) 94. name = "King of "; 95. else // use the numerical value 96. name = value + " of "; 97. 98. // Add on the suit onto the name 99. 100. if (suit == 1) 101. name += "Hearts"; 102. else if ( suit == 2) 103. name += "Diamonds"; 104. else if ( suit == 3) 105. name += "Clubs"; 106. else 107. name += "Spades"; 108. return name; 109. } 110. }

The Complete Application //////////Deck. java////////// 111. 112. 113. 114. 115. 116. 117. 118. 119. The Complete Application //////////Deck. java////////// 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. import java. util. *; // for Random public class Deck { private Card deck[]; private int next; // holds position of next card to be dealt public Deck() { deck =new Card[53]; // does not use position 0, uses 1. . 52 for (int rank=1; rank <= 13; rank++) { // place cards in order in deck deck[rank] = new Card(1, rank); // "rank of first suit" e. g. 3 of hearts deck[rank+13] = new Card(2, rank); // rank of second suit e. g. 3 of diamonds deck[rank+26] = new Card(3, rank); // "rank of third suit" e. g 3 of clubs deck[rank+39] = new Card(4, rank); // "rank of fourth suit" e. g. 3 of spades } next = 1; } public void shuffle() { Random random. Number = new Random(); for (int card = 1; card <= 52; card++) { // find a random place in the deck int rand= random. Number. next. Int(52) + 1; //swap deck[i] with deck[m] Card temp = deck[card]; deck[card] = deck[rand]; deck[rand] = temp; } next = 1; // top card of the deck } 145. public Card deal() 146. { 147. if ( next > 52) // if deck is depleted 148. shuffle(); 149. Card c = deck[next]; 150. next++; 151. return c; 152. } 153. } 154.

The Complete Application //////////Hand. java////////// 155. public class Hand 156. { 157. private Card[] The Complete Application //////////Hand. java////////// 155. public class Hand 156. { 157. private Card[] cards; 158. private Deck deck; 159. private int suits[]; // holds the number of each suit in a hand 160. private int values[]; // holds the number of each type card (A, 2, 3, 4, . . . K) 161. 162. public Hand() 163. { 164. cards = new Card[5]; 165. suits = new int[5]; // uses indices 1. . 4 166. values = new int[14]; // uses indices 1. . 13 167. deck = new Deck(); 168. } 169. 170. public void new. Hand() 171. { 172. deck. shuffle(); 173. for (int i = 0; i < 5; i++) 174. { 175. cards[i] = deck. deal(); 176. suits[cards[i]. get. Suit()]++ ; 177. values[cards[i]. get. Value()]++; 178. } 179. sort(); 180. } 181. public void update. Hand(boolean[] x) 182. { 183. for (int i = 0; i < 5; i++) 184. if ( !x[i]) 185. { 186. // remove card data for card i 187. suits[cards[i]. get. Suit()]-- ; 188. values[cards[i]. get. Value()]--; 189. // get a new card 190. cards[i] = deck. deal(); 191. // update data for card i 192. suits[cards[i]. get. Suit()]++ ; 193. values[cards[i]. get. Value()]++; 194. } 195. sort(); 196. } 197. public String[] get. Hand() 198. { 199. 200. String[] cards. In. Hand = new String[5]; 201. for (int i = 0; i < 5; i++) 202. cards. In. Hand[i] = cards[i]. get. Name(); 203. return cards. In. Hand; 204. } 205. 206. private void sort() // orders cards by value field; a helper function 207. { 208. int max; // holds the position of the highest valued card 209. for (int place = 4; place > 0; place--) 210. { 211. max = 0; 212. // find the position of the highest valued card between 0 ans place 213. // the position of the high card is stored in max 214. for (int i = 1; i <= place; i++) 215. if ( cards[i]. get. Value() > cards[max]. get. Value()) 216. max = i; 217. // swap the highest wlaued card with the card in position place 218. Card temp = cards[place]; 219. cards[place] = cards[max]; 220. cards[max] = temp; 221. } 222. }

The Complete Application 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. The Complete Application 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. public int evaluate. Hand() { if (royal. Flush()) // royal flush pays 250: 1 return 250; else if (straight. Flush()) // straight flush pays 50: 1 return 50; else if (four. Of. AKind()) // four of a kind return 25; // four of a kind pays 25: 1 else if (full. House()) // full house return 9; else if (flush()) return 6; else if (straight()) return 4; else if (three. Of. AKind()) // three of a kind return 3; else if (two. Pair()) return 2; else if (pair()) // Jacks or better return 1; return -1; // losing hand } private boolean royal. Flush() { //10, J, Q, K, A of the same suit boolean same. Suit= false; // true if all same suit boolean is. Royalty= false; // true if cards are 10, J, K, Q, A for(int i = 1; i <=4; i++) if (suits[i] == 5) // all five cards of one suit? same. Suit = true; is. Royalty = (values[1] == 1 && values[10] ==1 && values[11] ==1 && values[12] == 1 && values[13] == 1); //one Ace && one 10 && one J &&one Q&&one K return (same. Suit && is. Royalty); // true if both conditions are true } 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. private boolean straight. Flush() { boolean same. Suit = false; boolean ranks. In. Order = false; for(int i = 1; i <=4; i++) if (suits[i] == 5) same. Suit = true; // same suit? // cards in sequence? ranks. In. Order = cards[1]. get. Value() == (cards[0]. get. Value() + 1) && cards[2]. get. Value() == (cards[0]. get. Value() + 2) && cards[3]. get. Value() == (cards[0]. get. Value() + 3) && cards[4]. get. Value() == (cards[0]. get. Value() + 4); return (same. Suit && ranks. In. Order); } private boolean flush() { for(int i = 1; i <=4; i++) if (suits[i] == 5) // all the same suit? return true; return false; } private boolean four. Of. AKind() { for(int i =1 ; i <= 13; i++) if (values[i] == 4) return true; return false; } 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. private boolean full. House() { boolean three= false; boolean two= false; for(int i =1 ; i <= 13; i++) if (values[i] == 3) // three of one kind three= true; else if (values[i] ==2) // two of another kind two = true; return two && three; // both conditions }

The Complete Application 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. The Complete Application 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. private boolean straight() { // cards in sequence? return // Ace precedes 2 (cards[1]. get. Value() == (cards[0]. get. Value() + 1) && cards[2]. get. Value() == (cards[0]. get. Value() + 2) && cards[3]. get. Value() == (cards[0]. get. Value() + 3) && cards[4]. get. Value() == (cards[0]. get. Value() + 4)) || //Ace follows King (values[1] == 1 && //Ace values[10] ==1 && //Ten values[11]==1 && //Jack values[12] == 1 && //Queen values[13] == 1); //King } private boolean three. Of. AKind() { for(int i =1 ; i <= 13; i++) if (values[i] == 3) return true; return false; } private boolean two. Pair() { int count = 0; for( int i = 1; i <= 13; i++) if(values[i] == 2) // count the number of pairs count++; return (count == 2); } private boolean pair() // Jacks or Higher { if (values[1] == 2) //pair of aces return true; for( int i = 11; i <= 13; i++) // pair of Jacks or higher if(values[i] ==2) return true; return false; } } 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382. 383. 384. 385. 386. /////////Poker. Game. java////////// public class Poker. Game { private Bankroll bankroll; private Bet bet; private Hand hand; private Player player; private boolean[] hold. Cards; public Poker. Game(Bet coins. Bet, Bankroll br, Player pl) { bankroll = br; bet = coins. Bet; player = pl; hand = new Hand(); hold. Cards = new boolean[5]; } int update. Bankroll(int payoff) { int winnings = payoff*(bet. get. Bet()); // negative for a loss bankroll. alter. Bankroll(winnings); return winnings; } public void view. Initial. Hand() { hand. new. Hand(); player. display. Hand(hand. get. Hand()); } public void discard. Or. Hold. Cards() { player. get. Discard(hold. Cards); hand. update. Hand(hold. Cards); player. display. Hand(hand. get. Hand()); int payoff = hand. evaluate. Hand(); int winnings = update. Bankroll(payoff); player. display. Results(payoff, winnings); // the hand & the number of coins won(lost) } }

The Complete Application 387. 388. 389. 390. 391. 392. 393. 394. 395. 396. 397. The Complete Application 387. 388. 389. 390. 391. 392. 393. 394. 395. 396. 397. 398. 399. 400. 401. 402. 403. 404. 405. 406. 407. 408. 409. 410. 411. 412. 413. 414. 415. 416. 417. 418. 419. 420. 421. 422. 423. 424. 425. 426. 427. //////////Player. java////////// import java. util. *; public class Player { private Scanner input; Bankroll bankroll; Poker. Game poker. Game; Bet bet; Hand hand; Player() { input = new Scanner(System. in); } void get. Initial. Bankroll() { int num. Coins; do { System. out. print("How many coins do you wish to insert into the machine: "); num. Coins = input. next. Int(); }while (num. Coins <= 0); System. out. println(); bankroll = new Bankroll(num. Coins); } void add. Coins() { int num. Coins; do { System. out. print("How many coins do you wish to insert into the machine: "); num. Coins = input. next. Int(); } while (num. Coins <= 0); bankroll. alter. Bankroll(num. Coins); System. out. println("Currently you have "+ bankroll. get. Bankroll()+ " coins"); System. out. println(); } 428. 429. 430. 431. 432. 433. 434. 435. 436. 437. 438. 439. 440. 441. 442. 443. 444. 445. 446. 447. 448. 449. 450. 451. 452. 453. 454. 455. 456. 457. 458. 459. 460. 461. 462. 463. 464. 465. 466. 467. 468. 469. 470. public void bet. And. Play() { int coins; do { System. out. print("Enter a bet: 1 to 5 coins: "); coins = input. next. Int(); } while (coins <=0 || coins > 5 || coins > bankroll. get. Bankroll()); bet = new Bet(coins); poker. Game = new Poker. Game(bet, bankroll, this); poker. Game. view. Initial. Hand(); poker. Game. discard. Or. Hold. Cards(); } public void display. Hand(String[] hand. String) { System. out. println("***** Your Hand *****"); for(int i = 0; i < 5; i++) System. out. println((i+1) +". "+hand. String[i]); System. out. println("****************"); System. out. println(); } public void get. Discard(boolean[] x) { String ans; System. out. println("Hold or discard? "); for (int i = 0; i < 5; i++) { do { System. out. print("Hold (h) or Discard (d) card number "+(i+1)+": "); ans = input. next(); if (ans. equals("h") ) x[i] = true; // hold else if (ans. equals("h") ) x[i] = false; // discard }while (!(ans. equals("h") || ans. equals("d"))); } System. out. println(); }

The Complete Application 471. 472. 473. 474. 475. 476. 477. 478. 479. 480. 481. The Complete Application 471. 472. 473. 474. 475. 476. 477. 478. 479. 480. 481. 482. 483. 484. 485. 486. 487. 488. 489. 490. 491. 492. 493. 494. 495. 496. 497. 498. 499. 500. 501. 502. 503. 504. 505. 506. 507. 508. 509. 510. 511. 512. 513. public void display. Results(int payoff, int winnings) { String name. Of. Hand = "Lose"; if (payoff == 250) name. Of. Hand = "Royal Flush"; else if (payoff == 50) name. Of. Hand = "Straight Flush"; else if (payoff == 25) name. Of. Hand = "Four of a Kind"; else if (payoff == 9) name. Of. Hand = "Full House"; else if (payoff == 6) name. Of. Hand = " Flush"; else if (payoff == 4) name. Of. Hand = "Straight "; else if (payoff == 3) name. Of. Hand = "Three of a Kind"; else if (payoff == 2) name. Of. Hand = "Two Pair"; else if (payoff == 1) name. Of. Hand = " Pair of Jacks or Better"; if (winnings >0 ) { System. out. println("Winner: "+ name. Of. Hand); System. out. println("Payoff is "+ winnings + " coins. "); } else System. out. println("You lost your bet of "+ bet. get. Bet()); System. out. println("Current Bankroll is " + bankroll. get. Bankroll()); System. out. println(); } public void quit() { int br = bankroll. get. Bankroll(); System. out. println("n******Game Over****** n"); if (br > 0) System. out. println("Returned: "+br+" coin(s)"); else System. out. println("No coins remain"); System. out. println("n***********"); } 514. 515. 516. 517. 518. 519. 520. 521. 522. 523. 524. 525. 526. 527. 528. 529. 530. 531. 532. 533. 534. 535. 536. 537. 538. 539. public void menu() { String choice; do { System. out. println("Choose"); System. out. println("1: Make a bet and play poker"); System. out. println("2: Add coins to the machine "); System. out. println("3: Cash out and quit"); System. out. print("Your choice: "); choice = input. next(); if (choice. equals("1")) bet. And. Play(); else if (choice. equals("2")) add. Coins(); }while ((!(choice. equals("3") ) && bankroll. get. Bankroll() >0)); } public static void main(String[] args) { Player player= new Player(); player. get. Initial. Bankroll(); player. menu(); player. quit(); } }