Question: Hello, I am developing a simple poker game. I got my code to print out four of a kind and a flush, but I do

Hello, I am developing a simple poker game. I got my code to print out four of a kind and a flush, but I do not know how to code it to find if there is a pair or a straight please help

public class DeckOfCardsCompare { //================================================================================================= //Listing 7.2 from Introduction to JAVA PROGRAMMING by Y. Daniel Liang //Modified to match the CS-152 coding standard and made deck[], SUITS[] and RANKS[] class fields //This version replaces the array: // private static final String[] SUITS = {"Spades", "Hearts", "Diamonds", "Clubs"}; //With UNICODE symbols for each suit. //For a full list of UNICODE symbols see: http://www.ssec.wisc.edu/~tomw/java/unicode.html // //This version also adds color printing to the console (printing hearts and diamonds in red). //It is possible to define many other colors. For example try: // private static final String ANSI_GREEN = "\u001B[32m"; // private static final String ANSI_YELLOW = "\u001B[33m"; // private static final String ANSI_BLUE = "\u001B[34m"; // private static final String ANSI_PURPLE = "\u001B[35m"; // private static final String ANSI_CYAN = "\u001B[36m"; // private static final String ANSI_WHITE = "\u001B[37m"; //================================================================================================= //================================================================================================ // Class fields //================================================================================================ // The two fields below are special "escape codes". // If your terminal supports it, you can use ANSI escape codes to use color in your output. // It works in the IntelliJ console. It also generally works for Unix shell prompts; // however, it doesn't work for Windows Command Prompt. private static final String ANSI_BLACK = "\u001B[30m"; private static final String ANSI_RED = "\u001B[31m"; private static final char[] SUITS = {'\u2660', '\u2665', '\u2666', '\u2663'}; private static final String[] RANKS = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; private int[] deck = new int[52]; //================================================================================================= // Constructor: DeckOfCards() // The constructor initializes the deck of cards (deck[]) by assigning each element to a different // card number and then shuffling the card numbers within deck[]. //================================================================================================= public DeckOfCardsCompare() { // Initialize cards for (int i = 0; i < deck.length; i++) { deck[i] = i; } // Shuffle the cards for (int i = 0; i < deck.length; i++) { // Generate a random index and swap it with deck[i]. int index = (int)(Math.random() * deck.length); int temp = deck[i]; deck[i] = deck[index]; deck[index] = temp; } } //================================================ // //================================================ private static int getRankIdx(int cardIdx) { return cardIdx % 13; } //================================================ // //================================================ private static int getSuitIdx(int cardIdx) { return cardIdx / 13; } //================================================================================================= //isFourOfAKind() //================================================================================================= private boolean isFourOfAKind() { if (compareFourCards(0, 1, 2, 3)) return true; if (compareFourCards(0, 2, 3, 4)) return true; if (compareFourCards(0, 1, 3, 4)) return true; if (compareFourCards(0, 1, 2, 4)) return true; if (compareFourCards(1, 2, 3, 4)) return true; return false; } //================================================================================================= //compareFourCards(int a, int b, int c, int d) //================================================================================================= private boolean compareFourCards(int a, int b, int c, int d) { if (getRankIdx(deck[a]) == getRankIdx(deck[b])) { if (getRankIdx(deck[a]) == getRankIdx(deck[c])) { if (getRankIdx(deck[a]) == getRankIdx(deck[d])) { return true; } } } return false; } private boolean isFlush() { if (compareFourCards(0, 1, 2, 3)) return true; if (compareFourSuits(0, 2, 3, 4)) return true; if (compareFourSuits(0, 1, 3, 4)) return true; if (compareFourSuits(0, 1, 2, 4)) return true; if (compareFourSuits(1, 2, 3, 4)) return true; return false; } private boolean compareFourSuits(int e, int f, int g, int h) { if (getSuitIdx(deck[e]) == getSuitIdx(deck[f])) { if (getSuitIdx(deck[e]) == getSuitIdx(deck[g])) { if (getSuitIdx(deck[e]) == getSuitIdx(deck[h])) { return true; } } } return false; } //================================================================================================= // main(String[] args) // Creates an instance of DeckOfCards(), then displays the first 4 cards in the shuffled deck. //================================================================================================= public static void main(String[] args) { for (int n= 0; n < 1000; n++) { DeckOfCardsCompare myDeck = new DeckOfCardsCompare(); // Display the first five cards for (int i = 0; i < 5; i++) { int suitIdx = getSuitIdx(myDeck.deck[i]); String suit = String.valueOf(SUITS[suitIdx]); //If the suit is a heart or diamond, then sandwich the symbol between the escape codes that // change color to red, back to black. if (suitIdx == 1 || suitIdx == 2) { suit = ANSI_RED + suit + ANSI_BLACK; } int rankIdx = getRankIdx(myDeck.deck[i]); String rank = RANKS[rankIdx]; System.out.println("Card number " + myDeck.deck[i] + ": " + rank + " of " + suit); } if (myDeck.isFourOfAKind()) { System.out.println("four of a kind "); } if (myDeck.isFlush()) { System.out.println("Flush "); } } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!