java21点游戏代码
时间: 2023-10-03 14:07:19 浏览: 73
以下是一个简单的 Java 21点游戏代码示例:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class TwentyOneGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> deck = new ArrayList<String>();
String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
String[] values = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int[] points = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
int playerScore = 0;
int dealerScore = 0;
// Initialize the deck
for (String suit : suits) {
for (String value : values) {
deck.add(value + " of " + suit);
}
}
// Shuffle the deck
Collections.shuffle(deck);
// Deal the cards
System.out.println("Dealing cards...");
playerScore += drawCard(deck);
dealerScore += drawCard(deck);
playerScore += drawCard(deck);
dealerScore += drawCard(deck, true);
// Player turn
while (playerScore < 21) {
System.out.println("Your score is " + playerScore + ". Do you want to hit (H) or stand (S)?");
String choice = sc.nextLine();
if (choice.equalsIgnoreCase("H")) {
playerScore += drawCard(deck);
} else if (choice.equalsIgnoreCase("S")) {
break;
}
}
// Dealer turn
while (dealerScore < 17) {
System.out.println("Dealer's score is " + dealerScore + ". Dealer hits.");
dealerScore += drawCard(deck);
}
// Determine the winner
System.out.println("Your score is " + playerScore + ".");
System.out.println("Dealer's score is " + dealerScore + ".");
if (playerScore > 21) {
System.out.println("You bust! Dealer wins.");
} else if (dealerScore > 21) {
System.out.println("Dealer busts! You win.");
} else if (playerScore > dealerScore) {
System.out.println("You win!");
} else if (dealerScore > playerScore) {
System.out.println("Dealer wins.");
} else {
System.out.println("It's a tie.");
}
}
public static int drawCard(ArrayList<String> deck) {
return drawCard(deck, false);
}
public static int drawCard(ArrayList<String> deck, boolean hide) {
String card = deck.remove(0);
int score = getCardScore(card);
if (!hide) {
System.out.println("You drew a " + card + ".");
} else {
System.out.println("Dealer drew a card.");
}
return score;
}
public static int getCardScore(String card) {
String[] parts = card.split(" ");
String value = parts[0];
for (int i = 0; i < values.length; i++) {
if (value.equalsIgnoreCase(values[i])) {
return points[i];
}
}
return 0;
}
}
```
这个代码使用了 ArrayList 来表示一副牌,同时使用 Scanner 来获取用户输入。程序首先初始化牌堆,然后洗牌并发牌。接着进入玩家回合和庄家回合,最后确定胜负并输出结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)