Skip to content
Snippets Groups Projects
Commit 74eb1c56 authored by iliya's avatar iliya
Browse files

feat: continuing to implement the advanced version of the ai player

parent cc081adf
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,8 @@ public class AIPlayer implements IPlayer { ...@@ -17,7 +17,8 @@ public class AIPlayer implements IPlayer {
private int insuranceBet; private int insuranceBet;
private boolean hasDoubledBet; private boolean hasDoubledBet;
private static Map<Integer, Map<Integer, Action>> strategyTable = new HashMap<>(); private static Map<Integer, Map<Integer, Action>> strategyTableWithoutAces = new HashMap<>();
private static Map<Integer, Map<Integer, Action>> strategyTableWithAces = new HashMap<>();
static { static {
Map<Integer, Action> handWeight8orLess = new HashMap<>(); Map<Integer, Action> handWeight8orLess = new HashMap<>();
...@@ -107,23 +108,121 @@ public class AIPlayer implements IPlayer { ...@@ -107,23 +108,121 @@ public class AIPlayer implements IPlayer {
} }
for (int i = 1; i <= 8; i++) { for (int i = 1; i <= 8; i++) {
strategyTable.put(i, handWeight8orLess); strategyTableWithoutAces.put(i, handWeight8orLess);
} }
strategyTable.put(9, handWeight9); strategyTableWithoutAces.put(9, handWeight9);
strategyTable.put(10, handWeight10); strategyTableWithoutAces.put(10, handWeight10);
strategyTable.put(11, handWeight11); strategyTableWithoutAces.put(11, handWeight11);
strategyTable.put(12, handWeight12); strategyTableWithoutAces.put(12, handWeight12);
strategyTable.put(13, handWeight13); strategyTableWithoutAces.put(13, handWeight13);
strategyTable.put(14, handWeight14); strategyTableWithoutAces.put(14, handWeight14);
strategyTable.put(15, handWeight15); strategyTableWithoutAces.put(15, handWeight15);
strategyTable.put(16, handWeight16); strategyTableWithoutAces.put(16, handWeight16);
for (int i = 17; i < 21; i++) { for (int i = 17; i < 21; i++) {
strategyTable.put(i, handWeight17OrAbove); strategyTableWithoutAces.put(i, handWeight17OrAbove);
} }
} }
static {
Map<Integer, Action> ace8orHigher = new HashMap<>();
for (int i = 1; i <= 10; i++) {
ace8orHigher.put(i, Action.STAND);
}
Map<Integer, Action> ace7 = new HashMap<>();
ace7.put(1, Action.HIT);
ace7.put(2, Action.STAND);
for (int i = 3; i <= 6; i++) {
ace7.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 8; i++) {
ace7.put(i, Action.STAND);
}
for (int i = 9; i <= 10; i++) {
ace7.put(i, Action.HIT);
}
Map<Integer, Action> ace6 = new HashMap<>();
ace6.put(1, Action.HIT);
ace6.put(2, Action.HIT);
for (int i = 3; i <= 6; i++) {
ace6.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 10; i++) {
ace6.put(i, Action.HIT);
}
Map<Integer, Action> ace5 = new HashMap<>();
for (int i = 1; i <= 3; i++) {
ace5.put(i, Action.HIT);
}
for (int i = 4; i <= 6; i++) {
ace5.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 10; i++) {
ace5.put(i, Action.HIT);
}
Map<Integer, Action> ace4 = new HashMap<>();
for (int i = 1; i <= 3; i++) {
ace4.put(i, Action.HIT);
}
for (int i = 4; i <= 6; i++) {
ace4.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 10; i++) {
ace4.put(i, Action.HIT);
}
Map<Integer, Action> ace3 = new HashMap<>();
for (int i = 1; i <= 4; i++) {
ace3.put(i, Action.HIT);
}
for (int i = 5; i <= 6; i++) {
ace3.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 10; i++) {
ace3.put(i, Action.HIT);
}
Map<Integer, Action> ace2 = new HashMap<>();
for (int i = 1; i <= 4; i++) {
ace2.put(i, Action.HIT);
}
for (int i = 5; i <= 6; i++) {
ace2.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 10; i++) {
ace2.put(i, Action.HIT);
}
strategyTableWithAces.put(8, ace7);
strategyTableWithAces.put(18, ace7);
strategyTableWithAces.put(7, ace6);
strategyTableWithAces.put(17, ace6);
strategyTableWithAces.put(6, ace5);
strategyTableWithAces.put(16, ace5);
strategyTableWithAces.put(5, ace4);
strategyTableWithAces.put(15, ace4);
strategyTableWithAces.put(4, ace3);
strategyTableWithAces.put(14, ace3);
strategyTableWithAces.put(3, ace2);
strategyTableWithAces.put(13, ace2);
strategyTableWithAces.put(19, ace8orHigher);
strategyTableWithAces.put(20, ace8orHigher);
strategyTableWithAces.put(21, ace8orHigher);
}
@Override @Override
public boolean hasSplitHand() { public boolean hasSplitHand() {
if (splittedHand != null) { if (splittedHand != null) {
...@@ -265,12 +364,15 @@ public class AIPlayer implements IPlayer { ...@@ -265,12 +364,15 @@ public class AIPlayer implements IPlayer {
} }
public void advancedStrategy(Deck currentGame, Hand croupierHand) { public void advancedStrategy(Deck currentGame, Hand croupierHand) {
boolean handContainsAce = false;
Map<Integer, Action> innerMap;
while (this.getHand().computeHandValue() < 21) { while (this.getHand().computeHandValue() < 21) {
this.displayPlayerHand(); this.displayPlayerHand();
for (Card card : this.getHand()) { for (Card card : this.getHand()) {
if (card.getRankName().equals("As")) { if (card.getRankName().equals("As")) {
handContainsAce = true;
if (this.getHand().computeHandValue() + 10 <= 21) { if (this.getHand().computeHandValue() + 10 <= 21) {
System.out.println("AI assigned the value 11 to the ace"); System.out.println("AI assigned the value 11 to the ace");
card.setWeight(11); card.setWeight(11);
...@@ -278,12 +380,23 @@ public class AIPlayer implements IPlayer { ...@@ -278,12 +380,23 @@ public class AIPlayer implements IPlayer {
if (this.getHand().computeHandValue() == 21) { if (this.getHand().computeHandValue() == 21) {
break; break;
} }
// System.out.println("AI drew a new card");
// this.getHand().addToHand(currentGame.removeFromDeck());
} }
} }
} }
Map<Integer, Action> inner = strategyTable.get(this.getHand().computeHandValue()); if (handContainsAce) {
Action act = inner.get(croupierHand.computeHandValue()); innerMap = strategyTableWithAces.get(this.getHand().computeHandValue());
} else {
innerMap = strategyTableWithoutAces.get(this.getHand().computeHandValue());
}
System.out.println("DEBUG(" + handContainsAce + "): innerMap = " + innerMap);
if (innerMap.containsKey(croupierHand.computeHandValue())) {
Action act = innerMap.get(croupierHand.computeHandValue());
switch (act) { switch (act) {
case HIT: case HIT:
...@@ -296,6 +409,9 @@ public class AIPlayer implements IPlayer { ...@@ -296,6 +409,9 @@ public class AIPlayer implements IPlayer {
case DOUBLEDOWN: case DOUBLEDOWN:
System.out.println("AI player has doubled its bet"); System.out.println("AI player has doubled its bet");
this.setDoubledBet(true); this.setDoubledBet(true);
System.out.println("AI drew a new card");
this.getHand().addToHand(currentGame.removeFromDeck());
break;
case SPLIT: case SPLIT:
// TODO // TODO
// System.out.println("AI player has doubled its bet"); // System.out.println("AI player has doubled its bet");
...@@ -307,6 +423,7 @@ public class AIPlayer implements IPlayer { ...@@ -307,6 +423,7 @@ public class AIPlayer implements IPlayer {
default: default:
break; break;
} }
}
try { try {
Thread.sleep(4000); Thread.sleep(4000);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment