Skip to content

Commit

Permalink
[Implement] AI Development devprnvk
Browse files Browse the repository at this point in the history
  • Loading branch information
devprnvk committed Jul 9, 2024
1 parent a440bd5 commit b8a8fc5
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 18 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions BlackJack/src/AI/AIPlayer.java

This file was deleted.

6 changes: 6 additions & 0 deletions BlackJack/src/AIPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class AIPlayer extends Player {
public AIPlayer(int initialBalance) {
super(initialBalance);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
package AI;

public class AIStrategy {
}
58 changes: 46 additions & 12 deletions BlackJack/src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,39 @@ public class Main {
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Player player = new Player(1000); // Initialize player with a balance
Player player = null;
AIPlayer aiPlayer = null;
GameStatistics stats = new GameStatistics();
BlackjackGameLogic game = new BlackjackGameLogic(player);
BlackjackGameLogic game = null;

System.out.println("Welcome to AI Blackjack!");
System.out.print("Choose game mode: Easy or AI? ");
String gameMode = scanner.nextLine().trim().toLowerCase();

if (gameMode.equals("ai")) {
aiPlayer = new AIPlayer(1000);
game = new BlackjackGameLogic(aiPlayer);
} else {
player = new Player(1000);
game = new BlackjackGameLogic(player);
}


boolean playAgain = true;
while (playAgain && player.getBalance() > 0) {
while (playAgain && (gameMode.equals("easy") ? player.getBalance() > 0 : aiPlayer.getBalance() > 0)) {
stats.incrementGamesPlayed();

System.out.println("Current Balance: " + player.getBalance() + " chips");
System.out.println("Current Balance: " + (gameMode.equals("easy") ? player.getBalance() : aiPlayer.getBalance()) + " chips");
int betAmount = 0;
while (true) {
try {
System.out.print("Place your bet: ");
betAmount = Integer.parseInt(scanner.nextLine());
player.placeBet(betAmount);
if (gameMode.equals("easy")) {
player.placeBet(betAmount);
} else {
aiPlayer.placeBet(betAmount);
}
break;
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
Expand All @@ -51,7 +69,11 @@ public static void main(String[] args) {
if (game.getPlayerHand().hasBusted()) {
System.out.println("Player's hand: " + game.getPlayerHand() + " (Total: " + game.getPlayerHand().getTotalValue() + ")");
System.out.println("Player busted!");
player.loseBet(betAmount);
if (gameMode.equals("easy")) {
player.loseBet(betAmount);
} else {
aiPlayer.loseBet(betAmount);
}
stats.incrementGamesLost();
break;
}
Expand All @@ -71,21 +93,33 @@ public static void main(String[] args) {
int dealerTotal = game.getDealerHand().getTotalValue();

if (playerTotal > dealerTotal && !game.getPlayerHand().hasBusted() || game.getDealerHand().hasBusted()) {
player.winBet(2 * betAmount);
if (gameMode.equals("easy")) {
player.winBet(2 * betAmount);
} else {
aiPlayer.winBet(2 * betAmount);
}
stats.incrementGamesWon();
} else if (dealerTotal > playerTotal && !game.getDealerHand().hasBusted()) {
player.loseBet(betAmount);
if (gameMode.equals("easy")) {
player.loseBet(betAmount);
} else {
aiPlayer.loseBet(betAmount);
}
stats.incrementGamesLost();
} else {
player.tieBet(betAmount);
if (gameMode.equals("easy")) {
player.tieBet(betAmount);
} else {
aiPlayer.tieBet(betAmount);
}
stats.incrementGamesTied();
}
}

System.out.println("Current Score: Games Played: " + stats.getGamesPlayed() + " | Wins: " + stats.getGamesWon() + " | Losses: " + stats.getGamesLost() + " | Ties: " + stats.getGamesTied());
System.out.println("Current Balance: " + player.getBalance() + " chips");
System.out.println("Current Balance: " + (gameMode.equals("easy") ? player.getBalance() : aiPlayer.getBalance()) + " chips");

if (player.getBalance() <= 0) {
if ((gameMode.equals("easy") ? player.getBalance() : aiPlayer.getBalance()) <= 0) {
System.out.println("You are out of chips!");
break;
}
Expand All @@ -102,6 +136,6 @@ public static void main(String[] args) {
scanner.close();
System.out.println("Final Score: Games Played: " + stats.getGamesPlayed() + " | Wins: " + stats.getGamesWon() + " | Losses: " + stats.getGamesLost() + " | Ties: " + stats.getGamesTied());
System.out.println("Win Percentage: " + stats.getWinPercentage() + "%");
System.out.println("Final Balance: " + player.getBalance() + " chips");
System.out.println("Final Balance: " + (gameMode.equals("easy") ? player.getBalance() : aiPlayer.getBalance()) + " chips");
}
}
Binary file removed out/production/AIBlackJack/AI/AIPlayer.class
Binary file not shown.
Binary file removed out/production/AIBlackJack/AI/AIStrategy.class
Binary file not shown.
Binary file added out/production/AIBlackJack/AIPlayer.class
Binary file not shown.
Binary file added out/production/AIBlackJack/AIStrategy.class
Binary file not shown.
Binary file modified out/production/AIBlackJack/Main.class
Binary file not shown.

0 comments on commit b8a8fc5

Please sign in to comment.