Skip to content

Instantly share code, notes, and snippets.

@torazuka torazuka/Main.java
Created Jun 24, 2012

Embed
What would you like to do?
鍋谷さんの参考問題: Java解答
package sample;
import java.util.ArrayList;
import java.util.List;
class Card {
char suit;
String rank;
Card() {
rank = null;
}
}
/**
* 問題: http://qiita.com/items/cbc3af152ee3f50a822f
*/
public class Main {
protected List<Card> convertToCard(String str) {
List<Card> list = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'S' || ch == 'H' || ch == 'D' || ch == 'C') {
Card card = new Card();
card.suit = ch;
list.add(card);
} else {
Card card2 = list.remove(list.size() - 1);
if (card2.rank != null) {
String rank = card2.rank;
card2.rank = rank.concat(String.valueOf(ch));
} else {
card2.rank = String.valueOf(ch);
}
list.add(card2);
}
}
return list;
}
protected String confirmHand(List<Card> list) {
String result = null;
List<String> ranks = new ArrayList<String>();
List<String> pairs = new ArrayList<>();
for (Card card : list) {
for (String r : ranks) {
if (r.equals(card.rank)) {
pairs.add(card.rank);
break;
}
}
ranks.add(card.rank);
}
if (pairs == null || pairs.size() == 0) {
result = "--";
} else if (pairs.size() == 1) {
result = "1P"; // one pair
} else if (pairs.size() == 2) {
if (pairs.get(0).equals(pairs.get(1))) {
result = "3K"; // three cards
} else {
result = "2P"; // two pair
}
} else if (pairs.size() == 3) {
if (pairs.get(0).equals(pairs.get(1))
&& pairs.get(0).equals(pairs.get(2))) {
result = "4K"; // four cards
} else {
result = "FH"; // full house
}
}
return result;
}
}
package sample;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
public class TestMain {
Main main;
@Test
public void testOnePair() throws Exception {
main = new Main();
List<Card> list0 = main.convertToCard("S8S9HJS10D8");
assertEquals("1枚目と5枚目でワンペア。", "1P", main.confirmHand(list0));
List<Card> list1 = main.convertToCard("S8D8HJS10CQ");
assertEquals("1枚目と2枚目でワンペア。", "1P", main.confirmHand(list1));
List<Card> list2 = main.convertToCard("S5C8HJD8CQ");
assertEquals("2枚目と4枚目でワンペア。", "1P", main.confirmHand(list2));
List<Card> list3 = main.convertToCard("S2C8H8DJCQ");
assertEquals("2枚目と3枚目でワンペア。", "1P", main.confirmHand(list3));
List<Card> list4 = main.convertToCard("S2C8H3D8CQ");
assertEquals("2枚目と5枚目でワンペア。", "1P", main.confirmHand(list4));
List<Card> list5 = main.convertToCard("S2C8H2DJCQ");
assertEquals("1枚目と3枚目でワンペア。", "1P", main.confirmHand(list5));
List<Card> list6 = main.convertToCard("S2C8H3D2CQ");
assertEquals("1枚目と4枚目でワンペア。", "1P", main.confirmHand(list6));
List<Card> list7 = main.convertToCard("S2C10H8D8CQ");
assertEquals("3枚目と4枚目でワンペア。", "1P", main.confirmHand(list7));
List<Card> list8 = main.convertToCard("S2C5H8DJC8");
assertEquals("3枚目と5枚目でワンペア。", "1P", main.confirmHand(list8));
List<Card> list9 = main.convertToCard("S2C8H4DJCJ");
assertEquals("4枚目と5枚目でワンペア。", "1P", main.confirmHand(list9));
}
@Test
public void testTwoPair() throws Exception {
main = new Main();
List<Card> list0 = main.convertToCard("S8C8HJSJD2");
assertEquals("1、2枚目と3、4枚目でツーペア。", "2P", main.confirmHand(list0));
List<Card> list1 = main.convertToCard("S8D8HJS10CJ");
assertEquals("1、2枚目と3、5枚目でツーペア。", "2P", main.confirmHand(list1));
List<Card> list2 = main.convertToCard("S5CJHJD5CQ");
assertEquals("2、3枚目と1、4枚目でツーペア。", "2P", main.confirmHand(list2));
List<Card> list3 = main.convertToCard("S2C8H8DJC2");
assertEquals("2、3枚目と1、5枚目でツーペア。", "2P", main.confirmHand(list3));
List<Card> list4 = main.convertToCard("S2C2H3DQCQ");
assertEquals("1、2枚目と4、5枚目でツーペア。", "2P", main.confirmHand(list4));
List<Card> list5 = main.convertToCard("S2C8H8DQCQ");
assertEquals("2、3枚目と4、5枚目でツーペア。", "2P", main.confirmHand(list5));
List<Card> list6 = main.convertToCard("SQC8H3D3CQ");
assertEquals("3、4枚目と1、5枚目でツーペア。", "2P", main.confirmHand(list6));
List<Card> list7 = main.convertToCard("S2C10H2D10CQ");
assertEquals("1、3枚目と2、4枚目でツーペア。", "2P", main.confirmHand(list7));
List<Card> list8 = main.convertToCard("S2C5H2DJH5");
assertEquals("1、3枚目と2、5枚目でツーペア。", "2P", main.confirmHand(list8));
List<Card> list9 = main.convertToCard("S2C8H2DJCJ");
assertEquals("1、3枚目と4、5枚目でツーペア。", "2P", main.confirmHand(list9));
List<Card> list10 = main.convertToCard("S2CJH3D2CJ");
assertEquals("1、4枚目と2、5枚目でツーペア。", "2P", main.confirmHand(list10));
}
@Test
public void testThreeCard() throws Exception {
main = new Main();
List<Card> list0 = main.convertToCard("S8C8H8SJD2");
assertEquals("1、2、3枚目でスリーカード。", "3K", main.confirmHand(list0));
List<Card> list1 = main.convertToCard("S8D8HJH8C2");
assertEquals("1、2、4枚目でスリーカード。", "3K", main.confirmHand(list1));
List<Card> list2 = main.convertToCard("S5C5HJD7D5");
assertEquals("1、2、5枚目でスリーカード。", "3K", main.confirmHand(list2));
List<Card> list3 = main.convertToCard("S2C8H2D2C9");
assertEquals("1、3、4枚目でスリーカード。", "3K", main.confirmHand(list3));
List<Card> list4 = main.convertToCard("S2CJH2DQC2");
assertEquals("1、3、5枚目でスリーカード。", "3K", main.confirmHand(list4));
List<Card> list5 = main.convertToCard("S2C8HQD2C2");
assertEquals("1、4、5枚目でスリーカード。", "3K", main.confirmHand(list5));
List<Card> list6 = main.convertToCard("SQC8H8D8C2");
assertEquals("2、3、4枚目でスリーカード。", "3K", main.confirmHand(list6));
List<Card> list7 = main.convertToCard("S2C10H10D4D10");
assertEquals("2、3、5枚目でスリーカード。", "3K", main.confirmHand(list7));
List<Card> list8 = main.convertToCard("S2C5HKD5H5");
assertEquals("2、4、5枚目でスリーカード。", "3K", main.confirmHand(list8));
List<Card> list9 = main.convertToCard("S2C8H3D3C3");
assertEquals("3、4、5枚目でスリーカード。", "3K", main.confirmHand(list9));
}
@Test
public void testFourCard() throws Exception {
main = new Main();
List<Card> list0 = main.convertToCard("S8C8H8D8D2");
assertEquals("1、2、3、4枚目でフォーカード。", "4K", main.confirmHand(list0));
List<Card> list1 = main.convertToCard("S8D8H8HJC8");
assertEquals("1、2、3、5枚目でフォーカード。", "4K", main.confirmHand(list1));
List<Card> list2 = main.convertToCard("S5C5HJH5D5");
assertEquals("1、2、4、5枚目でフォーカード。", "4K", main.confirmHand(list2));
List<Card> list3 = main.convertToCard("S2C8H2D2C2");
assertEquals("1、3、4、5枚目でフォーカード。", "4K", main.confirmHand(list3));
List<Card> list4 = main.convertToCard("SKS2H2D2C2");
assertEquals("2、3、4、5枚目でフォーカード。", "4K", main.confirmHand(list4));
}
@Test
public void testFullHouse() throws Exception {
main = new Main();
List<Card> list0 = main.convertToCard("S8C8HJDJCJ");
assertEquals("1、2枚目でワンペア、3、4、5枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list0));
List<Card> list1 = main.convertToCard("S8DJH8HJCJ");
assertEquals("1、3枚目でワンペア、2、4、5枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list1));
List<Card> list2 = main.convertToCard("S5C2H2H5D2");
assertEquals("1、4枚目でワンペア、2、3、5枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list2));
List<Card> list3 = main.convertToCard("S2C8H8D8C2");
assertEquals("1、5枚目でワンペア、2、3、4枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list3));
List<Card> list4 = main.convertToCard("SKS2H2DKCK");
assertEquals("2、3枚目でワンペア、1、4、5枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list4));
List<Card> list5 = main.convertToCard("SKS2HKD2CK");
assertEquals("2、4枚目でワンペア、1、3、5枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list5));
List<Card> list6 = main.convertToCard("SKS2HKDKC2");
assertEquals("2、5枚目でワンペア、1、3、4枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list6));
List<Card> list7 = main.convertToCard("SKDKH2D2CK");
assertEquals("3、4枚目でワンペア、1、2、5枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list7));
List<Card> list8 = main.convertToCard("C8S8H2D8C2");
assertEquals("3、5枚目でワンペア、1、2、4枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list8));
List<Card> list9 = main.convertToCard("SKCKHKD2C2");
assertEquals("4、5枚目でワンペア、1、2、3枚目でスリーカードのフルハウス。", "FH",
main.confirmHand(list9));
}
@Test
public void testOther() throws Exception {
main = new Main();
List<Card> list0 = main.convertToCard("S1C2H3D4C5");
assertEquals("すべて別のランク。", "--", main.confirmHand(list0));
List<Card> list1 = main.convertToCard("S8S7S6S5S4");
assertEquals("同じSuiteだが、すべて別のランク。", "--", main.confirmHand(list1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.