summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Hunteman <michael@huntm.net>2023-01-28 12:07:30 -0600
committerMichael Hunteman <michael@huntm.net>2023-01-28 12:07:30 -0600
commitcf689151f984e751405f04baf569450fa2cd584c (patch)
tree72ffed9a75cf3c3ff91e6ee785df0f0accebd635
parent7ff5e12124aebe9255c7962ce6eb0ef49ceea9c1 (diff)
Add game logicHEADmaster
-rw-r--r--Game.classbin5582 -> 8354 bytes
-rw-r--r--Game.java82
-rw-r--r--card/Card$1.classbin661 -> 661 bytes
-rw-r--r--card/Card.classbin1351 -> 1741 bytes
-rw-r--r--card/Suit.classbin883 -> 942 bytes
5 files changed, 59 insertions, 23 deletions
diff --git a/Game.class b/Game.class
index 95ceca7..bfadce2 100644
--- a/Game.class
+++ b/Game.class
Binary files 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<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
index 2e6a9e7..3815ace 100644
--- a/card/Card$1.class
+++ b/card/Card$1.class
Binary files differ
diff --git a/card/Card.class b/card/Card.class
index 89c0106..acca707 100644
--- a/card/Card.class
+++ b/card/Card.class
Binary files differ
diff --git a/card/Suit.class b/card/Suit.class
index 0450003..f716f0e 100644
--- a/card/Suit.class
+++ b/card/Suit.class
Binary files differ