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

Stupid Computer Working

parent 31880223
No related branches found
No related tags found
No related merge requests found
Showing
with 154 additions and 18 deletions
......@@ -27,6 +27,7 @@ public class App {
int numPlayers;
int startMoney;
int computerStrategy = 1;
// Ask the number of players until a valide answer is given
while (true) {
......@@ -76,8 +77,34 @@ public class App {
}
}
// Ask if the Computer players should be smart of not
if (numPlayers > 1) {
while (true) {
System.out.print("Should the Compter players be [1] Smart or [2] Stupid ? ");
// Check that the input is an valid int
while (!App.in.hasNextInt()) {
System.out
.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
System.out.print("Should the Compter players be [1] Smart or [2] Stupid ? ");
App.in.next();
}
computerStrategy = App.in.nextInt();
// Check if the value i s valide
if (computerStrategy == 1 || computerStrategy == 2) {
System.out.print("");
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
}
}
}
// Create a new game
GameManager BlackJack = new GameManager(numPlayers, startMoney);
GameManager BlackJack = new GameManager(numPlayers, startMoney, computerStrategy);
boolean GameOver = false;
......
......@@ -7,9 +7,10 @@ public class GameManager {
private JeudeCarte Deck;
private ArrayList<Joueur> Players;
private JoueurCroupier Dealer;
private int ComputerPlayers;
private int Step;
public GameManager(int nbPlayer, int startMoney) {
public GameManager(int nbPlayer, int startMoney, int computerStrategy) {
// Create a new BlackJack Deck of card (6 x 52 cards game)
this.Deck = new JeudeCarte(new Paquet(6, 52));
......@@ -19,10 +20,13 @@ public class GameManager {
// First Player is always the humain
this.Players.add(new JoueurHumain(Deck, startMoney));
this.ComputerPlayers = 0;
// All other are controlled by the computer
if (nbPlayer > 1) {
for (int x = 1; x < nbPlayer; x++) {
this.Players.add(new JoueurOrdinateur(Deck, startMoney));
ComputerPlayers++;
this.Players.add(new JoueurOrdinateur(Deck, startMoney, computerStrategy));
}
}
......@@ -75,6 +79,11 @@ public class GameManager {
// Set the player bid for this turn
Players.get(0).SetBet(bet, 0);
// Set the Computer players bid for this turn
for (int x = 1; x <= this.ComputerPlayers; x++) {
Players.get(x).SetBet(10, 0);
}
}
// Phase 2 (PlayTurn):
......@@ -147,6 +156,28 @@ public class GameManager {
// Show the player hand
this.Players.get(0).ShowHand(HandNb);
// Show Computer Players hands
for (int x = 1; x <= this.ComputerPlayers; x++) {
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
+ 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_PURPLE);
if (this.Players.get(x).GetStrength(HandNbComputer) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET + " :");
} else if (this.Players.get(x).GetStrength(HandNbComputer) > 21) {
System.out.println(
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 + " :");
}
this.Players.get(x).ShowHand(HandNbComputer);
}
}
// Choices for player
System.out.println("\n--- " + App.ANSI_GREEN + "Choices" + App.ANSI_RESET + " ---\n");
......@@ -267,6 +298,23 @@ public class GameManager {
// Check if the player can continue et prepare for the next round
public boolean ResolveTurn() {
// Computer Players take turn to play
for (int x = 1; x <= this.ComputerPlayers; x++) {
// Check there strategy
/*
* 1 = Smart
* 2 = Stupid
*/
if (this.Players.get(x).GetStrategy() == 1) {
} else if (this.Players.get(x).GetStrategy() == 2) {
while (this.Players.get(x).GetStrength(0) <= 15) {
this.Players.get(x).DrawCard(0, this.Deck);
}
}
}
// Dealer draws card until he hits 17 or more
while (this.Dealer.GetStrength(0) <= 17) {
this.Dealer.DrawCard(0, this.Deck);
......@@ -303,6 +351,61 @@ public class GameManager {
"Insured : " + App.ANSI_BLUE + this.Players.get(0).GetInsured() + App.ANSI_RESET);
}
// Apply Computer Players Loss/Win
for (int x = 1; x <= this.ComputerPlayers; x++) {
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
&& this.Players.get(x).GetStrength(HandNbComputer) != 99) {
}
// If it's a Draw
else if (this.Dealer.GetStrength(0) == this.Players.get(x).GetStrength(HandNbComputer)) {
// Player get's back his bet
this.Players.get(x).AddMoney(this.Players.get(x).GetBet(HandNbComputer));
}
// If the player has done a BlackJack
else if (this.Players.get(x).GetStrength(HandNbComputer) == 99) {
// Player gets payed 1.5 to 1
this.Players.get(x).AddMoney(this.Players.get(x).GetBet(HandNbComputer) * 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
this.Players.get(x).AddMoney(this.Players.get(x).GetBet(HandNbComputer) * 2);
}
// If the Dealer has a better score
else if (this.Players.get(x).GetStrength(HandNbComputer) < this.Dealer.GetStrength(0)) {
}
// If the Player has a better score
else if (this.Players.get(x).GetStrength(HandNbComputer) > this.Dealer.GetStrength(0)) {
// Player wins and get payed 1 to 1
this.Players.get(x).AddMoney(this.Players.get(x).GetBet(HandNbComputer) * 2);
}
}
}
// Show Computer Players hands
for (int x = 1; x <= this.ComputerPlayers; x++) {
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
+ 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_PURPLE);
if (this.Players.get(x).GetStrength(HandNbComputer) == 99) {
System.out.println("BlackJack" + App.ANSI_RESET + " :");
} else if (this.Players.get(x).GetStrength(HandNbComputer) > 21) {
System.out.println(
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 + " :");
}
this.Players.get(x).ShowHand(HandNbComputer);
}
}
// Go thew all hands of the player
for (int HandNb = 0; HandNb < this.Players.get(0).GetNbHands(); HandNb++) {
......
......@@ -4,8 +4,8 @@ import java.util.ArrayList;
interface Joueur {
// Set the Play strategy
public void SetStrategy();
// Get the Computer Player Strategy
public int GetStrategy();
// Add amount of money to the player balance
public void AddMoney(double amount);
......@@ -93,7 +93,7 @@ class JoueurHumain implements Joueur {
this.Insured = false;
}
public void SetStrategy() {
public int GetStrategy() {
// Human player has no strategy
throw new RuntimeException("Humain Player has no Strategy.");
}
......@@ -336,7 +336,13 @@ class JoueurOrdinateur implements Joueur {
private double Insurance;
private boolean Insured;
public JoueurOrdinateur(JeudeCarte Jeu, int Money) {
/*
* 1 = Smart
* 2 = Stupid
*/
private int Strategy;
public JoueurOrdinateur(JeudeCarte Jeu, int Money, int Strategy) {
this.Hands = new ArrayList<>();
......@@ -348,11 +354,11 @@ class JoueurOrdinateur implements Joueur {
this.Money = Money;
this.Insurance = 0;
this.Insured = false;
this.Strategy = Strategy;
}
public void SetStrategy() {
// TODO
// Will be implemented in Part 3 of the project
public int GetStrategy() {
return this.Strategy;
}
public void AddMoney(double amount) {
......@@ -594,7 +600,7 @@ class JoueurCroupier implements Joueur {
this.Hand = new Hand(Jeu, 1);
}
public void SetStrategy() {
public int GetStrategy() {
// Dealer has no strategy
throw new RuntimeException("Dealer has no 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
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/Hand.java
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/Carte.java
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/Joueur.java
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/GameManager.java
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/App.java
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/Paquet.java
/home/padi/Git/java-card-game/Partie_2/src/main/java/ch/hepia/JeudeCarte.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/Paquet.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/Joueur.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/App.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/Hand.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/Carte.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/GameManager.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/JeudeCarte.java
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment