challenge ポーカーの役判定

引数に手札を与えると、ポーカーの役を表示するプログラムを作ってください。

条件:

  • スートはS,D,H,C、ランクはA,2~9,T,J,Q,Kのそれぞれ一文字で表します。
  • 手札は S2D5H3CQS9 のように10文字で指定されます。特にソートはされていません。
  • 手札にジョーカーは含まれません。
  • ストレートで取りうるランクの種類はA2345, 23456 ... 9TJQK, TJQKAの10種類で、JQKA2のようにK-A-2をまたぐものはストレートではありません。

実行例:

% ./poker SQSJSASKST
Royal flush

% ./poker D9D7D6D5D8
Straight flush

% ./poker C2D2S2H3H2
Four of a kind

% ./poker C2D3S2H3H2
Full house

% ./poker S9S4S8STSJ
Flush

% ./poker C4H7D5S6H3
Straight

% ./poker S6H6C5DQC6
Three of a kind

% ./poker S6HQC5DQC6
Two pair

% ./poker S6H4C5DQC6
One pair

% ./poker SJSQSKSAC2
No pair

お題にしようと思っていたのに間違えてしまいました。今から変更可能でしょうか?

(説明)
当初間違ってトピックに投稿していたので、このようなコメントを付けていたのですが、
このコメントに気づいた管理人さんにお題に移していただきました。
(最初の2つだけ投稿日時が早いのはそのためです)

Posted feedbacks - Java

素直に順番に条件チェックしてみました。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sample121 {
    private static final String RANK_LIST = "A23456789TJQKA";

    private static final String ROYAL = "royal ";
    private static final String STRAIGHT = "straight";
    private static final String FLUSH = "flush";
    private static final String FULL_HOUSE = "Full house";
    private static final String CARDS_4 = "Four of a kind";
    private static final String CARDS_3 = "Three of a kind";
    private static final String PAIR_2 = "Two pair";
    private static final String PAIR_1 = "One pair";
    private static final String Nothing = "No pair";

    public static String getPokerRole(String cards) {
        if (cards.length() != 2 * 5) throw new IllegalArgumentException();
        String rankList = createRankList(cards);
        boolean flush = isFlush(cards);
        boolean straight = isStraight(rankList);
        if (flush || straight) {
            boolean royal = isRoyal(rankList);
            if (royal) {
                return format(flush? ROYAL + FLUSH: ROYAL + STRAIGHT);
            } else {
                if (flush && straight) {
                    return format(STRAIGHT + " " + FLUSH);
                } else {
                    return format(flush? FLUSH: STRAIGHT);
                }
            }
        }
        Integer[] integers = countSameNumber(rankList);
        switch (integers[0]) {
            case 4:
                return CARDS_4;
            case 3:
                if (integers[1] == 2) {
                    return FULL_HOUSE;
                } else {
                    return CARDS_3;
                }
            case 2:
                if (integers[1] == 2) {
                    return PAIR_2;
                } else {
                    return PAIR_1;
                }
        }
        return Nothing;
    }

    private static String format(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    private static boolean isFlush(String cards) {
        char suit = cards.charAt(0);
        for (int index = 1; index < 5; index++) {
            if (suit != cards.charAt(index * 2)) {
                return false;
            }
        }
        return true;
    }

    private static String createRankList(String cards) {
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < 5; index++) {
            final char rank = cards.charAt(index * 2 + 1);
            int insert = 0;
            for (; insert < builder.length(); insert++) {
                if (RANK_LIST.indexOf(builder.charAt(insert)) > RANK_LIST.indexOf(rank)) {
                    break;
                }
            }
            builder.insert(insert, rank);
        }
        return builder.toString();
    }
    private static boolean isStraight(String rankList) {
        return RANK_LIST.indexOf(rankList) >= 0;
    }
    private static boolean isRoyal(String rankList) {
        return rankList.equals("ATJQK");
    }

    private static Integer[] countSameNumber(String rankList) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int index = 0; index < rankList.length(); index++) {
            char c = rankList.charAt(index);
            Integer integer = map.get(c);
            if (integer == null) {
                integer = 1;
                map.put(c, integer);
            } else {
                map.put(c, integer + 1);
            }
        }
        List<Integer> result = new ArrayList<Integer>(map.values());
        Collections.sort(result, Collections.reverseOrder());
        return result.toArray(new Integer[0]);
    }


    public static void main(String[] args) {
        System.out.println(getPokerRole("SQSJSASKST"));
        System.out.println(getPokerRole("D9D7D6D5D8"));
        System.out.println(getPokerRole("C2D2S2H3H2"));
        System.out.println(getPokerRole("C2D3S2H3H2"));
        System.out.println(getPokerRole("S9S4S8STSJ"));
        System.out.println(getPokerRole("C4H7D5S6H3"));
        System.out.println(getPokerRole("S6H6C5DQC6"));
        System.out.println(getPokerRole("S6HQC5DQC6"));
        System.out.println(getPokerRole("S6H4C5DQC6"));
        System.out.println(getPokerRole("SJSQSKSAC2"));
    }
}

既にJavaで解かれていますが、ちょっと捻ってみました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Poker {
    public static void main(String[] args) {
        System.out.println(getPokerRole("SQSJSASKST"));
        System.out.println(getPokerRole("D9D7D6D5D8"));
        System.out.println(getPokerRole("C2D2S2H3H2"));
        System.out.println(getPokerRole("C2D3S2H3H2"));
        System.out.println(getPokerRole("S9S4S8STSJ"));
        System.out.println(getPokerRole("C4H7D5S6H3"));
        System.out.println(getPokerRole("S6H6C5DQC6"));
        System.out.println(getPokerRole("S6HQC5DQC6"));
        System.out.println(getPokerRole("S6H4C5DQC6"));
        System.out.println(getPokerRole("SJSQSKSAC2"));
    }

    private static String getPokerRole(String aCards) {
        List<int[]> tCards = new ArrayList<int[]>();

        for (int i = 0; i < aCards.length(); i += 2) {
            tCards.add(new int[] { aCards.charAt(i), 1 + "A23456789TJQK".indexOf(aCards.charAt(i + 1)) });
        }

        Collections.sort(tCards, new Comparator<int[]>() {
            public int compare(int[] aLeftCard, int[] aRightCard) {
                return aLeftCard[1] - aRightCard[1];
            }
        });

        int tRoyalCardCount = 0;
        boolean tIsStraight = true;
        boolean tIsFlush = true;
        int[] tPairs = new int[5];
        int tPairKinds = 0;
        for (int i = 0; i < tCards.size(); i++) {
            if (Arrays.binarySearch(new int[] { 1, 10, 11, 12, 13 }, tCards.get(i)[1]) >= 0) {
                tRoyalCardCount++;
            }
            if (i == 0) {
                continue;
            }
            if (tCards.get(i - 1)[0] != tCards.get(i)[0]) {
                tIsFlush = false;
            }
            if (tCards.get(i - 1)[1] + 1 != tCards.get(i)[1]) {
                tIsStraight = false;
            }
            if (tCards.get(i - 1)[1] == tCards.get(i)[1]) {
                tPairs[tPairKinds]++;
            } else {
                tPairKinds++;
            }
        }

        Arrays.sort(tPairs);
        if (tRoyalCardCount == 5 && tIsFlush) {
            return "Royal flush";
        } else if (tIsFlush && tIsStraight) {
            return "Straight flush";
        } else if (tIsFlush) {
            return "Flush";
        } else if (tIsStraight) {
            return "Straight";
        } else if (tPairs[4] == 3) {
            return "Four of a kind";
        } else if (tPairs[4] == 2 && tPairs[3] == 1) {
            return "Full house";
        } else if (tPairs[4] == 2) {
            return "Three of a kind";
        } else if (tPairs[4] == 1 && tPairs[3] == 1) {
            return "Two pair";
        } else if (tPairs[4] == 1 && tPairs[3] == 0) {
            return "One pair";
        }

        return "No pair";
    }
}

Index

Feed

Other

Link

Pathtraq

loading...