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

Part 2 55%

parent c99740f9
No related branches found
No related tags found
No related merge requests found
Showing
with 542 additions and 28 deletions
package ch.hepia;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
public static final Scanner in = new Scanner(System.in);
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;
Paquet BlackJack = new Paquet(6, 52);
while (true) {
System.out.print("How many players ? ");
BlackJack.getPaquet().get(0).ShowPaquet();
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();
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);
}
}
BlackJack.getPaquet().get(1).ShowPaquet();
while (true) {
System.out.print("How much money do you start with ? ");
BlackJack.getPaquet().get(2).ShowPaquet();
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();
if (startMoney >= 10) {
System.out.print("");
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter an amount above 10." + App.ANSI_RESET);
}
}
GameManager BlackJack = new GameManager(numPlayers, startMoney);
BlackJack.StartTurn();
BlackJack.DisplayTurn();
App.in.close();
}
public static void main(String[] args) {
BlackJack.getPaquet().get(3).ShowPaquet();
BlackJack();
BlackJack.getPaquet().get(4).ShowPaquet();
}
}
......@@ -11,7 +11,7 @@ public class Carte {
final private COULEUR couleur;
final private int rang;
private int force;
final private int force;
private static final int NOMBRE_DE_RANGS = 13;
......@@ -31,18 +31,12 @@ public class Carte {
this.couleur = couleur;
this.rang = rang;
if (rang <= 10) {
this.force = rang;
} else {
this.force = 10;
}
public Carte(COULEUR couleur, int rang, int force) {
if (rang < 0 || rang > NOMBRE_DE_RANGS) {
throw new IllegalArgumentException("Carte invalide : rang incorrect");
}
this.couleur = couleur;
this.rang = rang;
this.force = force;
}
public COULEUR getCouleur() {
......
package ch.hepia;
import java.util.ArrayList;
import java.util.Scanner;
public class GameManager {
private JeudeCarte Deck;
private ArrayList<Joueur> Players;
private JoueurCroupier Dealer;
public GameManager(int nbPlayer, int startMoney) {
this.Deck = new JeudeCarte(new Paquet(6, 52));
this.Players = new ArrayList<>();
this.Players.add(new JoueurHumain(Deck, startMoney));
if (nbPlayer > 1) {
for (int x = 1; x < nbPlayer; x++) {
this.Players.add(new JoueurOrdinateur(Deck, startMoney));
}
}
this.Dealer = new JoueurCroupier(Deck);
}
public void DisplayTurn() {
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);
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);
this.Players.get(0).ShowHands();
}
public void StartTurn() {
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);
while (true) {
System.out.print("How much do you want to bet ? ");
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 ?");
App.in.next();
}
bet = App.in.nextDouble();
if (bet <= this.Players.get(0).GetMoney()) {
break;
} else {
System.out.println(App.ANSI_YELLOW + "You don't have enough money." + App.ANSI_RESET);
}
}
Players.get(0).SetBet(bet, 0);
}
}
......@@ -3,11 +3,11 @@ package ch.hepia;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Hand implements Comparable<Hand> {
private List<Carte> Hand;
private ArrayList<Carte> Hand;
private double Bet;
public Hand(Carte... cartes) {
......@@ -27,6 +27,22 @@ public class Hand implements Comparable<Hand> {
this.DrawCardFromGame(game, nb);
}
public void SetBet(double amount) {
if (amount < 0) {
throw new RuntimeException("Can't bet negative amout 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) {
......@@ -83,13 +99,35 @@ public class Hand implements Comparable<Hand> {
}
public void ShowHand() {
public int GetForce() {
this.SortHand();
int force = 0;
int as = 0;
System.out.println("-------- Main --------");
for (Carte carte : Hand) {
if (carte.getForce() == 1) {
as++;
}
force += carte.getForce();
}
while (force < 21 && as > 0) {
force += 10;
as--;
if (force > 21) {
force -= 10;
as = 0;
}
}
COULEUR currentcolor = null;
return force;
}
public void ShowHand() {
this.SortHand();
for (Carte carte : Hand) {
......@@ -97,7 +135,7 @@ public class Hand implements Comparable<Hand> {
}
System.out.println("\n----------------------");
System.out.println("");
}
@Override
......
......@@ -56,6 +56,23 @@ public class JeudeCarte {
}
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 jeuDeCartes;
}
public Carte GetTopCarteOfGame() {
return this.jeuDeCartes.get(0);
......
package ch.hepia;
import java.util.ArrayList;
interface Joueur {
public void DefineStrategy();
public void AddMoney(double amount);
public void RemoveMoney(double amount);
public double GetMoney();
public void SetBet(double amount, int handNb);
public double GetBet(int handNb);
public void DrawCard(int handNb, JeudeCarte Jeu);
public void ShowHand(int handNb);
public void ShowHands();
public void Split(int handNb);
public int GetForce(int handNb);
}
class JoueurHumain implements Joueur {
private ArrayList<Hand> Hands;
private double Money;
public JoueurHumain(JeudeCarte Jeu, int Money) {
this.Hands = new ArrayList<>();
this.Hands.add(new Hand(Jeu, 2));
this.Hands.get(0).SetBet(0);
this.Money = Money;
}
public void DefineStrategy() {
// Le Joueur Humain n'a pas de strategie
throw new RuntimeException("Humain Player has no Strategy.");
}
public void AddMoney(double amount) {
if (amount < 0) {
throw new RuntimeException("Can't add negative amount of Money.");
} else if (this.Money > 0 && this.Money + amount < 0) {
throw new RuntimeException("Money Underflow.");
}
this.Money += amount;
}
public void RemoveMoney(double amount) {
if (amount < 0) {
throw new RuntimeException("Can't subtract negative amount of Money.");
} else if (this.Money < 0 && this.Money + amount > 0) {
throw new RuntimeException("Money Overflow.");
}
this.Money -= amount;
}
public double GetMoney() {
return this.Money;
}
public int GetForce(int handNb) {
return this.Hands.get(handNb).GetForce();
}
public void SetBet(double amount, int handNb) {
if (amount <= 0) {
throw new RuntimeException("Can't bet a negative or null amount.");
} else if (amount > this.Money) {
throw new RuntimeException("Can't bet more than the Money you have.");
}
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
this.Money -= amount;
this.Hands.get(handNb).SetBet(amount);
}
public double GetBet(int handNb) {
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
return this.Hands.get(handNb).GetBet();
}
public void DrawCard(int handNb, JeudeCarte Jeu) {
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
this.Hands.get(handNb).DrawCardFromGame(Jeu);
}
public void ShowHands() {
for (Hand hand : this.Hands) {
hand.ShowHand();
}
}
public void ShowHand(int handNb) {
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
this.Hands.get(handNb).ShowHand();
}
public void Split(int handNb) {
if (Hands.size() == 3) {
throw new RuntimeException("Max number of slipts.");
}
Carte card = this.Hands.get(handNb).GetCarte(handNb);
this.Hands.get(handNb).RemoveCardFromHand(card);
this.Hands.add(new Hand(card));
}
}
class JoueurOrdinateur implements Joueur {
private ArrayList<Hand> Hands;
private double Money;
public JoueurOrdinateur(JeudeCarte Jeu, int Money) {
this.Hands = new ArrayList<>();
Hands.add(new Hand(Jeu, 2));
Hands.get(0).SetBet(0);
this.Money = Money;
}
public void DefineStrategy() {
// TODO
}
public void AddMoney(double amount) {
if (amount <= 0) {
throw new RuntimeException("Can't add negative amount of Money.");
} else if (this.Money > 0 && this.Money + amount < 0) {
throw new RuntimeException("Money Underflow.");
}
this.Money += amount;
}
public void RemoveMoney(double amount) {
if (amount < 0) {
throw new RuntimeException("Can't subtract negative amount of Money.");
} else if (this.Money < 0 && this.Money - amount > 0) {
throw new RuntimeException("Money Overflow.");
}
this.Money -= amount;
}
public double GetMoney() {
return this.Money;
}
public int GetForce(int handNb) {
return this.Hands.get(handNb).GetForce();
}
public void SetBet(double amount, int handNb) {
if (amount <= 0) {
throw new RuntimeException("Can't bet a negative or null amount.");
} else if (amount > this.Money) {
throw new RuntimeException("Can't bet more than the Money you have.");
}
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
this.Money -= amount;
this.Hands.get(handNb).SetBet(amount);
}
public double GetBet(int handNb) {
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
return Hands.get(handNb).GetBet();
}
public void DrawCard(int handNb, JeudeCarte Jeu) {
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
this.Hands.get(handNb).DrawCardFromGame(Jeu);
}
public void ShowHands() {
for (Hand hand : this.Hands) {
hand.ShowHand();
}
}
public void ShowHand(int handNb) {
if (handNb < 0 || handNb > Hands.size()) {
throw new RuntimeException("Hand number not valid.");
}
this.Hands.get(handNb).ShowHand();
}
public void Split(int handNb) {
if (Hands.size() == 3) {
throw new RuntimeException("Max number of slipts.");
}
Carte card = this.Hands.get(handNb).GetCarte(handNb);
this.Hands.get(handNb).RemoveCardFromHand(card);
this.Hands.add(new Hand(card));
}
}
class JoueurCroupier implements Joueur {
private Hand Hand;
public JoueurCroupier(JeudeCarte Jeu) {
this.Hand = new Hand(Jeu, 2);
}
public void DefineStrategy() {
// TODO
}
public void AddMoney(double amount) {
// La Banque n'a pas d'argent
throw new RuntimeException("Bank has no Money.");
}
public void RemoveMoney(double amount) {
// La Banque n'a pas d'argent
throw new RuntimeException("Bank has no Money.");
}
public double GetMoney() {
// La Banque n'a pas d'argent
throw new RuntimeException("Bank has no Money.");
}
public void SetBet(double amount, int handNb) {
// La Banque ne parie pas
throw new RuntimeException("Bank doesn't Bet.");
}
public double GetBet(int handNb) {
// La Banque ne parie pas
throw new RuntimeException("Bank doesn't Bet.");
}
public void DrawCard(int handNb, JeudeCarte Jeu) {
if (handNb != 0) {
throw new RuntimeException("Bank has only 1 hand.");
}
this.Hand.DrawCardFromGame(Jeu);
}
public void ShowHands() {
this.Hand.ShowHand();
}
public void ShowHand(int handNb) {
if (handNb != 0) {
throw new RuntimeException("Bank has only 1 hand.");
}
this.Hand.ShowHand();
}
public int GetForce(int handNb) {
if (handNb != 0) {
throw new RuntimeException("Bank has only 1 hand.");
}
return this.Hand.GetForce();
}
public void Split(int handNb) {
// La Banque ne peut pas slip
throw new RuntimeException("Bank can't Split.");
}
}
No preview for this file type
No preview for this file type
No preview for this file type
File added
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
File added
File added
File added
File added
No preview for this file type
ch/hepia/App.class
ch/hepia/Hand.class
ch/hepia/JeudeCarte.class
ch/hepia/Carte.class
ch/hepia/App.class
ch/hepia/COULEUR.class
ch/hepia/Hand$1.class
ch/hepia/Paquet.class
ch/hepia/JeudeCarte.class
ch/hepia/Carte.class
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment