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点。

相关推荐

编Java程序:2至9牌,按其原点数计算;K、Q、J和10牌都算作10点;A 牌(ace)既可算作1点也可算作11点,由玩家自己决定(当玩家停牌时,点数一律视为最大而尽量不爆,如A+9为20,A+4+8为13,A+3+A视为15)。设计基于指定策略的一个21点游戏的部分功能。 指定策略为:如果手中牌的点数之和小于17点则继续要下一张牌,直到大于等于17点为止。如果手里的牌有A,且A的点数当成11点没有超过21点,则此时A要按11点计算,如果超过21点,则A要按1点计算。 一个参考的设计为:1、设计一个card类,用于保存一张牌;2、设计一个hand类,用于保存一手牌;3、设计一个player类,该类可以基于指定策略完成一次游戏过程。 输入 若干行(至少2行),每行代表一张牌。具体格式见样例。 输出 若干行。 读入前两张牌不输出,从第三张牌开始(如果需要),则每次要牌,要先输出Hit,然后读入下一张牌,并依次输出该牌的花色及点数(A输出1 11,即它有两个点数)。当不再要牌时要先输出Stand,然后在一行内输出这一手牌,牌与牌之间用一个空格分隔。牌输出的顺序为先看牌面,牌面小的在前(牌面由小到大的顺序为A,2,3....J,Q,K),当牌面相同时看花色,输出顺序从前到后为Spade, Heart, Diamond, Club。最后一行输出这一手牌的结果,如果总点数超过21点,则输出Bust,如果是Blackjack(一手牌只有两张牌且点数相加和为21点),则输出Blackjack。其他情况则输出一个整数,代表这手牌的点数(尽量大且不爆)。具体格式见样例。 样例输入 Copy Spade 4 Heart A Heart 3 样例输出 Copy Hit Heart 3 Stand HeartA Heart3 Spade4 18

最新推荐

recommend-type

Java实现简单井字棋小游戏代码实例

主要介绍了Java实现简单井字棋小游戏代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Java实现的打地鼠小游戏完整示例【附源码下载】

主要介绍了Java实现的打地鼠小游戏,结合完整实例形式分析了Java多线程操作及键盘按键响应实现的打地鼠游戏功能相关操作技巧,需要的朋友可以参考下
recommend-type

java实现简单的推箱子小游戏

主要为大家详细介绍了java实现简单的推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java小程序赛马游戏实现过程详解

主要介绍了Java小程序赛马游戏实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

java实现猜数字小游戏(Swing版)

主要介绍了java实现猜数字小游戏,Swing编程版的猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。