From cf689151f984e751405f04baf569450fa2cd584c Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Sat, 28 Jan 2023 12:07:30 -0600 Subject: Add game logic --- Game.class | Bin 5582 -> 8354 bytes Game.java | 82 +++++++++++++++++++++++++++++++++++++++--------------- card/Card$1.class | Bin 661 -> 661 bytes card/Card.class | Bin 1351 -> 1741 bytes card/Suit.class | Bin 883 -> 942 bytes 5 files changed, 59 insertions(+), 23 deletions(-) diff --git a/Game.class b/Game.class index 95ceca7..bfadce2 100644 Binary files a/Game.class and b/Game.class differ diff --git a/Game.java b/Game.java index 8959282..903aa42 100644 --- a/Game.java +++ b/Game.java @@ -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 hand : hands) { + getHand(hand); + System.out.println(); + } + // flop, turn, and river + hands.add(setHand(5)); + List 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> combinations; + combinations = generate(hands.get(i)); + List combinationRanks = new ArrayList<>(); + for (List 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 setHand() { + public List setHand(int size) { List 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> generate(List hand) { + List> combinations = new ArrayList<>(); + helper(hand, new ArrayList(), combinations); + return combinations; + } + + public void helper(List hand, List combination, List> 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 hand) { if (isRoyalFlush(hand)) { return 9; @@ -303,7 +352,7 @@ class Game { return greater >= 2; } - public int getWinner() { + public int getWinner(List> hands, List 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 index 2e6a9e7..3815ace 100644 Binary files a/card/Card$1.class and b/card/Card$1.class differ diff --git a/card/Card.class b/card/Card.class index 89c0106..acca707 100644 Binary files a/card/Card.class and b/card/Card.class differ diff --git a/card/Suit.class b/card/Suit.class index 0450003..f716f0e 100644 Binary files a/card/Suit.class and b/card/Suit.class differ -- cgit v1.2.3