Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- public class _03FullHouse {
- public static void main(String[] args) {
- char[] suits = {'\u2660','\u2665','\u2666','\u2663'};
- char[] cards = {'2','3','4','5','6','7','8','9','T','J','Q','K','A',};
- String[] allCards= new String[52];
- //add all cards in array
- int indexCounter = 0;
- for (int i = 0; i < cards.length; i++) {
- for (int j = 0; j < suits.length; j++) {
- allCards[indexCounter] = Character.toString(cards[i]) + Character.toString(suits[j]);
- indexCounter++;
- }
- }
- int fullHouseCounter = 0;
- for (int i1 = 0; i1 < 48; i1++)
- for (int i2 = i1 + 1; i2 < 49; i2++)
- for (int i3 = i2 + 1; i3 < 50; i3++)
- for (int i4 = i3 + 1; i4 < 51; i4++)
- for (int i5 = i4 + 1; i5 < 52; i5++) {
- char[] handsCardsArr = {
- allCards[i1].charAt(0),
- allCards[i2].charAt(0),
- allCards[i3].charAt(0),
- allCards[i4].charAt(0),
- allCards[i5].charAt(0)
- };
- // the HashSet counts distinct values
- // the logic is that all full houses have only 2 different card values
- Set<Character> fullHouseSet = new HashSet<>();
- for (char c : handsCardsArr) {
- fullHouseSet.add(c);
- }
- if (fullHouseSet.size() == 2) {
- // four of a kind hands have 2 distinct cards as well so
- // we check if the hand is four of a kind
- char firstChar = handsCardsArr[0];
- char lastChar = handsCardsArr[4];
- int diffCounter1 = 0;
- int diffCounter2 = 0;
- for (int i = 0; i < handsCardsArr.length; i++) {
- if(handsCardsArr[i] == firstChar) {
- diffCounter1++;
- }
- else if (handsCardsArr[i] == lastChar) {
- diffCounter2++;
- }
- }
- boolean notFourOfAkind = diffCounter1 != 4 && diffCounter2 !=4;
- if (notFourOfAkind) {
- System.out.println(allCards[i1] + allCards[i2] +
- allCards[i3] + allCards[i4] + allCards[i5]);
- fullHouseCounter++;
- }
- }
- }
- System.out.println(fullHouseCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement