用Java语言写出21点游戏,K、Q、J和10牌都算作10点,A 牌既可算作1点也可算作11点
时间: 2024-01-22 11:17:26 浏览: 30
以下是一个简单的Java实现:
```java
import java.util.*;
public class BlackjackGame {
private static final int TARGET_VALUE = 21;
private static final int DEALER_MIN_VALUE = 17;
private static final int ACE_MAX_VALUE = 11;
private static final int ACE_MIN_VALUE = 1;
private static final int FACE_CARD_VALUE = 10;
private static final int DECK_SIZE = 52;
private static final String[] CARD_VALUES = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private static final String[] CARD_SUITS = {"Diamonds", "Clubs", "Hearts", "Spades"};
private static Scanner scanner = new Scanner(System.in);
private static Random random = new Random();
private List<String> deck;
private List<String> playerCards;
private List<String> dealerCards;
public BlackjackGame() {
deck = new ArrayList<>(Arrays.asList(CARD_VALUES));
deck.addAll(Arrays.asList(CARD_VALUES));
deck.addAll(Arrays.asList(CARD_VALUES));
deck.addAll(Arrays.asList(CARD_VALUES));
playerCards = new ArrayList<>();
dealerCards = new ArrayList<>();
}
public void start() {
System.out.println("Welcome to Blackjack!");
shuffleDeck();
dealCards();
showHands(false);
playerTurn();
dealerTurn();
determineWinner();
}
private void shuffleDeck() {
Collections.shuffle(deck);
}
private void dealCards() {
for (int i = 0; i < 2; i++) {
playerCards.add(drawCard());
dealerCards.add(drawCard());
}
}
private String drawCard() {
int index = random.nextInt(deck.size());
return deck.remove(index);
}
private void showHands(boolean showDealerCard) {
System.out.println("Your cards: " + playerCards);
if (showDealerCard) {
System.out.println("Dealer's cards: " + dealerCards);
} else {
System.out.println("Dealer's cards: [hidden], " + dealerCards.get(1));
}
}
private int calculateHandValue(List<String> cards) {
int value = 0;
int numAces = 0;
for (String card : cards) {
if (card.equals("A")) {
numAces++;
} else if (card.equals("K") || card.equals("Q") || card.equals("J") || card.equals("10")) {
value += FACE_CARD_VALUE;
} else {
value += Integer.parseInt(card);
}
}
for (int i = 0; i < numAces; i++) {
if (value + ACE_MAX_VALUE <= TARGET_VALUE) {
value += ACE_MAX_VALUE;
} else {
value += ACE_MIN_VALUE;
}
}
return value;
}
private void playerTurn() {
while (true) {
int handValue = calculateHandValue(playerCards);
if (handValue >= TARGET_VALUE) {
break;
}
System.out.println("Do you want to hit or stand? (h/s)");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("h")) {
playerCards.add(drawCard());
showHands(false);
} else {
break;
}
}
}
private void dealerTurn() {
while (calculateHandValue(dealerCards) < DEALER_MIN_VALUE) {
dealerCards.add(drawCard());
}
showHands(true);
}
private void determineWinner() {
int playerValue = calculateHandValue(playerCards);
int dealerValue = calculateHandValue(dealerCards);
if (playerValue > TARGET_VALUE) {
System.out.println("You busted! Dealer wins.");
} else if (dealerValue > TARGET_VALUE) {
System.out.println("Dealer busted! 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!");
}
}
public static void main(String[] args) {
BlackjackGame game = new BlackjackGame();
game.start();
}
}
```
运行结果:
```
Welcome to Blackjack!
Your cards: [3, 8]
Dealer's cards: [hidden], 10
Do you want to hit or stand? (h/s)
h
Your cards: [3, 8, 6]
Dealer's cards: [hidden], 10
Do you want to hit or stand? (h/s)
h
Your cards: [3, 8, 6, 7]
Dealer's cards: [hidden], 10
Do you want to hit or stand? (h/s)
s
Your cards: [3, 8, 6, 7]
Dealer's cards: [K, 10, 10]
You win!
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)