Skip to content
Snippets Groups Projects
Commit ed35bd55 authored by michael.divia's avatar michael.divia
Browse files

Resolved AI that coudn't loose

parent 78468fc2
No related branches found
No related tags found
No related merge requests found
Showing
with 372 additions and 270 deletions
Human,100.0,0
Player_1,190.0,1
Player_2,305.0,1
Human,90.0,0,0
Player_1,180.0,1,false
Player_2,2.0,1,true
......@@ -65,7 +65,7 @@ public class App {
// Load save to create game
Save Game = new Save();
BlackJack = new GameManager(Game.loadMoney(), Game.loadStrategy());
BlackJack = new GameManager(Game.loadMoney(), Game.loadStrategy(), Game.loadGameOver());
} else {
// Ask the number of players until a valide answer is given
......
......@@ -33,7 +33,7 @@ public class GameManager {
this.Dealer = new JoueurCroupier(Deck);
}
public GameManager(ArrayList<Double> Money, ArrayList<Integer> Strategy) {
public GameManager(ArrayList<Double> Money, ArrayList<Integer> Strategy, ArrayList<Boolean> GameOver) {
// Create a new BlackJack Deck of card (6 x 52 cards game)
this.Deck = new JeudeCarte(new Paquet(6, 52));
......@@ -48,9 +48,12 @@ public class GameManager {
// All other are controlled by the computer
if (Money.size() > 1) {
for (int x = 1; x < Money.size(); x++) {
if (!GameOver.get(x)) {
ComputerPlayers++;
this.Players.add(new JoueurOrdinateur(Deck, Money.get(x), Strategy.get(x)));
}
}
}
this.Dealer = new JoueurCroupier(Deck);
......@@ -105,9 +108,11 @@ public class GameManager {
// Set the Computer players bid for this turn
for (int x = 1; x <= this.ComputerPlayers; x++) {
if (!Players.get(x).IsGameOver()) {
Players.get(x).SetBet(10, 0);
}
}
}
// Phase 2 (PlayTurn):
// Ask the player for all his interactions with this cards
......@@ -181,12 +186,17 @@ public class GameManager {
// Show Computer Players hands
for (int x = 1; x <= this.ComputerPlayers; x++) {
for (int HandNbComputer = 0; HandNbComputer < this.Players.get(x).GetNbHands(); HandNbComputer++) {
if (!Players.get(x).IsGameOver()) {
for (int HandNbComputer = 0; HandNbComputer < this.Players.get(x)
.GetNbHands(); HandNbComputer++) {
System.out
.print("\nComputer " + App.ANSI_GREEN + x + App.ANSI_RESET + ", Money " + App.ANSI_BLUE
.print("\nComputer " + App.ANSI_GREEN + x + App.ANSI_RESET + ", Money "
+ App.ANSI_BLUE
+ this.Players.get(x).GetMoney() + App.ANSI_RESET + ", Hand "
+ App.ANSI_GREEN + HandNbComputer + App.ANSI_RESET + ", Bet " + App.ANSI_BLUE
+ this.Players.get(x).GetBet(HandNbComputer) + App.ANSI_RESET + ", Strength "
+ App.ANSI_GREEN + HandNbComputer + App.ANSI_RESET + ", Bet "
+ App.ANSI_BLUE
+ this.Players.get(x).GetBet(HandNbComputer) + App.ANSI_RESET
+ ", Strength "
+ App.ANSI_PURPLE);
if (this.Players.get(x).GetStrength(HandNbComputer) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET + " :");
......@@ -195,11 +205,13 @@ public class GameManager {
this.Players.get(x).GetStrength(HandNbComputer) + App.ANSI_RED + " [BUSTED]"
+ App.ANSI_RESET + " :");
} else {
System.out.println(this.Players.get(x).GetStrength(HandNbComputer) + App.ANSI_RESET + " :");
System.out.println(
this.Players.get(x).GetStrength(HandNbComputer) + App.ANSI_RESET + " :");
}
this.Players.get(x).ShowHand(HandNbComputer);
}
}
}
// Choices for player
System.out.println("\n--- " + App.ANSI_GREEN + "Choices" + App.ANSI_RESET + " ---\n");
......@@ -323,7 +335,7 @@ public class GameManager {
// Computer Players take turn to play
for (int x = 1; x <= this.ComputerPlayers; x++) {
if (!Players.get(x).IsGameOver()) {
// Check there strategy
/*
* 1 = Smart
......@@ -474,7 +486,8 @@ public class GameManager {
if (this.Players.get(x).GetNbCards(HandNb) == 2) {
Choice = Choices_2Cards[this.Dealer.GetCardStrength(0, 0) - 1][this.Players.get(x)
.GetCardStrength(HandNb, 0) - 1][this.Players.get(x).GetCardStrength(HandNb, 1) - 1];
.GetCardStrength(HandNb, 0) - 1][this.Players.get(x).GetCardStrength(HandNb, 1)
- 1];
} else {
if (this.Players.get(x).GetStrength(HandNb) <= 8) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][0];
......@@ -530,6 +543,7 @@ public class GameManager {
}
}
}
}
// Dealer draws card until he hits 17 or more
while (this.Dealer.GetStrength(0) <= 17) {
......@@ -569,6 +583,7 @@ public class GameManager {
// Apply Computer Players Loss/Win
for (int x = 1; x <= this.ComputerPlayers; x++) {
if (!Players.get(x).IsGameOver()) {
for (int HandNbComputer = 0; HandNbComputer < this.Players.get(x).GetNbHands(); HandNbComputer++) {
// If the player is Busted (strength > 21 but not BlackJack (99))
if (this.Players.get(x).GetStrength(HandNbComputer) > 21
......@@ -599,9 +614,11 @@ public class GameManager {
}
}
}
}
// Show Computer Players hands
for (int x = 1; x <= this.ComputerPlayers; x++) {
if (!Players.get(x).IsGameOver()) {
for (int HandNbComputer = 0; HandNbComputer < this.Players.get(x).GetNbHands(); HandNbComputer++) {
System.out
.print("\nComputer " + App.ANSI_GREEN + x + App.ANSI_RESET + ", Money " + App.ANSI_BLUE
......@@ -621,6 +638,7 @@ public class GameManager {
this.Players.get(x).ShowHand(HandNbComputer);
}
}
}
// Go thew all hands of the player
for (int HandNb = 0; HandNb < this.Players.get(0).GetNbHands(); HandNb++) {
......@@ -726,7 +744,7 @@ public class GameManager {
if (choice == 'c') {
// If the player has less than the minimum bid amount allowed
if (this.Players.get(0).GetMoney() <= 10) {
if (this.Players.get(0).GetMoney() < 10) {
return true;
}
......
......@@ -67,6 +67,12 @@ interface Joueur {
// Has the player insured himself
public boolean HasInsured();
// Set the player game status
public void SetGameOver(boolean state);
// Is the player game status
public boolean IsGameOver();
// Reset player for next turn
public void Reset(JeudeCarte Jeu);
}
......@@ -314,6 +320,16 @@ class JoueurHumain implements Joueur {
return this.Insured;
}
public void SetGameOver(boolean state) {
// Human Player doesn't GameOver like That
throw new RuntimeException("Human Player doesn't GameOver like That.");
}
public boolean IsGameOver() {
// Human Player can't be in state GameOver
throw new RuntimeException("Human Player can't be in state GameOver.");
}
public void Reset(JeudeCarte Jeu) {
this.Insured = false;
......@@ -335,6 +351,7 @@ class JoueurOrdinateur implements Joueur {
private double Money;
private double Insurance;
private boolean Insured;
private boolean GameOver;
/*
* 1 = Smart
......@@ -354,6 +371,24 @@ class JoueurOrdinateur implements Joueur {
this.Money = Money;
this.Insurance = 0;
this.Insured = false;
this.GameOver = false;
this.Strategy = Strategy;
}
public JoueurOrdinateur(JeudeCarte Jeu, double Money, int Strategy, boolean GameOver) {
this.Hands = new ArrayList<>();
// At creation, player get's 1 hand with 2 cards
Hands.add(new Hand(Jeu, 2));
Hands.get(0).SetBet(0);
this.Money = Money;
this.Insurance = 0;
this.Insured = false;
this.GameOver = GameOver;
this.Strategy = Strategy;
}
......@@ -578,8 +613,18 @@ class JoueurOrdinateur implements Joueur {
return this.Insured;
}
public void SetGameOver(boolean state) {
this.GameOver = state;
}
public boolean IsGameOver() {
return this.GameOver;
}
public void Reset(JeudeCarte Jeu) {
if (!this.IsGameOver() && this.GetMoney() >= 10) {
this.Insured = false;
// Reset hands
......@@ -589,6 +634,10 @@ class JoueurOrdinateur implements Joueur {
this.Hands.add(new Hand(Jeu, 2));
this.Hands.get(0).SetBet(0);
} else {
this.SetGameOver(true);
}
}
}
......@@ -739,6 +788,16 @@ class JoueurCroupier implements Joueur {
}
public void SetGameOver(boolean state) {
// Dealer can't GameOver
throw new RuntimeException("Dealer can't GameOver.");
}
public boolean IsGameOver() {
// Dealer can't GameOver
throw new RuntimeException("Dealer can't GameOver.");
}
public void Reset(JeudeCarte Jeu) {
// Create a new hand with 1 card
......
......@@ -28,7 +28,7 @@ public class Save {
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
String[] data = { "Human", Double.toString(Players.get(0).GetMoney()), "0" };
String[] data = { "Human", Double.toString(Players.get(0).GetMoney()), "0", "false" };
writer.writeNext(data);
......@@ -37,6 +37,7 @@ public class Save {
data[0] = "Player_" + x;
data[1] = Double.toString(Players.get(x).GetMoney());
data[2] = Integer.toString(Players.get(x).GetStrategy());
data[3] = Boolean.toString(Players.get(x).IsGameOver());
writer.writeNext(data);
}
......@@ -97,4 +98,28 @@ public class Save {
return Strategy;
}
public ArrayList<Boolean> loadGameOver() {
File file = new File("save.csv");
ArrayList<Boolean> Strategy = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String string : lines) {
String[] array = string.split(",");
Strategy.add(Boolean.valueOf(array[3]));
}
} catch (IOException e) {
e.printStackTrace();
}
return Strategy;
}
}
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment