isConsecutiveStraightFlush连续数组检查

时间:2017-03-29 11:01:45

标签: java arrays junit

我所拥有的任务是,如果一副牌是连续的同花顺,则返回true,这意味着整个牌组由一系列具有连续排名的同一套牌组成。由于这些是卡片,这意味着等级为14(A)的卡片将在等级为2的卡片之前出现。

我如何确定卡的顺序为12,13,14,2,3?

这是卡类

public class Card {
    private String suit;
    private int rank;

    public Card(String suit, int rank) {
        super();
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "[" + rank  + "," + suit + "]";
    }

    @Override
    public boolean equals(Object c2) {
        if (!(c2 instanceof Card)) {
            throw new RuntimeException("Illegal argument to Card.equals()");
        }
        Card card2 = (Card) c2;
        return ((this.getSuit().equals(card2.getSuit())) && 
                (this.getRank() == card2.getRank()));
    }

    public String getSuit() {
        return suit;
    }

    public int getRank() {
        return rank;
    }

    public boolean sameSuitAs(Card card2) {
        return (this.getSuit().equals(card2.getSuit()));
    }

    public boolean sameRankAs(Card card2) {
        return (this.getRank() == card2.getRank());
    }

    public boolean isAnA() {
        if (getRank() != 14) {
            return false;
        }
        return true;
    }

    public boolean isPair(Card c) {
        int rank = getRank();
        if (rank == c.getRank()) {
            return true;
        }
        return false;
    }

    public boolean isTrio(Card c1, Card c2) {
        if (c1.isPair(c2) == true ) {
            if (isPair(c1) == true) {
                return true;
            }
        }
        return false;
    }

    public boolean isFourTuple(Card c1, Card c2, Card c3) {
        if (c1.isPair(c2) == true ) {
            if (c2.isPair(c3) == true) {
                if (isPair(c1) == true) {
                    return true;
                }
            }
        }
        return false;
    }

    public static long countCardsBySuit(Card[] deck, Card target) {
        long result = 0;
        String suit = target.getSuit();
        for (int i = 0; i < deck.length; i++) {
            if (suit == deck[i].getSuit()) {
                result ++;
            }
        }
        return result;
    }

     public static boolean cardExists(Card[] deck, Card target) {
         String targetsuit = target.getSuit();
         int targetrank = target.getRank();
         for (int i = 0; i < deck.length; i++) {
             if (targetsuit == deck[i].getSuit() && targetrank == deck[i].getRank()) {
                 return true;
             }
         }
         return false;
     }

     public static boolean consecutivePairExists(Card[] deck) {
         int pairs = 0;
         for (int i = 0; i < deck.length; i++) {
             int targetrank = deck[i].getRank();
             for (int j = 0 + i + 1; i < deck.length - 3; j ++) {
                 if (targetrank == deck[j].getRank()) {
                     pairs++;
                     if (pairs > 1) {
                         return true;
                     }
                 }
             }
         }
         return false;
     }

     public static boolean pairExists(Card[] deck) {
         for (int i = 0; i < deck.length; i++) {
             int targetrank = deck[i].getRank();
             if (i == 0) {
                 for (int k = 1 ; k <deck.length; k++) {
                     if (targetrank == deck[k].getRank()) {
                         return true;
                     }
                 }
             }
             else {
                 for (int j = i + 1 ; j <deck.length; j++) {
                     if (targetrank == deck[j].getRank()) {
                         return true;
                     }
                 }
             }
         }
         return false;
     }

     public static boolean isConsecutiveStraightFlush(Card[] deck) {
         if (deck.length > 1) {
             long result = 0;
             String suit = deck[0].getSuit();
                for (int i = 1; i < deck.length; i++) {
                    if (suit == deck[i].getSuit()) {
                        result ++;
                    }
                }
                if (result == deck.length - 1) {
                    int count = 0;
                    int rank = deck[0].getRank();
                    for (int j = 1; j < deck.length; j++) {
                        if (rank - deck[j].getRank() == -1) {
                            count++;
                            rank = deck[j].getRank();
                            if (count == deck.length - 1) {
                                return true;
                            }
                        }
                    }
                }
         }
         return false;
     }
}

这是JUnit Test Case CardTest

import static org.junit.Assert.*;
import org.junit.Test;

public class CardTest {

    Card diamond1 = new Card("Diamond", 1);
    Card diamond2 = new Card("Diamond", 2);
    Card spade1 = new Card("Spade", 1);
    Card spade2 = new Card("Spade", 2);
    Card spadeA = new Card("Spade", 14);

        Card card2C = new Card("Club", 2);
        Card card2S = new Card("Spade", 2);
        Card card2D = new Card("Diamond", 2);
        Card card2H = new Card("Heart", 2);

        Card card3C = new Card("Club", 3);
        Card card4C = new Card("Club", 4);
        Card card5C = new Card("Club", 5);
        Card card6C = new Card("Club", 6);
        Card card7C = new Card("Club", 7);
        Card card8C = new Card("Club", 8);
        Card card9C = new Card("Club", 9);
        Card card10C = new Card("Club", 10);
        Card cardJC = new Card("Club", 11);
        Card cardQC = new Card("Club", 12);
        Card cardKC = new Card("Club", 13);
        Card cardAC = new Card("Club", 14);

        Card[] deck1 = { card2C, card2S, card2D, card3C };

        Card[] deck2 = { card2C, card3C, card2D};

        Card[] deck3 = { card2C, card3C, spadeA};

        Card[] straightFlush1 = { card2C, card3C, card4C, card5C, card6C};
        Card[] straightFlush2 = { cardQC, cardKC, cardAC, card2C, card3C};

        Card[] emptyDeck = {};

    @Test
    public void testToString() {
        assertTrue("Card.toString: generates incorrect String", diamond1.toString().equals("[1,Diamond]"));
        assertTrue("Card.toString: generates incorrect String", spade2.toString().equals("[2,Spade]"));
    }

    @Test
    public void testEquals() {
        assertTrue("Card.equals: Yields false incorrectly", diamond1.equals(diamond1));
        assertFalse("Card.equals: Yields true incorrectly", diamond1.equals(diamond2));
    }

    @Test
    public void testSameSuitRank() {
        assertTrue("Card.sameRankAs: Yields false incorrectly", diamond1.sameRankAs(spade1));
        assertTrue("Card.sameSuitAs: Yields false incorrectly", diamond1.sameSuitAs(diamond2));
        assertFalse("Card.sameRankAs: Yields true incorrectly", spade1.sameRankAs(spade2));
        assertFalse("Card.sameSuitAs: Yields true incorrectly", spade2.sameSuitAs(diamond1));
    }

    @Test
    public void testIsConsecutiveStraightFlush() {
        assertFalse("isConsecutiveStraightFlush: returns true incorrectly", Card.isConsecutiveStraightFlush(emptyDeck));
        assertFalse("isConsecutiveStraightFlush: returns true incorrectly", Card.isConsecutiveStraightFlush(deck1));
        assertTrue("isConsecutiveStraightFlush: returns false incorrectly", Card.isConsecutiveStraightFlush(straightFlush1));
        assertTrue("isConsecutiveStraightFlush: returns false incorrectly", Card.isConsecutiveStraightFlush(straightFlush2));
    }
}

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

您需要将此特殊条件添加到if (rank - deck[j].getRank() == -1)

if (rank - deck[j].getRank() == -1 || (rank == 14 && deck[j].getRank() == 2))

答案 1 :(得分:0)

我不打算为你分配任何代码示例,因为关键是你自己编写代码,但我确实想给你一些建议和指导,以帮助你顺利完成

更聪明,更努力

对于这样的问题,通常更容易判断条件是false还是告诉它是true。这是你的方法:

public static boolean isConsecutiveStraightFlush(Card[] deck) {
    if (deck.length > 1) {
        long result = 0;
        String suit = deck[0].getSuit();
           for (int i = 1; i < deck.length; i++) {
               if (suit == deck[i].getSuit()) {
                   result ++;
               }
           }
           if (result == deck.length - 1) {
               int count = 0;
               int rank = deck[0].getRank();
               for (int j = 1; j < deck.length; j++) {
                   if (rank - deck[j].getRank() == -1) {
                       count++;
                       rank = deck[j].getRank();
                       if (count == deck.length - 1) {
                           return true;
                       }
                   }
               }
           }
    }
    return false;
}

您花费了大量精力来跟踪所有内容,以便每次检查都可以return true。如果你将它翻转成圆形,以便假设一次检查失败,那么你就认为它是一个同花顺的return false,这种方法变得更加简单。

字符串相等

非常罕见的情况下,您希望使用==运算符来测试Java中的字符串相等性。请改用string1.equals(string2)。有关详细信息,请参阅this answer

尝试使用运算符

您正在尝试使用排名差异来测试这些卡是否按顺序排列并且运行良好...直到您发现要求允许序列回绕到开始。模运算符(%)可用于将数字约束到给定范围。例如,以下代码段将打印保持循环显示值,即使i将远远大于数组的长度:

String[] values = { "one", "two", "three" };
for (int i = 0; i <= 10; i++) {
    System.out.println(values[i % 3]);
}

有一个解决方案(不是唯一的解决方案)可以使用此包装排名要求(提示:如果"current rank" + 1 == "next rank",排名是顺序的。)

小心不要将假设变为要求

作业是否指定序列Q,K,A,2,3是有效的同花顺,或者你是否使用自己对扑克牌的理解?

According to Wikipedia(可靠,我知道),&#34; Q♣K♣A♣2♣3♣是一个高级同花顺,而不是同花顺&#34;所以你可能正在制作分配比它需要的更难。

希望我在这里给你一些帮助。祝你好运。