import java.util.Scanner; public class Main { public static int playerTurn(Player player, Deck deck) { // do player's turn while (player.getScore() < 21 && player.hit()) { player.addCard(deck.deal()); } player.printHand(); if (player.getScore() == 21) { System.out.println("Blackjack!"); } else if (player.getScore() > 21) { System.out.println("You busted."); } else { System.out.println("Player has " + player.getScore() + " points."); } return player.getScore(); } public static int dealerTurn(Dealer dealer, Deck deck, int playerScore) { // do player's turn while (dealer.getScore() < 21 && dealer.hit(playerScore)) { dealer.addCard(deck.deal()); } System.out.println("\nDealer's hand:"); dealer.printHand(); if (dealer.getScore() == 21) { System.out.println("Blackjack!"); } else if (dealer.getScore() > 21) { System.out.println("Dealer busted."); } else { System.out.println("Dealer has " + dealer.getScore() + " points."); } return dealer.getScore(); } public static void game(Player player, Dealer dealer) { // setup the deck Deck deck = new Deck(); deck.add52(); deck.shuffle(); // give player two cards player.clearHand(); player.addCard(deck.deal()); player.addCard(deck.deal()); // give dealer two cards dealer.clearHand(); dealer.addCard(deck.deal()); dealer.addCard(deck.deal()); // check for off-the-bat winners if (dealer.getScore() == 21) { System.out.println("The dealer has Blackjack!"); dealer.addWin(); return; } else if (player.getScore() == 21) { System.out.println("You have Blackjack!"); } // show dealer's starting cards to player System.out.println("Dealer starts with:"); dealer.printHand(); System.out.println("\nYour hand:"); // do each turn int playerScore = playerTurn(player, deck); int computerScore = dealerTurn(dealer, deck, playerScore); if (playerScore <= 21 && playerScore > computerScore) { // player won! player.addWin(); System.out.println("You won!"); } else if (computerScore <= 21 && computerScore >= playerScore) { // computer won dealer.addWin(); System.out.println("Sorry, you lost."); } else if (playerScore > 21) { // computer won dealer.addWin(); System.out.println("Sorry, you lost."); } else if (computerScore > 21 && playerScore <= 21) { // player won! player.addWin(); System.out.println("You won!"); } } public static void main(String args[]) { // create our two players Player player = new Player(); Dealer dealer = new Dealer(); String response; do { game(player, dealer); Scanner in = new Scanner(System.in); System.out.print("\nDo you want to play again (yes/no)? "); response = in.next(); } while (response.equalsIgnoreCase("yes")); System.out.println("\nOverall score:"); System.out.println("Player " + player.getWins()); System.out.println("Dealer " + dealer.getWins()); } }