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

feat: basic implementation of advanced strategy seems to be working

parent c3f764fc
No related branches found
No related tags found
No related merge requests found
package hepia; package hepia;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* AIPlayer * AIPlayer
...@@ -15,6 +17,113 @@ public class AIPlayer implements IPlayer { ...@@ -15,6 +17,113 @@ 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<>();
static {
Map<Integer, Action> handWeight8orLess = new HashMap<>();
// value 1 should be ACE!
for (int i = 1; i <= 10; i++) {
handWeight8orLess.put(i, Action.HIT);
}
Map<Integer, Action> handWeight9 = new HashMap<>();
for (int i = 1; i <= 2; i++) {
handWeight9.put(i, Action.HIT);
}
for (int i = 3; i <= 6; i++) {
handWeight9.put(i, Action.DOUBLEDOWN);
}
for (int i = 7; i <= 10; i++) {
handWeight9.put(i, Action.HIT);
}
Map<Integer, Action> handWeight10 = new HashMap<>();
handWeight10.put(1, Action.HIT);
for (int i = 2; i <= 9; i++) {
handWeight10.put(i, Action.DOUBLEDOWN);
}
handWeight10.put(10, Action.HIT);
Map<Integer, Action> handWeight11 = new HashMap<>();
handWeight11.put(1, Action.HIT);
for (int i = 2; i <= 10; i++) {
handWeight11.put(i, Action.DOUBLEDOWN);
}
Map<Integer, Action> handWeight12 = new HashMap<>();
for (int i = 1; i <= 3; i++) {
handWeight12.put(i, Action.HIT);
}
for (int i = 4; i <= 6; i++) {
handWeight12.put(i, Action.STAND);
}
for (int i = 7; i <= 10; i++) {
handWeight12.put(i, Action.HIT);
}
Map<Integer, Action> handWeight13 = new HashMap<>();
handWeight13.put(1, Action.HIT);
for (int i = 2; i <= 6; i++) {
handWeight13.put(i, Action.STAND);
}
for (int i = 7; i <= 10; i++) {
handWeight13.put(i, Action.HIT);
}
Map<Integer, Action> handWeight14 = new HashMap<>();
handWeight14.put(1, Action.HIT);
for (int i = 2; i <= 6; i++) {
handWeight14.put(i, Action.STAND);
}
for (int i = 7; i <= 10; i++) {
handWeight14.put(i, Action.HIT);
}
Map<Integer, Action> handWeight15 = new HashMap<>();
handWeight15.put(1, Action.HIT);
for (int i = 2; i <= 6; i++) {
handWeight15.put(i, Action.STAND);
}
for (int i = 7; i <= 9; i++) {
handWeight15.put(i, Action.HIT);
}
handWeight15.put(10, Action.SURRENDER);
Map<Integer, Action> handWeight16 = new HashMap<>();
handWeight16.put(1, Action.SURRENDER);
for (int i = 2; i <= 6; i++) {
handWeight16.put(i, Action.STAND);
}
for (int i = 7; i <= 8; i++) {
handWeight16.put(i, Action.HIT);
}
for (int i = 9; i <= 10; i++) {
handWeight16.put(i, Action.SURRENDER);
}
Map<Integer, Action> handWeight17OrAbove = new HashMap<>();
for (int i = 1; i <= 10; i++) {
handWeight17OrAbove.put(i, Action.STAND);
}
for (int i = 1; i <= 8; i++) {
strategyTable.put(i, handWeight8orLess);
}
strategyTable.put(9, handWeight9);
strategyTable.put(10, handWeight10);
strategyTable.put(11, handWeight11);
strategyTable.put(12, handWeight12);
strategyTable.put(13, handWeight13);
strategyTable.put(14, handWeight14);
strategyTable.put(15, handWeight15);
strategyTable.put(16, handWeight16);
for (int i = 17; i < 21; i++) {
strategyTable.put(i, handWeight17OrAbove);
}
}
@Override @Override
public boolean hasSplitHand() { public boolean hasSplitHand() {
if (splittedHand != null) { if (splittedHand != null) {
...@@ -153,7 +262,58 @@ public class AIPlayer implements IPlayer { ...@@ -153,7 +262,58 @@ public class AIPlayer implements IPlayer {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
public void advancedStrategy(Deck currentGame, Hand croupierHand) {
while (this.getHand().computeHandValue() < 21) {
this.displayPlayerHand();
for (Card card : this.getHand()) {
if (card.getRankName().equals("As")) {
if (this.getHand().computeHandValue() + 10 <= 21) {
System.out.println("AI assigned the value 11 to the ace");
card.setWeight(11);
if (this.getHand().computeHandValue() == 21) {
break;
}
}
}
}
Map<Integer, Action> inner = strategyTable.get(this.getHand().computeHandValue());
Action act = inner.get(croupierHand.computeHandValue());
switch (act) {
case HIT:
System.out.println("AI drew a new card");
this.getHand().addToHand(currentGame.removeFromDeck());
break;
case STAND:
System.out.println("AI has decided to stand");
return;
case DOUBLEDOWN:
System.out.println("AI player has doubled its bet");
this.setDoubledBet(true);
case SPLIT:
// TODO
// System.out.println("AI player has doubled its bet");
// this.setDoubledBet(true);
break;
case SURRENDER:
System.out.println("AI has decided to surrender");
return;
default:
break;
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
@Override @Override
......
...@@ -355,7 +355,7 @@ public class GameManager { ...@@ -355,7 +355,7 @@ public class GameManager {
for (AIPlayer aiPlayer : this.aiPlayers) { for (AIPlayer aiPlayer : this.aiPlayers) {
System.out.println(aiPlayer); System.out.println(aiPlayer);
aiPlayer.basicStrategy(this.currentGame); aiPlayer.advancedStrategy(this.currentGame, this.croupier.getHand());
} }
// Croupier's turn // Croupier's turn
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment