Question 2: Classes

Part A: Write the complete HiddenWord class, including any necessary instance variables, its constructor, and the method, getHint, described above. You may assume that the length of the guess is the same as the length of the hidden word.

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class PokerHand {
    private String[] deck = {
        "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
        "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
        "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS",
        "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC"
    };
    private Set<String> drawnCards = new HashSet<>(); // keeps track of drawn cards
    private String[] hand = new String[5]; // stores poker hand
    private Random rand = new Random(); // random number generator

    // constructor for random hand
    public PokerHand() {
        generateHand();
    }

    // right here actually generates the poker hand
    private void generateHand() {
        int drawn = 0;
        while (drawn < hand.length) {
            String card = deck[rand.nextInt(deck.length)]; // picks a random card from the deck
            if (drawnCards.add(card)) { // iff the card hasn't been drawn before, add it to the hand
                hand[drawn++] = card;
            }
        }
    }

    // method to display the current poker hand
    public void displayHand() {
        System.out.println("Your hand:");
        for (String card : hand) {
            System.out.println(card);
        }
    }

    // method to check if the current hand is a flush
    public boolean isFlush() {
        char suit = hand[0].charAt(hand[0].length() - 1); // Get the suit of the first card
        for (String card : hand) {
            if (card.charAt(card.length() - 1) != suit) { // If any card has a different suit, it's not a flush
                return false;
            }
        }
        return true; // If all cards have the same suit, it's a flush
    }

    // running program scanner
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); 
        PokerHand pokerHand = new PokerHand(); 
        pokerHand.displayHand(); 
        
        
        System.out.println("Enter your guess (flush, straight, high card, etc.): ");
        String guess = scanner.nextLine(); // Read the user's input
        
       
        System.out.println("Your guess: " + guess);
        
        // checks if user guess is correct
        if (guess.equalsIgnoreCase("flush") && pokerHand.isFlush()) {
            System.out.println("Correct guess! You have a Flush.");
        } else {
            System.out.println("Incorrect guess or you don't have a " + guess + ".");
        }
        
        scanner.close(); 
    }
}

PokerHand.main(null);
Your hand:
6H
7S
AD
5H
8C
Enter your guess (flush, straight, high card, etc.): 
Your guess: Flush
Incorrect guess or you don't have a Flush.

Question 2 Part A explanation

In this example, the PokerHand class creates a guessing game where the user tries to guess a poker hand pattern. The hiddenPattern is the pattern to be guessed and the getHint method gives feedback on the user’s guess. The feedback is simplified to symbols to indicate correct characters in correct hands.

The main method demonstrates how the class might be used with a user guess. For PLACE.com’s Poker feature, we could integrate user input and a more sophisticated method for generating and evaluating poker hands