Java Write a little poker game which lets the player play a round of Poker again
ID: 3604458 • Letter: J
Question
Java
Write a little poker game which lets the player play a round of Poker against the computer The ranking of the 5 cards are as follows POKER HAND RANKING CHART Consists ofthe following cards: ten, jack, queen, king, and an ace all of the same suit Royal Flush 10 straight Flush Five cards in sequence, all of the 3 4 5 6 7 same suit Four cards of the same denomination, one in each suit. | Three cards of one denomination Four of a Kind 10 10 10 I10 4 11 Full House 7 17 nd two cards of another denomination Flush Five cards all of the same suit. 2 16 9 Q K Straight Five cards in sequence of any 3 4 15 16 7 Suit Three of a Kind Three cards of the same denomination and two unmatched cards Two sets of two cards of the 9 9 9 6 2 Two Pairs same denomination and any fifth | | | | | | card Two cards of the same denomination and three unmatched cards One Pair 6 63 Q 10 14 1110 All five cards of different rank and a variety of suits No Pair A You will calculate 5 random cards for the players, each of which has 1 of 4 suits (hearts, diamonds, clubs, and spades) and of of the following faces: 2, 3, 4, 5, 6,7, 8, 9, 10, Jack, Queen, King, Ace Once you have the 5 cards for the player, you will create five cards for the computer, in the same way. Once all cards are determined, you will compare the hands and determine the winner This exercise requires no input from the player, the only thing you need to output is the hand of the player, the hand of the computer, and who the winner is Tip: work your way bottom up in the list of winning hands: check for one pair first, then for 2 pairs, then for 3 of a kind, etc.Explanation / Answer
public class Card implements Comparable<Card> {
protected static enum Suit {
Hearts, Diamonds, Clubs, Spades
}
// Instance variables
private int value;
private Suit suit;
/**
* Constructor
*/
public Card(int value, Suit suit) {
this.value = value;
this.suit = suit;
}
/**
* Returns the value
*/
public int getValue() {
return value;
}
/**
* Returns the value of the card
*/
public String getValueStr() {
String valueStr = "";
switch (value) {
case 2:
valueStr = "2";
break;
case 3:
valueStr = "3";
break;
case 4:
valueStr = "4";
break;
case 5:
valueStr = "5";
break;
case 6:
valueStr = "6";
break;
case 7:
valueStr = "7";
break;
case 8:
valueStr = "8";
break;
case 9:
valueStr = "9";
break;
case 10:
valueStr = "10";
break;
case 11:
valueStr = "Jack";
break;
case 12:
valueStr = "Queen";
break;
case 13:
valueStr = "King";
break;
case 14:
valueStr = "Ace";
}
return valueStr;
}
/**
* Returns the suit of the card
*/
public String getSuit() {
return suit.toString();
}
@Override
public String toString() {
return getValueStr() + " of " + getSuit();
}
/**
* Compares two cards
*/
@Override
public int compareTo(Card c2) {
int diff = this.value - c2.value;
// Check if values are same, return suit comparison
if (diff == 0)
return suit.ordinal() - c2.suit.ordinal();
else
return diff;
}
}
/**
* This class represents a deck of 52 cards.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Deck {
private static final int NUM_CARDS = 52;
// Instance variables
private List<Card> deck;
/**
* Default constructor. Creates a deck of cards.
*/
public Deck() {
deck = new ArrayList<Card>();
// Add cards
addCards();
// Shuffle deck
shuffle();
}
/**
* Creates card with values 2 to 10, Jack, Queen, King, Ace for all suits
*/
private void addCards() {
// For each suit
for (Card.Suit suit : Card.Suit.values()) {
for (int i = 2; i <= 14; i++) {
// Create Card
Card card = new Card(i, suit);
// Add card to the list
deck.add(card);
}
}
}
/**
* Shuffles the deck of cards
*/
private void shuffle() {
Collections.shuffle(deck);
}
/**
* Returns and removes the first n cards from the deck
* @param n - number of cards to be returned
* @return - Returns a list of n cards
*/
public List<Card> getCards(int n) {
List<Card> list = new ArrayList<>(n);
// Get first n cards
for (int i = 0; i < n; i++) {
list.add(deck.remove(0));
}
return list;
}
}
/**
* This class determines the type of poker hand.
*/
import java.util.Collections;
import java.util.List;
public class Poker {
// Number of cards in each hand
private final static int NO_OF_CARDS = 5;
// Enum for poker hand
private static enum POKER_HAND {
Royal_Flush, Straight_Flush, Four_Of_A_Kind, Full_House, Flush, Straight, Three_Of_A_Kind, Two_Pair, One_Pair, No_Pair
}
/**
* Counts the number of times card appears in the hand and returns it
*/
public static int countCard(List<Card> hand, Card card) {
int count = 0;
// Find card with the value same as card
for (int i = 0; i < NO_OF_CARDS; i++) {
if (hand.get(i).getValue() == card.getValue())
count += 1;
}
return count;
}
/**
* Checks if a hand contains a pair.
*/
public static boolean containsPair(List<Card> hand) {
// Since the hand is sorted, check only for the first 3 cards
for (int i = 0; i < 4; i++) {
if (countCard(hand, hand.get(i)) == 2)
return true;
}
// Default return
return false;
}
/**
* Checks if a hand contains two pair.
*/
public static boolean containsTwoPair(List<Card> hand) {
boolean pairOne = false;
boolean pairTwo = false;
for (int i = 0; i < NO_OF_CARDS; i++) {
if (pairOne && pairTwo) // If both pairs are found, return true
return true;
if (countCard(hand, hand.get(i)) == 2) {
if (!pairOne) {
pairOne = true;
i += 1;
} else if (!pairTwo)
pairTwo = true;
}
}
return false;
}
/**
* Checks if a hand contains 3 matching cards.
*/
public static boolean containsThreeOfaKind(List<Card> hand) {
// Since the hand is sorted, check only for the first 3 cards
for (int i = 0; i < 3; i++) {
if (countCard(hand, hand.get(i)) == 3)
return true;
}
// Default return
return false;
}
/**
* Checks if a hand contains a pair and 3 matching cards Pre-condition :
* hand should be sorted
*/
public static boolean containsFullHouse(List<Card> hand) {
// Check whether there is three of a kind and a pair
return (containsThreeOfaKind(hand) && containsPair(hand));
}
/**
* Checks if a hand contains 4 matching cards
*/
public static boolean containsFourOfaKind(List<Card> hand) {
// Since the hand is sorted, check only for the first 2 cards
for (int i = 0; i < 2; i++) {
if (countCard(hand, hand.get(i)) == 4)
return true;
}
// Default return
return false;
}
/**
* Checks whether the cards in hand have consecutive values
*/
public static boolean hasConsecutiveValues(List<Card> hand) {
for (int i = 1; i < 5; i++) {
if (hand.get(i - 1).getValue() != (hand.get(i).getValue() - 1))
return false;
}
// Default return
return true;
}
/**
* Checks whether all card in hand have same suit
*/
public static boolean hasSameSuit(List<Card> hand) {
for (int i = 1; i < 5; i++) {
if (hand.get(i - 1).getSuit() != hand.get(i).getSuit())
return false;
}
// Default return
return true;
}
/**
* Checks whether cards in hand have values 10, Jack, Queen, King, Ace
*/
public static boolean hasRoyals(List<Card> hand) {
if (hand.get(0).getValue() == 10) {
for (int i = 1; i < 5; i++) {
if (hand.get(i - 1).getValue() != (hand.get(i).getValue() - 1))
return false;
}
return true;
} else
return false;
}
/**
* Find poker hand.
*/
public static POKER_HAND findPokerHands(List<Card> hand) {
// Check whether all cards have same suit
boolean sameSuit = hasSameSuit(hand);
// Check whether all cards have royal values
boolean royal = hasRoyals(hand);
// Check whether all cards have consecutive values
boolean consecutiveValues = hasConsecutiveValues(hand);
// If suit is the same
if (sameSuit) {
if (royal) // Check for royal flush
return POKER_HAND.Royal_Flush;
else if (consecutiveValues) // Check for straight flush
return POKER_HAND.Straight_Flush;
else // Flush
return POKER_HAND.Flush;
} else { // If suit is not same
if (containsFourOfaKind(hand)) // Four of a kind
return POKER_HAND.Four_Of_A_Kind;
else if (containsFullHouse(hand)) // Full house
return POKER_HAND.Full_House;
else if (consecutiveValues) // Straight
return POKER_HAND.Straight;
else if (containsThreeOfaKind(hand)) // Three of a kind
return POKER_HAND.Three_Of_A_Kind;
else if (containsTwoPair(hand)) // Two pairs
return POKER_HAND.Two_Pair;
else if (containsPair(hand)) // One pair
return POKER_HAND.One_Pair;
}
// No pair
return POKER_HAND.No_Pair;
}
/**
* Displays the cards in hand.
*
* @param hand
* - hand
*/
public static void displayHand(List<Card> hand) {
for (Card card : hand)
System.out.println(" " + card);
}
public static void main(String[] args) {
// Create a deck
Deck deck = new Deck();
// Get Player's cards
List<Card> player = deck.getCards(NO_OF_CARDS);
// Sort player's cards
Collections.sort(player);
// Display computer's hand
System.out.println("Player's hand:");
displayHand(player);
// Get Computer's cards
List<Card> comp = deck.getCards(NO_OF_CARDS);
// Sort computer's cards
Collections.sort(comp);
// Display computer's hand
System.out.println("Computer's hand:");
displayHand(comp);
System.out.println(" Poker hands:");
// Find and display player's poker hand
POKER_HAND player_res = findPokerHands(player);
System.out.println("Player: " + player_res);
// Find and display computer's poker hand
POKER_HAND comp_res = findPokerHands(comp);
System.out.println("Computer: " + comp_res);
// Display winner
System.out.println(" Result:");
if (player_res.ordinal() < comp_res.ordinal())
System.out.println("Player wins!!!");
else if (player_res.ordinal() > comp_res.ordinal())
System.out.println("Computer wins!!!");
else {
if ((player_res == POKER_HAND.No_Pair) && (comp_res == POKER_HAND.No_Pair)) {
// Compare the highest value card
int diff = player.get(NO_OF_CARDS - 1).getValue() - comp.get(NO_OF_CARDS - 1).getValue();
if (diff > 0)
System.out.println("Player wins!!! (High Card)");
else if (diff < 0)
System.out.println("Computer wins!!! (High Card)");
else
System.out.println("No one wins.");
} else
System.out.println("No one wins.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.