블랙잭(Blackjack)은 카지x에서 가장 인기 있는 카드 게임 중 하나입니다. 블랙잭은 딜러와 플레이어가 참여하는 게임으로, 각 플레이어는 딜러와 두 장의 카드를 받습니다. 플레이어는 자신의 카드의 합계를 계산하여 21에 가까워지도록 더 많은 카드를 받거나, 그대로 멈출 수 있습니다. 딜러는 자신의 카드를 공개하고, 17 이상이 될 때까지 카드를 계속 뽑습니다. 게임의 목표는 딜러보다 높은 합계를 가지면서 21에 가까워지는 것입니다. 하지만, 21을 초과하면 패배하게 됩니다. 블랙잭은 간단하면서도 전략적인 요소가 있는 게임으로, 다양한 버전과 변형이 존재함.
public class Blackjack {
private static final int CARD_LIMIT = 21;
private static final int DEALER_MIN = 17;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Blackjack!");
// Initialize the deck of cards and shuffle it
List<String> deck = createDeck();
Collections.shuffle(deck);
// Initialize the player and dealer hands
List<String> playerHand = new ArrayList<>();
List<String> dealerHand = new ArrayList<>();
// Deal the initial two cards to the player and dealer
dealCard(playerHand, deck);
dealCard(playerHand, deck);
dealCard(dealerHand, deck);
dealCard(dealerHand, deck);
// Show the player's hand and the dealer's first card
System.out.println("Your hand: " + playerHand);
System.out.println("Dealer's hand: [" + dealerHand.get(0) + ", *]");
// Allow the player to hit or stand until they go over 21 or choose to stand
while (getHandValue(playerHand) < CARD_LIMIT) {
System.out.print("Do you want to hit or stand? ");
String choice = scanner.nextLine().toLowerCase();
if (choice.equals("hit")) {
dealCard(playerHand, deck);
System.out.println("Your hand: " + playerHand);
} else if (choice.equals("stand")) {
break;
} else {
System.out.println("Invalid choice. Please choose hit or stand.");
}
}
// Show the dealer's full hand
System.out.println("Dealer's hand: " + dealerHand);
// Dealer's turn
while (getHandValue(dealerHand) < DEALER_MIN) {
dealCard(dealerHand, deck);
System.out.println("Dealer hits: " + dealerHand.get(dealerHand.size() - 1));
}
// Determine the winner
int playerValue = getHandValue(playerHand);
int dealerValue = getHandValue(dealerHand);
if (playerValue > CARD_LIMIT) {
System.out.println("You bust. Dealer wins!");
} else if (dealerValue > CARD_LIMIT) {
System.out.println("Dealer busts. You win!");
} else if (playerValue > dealerValue) {
System.out.println("You win!");
} else if (dealerValue > playerValue) {
System.out.println("Dealer wins!");
} else {
System.out.println("It's a tie!");
}
}
private static List<String> createDeck() {
List<String> deck = new ArrayList<>();
for (int i = 2; i <= 10; i++) {
for (int j = 0; j < 4; j++) {
deck.add(Integer.toString(i));
}
}
for (int i = 0; i < 4; i++) {
deck.add("J");
deck.add("Q");
deck.add("K");
deck.add("A");
}
return deck;
}
private static void dealCard(List<String> hand, List<String> deck) {
String card = deck.remove(0);
hand.add(card);
}
private static int getHandValue(List<String> hand) {
int value = 0;
int aces = 0;
for (String card : hand) {
if (card.equals("J") || card.equals("Q") || card.equals("K")) {
value += 10;
} else if (card.equals("A")) {
aces++;
} else {
value += Integer.parseInt(card);
}
}
for (int i = 0; i < aces; i++) {
if (value + 11 > CARD_LIMIT) {
value += 1;
} else {
value += 11;
}
}
return value;
}
}

위 코드에서 Blackjack 클래스는 블랙잭 게임의 로직을 담고 있습니다.
createDeck 메소드는 덱을 초기화하고 셔플하는 역할을 합니다.
dealCard 메소드는 카드 한 장을 덱에서 뽑아 손에 추가하는 역할을 합니다.
getHandValue 메소드는 손의 카드 값 합계를 계산합니다.
main 메소드에서는 게임을 진행하며, 먼저 덱을 초기화하고 셔플합니다.
그 후, 플레이어와 딜러의 초기 카드를 나누어줍니다.
플레이어에게는 두 장의 카드를, 딜러에게는 한 장의 카드를 공개하고, 게임이 시작됩니다.
플레이어는 추가 카드를 받을지 말지 결정할 수 있습니다.
플레이어가 버스트(카드 값이 21을 초과)하면 패배합니다.
그 후, 딜러는 17 이상의 카드 값을 가질 때까지 카드를 계속해서 받습니다. 딜러가 버스트하면 플레이어가 이기고, 그렇지 않은 경우에는 더 높은 카드 값의 손이 이깁니다.