diff --git a/yitou/B017_Card.java b/yitou/B017_Card.java new file mode 100644 index 0000000000000000000000000000000000000000..b338a3ee2b2712e9381917f202e4bb3ad3e7b51e --- /dev/null +++ b/yitou/B017_Card.java @@ -0,0 +1,87 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class B017_Card { + + // 手役を管理するenum + public enum COMB { + NO_PAIR("NoPair"), // 0 + ONE_PAIR("OnePair"), // 1 + TWO_PAIR("TwoPair"), // 2 + THREE_CARD("ThreeCard"), // 3 + FOUR_CARD("FourCard"); // 4 + + private final String name; + + COMB(final String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + public static void main(final String[] args) { + final Scanner scanner = new Scanner(System.in); + final String input = scanner.nextLine(); + scanner.close(); + + System.out.println(getBestPair(input)); + } + + public static COMB getBestPair(final String cards) { + final Map countMap = new HashMap<>(); + int wildCardCount = 0; + + // カードのカウント + for (char card : cards.toCharArray()) { + if (card == '*') { + wildCardCount++; + } else { + countMap.put(card, countMap.getOrDefault(card, 0) + 1); + } + } + + // 0:ノーペア, 1:1ペア, 2:2ペア, 3:3カード, 4:4カード + final int[] counts = new int[5]; + for (int count : countMap.values()) { + counts[count]++; + } + + // 4カードの判定 + if (counts[4] > 0) { + return COMB.FOUR_CARD; + } + + // ワイルドカードを考慮した手役の判定 + if (counts[3] > 0) { + // 3カードがある場合、ワイルドカードを加えて4カードにできるかチェック + if (wildCardCount > 0) { + return COMB.FOUR_CARD; + } + return COMB.THREE_CARD; + } + + if (counts[2] > 1) { + return COMB.TWO_PAIR; + } + + if (counts[2] == 1) { + // 1ペアがあれば、ワイルドカードを使って3カードにできるか確認 + if (wildCardCount > 0) { + return COMB.THREE_CARD; + } + return COMB.ONE_PAIR; + } + + // ノーペアの場合、ワイルドカードがあれば1ペアにできる + if (wildCardCount > 0) { + return COMB.ONE_PAIR; + } + + return COMB.NO_PAIR; + } +} diff --git a/yitou/B017_CardTest.java b/yitou/B017_CardTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2080d2508de2abcc6da810779e6fdebb38fee026 --- /dev/null +++ b/yitou/B017_CardTest.java @@ -0,0 +1,40 @@ +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; +import org.junit.jupiter.api.Test; + +public class B017_CardTest { + + @Test + public void ノーペアを判断できる() { + assertThat(B017_Card.getBestPair("ABCD"), is(B017_Card.COMB.NO_PAIR)); + assertThat(B017_Card.getBestPair("XYZW"), is(B017_Card.COMB.NO_PAIR)); + assertThat(B017_Card.getBestPair("VCSD"), is(B017_Card.COMB.NO_PAIR)); + } + + @Test + public void ワンペアを判断できる() { + assertThat(B017_Card.getBestPair("AZBB"), is(B017_Card.COMB.ONE_PAIR)); + assertThat(B017_Card.getBestPair("A*BC"), is(B017_Card.COMB.ONE_PAIR)); + assertThat(B017_Card.getBestPair("ABCA"), is(B017_Card.COMB.ONE_PAIR)); + } + + @Test + public void ツーペアを判断できる() { + assertThat(B017_Card.getBestPair("AABB"), is(B017_Card.COMB.TWO_PAIR)); + assertThat(B017_Card.getBestPair("ZAAZ"), is(B017_Card.COMB.TWO_PAIR)); + } + + @Test + public void スリーカードを判断できる() { + assertThat(B017_Card.getBestPair("AAAB"), is(B017_Card.COMB.THREE_CARD)); + assertThat(B017_Card.getBestPair("A*AB"), is(B017_Card.COMB.THREE_CARD)); + assertThat(B017_Card.getBestPair("CCAC"), is(B017_Card.COMB.THREE_CARD)); + } + + @Test + public void フォーカードを判断できる() { + assertThat(B017_Card.getBestPair("AAAA"), is(B017_Card.COMB.FOUR_CARD)); + assertThat(B017_Card.getBestPair("AA*A"), is(B017_Card.COMB.FOUR_CARD)); + assertThat(B017_Card.getBestPair("AAA*"), is(B017_Card.COMB.FOUR_CARD)); + } +}