Java实现21点小游戏
时间: 2024-05-15 07:14:29 浏览: 9
21点是一种非常受欢迎的扑克牌游戏,也称为“Blackjack”。在这个游戏中,玩家与庄家竞争,目标是获得最接近21点的手牌,但不要超过21点。在此,我将向您展示如何使用Java实现简单的21点游戏。
首先,我们需要定义一个Deck类来表示一副扑克牌。该类将包括一个cards数组,用于存储卡片,并具有以下方法:
```java
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
private ArrayList<Card> cards;
public Deck() {
this.cards = new ArrayList<Card>();
}
public void createFullDeck() {
for (Suit cardSuit : Suit.values()) {
for (Value cardValue : Value.values()) {
this.cards.add(new Card(cardSuit, cardValue));
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public void removeCard(int i) {
this.cards.remove(i);
}
public Card getCard(int i) {
return this.cards.get(i);
}
public void addCard(Card addCard) {
this.cards.add(addCard);
}
public void draw(Deck comingFrom) {
this.cards.add(comingFrom.getCard(0));
comingFrom.removeCard(0);
}
public int deckSize() {
return this.cards.size();
}
public void moveAllToDeck(Deck moveTo) {
int thisDeckSize = this.cards.size();
for (int i = 0; i < thisDeckSize; i++) {
moveTo.addCard(this.getCard(i));
}
for (int i = 0; i < thisDeckSize; i++) {
this.removeCard(0);
}
}
public int cardsValue() {
int totalValue = 0;
int aces = 0;
for (Card card : this.cards) {
switch (card.getValue()) {
case TWO:
totalValue += 2;
break;
case THREE:
totalValue += 3;
break;
case FOUR:
totalValue += 4;
break;
case FIVE:
totalValue += 5;
break;
case SIX:
totalValue += 6;
break;
case SEVEN:
totalValue += 7;
break;
case EIGHT:
totalValue += 8;
break;
case NINE:
totalValue += 9;
break;
case TEN:
totalValue += 10;
break;
case JACK:
totalValue += 10;
break;
case QUEEN:
totalValue += 10;
break;
case KING:
totalValue += 10;
break;
case ACE:
aces += 1;
break;
}
}
for (int i = 0; i < aces; i++) {
if (totalValue > 10) {
totalValue += 1;
} else {
totalValue += 11;
}
}
return totalValue;
}
}
```
接下来,我们需要定义一个Card类来表示一张扑克牌。该类将包括一个suit和一个value属性,并具有以下方法:
```java
public class Card {
private Suit suit;
private Value value;
public Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
public String toString() {
return this.suit.toString() + "-" + this.value.toString();
}
public Value getValue() {
return this.value;
}
}
```
然后,我们需要定义Suit和Value枚举,以便我们可以从中选择扑克牌的花色和面值。
```java
public enum Suit {
HEARTS,
SPADES,
DIAMONDS,
CLUBS
}
public enum Value {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE
}
```
现在,我们可以创建一个Player类来表示玩家。该类将包括一个hand数组,用于存储卡片,并具有以下方法:
```java
public class Player {
private String name;
private Deck hand;
public Player(String name) {
this.name = name;
this.hand = new Deck();
this.hand.createFullDeck();
this.hand.shuffle();
}
public void draw(Deck deck) {
this.hand.draw(deck);
}
public int handValue() {
return this.hand.cardsValue();
}
public void printHand() {
System.out.println(this.name + "'s hand:");
System.out.println(this.hand.toString());
System.out.println("Hand value: " + this.handValue());
}
}
```
最后,我们需要创建一个Blackjack类来处理游戏逻辑。该类将包括一个deck属性,用于表示当前扑克牌的状态,并具有以下方法:
```java
import java.util.Scanner;
public class Blackjack {
private Deck deck;
private Player player;
private Player dealer;
private Scanner userInput;
public Blackjack() {
this.deck = new Deck();
this.player = new Player("Player");
this.dealer = new Player("Dealer");
this.userInput = new Scanner(System.in);
}
public void startGame() {
System.out.println("Welcome to Blackjack!");
while (true) {
this.deck.createFullDeck();
this.deck.shuffle();
this.player.draw(deck);
this.player.draw(deck);
this.dealer.draw(deck);
this.dealer.draw(deck);
System.out.println("Your hand is:");
this.player.printHand();
System.out.println("Dealer's hand is:");
this.dealer.printHand();
if (this.player.handValue() == 21) {
System.out.println("Blackjack! You win!");
break;
}
while (true) {
System.out.println("Would you like to hit or stand? (Enter H or S)");
String userInput = this.userInput.next();
if (userInput.equalsIgnoreCase("H")) {
this.player.draw(deck);
this.player.printHand();
if (this.player.handValue() > 21) {
System.out.println("Bust! You lose!");
break;
}
} else if (userInput.equalsIgnoreCase("S")) {
while (this.dealer.handValue() < 17) {
this.dealer.draw(deck);
}
System.out.println("Your hand:");
this.player.printHand();
System.out.println("Dealer's hand:");
this.dealer.printHand();
if (this.dealer.handValue() > 21) {
System.out.println("Dealer busts! You win!");
} else if (this.player.handValue() > this.dealer.handValue()) {
System.out.println("You win!");
} else if (this.player.handValue() == this.dealer.handValue()) {
System.out.println("Push!");
} else {
System.out.println("You lose!");
}
break;
} else {
System.out.println("Invalid input. Please enter H or S.");
}
}
System.out.println("Would you like to play again? (Enter Y or N)");
String userInput = this.userInput.next();
if (!userInput.equalsIgnoreCase("Y")) {
break;
}
}
System.out.println("Thanks for playing!");
}
}
```
现在,我们可以创建一个Main类来启动程序,并调用Blackjack类的startGame方法。
```java
public class Main {
public static void main(String[] args) {
Blackjack game = new Blackjack();
game.startGame();
}
}
```
这就是Java实现简单的21点游戏的全部内容。运行程序后,您将可以与庄家玩21点游戏,并尝试赢得最多的手牌,但不要超过21点。