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

Part 2 75%

parent 4f09cb9e
No related branches found
No related tags found
No related merge requests found
Showing
with 434 additions and 29 deletions
......@@ -67,9 +67,20 @@ public class App {
GameManager BlackJack = new GameManager(numPlayers, startMoney);
boolean GameOver = false;
while (!GameOver) {
BlackJack.StartTurn();
BlackJack.PlayTurn();
GameOver = BlackJack.ResolveTurn();
}
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
BlackJack.DisplayTurn();
System.out.println(App.ANSI_PURPLE + "Game Over !" + App.ANSI_RESET);
App.in.close();
}
......
......@@ -8,6 +8,7 @@ public class GameManager {
private JeudeCarte Deck;
private ArrayList<Joueur> Players;
private JoueurCroupier Dealer;
private int Step;
public GameManager(int nbPlayer, int startMoney) {
......@@ -26,25 +27,162 @@ public class GameManager {
this.Dealer = new JoueurCroupier(Deck);
}
public void DisplayTurn() {
public boolean ResolveTurn() {
this.Dealer.DrawCard(0, this.Deck);
// If the player has less than the minimum bid amount allowed
if (this.Players.get(0).GetMoney() <= 10) {
return true;
}
this.Dealer.Reset(this.Deck);
for (Joueur Player : this.Players) {
Player.Reset(this.Deck);
}
return false;
}
public void PlayTurn() {
boolean EndTurn = false;
while (!EndTurn) {
boolean CanSplit = false, CanInsure = false, CanDouble = false, CanDraw = false;
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
System.out.println("Dealer Score : " + App.ANSI_PURPLE + this.Dealer.GetForce(0) + App.ANSI_RESET);
System.out.println("Cards in Deck : " + App.ANSI_GREEN + this.Deck.GetNbCards() + App.ANSI_RESET + "\n");
System.out.print("Dealer Score : " + App.ANSI_PURPLE);
if (this.Dealer.GetForce(0) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else {
System.out.println(this.Dealer.GetForce(0) + App.ANSI_RESET);
}
this.Dealer.ShowHands();
System.out.println("\nMoney : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET + " // Bet : "
+ App.ANSI_BLUE + this.Players.get(0).GetBet(0) + App.ANSI_RESET);
System.out.println("Player Score : " + App.ANSI_PURPLE + this.Players.get(0).GetForce(0) + App.ANSI_RESET);
System.out.println("\nMoney : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET);
if (this.Players.get(0).HasInsured()) {
System.out.println("Insured : " + App.ANSI_BLUE + this.Players.get(0).GetInsured() + App.ANSI_RESET);
}
char choice = 'x';
for (int HandNb = 0; HandNb < this.Players.get(0).NbHands(); HandNb++) {
System.out
.println("Bet on Hand " + App.ANSI_GREEN + (HandNb + 1) + App.ANSI_RESET + " : " + App.ANSI_BLUE
+ this.Players.get(0).GetBet(HandNb) + App.ANSI_RESET);
System.out
.print("Score of Hand " + App.ANSI_GREEN + (HandNb + 1) + App.ANSI_RESET + " : "
+ App.ANSI_PURPLE);
if (this.Players.get(0).GetForce(HandNb) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else {
System.out.println(this.Players.get(0).GetForce(HandNb) + App.ANSI_RESET);
}
this.Players.get(0).ShowHands();
// Choices for player
System.out.println("\n--- " + App.ANSI_GREEN + "Choices" + App.ANSI_RESET + " ---\n");
// Insurance
// Can only insure if it's the first thing that the players does
// AND
// That he hasn't already insured during this round
if (this.Step == 0 && this.Dealer.OnlyAs()
&& !this.Players.get(0).HasInsured()) {
CanInsure = true;
System.out.println(App.ANSI_BLUE + "[i]" + App.ANSI_RESET + " Insurance against Dealer");
}
// Split
// Can only split if this hand can be split (if it wasn't already split AND if
// the 2 cards have the same strength)
// AND
// if we have less that 3 hands already (Max 2 splits)
if (this.Players.get(0).CanSplit(HandNb)
&& this.Players.get(0).NbHands() < 3) {
CanSplit = true;
System.out.println(App.ANSI_BLUE + "[s]" + App.ANSI_RESET + " Split your Hand");
}
// Double
// Can only double if this hand wasn't already doubled
// AND
// if the hand has only 2 cards
// AND
// if the hand is not a BlackJack
if (!this.Players.get(0).HasDoubled(HandNb)
&& this.Players.get(0).NbCards(HandNb) == 2
&& this.Players.get(0).GetForce(HandNb) != 99) {
CanDouble = true;
System.out.println(App.ANSI_BLUE + "[d]" + App.ANSI_RESET + " Double your Hand");
}
// Draw a Card (Hit)
// Can NOT draw if this hand was doubled AND has already 3 cards
// AND
// if the hand has been splitted with a pair of Asses
// AND
// if the player has a BlackJack
if (!(this.Players.get(0).HasDoubled(HandNb) && this.Players.get(0).NbCards(HandNb) == 3)
&& !(this.Players.get(0).CanSplit(HandNb) && this.Players.get(0).GetCardForce(HandNb, 0) == 1)
&& this.Players.get(0).GetForce(HandNb) != 99) {
CanDraw = true;
System.out.println(App.ANSI_BLUE + "[h]" + App.ANSI_RESET + " Hit");
}
// Stand
System.out.println(App.ANSI_BLUE + "[s]" + App.ANSI_RESET + " Stand");
if (CanInsure || CanSplit || CanDouble || CanDraw || this.Players.get(0).GetForce(HandNb) == 99) {
while (true) {
System.out.print("> ");
choice = App.in.next().charAt(0);
if ((choice == 'i' && CanInsure) || (choice == 's' && CanSplit) || (choice == 'd' && CanDouble)
|| (choice == 'h' && CanDraw)
|| choice == 's') {
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
}
}
}
if (choice == 'h') {
this.Players.get(0).DrawCard(HandNb, this.Deck);
}
Step++;
}
if (choice == 'i') {
this.Players.get(0).Insure();
EndTurn = false;
} else if (choice == 'h') {
EndTurn = false;
} else {
EndTurn = true;
}
}
}
public void StartTurn() {
this.Step = 0;
System.out.print("\033[H\033[2J");
System.out.flush();
......@@ -56,7 +194,7 @@ public class GameManager {
while (true) {
System.out.print("How much do you want to bet ? ");
System.out.print("How much do you want to bet (Min. 10) ? ");
while (!App.in.hasNextDouble()) {
System.out.println(App.ANSI_YELLOW + "Please enter a valid number to bet." + App.ANSI_RESET);
......@@ -67,7 +205,12 @@ public class GameManager {
bet = App.in.nextDouble();
if (bet <= this.Players.get(0).GetMoney()) {
if (bet < 10) {
System.out.println(App.ANSI_YELLOW + "Minimum bid amount is 10." + App.ANSI_RESET);
} else {
break;
}
} else {
System.out.println(App.ANSI_YELLOW + "You don't have enough money." + App.ANSI_RESET);
}
......
......@@ -8,23 +8,51 @@ public class Hand implements Comparable<Hand> {
private ArrayList<Carte> Hand;
private double Bet;
private boolean Splitted;
private boolean Doubled;
public Hand(Carte... cartes) {
Hand = new ArrayList<>();
this.Hand = new ArrayList<>();
for (Carte carte : cartes) {
Hand.add(carte);
}
this.Splitted = false;
this.Doubled = false;
}
public Hand(JeudeCarte game, int nb) {
Hand = new ArrayList<>();
this.Hand = new ArrayList<>();
this.DrawCardFromGame(game, nb);
this.Splitted = false;
this.Doubled = false;
}
public boolean HasDoubled() {
return this.Doubled;
}
public void Double() {
this.Doubled = true;
}
public boolean HasSplit() {
return this.Splitted;
}
public void Splitted() {
this.Splitted = true;
}
public int NbCard() {
return this.Hand.size();
}
public void SetBet(double amount) {
......@@ -122,6 +150,33 @@ public class Hand implements Comparable<Hand> {
}
}
// Edge Case = BlackJack
if (force == 21) {
boolean isAs = false;
boolean is10 = false;
boolean isOther = false;
for (Carte carte : Hand) {
switch (carte.getForce()) {
case 1:
isAs = true;
break;
case 10:
is10 = true;
break;
default:
isOther = true;
break;
}
}
if (isAs && is10 && !isOther) {
return 99;
}
}
return force;
}
......@@ -141,16 +196,8 @@ public class Hand implements Comparable<Hand> {
@Override
public int compareTo(Hand otherHand) {
int Hand_1_Power = 0;
int Hand_2_Power = 0;
for (Carte carte : Hand) {
Hand_1_Power += carte.getRang();
}
for (Carte carte : otherHand.Hand) {
Hand_2_Power += carte.getRang();
}
int Hand_1_Power = this.GetForce();
int Hand_2_Power = otherHand.GetForce();
if (Hand_1_Power > Hand_2_Power) {
return 1;
......
......@@ -70,7 +70,7 @@ public class JeudeCarte {
}
public List<Carte> getCartes() {
return jeuDeCartes;
return this.jeuDeCartes;
}
public Carte GetTopCarteOfGame() {
......@@ -103,6 +103,10 @@ public class JeudeCarte {
}
public int GetNbCards() {
return this.jeuDeCartes.size();
}
public void ShowPaquet() {
System.out.println("------- Paquet -------");
......
......@@ -25,12 +25,34 @@ interface Joueur {
public void Split(int handNb);
public int GetForce(int handNb);
public boolean CanSplit(int handNb);
public void Reset(JeudeCarte Jeu);
public boolean HasInsured();
public void Insure();
public double GetInsured();
public boolean HasDoubled(int handNb);
public void Double(int handNb);
public int NbHands();
public int NbCards(int handNb);
public int GetCardForce(int handNb, int cardNb);
}
class JoueurHumain implements Joueur {
private ArrayList<Hand> Hands;
private double Money;
private double Insurance;
private boolean Insured;
public JoueurHumain(JeudeCarte Jeu, int Money) {
this.Hands = new ArrayList<>();
......@@ -40,6 +62,49 @@ class JoueurHumain implements Joueur {
this.Hands.get(0).SetBet(0);
this.Money = Money;
this.Insurance = 0;
this.Insured = false;
}
public void Reset(JeudeCarte Jeu) {
this.Insured = false;
this.Hands = new ArrayList<>();
this.Hands.add(new Hand(Jeu, 2));
this.Hands.get(0).SetBet(0);
}
public boolean HasDoubled(int handNb) {
return this.Hands.get(handNb).HasDoubled();
}
public void Double(int handNb) {
this.Hands.get(handNb).Double();
}
public boolean CanSplit(int handNb) {
if (!this.Hands.get(handNb).HasSplit()
&& this.Hands.get(handNb).GetCarte(0).getForce() == this.Hands.get(handNb).GetCarte(1).getForce()) {
return true;
}
return false;
}
public boolean HasInsured() {
return this.Insured;
}
public void Insure() {
this.Insured = true;
this.Insurance = this.Hands.get(0).GetBet() / 2;
}
public double GetInsured() {
return this.Insurance;
}
public void DefineStrategy() {
......@@ -125,22 +190,40 @@ class JoueurHumain implements Joueur {
public void Split(int handNb) {
if (Hands.size() == 3) {
if (this.Hands.size() == 3) {
throw new RuntimeException("Max number of slipts.");
} else if (this.Hands.get(handNb).HasSplit()) {
throw new RuntimeException("Can't split Hand twice.");
}
Carte card = this.Hands.get(handNb).GetCarte(handNb);
this.Hands.get(handNb).RemoveCardFromHand(card);
this.Hands.get(handNb).Splitted();
this.Hands.add(new Hand(card));
}
public int NbHands() {
return this.Hands.size();
}
public int NbCards(int handNb) {
return this.Hands.get(handNb).NbCard();
}
public int GetCardForce(int handNb, int cardNb) {
return this.Hands.get(handNb).GetCarte(cardNb).getForce();
}
}
class JoueurOrdinateur implements Joueur {
private ArrayList<Hand> Hands;
private double Money;
private double Insurance;
private boolean Insured;
public JoueurOrdinateur(JeudeCarte Jeu, int Money) {
this.Hands = new ArrayList<>();
......@@ -150,6 +233,8 @@ class JoueurOrdinateur implements Joueur {
Hands.get(0).SetBet(0);
this.Money = Money;
this.Insurance = 0;
this.Insured = false;
}
public void DefineStrategy() {
......@@ -157,6 +242,48 @@ class JoueurOrdinateur implements Joueur {
}
public boolean HasDoubled(int handNb) {
return this.Hands.get(handNb).HasDoubled();
}
public void Double(int handNb) {
this.Hands.get(handNb).Double();
}
public boolean CanSplit(int handNb) {
if (this.Hands.get(handNb).HasSplit()
|| this.Hands.get(handNb).GetCarte(0).getForce() != this.Hands.get(handNb).GetCarte(1).getForce()) {
return false;
}
return true;
}
public boolean HasInsured() {
return this.Insured;
}
public double GetInsured() {
return this.Insurance;
}
public void Insure() {
this.Insured = true;
this.Insurance = this.Hands.get(0).GetBet() / 2;
}
public void Reset(JeudeCarte Jeu) {
this.Insured = false;
this.Hands = new ArrayList<>();
this.Hands.add(new Hand(Jeu, 2));
this.Hands.get(0).SetBet(0);
}
public void AddMoney(double amount) {
if (amount <= 0) {
throw new RuntimeException("Can't add negative amount of Money.");
......@@ -237,14 +364,30 @@ class JoueurOrdinateur implements Joueur {
if (Hands.size() == 3) {
throw new RuntimeException("Max number of slipts.");
} else if (this.Hands.get(handNb).HasSplit()) {
throw new RuntimeException("Can't split Hand twice.");
}
Carte card = this.Hands.get(handNb).GetCarte(handNb);
this.Hands.get(handNb).RemoveCardFromHand(card);
this.Hands.get(handNb).Splitted();
this.Hands.add(new Hand(card));
}
public int NbHands() {
return this.Hands.size();
}
public int NbCards(int handNb) {
return this.Hands.get(handNb).NbCard();
}
public int GetCardForce(int handNb, int cardNb) {
return this.Hands.get(handNb).GetCarte(cardNb).getForce();
}
}
class JoueurCroupier implements Joueur {
......@@ -252,7 +395,21 @@ class JoueurCroupier implements Joueur {
private Hand Hand;
public JoueurCroupier(JeudeCarte Jeu) {
this.Hand = new Hand(Jeu, 2);
this.Hand = new Hand(Jeu, 1);
}
public boolean OnlyAs() {
if (this.Hand.GetCarte(0).getNomRang() == "As" && this.Hand.NbCard() == 1) {
return true;
}
return false;
}
public void Reset(JeudeCarte Jeu) {
this.Hand = new Hand(Jeu, 1);
}
public void DefineStrategy() {
......@@ -260,6 +417,37 @@ class JoueurCroupier implements Joueur {
}
public boolean HasDoubled(int handNb) {
// La Banque ne peut pas doubler
throw new RuntimeException("Bank cant Double.");
}
public void Double(int handNb) {
// La Banque ne peut pas doubler
throw new RuntimeException("Bank cant Double.");
}
public boolean CanSplit(int handNb) {
// La Banque ne peut pas split
throw new RuntimeException("Bank cant Split.");
}
public boolean HasInsured() {
// La Banque ne s'assure pas contre elle même
throw new RuntimeException("Bank cant get Insurance.");
}
public void Insure() {
// La Banque ne s'assure pas contre elle même
throw new RuntimeException("Bank cant get Insurance.");
}
public double GetInsured() {
// La Banque ne s'assure pas contre elle même
throw new RuntimeException("Bank has no Insurance.");
}
public void AddMoney(double amount) {
// La Banque n'a pas d'argent
throw new RuntimeException("Bank has no Money.");
......@@ -320,4 +508,16 @@ class JoueurCroupier implements Joueur {
// La Banque ne peut pas slip
throw new RuntimeException("Bank can't Split.");
}
public int NbHands() {
return 1;
}
public int NbCards(int handNb) {
return this.Hand.NbCard();
}
public int GetCardForce(int handNb, int cardNb) {
return this.Hand.GetCarte(cardNb).getForce();
}
}
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