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

Part 3 Without GFX 100%

parent 8bffd329
Branches
No related tags found
No related merge requests found
Showing
with 768 additions and 118 deletions
...@@ -55,5 +55,6 @@ ...@@ -55,5 +55,6 @@
"C_Cpp_Runner.useLeakSanitizer": false, "C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false, "C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false "C_Cpp_Runner.msvcSecureNoWarnings": false,
"java.configuration.updateBuildConfiguration": "interactive"
} }
\ No newline at end of file
...@@ -19,7 +19,13 @@ ...@@ -19,7 +19,13 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
...@@ -27,6 +33,11 @@ ...@@ -27,6 +33,11 @@
<artifactId>javafx-controls</artifactId> <artifactId>javafx-controls</artifactId>
<version>19</version> <version>19</version>
</dependency> </dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.9</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
Human,100.0,0
Player_1,190.0,1
Player_2,305.0,1
package ch.hepia; package ch.hepia;
import java.io.File;
import java.util.Scanner; import java.util.Scanner;
public class App { public class App {
...@@ -28,7 +29,45 @@ public class App { ...@@ -28,7 +29,45 @@ public class App {
int numPlayers; int numPlayers;
int startMoney; int startMoney;
int computerStrategy = 1; int computerStrategy = 1;
boolean loadSave = false;
String filePath = "save.csv";
File f = new File(filePath);
// Check if a save file exists
if (f.exists() && !f.isDirectory()) {
char choice = 'x';
System.out.print("Do you want to load last game save (y/n) ? ");
while (true) {
choice = App.in.next().charAt(0);
// Choices are checked with available one for confirmation
if (choice == 'y' || choice == 'n') {
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
}
}
if (choice == 'y') {
loadSave = true;
}
}
GameManager BlackJack;
if (loadSave) {
// Load save to create game
Save Game = new Save();
BlackJack = new GameManager(Game.loadMoney(), Game.loadStrategy());
} else {
// Ask the number of players until a valide answer is given // Ask the number of players until a valide answer is given
while (true) { while (true) {
...@@ -37,7 +76,8 @@ public class App { ...@@ -37,7 +76,8 @@ public class App {
// Check that the input is an valid int // Check that the input is an valid int
while (!App.in.hasNextInt()) { while (!App.in.hasNextInt()) {
System.out.println( System.out.println(
App.ANSI_YELLOW + "Please enter a valid number for the number of players." + App.ANSI_RESET); App.ANSI_YELLOW + "Please enter a valid number for the number of players."
+ App.ANSI_RESET);
System.out.print("How many players ? "); System.out.print("How many players ? ");
App.in.next(); App.in.next();
} }
...@@ -61,7 +101,8 @@ public class App { ...@@ -61,7 +101,8 @@ public class App {
// Check that the input is an valid int // Check that the input is an valid int
while (!App.in.hasNextInt()) { while (!App.in.hasNextInt()) {
System.out.println(App.ANSI_YELLOW + "Please enter a valid number for your Money." + App.ANSI_RESET); 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 ? "); System.out.print("How much money do you start with ? ");
App.in.next(); App.in.next();
} }
...@@ -104,7 +145,8 @@ public class App { ...@@ -104,7 +145,8 @@ public class App {
} }
// Create a new game // Create a new game
GameManager BlackJack = new GameManager(numPlayers, startMoney, computerStrategy); BlackJack = new GameManager(numPlayers, startMoney, computerStrategy);
}
boolean GameOver = false; boolean GameOver = false;
......
...@@ -33,6 +33,29 @@ public class GameManager { ...@@ -33,6 +33,29 @@ public class GameManager {
this.Dealer = new JoueurCroupier(Deck); this.Dealer = new JoueurCroupier(Deck);
} }
public GameManager(ArrayList<Double> Money, ArrayList<Integer> Strategy) {
// 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, Money.get(0)));
this.ComputerPlayers = 0;
// All other are controlled by the computer
if (Money.size() > 1) {
for (int x = 1; x < Money.size(); x++) {
ComputerPlayers++;
this.Players.add(new JoueurOrdinateur(Deck, Money.get(x), Strategy.get(x)));
}
}
this.Dealer = new JoueurCroupier(Deck);
}
// Phase 1 (StartTurn): // Phase 1 (StartTurn):
// Ask the player how much he wan't to bid // Ask the player how much he wan't to bid
public void StartTurn() { public void StartTurn() {
...@@ -308,6 +331,199 @@ public class GameManager { ...@@ -308,6 +331,199 @@ public class GameManager {
*/ */
if (this.Players.get(x).GetStrategy() == 1) { if (this.Players.get(x).GetStrategy() == 1) {
char[][][] Choices_2Cards = {
{
{ 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'T', 'T', 'S', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'D', 'T', 'T', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'R', 'S', 'R', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'R', 'R', 'R', 'S', 'R', 'R' },
{ 'R', 'T', 'T', 'R', 'R', 'R', 'R', 'R', 'S', 'R' },
{ 'R', 'T', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'T', 'T', 'D', 'D', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'D', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'D', 'T', 'R' },
{ 'T', 'T', 'T', 'T', 'D', 'D', 'D', 'T', 'R', 'R' },
{ 'T', 'T', 'T', 'D', 'D', 'D', 'T', 'R', 'R', 'R' },
{ 'D', 'T', 'D', 'D', 'D', 'S', 'R', 'R', 'R', 'R' },
{ 'D', 'D', 'D', 'D', 'T', 'R', 'S', 'R', 'R', 'R' },
{ 'R', 'D', 'D', 'T', 'R', 'R', 'R', 'S', 'R', 'R' },
{ 'R', 'D', 'T', 'R', 'R', 'R', 'R', 'R', 'S', 'R' },
{ 'R', 'T', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'D', 'D', 'D', 'D', 'R', 'R', 'R' },
{ 'T', 'S', 'T', 'T', 'T', 'T', 'D', 'D', 'D', 'R' },
{ 'T', 'T', 'S', 'T', 'T', 'D', 'D', 'D', 'R', 'R' },
{ 'D', 'T', 'T', 'T', 'D', 'D', 'D', 'R', 'R', 'R' },
{ 'D', 'T', 'T', 'D', 'D', 'D', 'R', 'R', 'R', 'R' },
{ 'D', 'T', 'D', 'D', 'D', 'S', 'R', 'R', 'R', 'R' },
{ 'D', 'D', 'D', 'D', 'R', 'R', 'S', 'R', 'R', 'R' },
{ 'R', 'D', 'D', 'R', 'R', 'R', 'R', 'S', 'R', 'R' },
{ 'R', 'D', 'R', 'R', 'R', 'R', 'R', 'R', 'S', 'R' },
{ 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'D', 'D', 'D', 'D', 'D', 'D', 'R', 'R', 'R' },
{ 'D', 'S', 'T', 'T', 'T', 'T', 'D', 'D', 'D', 'R' },
{ 'D', 'T', 'S', 'T', 'T', 'D', 'D', 'D', 'R', 'R' },
{ 'D', 'T', 'T', 'T', 'D', 'D', 'D', 'R', 'R', 'R' },
{ 'D', 'T', 'T', 'D', 'D', 'D', 'R', 'R', 'R', 'R' },
{ 'D', 'T', 'D', 'D', 'D', 'S', 'R', 'R', 'R', 'R' },
{ 'D', 'D', 'D', 'D', 'R', 'R', 'S', 'R', 'R', 'R' },
{ 'R', 'D', 'D', 'R', 'R', 'R', 'R', 'S', 'R', 'R' },
{ 'R', 'D', 'R', 'R', 'R', 'R', 'R', 'R', 'S', 'R' },
{ 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'D', 'D', 'D', 'D', 'D', 'D', 'R', 'R', 'R' },
{ 'D', 'S', 'T', 'T', 'T', 'T', 'D', 'D', 'D', 'R' },
{ 'D', 'T', 'S', 'T', 'T', 'D', 'D', 'D', 'R', 'R' },
{ 'D', 'T', 'T', 'T', 'D', 'D', 'D', 'R', 'R', 'R' },
{ 'D', 'T', 'T', 'D', 'D', 'D', 'R', 'R', 'R', 'R' },
{ 'D', 'T', 'D', 'D', 'D', 'S', 'R', 'R', 'R', 'R' },
{ 'D', 'D', 'D', 'D', 'R', 'R', 'S', 'R', 'R', 'R' },
{ 'R', 'D', 'D', 'R', 'R', 'R', 'R', 'S', 'R', 'R' },
{ 'R', 'D', 'R', 'R', 'R', 'R', 'R', 'R', 'S', 'R' },
{ 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
{ 'T', 'S', 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T' },
{ 'T', 'T', 'S', 'T', 'T', 'T', 'D', 'D', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'T' },
{ 'R', 'T', 'D', 'D', 'T', 'T', 'S', 'T', 'T', 'R' },
{ 'R', 'D', 'D', 'T', 'T', 'T', 'T', 'S', 'R', 'R' },
{ 'R', 'D', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'T' },
{ 'R', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'T', 'R' },
{ 'R', 'D', 'D', 'T', 'T', 'T', 'T', 'S', 'R', 'R' },
{ 'R', 'D', 'T', 'T', 'T', 'T', 'T', 'R', 'S', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'R' },
{ 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'R', 'R' },
{ 'R', 'D', 'D', 'T', 'T', 'T', 'T', 'S', 'R', 'R' },
{ 'R', 'D', 'T', 'T', 'T', 'T', 'R', 'R', 'S', 'R' },
{ 'R', 'T', 'T', 'T', 'T', 'R', 'R', 'R', 'R', 'R' },
},
{
{ 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R', 'R' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'T', 'D', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T', 'D', 'T', 'T', 'T', 'R' },
{ 'T', 'T', 'T', 'T', 'D', 'T', 'T', 'T', 'R', 'R' },
{ 'T', 'T', 'T', 'D', 'T', 'T', 'T', 'R', 'R', 'R' },
{ 'R', 'T', 'D', 'T', 'T', 'T', 'R', 'S', 'R', 'R' },
{ 'R', 'D', 'T', 'T', 'T', 'R', 'R', 'R', 'R', 'R' },
{ 'R', 'T', 'T', 'T', 'R', 'R', 'R', 'R', 'R', 'R' },
}
};
char[][] Choices_MoreThan2Cards = {
{ 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'R', 'R' },
{ 'T', 'T', 'D', 'D', 'T', 'R', 'R', 'R', 'R', 'R' },
{ 'T', 'D', 'D', 'D', 'T', 'R', 'R', 'R', 'R', 'R' },
{ 'T', 'D', 'D', 'D', 'R', 'R', 'R', 'R', 'R', 'R' },
{ 'T', 'D', 'D', 'D', 'R', 'R', 'R', 'R', 'R', 'R' },
{ 'T', 'D', 'D', 'D', 'R', 'R', 'R', 'R', 'R', 'R' },
{ 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'T', 'R' },
{ 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'T', 'R' },
{ 'T', 'T', 'D', 'D', 'T', 'T', 'T', 'T', 'R', 'R' },
{ 'T', 'T', 'T', 'D', 'T', 'T', 'T', 'R', 'R', 'R' }
};
char Choice = 'R';
// Go thew all the hands of the computer player
for (int HandNb = 0; HandNb < this.Players.get(x).GetNbHands(); HandNb++) {
if (this.Players.get(x).GetNbCards(HandNb) == 2) {
Choice = Choices_2Cards[this.Dealer.GetCardStrength(0, 0) - 1][this.Players.get(x)
.GetCardStrength(HandNb, 0) - 1][this.Players.get(x).GetCardStrength(HandNb, 1) - 1];
} else {
if (this.Players.get(x).GetStrength(HandNb) <= 8) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][0];
} else if (this.Players.get(x).GetStrength(HandNb) == 9) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][1];
} else if (this.Players.get(x).GetStrength(HandNb) == 10) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][2];
} else if (this.Players.get(x).GetStrength(HandNb) == 11) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][3];
} else if (this.Players.get(x).GetStrength(HandNb) == 12) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][4];
} else if (this.Players.get(x).GetStrength(HandNb) == 13) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][5];
} else if (this.Players.get(x).GetStrength(HandNb) == 14) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][6];
} else if (this.Players.get(x).GetStrength(HandNb) == 15) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][7];
} else if (this.Players.get(x).GetStrength(HandNb) == 16) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][8];
} else if (this.Players.get(x).GetStrength(HandNb) >= 17) {
Choice = Choices_MoreThan2Cards[this.Dealer.GetCardStrength(0, 0) - 1][9];
}
}
if (Choice == 'T') {
// Draw
this.Players.get(x).DrawCard(HandNb, this.Deck);
// Go back 1 hand to stay on the same hand in the for loop
HandNb--;
} else if (Choice == 'R') {
// Stay
} else if (Choice == 'D') {
// Double
this.Players.get(x).Double(HandNb, this.Deck);
// Go back 1 hand to stay on the same hand in the for loop
HandNb--;
} else if (Choice == 'S') {
// Split
this.Players.get(x).Split(HandNb, this.Deck);
// Go back 1 hand to stay on the same hand in the for loop
HandNb--;
}
}
} else if (this.Players.get(x).GetStrategy() == 2) { } else if (this.Players.get(x).GetStrategy() == 2) {
while (this.Players.get(x).GetStrength(0) <= 15) { while (this.Players.get(x).GetStrength(0) <= 15) {
this.Players.get(x).DrawCard(0, this.Deck); this.Players.get(x).DrawCard(0, this.Deck);
...@@ -486,14 +702,29 @@ public class GameManager { ...@@ -486,14 +702,29 @@ public class GameManager {
// Show the final player balance // Show the final player balance
System.out.println("\nMoney : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET); System.out.println("\nMoney : " + App.ANSI_BLUE + this.Players.get(0).GetMoney() + App.ANSI_RESET);
// Show Choices
System.out.println("\n--- " + App.ANSI_GREEN + "Choices" + App.ANSI_RESET + " ---\n");
System.out.println(App.ANSI_BLUE + "[c]" + App.ANSI_RESET + " Continue");
System.out.println(App.ANSI_BLUE + "[s]" + App.ANSI_RESET + " Save & Quit");
char choice = 'x';
// Ask for the player choice until a valide one is given
while (true) {
System.out.print("> "); System.out.print("> ");
// Wait for a simple input of the player choice = App.in.next().charAt(0);
try {
System.in.read(); // Choices are checked with available one for confirmation
} catch (Exception e) { if (choice == 'c' || choice == 's') {
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
}
} }
if (choice == 'c') {
// If the player has less than the minimum bid amount allowed // If the player has less than the minimum bid amount allowed
if (this.Players.get(0).GetMoney() <= 10) { if (this.Players.get(0).GetMoney() <= 10) {
return true; return true;
...@@ -510,6 +741,13 @@ public class GameManager { ...@@ -510,6 +741,13 @@ public class GameManager {
for (Joueur Player : this.Players) { for (Joueur Player : this.Players) {
Player.Reset(this.Deck); Player.Reset(this.Deck);
} }
} else if (choice == 's') {
Save Game = new Save();
Game.save(this.Players, this.ComputerPlayers);
return true;
}
return false; return false;
} }
......
...@@ -79,7 +79,7 @@ class JoueurHumain implements Joueur { ...@@ -79,7 +79,7 @@ class JoueurHumain implements Joueur {
private double Insurance; private double Insurance;
private boolean Insured; private boolean Insured;
public JoueurHumain(JeudeCarte Jeu, int Money) { public JoueurHumain(JeudeCarte Jeu, double Money) {
this.Hands = new ArrayList<>(); this.Hands = new ArrayList<>();
...@@ -342,7 +342,7 @@ class JoueurOrdinateur implements Joueur { ...@@ -342,7 +342,7 @@ class JoueurOrdinateur implements Joueur {
*/ */
private int Strategy; private int Strategy;
public JoueurOrdinateur(JeudeCarte Jeu, int Money, int Strategy) { public JoueurOrdinateur(JeudeCarte Jeu, double Money, int Strategy) {
this.Hands = new ArrayList<>(); this.Hands = new ArrayList<>();
...@@ -354,6 +354,7 @@ class JoueurOrdinateur implements Joueur { ...@@ -354,6 +354,7 @@ class JoueurOrdinateur implements Joueur {
this.Money = Money; this.Money = Money;
this.Insurance = 0; this.Insurance = 0;
this.Insured = false; this.Insured = false;
this.Strategy = Strategy; this.Strategy = Strategy;
} }
......
package ch.hepia;
import com.opencsv.CSVWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class Save {
public void save(ArrayList<Joueur> Players, int ComputerPlayers) {
String filePath = "save.csv";
File file = new File(filePath);
try {
// Create FileWriter object
FileWriter outputfile = new FileWriter(file);
// Create CSVWriter object filewriter
CSVWriter writer = new CSVWriter(outputfile, ',',
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
String[] data = { "Human", Double.toString(Players.get(0).GetMoney()), "0" };
writer.writeNext(data);
for (int x = 1; x <= ComputerPlayers; x++) {
data[0] = "Player_" + x;
data[1] = Double.toString(Players.get(x).GetMoney());
data[2] = Integer.toString(Players.get(x).GetStrategy());
writer.writeNext(data);
}
// Closing writer connection
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<Double> loadMoney() {
File file = new File("save.csv");
ArrayList<Double> Money = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String string : lines) {
String[] array = string.split(",");
Money.add(Double.valueOf(array[1]));
}
} catch (IOException e) {
e.printStackTrace();
}
return Money;
}
public ArrayList<Integer> loadStrategy() {
File file = new File("save.csv");
ArrayList<Integer> Strategy = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String string : lines) {
String[] array = string.split(",");
Strategy.add(Integer.valueOf(array[2]));
}
} catch (IOException e) {
e.printStackTrace();
}
return Strategy;
}
}
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
package ch.hepia;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test;
public class JoueurTest {
@Test
public void testAddMoney() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.AddMoney(100);
assertEquals(100, joueur.GetMoney(), 0);
}
@Test
public void testAddZeroMoney() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.AddMoney(0);
assertEquals(0, joueur.GetMoney(), 0);
}
@Test
public void testAddNegativeMoney() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.AddMoney(-50));
}
@Test
public void testRemoveMoney() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
joueur.RemoveMoney(10);
assertEquals(90, joueur.GetMoney(), 0);
}
@Test
public void testRemoveZeroMoney() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.RemoveMoney(0);
assertEquals(0, joueur.GetMoney(), 0);
}
@Test
public void testRemoveNegativeMoney() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
assertThrows(RuntimeException.class, () -> joueur.RemoveMoney(-50));
}
@Test
public void testAddBet() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
joueur.SetBet(10, 0);
assertEquals(10, joueur.GetBet(0), 0);
}
@Test
public void testAddNegativeBet() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
assertThrows(RuntimeException.class, () -> joueur.SetBet(-10, 0));
}
@Test
public void testAddTooBigBet() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
assertThrows(RuntimeException.class, () -> joueur.SetBet(101, 0));
}
@Test
public void testAddBetWrongHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
assertThrows(RuntimeException.class, () -> joueur.SetBet(1, 1));
}
@Test
public void testGetBetWrongHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 100);
joueur.SetBet(10, 0);
assertThrows(RuntimeException.class, () -> joueur.GetBet(1));
}
@Test
public void testDrawCard() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.DrawCard(0, Deck);
assertEquals(3, joueur.GetNbCards(0));
}
@Test
public void testDrawCardWrongHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(6, 52));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.DrawCard(1, Deck));
}
@Test
public void testCardStrength() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertEquals(0, joueur.GetCardStrength(0, 0));
}
@Test
public void testCardStrengthWrongCard() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.GetCardStrength(0, 2));
}
@Test
public void testCardStrengthWrongHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.GetCardStrength(1, 0));
}
@Test
public void testCardCount() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertEquals(2, joueur.GetNbCards(0));
}
@Test
public void testCardCountWrongHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.GetNbCards(1));
}
@Test
public void testShowHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStreamCaptor));
joueur.ShowHand(0);
System.setOut(System.out);
String printedOutput = outputStreamCaptor.toString().trim();
assertTrue(printedOutput.contains("🃟 🃟"));
}
@Test
public void testWrongShowHand() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.ShowHand(1));
}
@Test
public void testShowHands() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStreamCaptor));
joueur.ShowHands();
System.setOut(System.out);
String printedOutput = outputStreamCaptor.toString().trim();
assertTrue(printedOutput.contains("🃟 🃟"));
}
@Test
public void testNumberHands() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertEquals(1, joueur.GetNbHands());
}
@Test
public void testHandStrength() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertEquals(0, joueur.GetStrength(0));
}
@Test
public void testWrongHandStrength() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertThrows(RuntimeException.class, () -> joueur.GetStrength(1));
}
@Test
public void testDoubled() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertEquals(false, joueur.HasDoubled(0));
}
@Test
public void testCanSplit() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 2));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
assertEquals(true, joueur.CanSplit(0));
}
@Test
public void testCantSplit() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 3));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.DrawCard(0, Deck);
assertEquals(false, joueur.CanSplit(0));
}
@Test
public void testSplit() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 5));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.Split(0, Deck);
assertEquals(true, joueur.HasSplit(0));
}
@Test
public void testSplitMax() {
JeudeCarte Deck = new JeudeCarte(new Paquet(1, 15));
JoueurHumain joueur = new JoueurHumain(Deck, 0);
joueur.Split(0, Deck);
joueur.Split(1, Deck);
assertThrows(RuntimeException.class, () -> joueur.Split(0, Deck));
}
}
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
File added
...@@ -5,3 +5,4 @@ ...@@ -5,3 +5,4 @@
/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/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/GameManager.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/JeudeCarte.java /home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/JeudeCarte.java
/home/padi/Git/java-card-game/Partie_3/src/main/java/ch/hepia/Save.java
File deleted
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment