java21点扑克牌小游戏
时间: 2023-07-06 11:33:50 浏览: 104
好的,我可以为您提供一个简单的Java 21点扑克牌小游戏的示例代码。
首先,您需要创建一个名为`Blackjack`的Java类,并使用以下代码:
```java
import java.util.*;
public class Blackjack {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Deck deck = new Deck();
deck.shuffle();
System.out.println("Welcome to Blackjack!");
System.out.print("Enter your name: ");
String playerName = input.nextLine();
Player player = new Player(playerName);
Dealer dealer = new Dealer();
System.out.println("Dealing cards...");
player.addCard(deck.dealCard());
dealer.addCard(deck.dealCard());
player.addCard(deck.dealCard());
dealer.addCard(deck.dealCard());
System.out.println(player);
System.out.println(dealer);
boolean playerBust = false;
boolean dealerBust = false;
while (true) {
System.out.print("Would you like to hit or stand? ");
String choice = input.nextLine();
if (choice.equalsIgnoreCase("hit")) {
player.addCard(deck.dealCard());
System.out.println(player);
if (player.getScore() > 21) {
System.out.println("Bust! You lose.");
playerBust = true;
break;
}
} else if (choice.equalsIgnoreCase("stand")) {
break;
}
}
if (!playerBust) {
while (dealer.getScore() < 17) {
dealer.addCard(deck.dealCard());
System.out.println(dealer);
if (dealer.getScore() > 21) {
System.out.println("Dealer busts! You win.");
dealerBust = true;
break;
}
}
if (!dealerBust) {
if (player.getScore() > dealer.getScore()) {
System.out.println("You win!");
} else if (player.getScore() < dealer.getScore()) {
System.out.println("You lose.");
} else {
System.out.println("It's a tie.");
}
}
}
}
}
```
这个示例代码使用了三个类:`Player`,`Dealer`和`Deck`。您需要在程序中创建这些类的实例。下面是这些类的代码:
```java
import java.util.*;
public class Player {
private String name;
private List<Card> hand;
public Player(String name) {
this.name = name;
this.hand = new ArrayList<Card>();
}
public void addCard(Card card) {
this.hand.add(card);
}
public int getScore() {
int score = 0;
for (Card card : this.hand) {
score += card.getValue();
}
for (Card card : this.hand) {
if (card.getValue() == 11 && score > 21) {
score -= 10;
}
}
return score;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.name);
sb.append(": ");
for (Card card : this.hand) {
sb.append(card);
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append(" (");
sb.append(this.getScore());
sb.append(")");
return sb.toString();
}
}
```
```java
import java.util.*;
public class Dealer {
private List<Card> hand;
public Dealer() {
this.hand = new ArrayList<Card>();
}
public void addCard(Card card) {
this.hand.add(card);
}
public int getScore() {
int score = 0;
for (Card card : this.hand) {
score += card.getValue();
}
for (Card card : this.hand) {
if (card.getValue() == 11 && score > 21) {
score -= 10;
}
}
return score;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Dealer: ");
sb.append(this.hand.get(0));
sb.append(", [Hidden]");
return sb.toString();
}
}
```
```java
import java.util.*;
public class Deck {
private List<Card> cards;
public Deck() {
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
this.cards = new ArrayList<Card>();
for (String suit : suits) {
for (String rank : ranks) {
int value = Integer.parseInt(rank);
if (rank.equals("Ace")) {
value = 11;
} else if (rank.equals("Jack") || rank.equals("Queen") || rank.equals("King")) {
value = 10;
}
Card card = new Card(suit, rank, value);
this.cards.add(card);
}
}
}
public void shuffle() {
Collections.shuffle(this.cards);
}
public Card dealCard() {
return this.cards.remove(0);
}
}
```
这些类中的`Card`类也需要定义,这个类表示一张扑克牌:
```java
public class Card {
private String suit;
private String rank;
private int value;
public Card(String suit, String rank, int value) {
this.suit = suit;
this.rank = rank;
this.value = value;
}
public int getValue() {
return this.value;
}
public String toString() {
return this.rank + " of " + this.suit;
}
}
```
当您运行这个程序时,它将提示您输入您的姓名,然后开始发牌。您可以选择要“hit”或“stand”。如果您的得分超过21分,则会输掉比赛。如果您选择“stand”,则荷官将继续抽牌,直到得分达到17分或更高。如果荷官超过21分,则您将赢得比赛。否则,将比较您和荷官的得分,得分更高的人将赢得比赛。
阅读全文