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

Part 3 0%

parent 893971c5
No related branches found
No related tags found
No related merge requests found
Showing
with 1957 additions and 0 deletions
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
\ No newline at end of file
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/padi/Téléchargements",
"program": "/home/padi/Téléchargements/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
\ No newline at end of file
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.hepia</groupId>
<artifactId>Java_Card_Game</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Java_Card_Game</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>ch.hepia.App</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
\ No newline at end of file
package ch.hepia;
import java.util.Scanner;
public class App {
// Scanner will be used all along to take user inputs in the terminal
public static final Scanner in = new Scanner(System.in);
// All Color choices to be used in terminal prints
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
public static void BlackJack() {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
int numPlayers;
int startMoney;
// Ask the number of players until a valide answer is given
while (true) {
System.out.print("How many players ? ");
// Check that the input is an valid int
while (!App.in.hasNextInt()) {
System.out.println(
App.ANSI_YELLOW + "Please enter a valid number for the number of players." + App.ANSI_RESET);
System.out.print("How many players ? ");
App.in.next();
}
numPlayers = App.in.nextInt();
// Player count must be between 1 and 7
if (numPlayers >= 1 && numPlayers <= 7) {
System.out.print("");
break;
} else {
System.out.println(
App.ANSI_YELLOW + "Please enter a number of players between 1 and 7." + App.ANSI_RESET);
}
}
// Ask the start money count for each players until a valide answer is given
while (true) {
System.out.print("How much money do you start with ? ");
// Check that the input is an valid int
while (!App.in.hasNextInt()) {
System.out.println(App.ANSI_YELLOW + "Please enter a valid number for your Money." + App.ANSI_RESET);
System.out.print("How much money do you start with ? ");
App.in.next();
}
startMoney = App.in.nextInt();
// Starting money must be more or equal to minimum bid amount
if (startMoney >= 10) {
System.out.print("");
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter an amount above 10." + App.ANSI_RESET);
}
}
// Create a new game
GameManager BlackJack = new GameManager(numPlayers, startMoney);
boolean GameOver = false;
// Game run's in 3 Phases :
// Phase 1 (StartTurn):
// Ask the player how much he wan't to bid
// Phase 2 (PlayTurn):
// Ask the player for all his interactions with this cards
// Phase 3 (ResolveTurn):
// Make the Dealer pick cards
// Do all the math for the player gains and losses
// Check if the player can continue et prepare for the next round
while (!GameOver) {
BlackJack.StartTurn();
BlackJack.PlayTurn();
GameOver = BlackJack.ResolveTurn();
}
// The player has less than the minimum bid amount allowed = Game Over
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
System.out.println(App.ANSI_PURPLE + "Game Over !" + App.ANSI_RESET);
App.in.close();
}
public static void main(String[] args) {
// Play 1 BlackJack Game
BlackJack();
}
}
package ch.hepia;
enum COULEUR {
coeur,
carreau,
pique,
trefle
}
public class Carte {
final private COULEUR couleur;
final private int rang;
final private int force;
private static final int NOMBRE_DE_RANGS = 13;
String[][] playingCards = {
{ "🃟", "🂱", "🂲", "🂳", "🂴", "🂵", "🂶", "🂷", "🂸", "🂹", "🂺", "🂻", "🂽", "🂾" },
{ "🃟", "🃁", "🃂", "🃃", "🃄", "🃅", "🃆", "🃇", "🃈", "🃉", "🃊", "🃋", "🃍", "🃎" },
{ "🃟", "🂡", "🂢", "🂣", "🂤", "🂥", "🂦", "🂧", "🂨", "🂩", "🂪", "🂫", "🂭", "🂮" },
{ "🃟", "🃑", "🃒", "🃓", "🃔", "🃕", "🃖", "🃗", "🃘", "🃙", "🃚", "🃛", "🃝", "🃞" }
};
public Carte(COULEUR couleur, int rang) {
if (rang < 0 || rang > NOMBRE_DE_RANGS) {
throw new IllegalArgumentException("Carte invalide : rang incorrect");
}
this.couleur = couleur;
this.rang = rang;
if (rang <= 10) {
this.force = rang;
} else {
this.force = 10;
}
}
public COULEUR getCouleur() {
return this.couleur;
}
public int getRang() {
return this.rang;
}
public int getForce() {
return this.force;
}
public String getNomCouleur() {
String[] NOMS_COULEURS = { "♥", "♦", "♠", "♣" };
return NOMS_COULEURS[couleur.ordinal()];
}
public String getNomRang() {
String[] NOMS_RANGS = { "Joker", "As", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Valet", "Dame", "Roi" };
return NOMS_RANGS[rang];
}
public String getNomComplet() {
if (getRang() == 0) {
return "🃟";
} else {
return playingCards[couleur.ordinal()][rang];
}
}
}
package ch.hepia;
import java.util.ArrayList;
public class GameManager {
private JeudeCarte Deck;
private ArrayList<Joueur> Players;
private JoueurCroupier Dealer;
private int Step;
public GameManager(int nbPlayer, int startMoney) {
// Create a new BlackJack Deck of card (6 x 52 cards game)
this.Deck = new JeudeCarte(new Paquet(6, 52));
this.Players = new ArrayList<>();
// First Player is always the humain
this.Players.add(new JoueurHumain(Deck, startMoney));
// All other are controlled by the computer
if (nbPlayer > 1) {
for (int x = 1; x < nbPlayer; x++) {
this.Players.add(new JoueurOrdinateur(Deck, startMoney));
}
}
this.Dealer = new JoueurCroupier(Deck);
}
// Phase 1 (StartTurn):
// Ask the player how much he wan't to bid
public void StartTurn() {
this.Step = 0;
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
double bet;
System.out.println("Money : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET);
// Ask the bid amount until a valide answer is given
while (true) {
System.out.print("How much do you want to bet (Min. 10) ? ");
// Check that the input is an valid double
while (!App.in.hasNextDouble()) {
System.out.println(App.ANSI_YELLOW + "Please enter a valid number to bet." + App.ANSI_RESET);
System.out.print("How much do you want to bet (Min. 10) ?");
App.in.next();
}
bet = App.in.nextDouble();
// Check if the player has enough money to place the bid
if (bet <= this.Players.get(0).GetMoney()) {
// Check if the minimum bid amount it cleared
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);
}
}
// Set the player bid for this turn
Players.get(0).SetBet(bet, 0);
}
// Phase 2 (PlayTurn):
// Ask the player for all his interactions with this cards
public void PlayTurn() {
boolean EndTurn = false, CanSplit = false, CanInsure = false, CanDouble = false, CanDraw = false;
// Continue interacting with the player until all actions have been taken on all
// his hands
while (!EndTurn) {
char choice = 'x';
// Go thew all hands of the player
for (int HandNb = 0; HandNb < this.Players.get(0).GetNbHands(); HandNb++) {
CanSplit = false;
CanInsure = false;
CanDouble = false;
CanDraw = false;
choice = 'x';
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
// Show the remaining amount of cards in the deck
System.out.println(
"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
.println("Dealer Strength : " + App.ANSI_PURPLE + this.Dealer.GetStrength(0) + App.ANSI_RESET);
// Show the dealer hand
this.Dealer.ShowHands();
// Show the current player balance
System.out.println("\nMoney : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET);
// Check if the player has put down an insurance
if (this.Players.get(0).HasInsured()) {
System.out.println(
"Insured : " + App.ANSI_BLUE + this.Players.get(0).GetInsured() + App.ANSI_RESET);
}
// Show the hand number of the player
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);
// Show the hand strength of the player
// 99 = BlackJack
// >21 = Busted
// <=21 = show the strength
System.out
.print("Strength of Hand " + App.ANSI_GREEN + (HandNb + 1) + App.ANSI_RESET + " : "
+ App.ANSI_PURPLE);
if (this.Players.get(0).GetStrength(HandNb) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else if (this.Players.get(0).GetStrength(HandNb) > 21) {
System.out.println(
this.Players.get(0).GetStrength(HandNb) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
} else {
System.out.println(this.Players.get(0).GetStrength(HandNb) + App.ANSI_RESET);
}
// Show the player hand
this.Players.get(0).ShowHand(HandNb);
// 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.HasOnlyAs()
&& !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)
// AND
// if the player has enough money
if (this.Players.get(0).CanSplit(HandNb)
&& 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");
}
// 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
// AND
// if the player has enough money
if (!this.Players.get(0).HasDoubled(HandNb)
&& 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");
}
// 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
// AND
// if the strength of the hand is more than 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");
}
// Keep (Stand)
System.out.println(App.ANSI_BLUE + "[k]" + App.ANSI_RESET + " Keep current Hand");
// Ask for the player choice until a valide one is given
while (true) {
System.out.print("> ");
choice = App.in.next().charAt(0);
// Choices are checked with available one for confirmation
if ((choice == 'i' && CanInsure) || (choice == 's' && CanSplit) || (choice == 'd' && CanDouble)
|| (choice == 'h' && CanDraw)
|| choice == 'k') {
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
}
}
// Hit
if (choice == 'h') {
this.Players.get(0).DrawCard(HandNb, this.Deck);
// Go back 1 hand to stay on the same hand in the for loop
HandNb--;
}
// Split
else if (choice == 's') {
this.Players.get(0).Split(HandNb, this.Deck);
// Go back 1 hand to stay on the same hand in the for loop
HandNb--;
}
// Double
else if (choice == 'd') {
this.Players.get(0).Double(HandNb, this.Deck);
// Go back 1 hand to stay on the same hand in the for loop
HandNb--;
}
Step++;
}
// Insure
if (choice == 'i') {
this.Players.get(0).Insure();
EndTurn = false;
}
// If this point is reached (and not because of the insurance), all hands have
// been played
else {
EndTurn = true;
}
}
}
// Phase 3 (ResolveTurn):
// Make the Dealer pick cards
// Do all the math for the player gains and losses
// Check if the player can continue et prepare for the next round
public boolean ResolveTurn() {
// Dealer draws card until he hits 17 or more
while (this.Dealer.GetStrength(0) <= 17) {
this.Dealer.DrawCard(0, this.Deck);
}
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
// Show the remaining amount of cards in the deck
System.out.println(
"Cards in Deck : " + App.ANSI_GREEN + this.Deck.GetNbCards() + App.ANSI_RESET + "\n");
// Show the hand strength of the dealer
// 99 = BlackJack
// >21 = Busted
// <=21 = show the strength
System.out.print("Dealer Score : " + App.ANSI_PURPLE);
if (this.Dealer.GetStrength(0) == 99) {
System.out.println("BlackJack" + 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.GetStrength(0) + App.ANSI_RESET);
}
// Show the dealers hand
this.Dealer.ShowHands();
// Check if the player has put down an insurance
if (this.Players.get(0).HasInsured()) {
System.out.println(
"Insured : " + App.ANSI_BLUE + this.Players.get(0).GetInsured() + App.ANSI_RESET);
}
// Go thew all hands of the player
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).GetStrength(HandNb) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET);
} else if (this.Players.get(0).GetStrength(HandNb) > 21) {
System.out.println(
this.Players.get(0).GetStrength(HandNb) + App.ANSI_RED + " [BUSTED]" + App.ANSI_RESET);
} else {
System.out.println(this.Players.get(0).GetStrength(HandNb) + App.ANSI_RESET);
}
// Show and apply gains and losses
System.out.print("\tResult : ");
// 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);
}
// 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));
}
// 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);
}
// 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);
}
// 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);
}
// 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 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) {
}
// If the player has less than the minimum bid amount allowed
if (this.Players.get(0).GetMoney() <= 10) {
return true;
}
// If more thn 75% of the Deck has been used, reshuffle all cards. In our case:
// simply recreate a new deck
if (this.Deck.GetNbCards() < 78) {
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);
}
return false;
}
}
package ch.hepia;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Hand implements Comparable<Hand> {
private ArrayList<Carte> Hand;
private double Bet;
private boolean Splitted;
private boolean Doubled;
public Hand(Carte... cartes) {
this.Hand = new ArrayList<>();
for (Carte carte : cartes) {
Hand.add(carte);
}
this.Splitted = false;
this.Doubled = false;
this.Bet = 0;
}
public Hand(JeudeCarte game, int nb) {
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;
this.Bet *= 2;
}
public boolean HasSplit() {
return this.Splitted;
}
public void Splitted() {
this.Splitted = true;
}
public int NbCard() {
return this.Hand.size();
}
public void SetBet(double amount) {
if (amount < 0) {
throw new RuntimeException("Can't bet negative amount of money.");
}
this.Bet = amount;
}
public double GetBet() {
return this.Bet;
}
public Carte GetCarte(int cardNb) {
return this.Hand.get(cardNb);
}
public void AddCardToHand(Carte... cartes) {
for (Carte carte : cartes) {
this.Hand.add(carte);
}
}
public void RemoveCardFromHand(Carte... cartes) {
for (Carte carte : cartes) {
this.Hand.remove(carte);
}
}
public void DrawCardFromGame(JeudeCarte game) {
this.AddCardToHand(game.GetTopCarteOfGame());
game.RemoveCarteFromGame(game.GetTopCarteOfGame());
}
public void DrawCardFromGame(JeudeCarte game, int nb) {
for (int x = 0; x < nb; x++) {
this.DrawCardFromGame(game);
}
}
public void SortHand() {
Collections.sort(Hand, new Comparator<Carte>() {
public int compare(Carte carte1, Carte carte2) {
int comparaisonCouleur = carte1.getCouleur().compareTo(carte2.getCouleur());
if (comparaisonCouleur == 0) {
return Integer.compare(carte1.getRang(), carte2.getRang());
} else {
return comparaisonCouleur;
}
}
});
}
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++;
}
force += carte.getForce();
}
// Test how many Asses we can but to 11 without going over 21
while (force < 21 && as > 0) {
force += 10;
as--;
if (force > 21) {
force -= 10;
as = 0;
}
}
// 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;
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;
}
public void ShowHand() {
for (Carte carte : Hand) {
System.out.print(carte.getNomComplet() + " ");
}
System.out.println("");
}
// Not used but asked for Part 2
@Override
public int compareTo(Hand otherHand) {
int Hand_1_Strength = this.GetStrength();
int Hand_2_Strength = otherHand.GetStrength();
if (Hand_1_Strength > Hand_2_Strength) {
return 1;
} else if (Hand_1_Strength < Hand_2_Strength) {
return -1;
} else {
return 0;
}
}
}
package ch.hepia;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class JeudeCarte {
private List<Carte> jeuDeCartes;
public JeudeCarte(int nb) {
jeuDeCartes = new ArrayList<>();
for (COULEUR couleur : COULEUR.values()) {
for (int rang = 7; rang <= (nb / 4) + 6; rang++) {
if (rang > 13) {
jeuDeCartes.add(new Carte(couleur, rang - 13));
} else {
jeuDeCartes.add(new Carte(couleur, rang));
}
}
}
if (nb % 4 != 0) {
for (int x = 0; x < nb % 4; x++) {
jeuDeCartes.add(new Carte(COULEUR.carreau, 0));
}
}
Collections.sort(jeuDeCartes, new Comparator<Carte>() {
public int compare(Carte carte1, Carte carte2) {
int comparaisonCouleur = carte1.getCouleur().compareTo(carte2.getCouleur());
if (comparaisonCouleur == 0) {
return Integer.compare(carte1.getRang(), carte2.getRang());
} else {
return comparaisonCouleur;
}
}
});
}
public JeudeCarte(Paquet paquet) {
jeuDeCartes = new ArrayList<>();
List<JeudeCarte> paquets = paquet.getPaquet();
for (JeudeCarte deck : paquets) {
List<Carte> cartes = deck.getCartes();
jeuDeCartes.addAll(cartes);
}
}
public List<Carte> getCartes() {
return this.jeuDeCartes;
}
public Carte GetTopCarteOfGame() {
return this.jeuDeCartes.get(0);
}
public void AddCarteToGame(Carte... cartes) {
for (Carte carte : cartes) {
this.jeuDeCartes.add(carte);
}
}
public void RemoveCarteFromGame(Carte... cartes) {
for (Carte carte : cartes) {
this.jeuDeCartes.remove(carte);
}
}
public void shuffle() {
Collections.shuffle(jeuDeCartes);
}
public int GetNbCards() {
return this.jeuDeCartes.size();
}
public void ShowPaquet() {
System.out.println("------- Paquet -------");
for (Carte carte : jeuDeCartes) {
System.out.println(carte.getNomComplet());
}
System.out.println("----------------------");
}
}
This diff is collapsed.
package ch.hepia;
import java.util.List;
import java.util.ArrayList;
public class Paquet {
private List<JeudeCarte> paquet;
public Paquet(int nb_paquet, int nb_carte) {
paquet = new ArrayList<>();
for (int i = 0; i < nb_paquet; ++i) {
JeudeCarte jeuDeCarte = new JeudeCarte(nb_carte);
jeuDeCarte.shuffle();
paquet.add(jeuDeCarte);
}
}
public List<JeudeCarte> getPaquet() {
return paquet;
}
}
package ch.hepia;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CarteTest {
@Test
public void testGetCouleur() {
Carte carte = new Carte(COULEUR.coeur, 2);
assertEquals(COULEUR.coeur, carte.getCouleur());
}
@Test
public void testGetRang() {
Carte carte = new Carte(COULEUR.carreau, 7);
assertEquals(7, carte.getRang());
}
@Test
public void testGetForce() {
Carte carte = new Carte(COULEUR.trefle, 12, 10);
assertEquals(10, carte.getForce());
}
@Test
public void testGetNomCouleur() {
Carte carte = new Carte(COULEUR.coeur, 0);
assertEquals("Coeur", carte.getNomCouleur());
}
@Test
public void testGetNomRang() {
Carte carte = new Carte(COULEUR.coeur, 2);
assertEquals("2", carte.getNomRang());
}
@Test
public void testGetNomComplet() {
Carte carte = new Carte(COULEUR.carreau, 12);
assertEquals("Dame de Carreau", carte.getNomComplet());
}
@Test
public void testGetNomComplet_2() {
Carte carte = new Carte(COULEUR.carreau, 0);
assertEquals("Joker", carte.getNomComplet());
}
}
\ No newline at end of file
File added
File added
File added
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment