Skip to content

Instantly share code, notes, and snippets.

@exoego
Created June 24, 2012 11:07
Show Gist options
  • Save exoego/2982838 to your computer and use it in GitHub Desktop.
Save exoego/2982838 to your computer and use it in GitHub Desktop.
解答→第一回 オフラインリアルタイムどう書くの参考問題
package net.exoego.doukaku;
import java.util.*;
public class Poker {
private static enum Suit {
S, H, D, C;
static Suit from(final String value) {
return Suit.valueOf(Suit.class, value);
}
};
private static enum Rank {
_2, _3, _4, _5, _6, _7, _8, _9, _10, J, Q, K, A;
static Rank from(final String value) {
try {
return Rank.valueOf(Rank.class, "_" + value);
} catch (final IllegalArgumentException ex) {
return Rank.valueOf(Rank.class, value);
}
}
}
private static class Card {
private final Suit suit;
private final Rank rank;
private Card(final Suit s, final Rank r) {
suit = s;
rank = r;
}
static Card create(final String input) {
final Suit s = Suit.from(input.substring(0, 1));
final Rank r = Rank.from(input.substring(1));
return new Card(s, r);
}
}
public static String draw(final String input) {
final String[] split = input.replaceAll("(S|H|D|C)([2-9JQKA]|10)", "$1$2,").split(",");
final List<Card> cards = toCards(split);
return toCombination(cards);
}
protected static List<Card> toCards(final String[] split) {
final List<Card> cards = new ArrayList<Card>();
for (final String s : split) {
cards.add(Card.create(s));
}
return cards;
}
private static String toCombination(final List<Card> cards) {
final Map<Rank, Integer> countByRank = groupByRank(cards);
final Map<Integer, Integer> counts = groupByCount(countByRank);
switch (counts.get(4)) {
case 1:
return "4K";
}
switch (counts.get(3)) {
case 1:
if (counts.get(2) == 1) {
return "FH";
}
return "3K";
}
switch (counts.get(2)) {
case 2:
return "2P";
case 1:
return "1P";
}
return "--";
}
private static Map<Rank, Integer> groupByRank(final List<Card> cards) {
final Map<Rank, Integer> group = initCountByGroup(Rank.class, Rank.values());
for (final Card card : cards) {
group.put(card.rank, group.get(card.rank) + 1);
}
return group;
}
private static <E extends Enum<E>> Map<E, Integer> initCountByGroup(final Class<E> klass,
final E[] values) {
final Map<E, Integer> group = new EnumMap<E, Integer>(klass);
for (final E e : values) {
group.put(e, 0);
}
return group;
}
private static <K> Map<Integer, Integer> groupByCount(final Map<K, Integer> counts) {
final Map<Integer, Integer> group = new HashMap<Integer, Integer>();
for (final int i : new int[] { 0, 1, 2, 3, 4 }) {
group.put(i, 0);
}
for (final Integer count : counts.values()) {
group.put(count, group.get(count) + 1);
}
return group;
}
}
package net.exoego.doukaku;
import static org.junit.Assert.*;
public class PokerTest {
@Test
public void Four_Card() {
assertThat(Poker.draw("D3C3S3H3CJ"), is("4K"));
assertThat(Poker.draw("DQCQCASQHQ"), is("4K"));
assertThat(Poker.draw("DJC3CJSJHJ"), is("4K"));
}
@Test
public void Full_House() {
assertThat(Poker.draw("D3C3C10D10S3"), is("FH"));
assertThat(Poker.draw("H2C2CJDJSJ"), is("FH"));
assertThat(Poker.draw("DQD4C4DQS4"), is("FH"));
}
@Test
public void Three_Card() {
assertThat(Poker.draw("HJD10HJS5CJ"), is("3K"));
assertThat(Poker.draw("HASADAH3H7"), is("3K"));
assertThat(Poker.draw("HASADAH3H7"), is("3K"));
}
@Test
public void Two_Pair() {
assertThat(Poker.draw("S8D10HJS10CJ"), is("2P"));
assertThat(Poker.draw("HJD5H7S5CJ"), is("2P"));
assertThat(Poker.draw("S2H10S5C5C2"), is("2P"));
}
@Test
public void One_Pair() {
assertThat(Poker.draw("S8D10H2S10CJ"), is("1P"));
assertThat(Poker.draw("HJD3H7S5CJ"), is("1P"));
assertThat(Poker.draw("S2H10S5C5CQ"), is("1P"));
}
@Test
public void No_Pair() {
assertThat(Poker.draw("DAC2S3H4C5"), is("--"));
assertThat(Poker.draw("H4D7D6D5DJ"), is("--"));
assertThat(Poker.draw("D3D7SAD5C6"), is("--"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment