summaryrefslogtreecommitdiff
path: root/Game.java
diff options
context:
space:
mode:
Diffstat (limited to 'Game.java')
-rw-r--r--Game.java82
1 files changed, 59 insertions, 23 deletions
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));
}
}