From 21e5e693ec6eb87a8c0e91bc332dd95b99646439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Wed, 2 Oct 2024 17:26:13 +0900 Subject: [PATCH] =?UTF-8?q?=E5=95=8F=E9=A1=8CB017?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/B017_Card.java | 87 ++++++++++++++++++++++++++++++++++++++++ yitou/B017_CardTest.java | 40 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 yitou/B017_Card.java create mode 100644 yitou/B017_CardTest.java diff --git a/yitou/B017_Card.java b/yitou/B017_Card.java new file mode 100644 index 0000000..b338a3e --- /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 0000000..2080d25 --- /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)); + } +} -- GitLab