diff options
-rw-r--r-- | Game.class | bin | 5582 -> 8354 bytes | |||
-rw-r--r-- | Game.java | 82 | ||||
-rw-r--r-- | card/Card$1.class | bin | 661 -> 661 bytes | |||
-rw-r--r-- | card/Card.class | bin | 1351 -> 1741 bytes | |||
-rw-r--r-- | card/Suit.class | bin | 883 -> 942 bytes |
5 files changed, 59 insertions, 23 deletions
Binary files differ @@ -12,8 +12,37 @@ class Game { public Game(int handsNum) { setDeck(); + // preflop + for (int i = 0; i < handsNum; i++) { + hands.add(setHand(2)); + } + for (List<Card> hand : hands) { + getHand(hand); + System.out.println(); + } + // flop, turn, and river + hands.add(setHand(5)); + List<Card> street = hands.get(hands.size() - 1); + getHand(hands.get(hands.size() - 1)); + System.out.println(); + for (int i = 0; i < handsNum; i++) { + for (int j = 0; j < 5; j++) { + hands.get(i).add(street.get(j)); + } + hands.set(i, sort(hands.get(i))); + } + // TODO: find a better way to calculate combinations for each hand + for (int i = 0; i < handsNum; i++) { + List<List<Card>> combinations; + combinations = generate(hands.get(i)); + List<Integer> combinationRanks = new ArrayList<>(); + for (List<Card> combination : combinations) { + combinationRanks.add(getRank(combination)); + } + int winner = getWinner(combinations, combinationRanks); + hands.set(i, combinations.get(winner)); + } for (int i = 0; i < handsNum; i++) { - hands.add(setHand()); ranks.add(getRank(hands.get(i))); } } @@ -75,10 +104,10 @@ class Game { return result; } - public List<Card> setHand() { + public List<Card> setHand(int size) { List<Card> hand = new ArrayList<>(); Random rand = new Random(); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < size; i++) { int randInt = rand.nextInt(deck.size()); hand.add(deck.get(randInt)); deck.remove(randInt); @@ -92,6 +121,26 @@ class Game { } } + public List<List<Card>> generate(List<Card> hand) { + List<List<Card>> combinations = new ArrayList<>(); + helper(hand, new ArrayList<Card>(), combinations); + return combinations; + } + + public void helper(List<Card> hand, List<Card> combination, List<List<Card>> combinations) { + if (combination.size() == 5) { + combinations.add(new ArrayList<>(sort(combination))); + } else { + for (int i = 0; i < 5; i++) { + if (combination.contains(hand.get(i))) + continue; + combination.add(hand.get(i)); + helper(hand, combination, combinations); + combination.remove(combination.size() - 1); + } + } + } + public int getRank(List<Card> hand) { if (isRoyalFlush(hand)) { return 9; @@ -303,7 +352,7 @@ class Game { return greater >= 2; } - public int getWinner() { + public int getWinner(List<List<Card>> hands, List<Integer> ranks) { int bestHand = 0, bestRank = 0; for (int i = 0; i < ranks.size(); i++) { if (ranks.get(i) > bestRank) { @@ -312,23 +361,8 @@ class Game { } else if (ranks.get(i) == bestRank) { switch (bestRank) { case 7: - if (compareTwoPair(hands.get(i), hands.get(bestHand))) { - bestHand = i; - bestRank = ranks.get(i); - } - break; case 6: - if (compareTwoPair(hands.get(i), hands.get(bestHand))) { - bestHand = i; - bestRank = ranks.get(i); - } - break; case 3: - if (compareTwoPair(hands.get(i), hands.get(bestHand))) { - bestHand = i; - bestRank = ranks.get(i); - } - break; case 2: if (compareTwoPair(hands.get(i), hands.get(bestHand))) { bestHand = i; @@ -341,6 +375,8 @@ class Game { bestRank = ranks.get(i); } break; + case 5: + case 4: case 0: if (compareHighCard(hands.get(i), hands.get(bestHand))) { bestHand = i; @@ -352,13 +388,13 @@ class Game { return bestHand; } - public static void main(String args[]) { - Game g = new Game(10); - for (int i = 0; i < g.hands.size(); i++) { + public static void main(String[] args) { + Game g = new Game(8); + for (int i = 0; i < 8; i++) { g.getHand(g.hands.get(i)); g.displayRank(g.hands.get(i), g.ranks.get(i)); System.out.println(); } - System.out.println(g.getWinner()); + System.out.println(g.getWinner(g.hands, g.ranks)); } } diff --git a/card/Card$1.class b/card/Card$1.class Binary files differindex 2e6a9e7..3815ace 100644 --- a/card/Card$1.class +++ b/card/Card$1.class diff --git a/card/Card.class b/card/Card.class Binary files differindex 89c0106..acca707 100644 --- a/card/Card.class +++ b/card/Card.class diff --git a/card/Suit.class b/card/Suit.class Binary files differindex 0450003..f716f0e 100644 --- a/card/Suit.class +++ b/card/Suit.class |