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

Part 2 100%

parent e851c001
No related branches found
No related tags found
No related merge requests found
package ch.hepia;
import java.util.ArrayList;
import java.util.Scanner;
public class GameManager {
......@@ -91,7 +90,7 @@ public class GameManager {
char choice = 'x';
// Go thew all hands of the player
for (int HandNb = 0; HandNb < this.Players.get(0).NbHands(); HandNb++) {
for (int HandNb = 0; HandNb < this.Players.get(0).GetNbHands(); HandNb++) {
CanSplit = false;
CanInsure = false;
......@@ -109,7 +108,8 @@ public class GameManager {
"Cards in Deck : " + App.ANSI_GREEN + this.Deck.GetNbCards() + App.ANSI_RESET + "\n");
// Dealer has only 1 card in his hand at this point in time
System.out.print("Dealer Strength : " + App.ANSI_PURPLE + this.Dealer.GetForce(0) + App.ANSI_RESET);
System.out
.println("Dealer Strength : " + App.ANSI_PURPLE + this.Dealer.GetStrength(0) + App.ANSI_RESET);
// Show the dealer hand
this.Dealer.ShowHands();
......@@ -135,13 +135,13 @@ public class GameManager {
System.out
.print("Strength of Hand " + App.ANSI_GREEN + (HandNb + 1) + App.ANSI_RESET + " : "
+ App.ANSI_PURPLE);
if (this.Players.get(0).GetForce(HandNb) == 99) {
if (this.Players.get(0).GetStrength(HandNb) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else if (this.Players.get(0).GetForce(HandNb) > 21) {
} else if (this.Players.get(0).GetStrength(HandNb) > 21) {
System.out.println(
this.Players.get(0).GetForce(HandNb) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
this.Players.get(0).GetStrength(HandNb) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
} else {
System.out.println(this.Players.get(0).GetForce(HandNb) + App.ANSI_RESET);
System.out.println(this.Players.get(0).GetStrength(HandNb) + App.ANSI_RESET);
}
// Show the player hand
......@@ -154,7 +154,7 @@ public class GameManager {
// 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()
if (this.Step == 0 && this.Dealer.HasOnlyAs()
&& !this.Players.get(0).HasInsured()) {
CanInsure = true;
System.out.println(App.ANSI_BLUE + "[i]" + App.ANSI_RESET + " Insurance against Dealer");
......@@ -168,7 +168,7 @@ public class GameManager {
// AND
// if the player has enough money
if (this.Players.get(0).CanSplit(HandNb)
&& this.Players.get(0).NbHands() < 3
&& this.Players.get(0).GetNbHands() < 3
&& this.Players.get(0).GetBet(HandNb) <= this.Players.get(0).GetMoney()) {
CanSplit = true;
System.out.println(App.ANSI_BLUE + "[s]" + App.ANSI_RESET + " Split your Hand");
......@@ -183,8 +183,8 @@ public class GameManager {
// AND
// if the player has enough money
if (!this.Players.get(0).HasDoubled(HandNb)
&& this.Players.get(0).NbCards(HandNb) == 2
&& this.Players.get(0).GetForce(HandNb) != 99
&& this.Players.get(0).GetNbCards(HandNb) == 2
&& this.Players.get(0).GetStrength(HandNb) != 99
&& this.Players.get(0).GetBet(HandNb) <= this.Players.get(0).GetMoney()) {
CanDouble = true;
System.out.println(App.ANSI_BLUE + "[d]" + App.ANSI_RESET + " Double your Hand");
......@@ -198,9 +198,10 @@ public class GameManager {
// if the player has a BlackJack
// AND
// if the strength of the hand is more than 21
if (!(this.Players.get(0).HasDoubled(HandNb) && this.Players.get(0).NbCards(HandNb) == 3)
&& !(this.Players.get(0).HasSplit(HandNb) && this.Players.get(0).GetCardForce(HandNb, 0) == 1)
&& this.Players.get(0).GetForce(HandNb) < 21) {
if (!(this.Players.get(0).HasDoubled(HandNb) && this.Players.get(0).GetNbCards(HandNb) == 3)
&& !(this.Players.get(0).HasSplit(HandNb)
&& this.Players.get(0).GetCardStrength(HandNb, 0) == 1)
&& this.Players.get(0).GetStrength(HandNb) < 21) {
CanDraw = true;
System.out.println(App.ANSI_BLUE + "[h]" + App.ANSI_RESET + " Hit");
}
......@@ -267,7 +268,7 @@ public class GameManager {
public boolean ResolveTurn() {
// Dealer draws card until he hits 17 or more
while (this.Dealer.GetForce(0) <= 17) {
while (this.Dealer.GetStrength(0) <= 17) {
this.Dealer.DrawCard(0, this.Deck);
}
......@@ -285,12 +286,12 @@ public class GameManager {
// >21 = Busted
// <=21 = show the strength
System.out.print("Dealer Score : " + App.ANSI_PURPLE);
if (this.Dealer.GetForce(0) == 99) {
if (this.Dealer.GetStrength(0) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else if (this.Dealer.GetForce(0) > 21) {
System.out.println(this.Dealer.GetForce(0) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
} else if (this.Dealer.GetStrength(0) > 21) {
System.out.println(this.Dealer.GetStrength(0) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
} else {
System.out.println(this.Dealer.GetForce(0) + App.ANSI_RESET);
System.out.println(this.Dealer.GetStrength(0) + App.ANSI_RESET);
}
// Show the dealers hand
......@@ -303,59 +304,88 @@ public class GameManager {
}
// Go thew all hands of the player
for (int HandNb = 0; HandNb < this.Players.get(0).NbHands(); HandNb++) {
for (int HandNb = 0; HandNb < this.Players.get(0).GetNbHands(); HandNb++) {
System.out.println("\nHand " + App.ANSI_GREEN + (HandNb + 1) + App.ANSI_RESET + " :");
// Show to bet amount of the hand
System.out.println("\tBet : " + App.ANSI_BLUE + this.Players.get(0).GetBet(HandNb) + App.ANSI_RESET);
// Show strength of the hand of the player
// 99 = BlackJack
// >21 = Busted
// <=21 = show the strength
System.out.print("\tStrength : " + App.ANSI_PURPLE);
if (this.Players.get(0).GetForce(HandNb) == 99) {
if (this.Players.get(0).GetStrength(HandNb) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else if (this.Players.get(0).GetForce(HandNb) > 21) {
} else if (this.Players.get(0).GetStrength(HandNb) > 21) {
System.out.println(
this.Players.get(0).GetForce(HandNb) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
this.Players.get(0).GetStrength(HandNb) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
} else {
System.out.println(this.Players.get(0).GetForce(HandNb) + App.ANSI_RESET);
System.out.println(this.Players.get(0).GetStrength(HandNb) + App.ANSI_RESET);
}
// Show and apply gains and losses
System.out.print("\tResult : ");
if (this.Players.get(0).GetForce(HandNb) > 21 && this.Players.get(0).GetForce(HandNb) != 99) {
// If the player is Busted (strength > 21 but not BlackJack (99))
if (this.Players.get(0).GetStrength(HandNb) > 21 && this.Players.get(0).GetStrength(HandNb) != 99) {
// Show player loss
System.out.println(App.ANSI_RED + "-" + this.Players.get(0).GetBet(HandNb) + App.ANSI_RESET);
} else if (this.Dealer.GetForce(0) == this.Players.get(0).GetForce(HandNb)) {
}
// If it's a Draw
else if (this.Dealer.GetStrength(0) == this.Players.get(0).GetStrength(HandNb)) {
// Player get's back his bet
System.out.println(App.ANSI_BLUE + "±0" + App.ANSI_RESET);
this.Players.get(0).AddMoney(this.Players.get(0).GetBet(HandNb));
} else if (this.Players.get(0).GetForce(HandNb) == 99) {
}
// If the player has done a BlackJack
else if (this.Players.get(0).GetStrength(HandNb) == 99) {
// Player gets payed 1.5 to 1
System.out.println(App.ANSI_GREEN + "+" + (this.Players.get(0).GetBet(HandNb) * 1.5) + App.ANSI_RESET);
this.Players.get(0).AddMoney(this.Players.get(0).GetBet(HandNb) * 2.5);
} else if (this.Dealer.GetForce(0) > 21 && this.Dealer.GetForce(0) != 99) {
}
// If the Dealer is Busted (strength > 21 but not BlackJack (99))
else if (this.Dealer.GetStrength(0) > 21 && this.Dealer.GetStrength(0) != 99) {
// Player wins and get payed 1 to 1
System.out.println(App.ANSI_GREEN + "+" + this.Players.get(0).GetBet(HandNb) + App.ANSI_RESET);
this.Players.get(0).AddMoney(this.Players.get(0).GetBet(HandNb) * 2);
} else if (this.Players.get(0).GetForce(HandNb) < this.Dealer.GetForce(0)) {
}
// If the Dealer has a better score
else if (this.Players.get(0).GetStrength(HandNb) < this.Dealer.GetStrength(0)) {
// Show player loss
System.out.println(App.ANSI_RED + "-" + this.Players.get(0).GetBet(HandNb) + App.ANSI_RESET);
} else if (this.Players.get(0).GetForce(HandNb) > this.Dealer.GetForce(0)) {
}
// If the Player has a better score
else if (this.Players.get(0).GetStrength(HandNb) > this.Dealer.GetStrength(0)) {
// Player wins and get payed 1 to 1
System.out.println(App.ANSI_GREEN + "+" + this.Players.get(0).GetBet(HandNb) + App.ANSI_RESET);
this.Players.get(0).AddMoney(this.Players.get(0).GetBet(HandNb) * 2);
}
}
// Check if the player had insurance
if (this.Players.get(0).HasInsured()) {
System.out.print("Insurance : ");
if (this.Dealer.GetForce(0) == 99) {
// If the Dealer did a BackJack
if (this.Dealer.GetStrength(0) == 99) {
// Player gets payed back 2 to 1
System.out.println(App.ANSI_GREEN + "+" + (this.Players.get(0).GetInsured() * 3) + App.ANSI_RESET);
this.Players.get(0).AddMoney(this.Players.get(0).GetInsured() * 3);
} else {
// Show player loss
System.out.println(App.ANSI_RED + "-" + (this.Players.get(0).GetInsured()) + App.ANSI_RESET);
}
}
// Show the final player balance
System.out.println("\nMoney : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET);
System.out.print("> ");
// Wait for a simple input of the player
try {
System.in.read();
} catch (Exception e) {
......@@ -372,6 +402,7 @@ public class GameManager {
this.Deck = new JeudeCarte(new Paquet(6, 52));
}
// Reset all hands
this.Dealer.Reset(this.Deck);
for (Joueur Player : this.Players) {
Player.Reset(this.Deck);
......
......@@ -23,6 +23,7 @@ public class Hand implements Comparable<Hand> {
this.Splitted = false;
this.Doubled = false;
this.Bet = 0;
}
public Hand(JeudeCarte game, int nb) {
......@@ -58,7 +59,7 @@ public class Hand implements Comparable<Hand> {
public void SetBet(double amount) {
if (amount < 0) {
throw new RuntimeException("Can't bet negative amout of money.");
throw new RuntimeException("Can't bet negative amount of money.");
}
this.Bet = amount;
......@@ -128,11 +129,12 @@ public class Hand implements Comparable<Hand> {
}
public int GetForce() {
public int GetStrength() {
int force = 0;
int as = 0;
// Add strength of cards and count how much Asses there is
for (Carte carte : Hand) {
if (carte.getForce() == 1) {
as++;
......@@ -140,6 +142,7 @@ public class Hand implements Comparable<Hand> {
force += carte.getForce();
}
// Test how many Asses we can but to 11 without going over 21
while (force < 21 && as > 0) {
force += 10;
......@@ -152,6 +155,8 @@ public class Hand implements Comparable<Hand> {
}
// Edge Case = BlackJack
// Check if a 21 is a BlackJack (As + 10) or a combination of 3 cards
// (simple 21)
if (force == 21) {
boolean isAs = false;
......@@ -183,8 +188,6 @@ public class Hand implements Comparable<Hand> {
public void ShowHand() {
// this.SortHand();
for (Carte carte : Hand) {
System.out.print(carte.getNomComplet() + " ");
......@@ -194,15 +197,16 @@ public class Hand implements Comparable<Hand> {
System.out.println("");
}
// Not used but asked for Part 2
@Override
public int compareTo(Hand otherHand) {
int Hand_1_Power = this.GetForce();
int Hand_2_Power = otherHand.GetForce();
int Hand_1_Strength = this.GetStrength();
int Hand_2_Strength = otherHand.GetStrength();
if (Hand_1_Power > Hand_2_Power) {
if (Hand_1_Strength > Hand_2_Strength) {
return 1;
} else if (Hand_1_Power < Hand_2_Power) {
} else if (Hand_1_Strength < Hand_2_Strength) {
return -1;
} else {
return 0;
......
This diff is collapsed.
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