HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Hand rankings for Poker game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pokerhandrankingsgamefor

Problem

I have tried to write a small part of a Poker game to check the Poker hand rankings. Please review and provide your feedback.

```
package com.sa.test.conditions.poker;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class Hand {
Card[] cards;

public Card[] getCards() {
return cards;
}

public void setCards(Card[] cards) {
this.cards = cards;
Arrays.sort(cards);
}

public void printHand() {
for (Card card : cards) {
System.out.print(card.suite + " => ");
System.out.println(card.rank);
}
}

public HandType getType() {

if (isAllFromSameSuite()) {

if (isARoyalFlush()) {
return HandType.ROYAL_FLUSH;
}
if (cardsInSequence()) {
return HandType.STRAIGHT_FLUSH;
}
if (!cardsInSequence()) {
return HandType.FLUSH;
}
}

if (hasNumberOfCardsEqualToN(4)) {
return HandType.FOUR_OF_A_KIND;
}

if (hasNumberOfCardsEqualToN(3) && hasNumberOfCardsEqualToN(2)) {
return HandType.FULL_HOUSE;
}

if (!isAllFromSameSuite() && cardsInSequence()) {
return HandType.STRAIGHT;
}

if (hasNumberOfCardsEqualToN(3)) {
return HandType.THREE_OF_A_KIND;
}

if (hasTotalPairsEqualToN(2)) {
return HandType.TWO_PAIR;
}

if (hasTotalPairsEqualToN(1)) {
return HandType.PAIR;
}

return HandType.HIGH_CARD;
}

private boolean isARoyalFlush() {
int royal = 10;
for (Card card : cards) {

if (card.getRank() maxDiff) {
return false;
}
}
tmpCard = card;
}
return true;
}

private boolean isAllFromSameSuite() {
Card tmpCard = null;
for (Card card : cards)

Solution

If this should be expandable in the future, you should inject a CardPrinter interface to the constructor, which takes a Cards[] for output of the cards. This interface could hold a CardFormatter interface which formats the apearance and returns a String. Then the CardPrinter will "print" the cards. By using this approach you can, if you decide to print e.g to Html, easily extend your application.

public interface OutputFormatter {
   public String format(Card[] cards);
}  

public interface CardPrinter {
    public void setFormatter(OutputFormatter formatter);
    public void pring(Card[] cards);
}


  • delete commented code. Commented code is dead code, which only removes readability.



-
you should use braces {} for single if statements to make your code less errorprone. No matter what you decide to use (braces or not) you should stick to it. Right now you are mixing the usage.

-
if you return true if a condition is true and otherwise false, you can simplify this by returning the condition.

if (pairCount == numberOfPair)
    return true;

return false;


will look like

return pairCount == numberOfPair;


-
if you need to check the current Card and the next Card it is more readable to use a for loop which ends at length -1. Like

private boolean hasTotalPairsEqualToN(int numberOfPair) {
    int pairCount = 0;
    for (int i = 0; i < cards.length - 1; i++) {

        if (cards[i].getRank() == cards[i + 1].getRank()) {
            pairCount++;
        }

    }
    return pairCount == numberOfPair;
}


-
comments like

// check if count matches the n
if (pairCount == numberOfPair)


won't add any value to your code. Comments should describe why something is done. Let the code speak for itself about what is done.

Code Snippets

public interface OutputFormatter {
   public String format(Card[] cards);
}  

public interface CardPrinter {
    public void setFormatter(OutputFormatter formatter);
    public void pring(Card[] cards);
}
if (pairCount == numberOfPair)
    return true;

return false;
return pairCount == numberOfPair;
private boolean hasTotalPairsEqualToN(int numberOfPair) {
    int pairCount = 0;
    for (int i = 0; i < cards.length - 1; i++) {

        if (cards[i].getRank() == cards[i + 1].getRank()) {
            pairCount++;
        }

    }
    return pairCount == numberOfPair;
}
// check if count matches the n
if (pairCount == numberOfPair)

Context

StackExchange Code Review Q#78189, answer score: 6

Revisions (0)

No revisions yet.