21点小游戏Python详细讲解

时间: 2023-06-15 18:04:34 浏览: 71
21点是一种常见的纸牌游戏,也叫做“Blackjack”。在这个游戏中,玩家需要比庄家更靠近21分,但不能超过21分。如果玩家手中的牌点数超过21分,则玩家输掉游戏。如果庄家手中的牌点数超过21分,则庄家输掉游戏。如果玩家手中的牌点数比庄家更靠近21分,则玩家赢得游戏。 下面我们来详细讲解一下如何使用 Python 编写一个简单的 21 点游戏。 1. 初始化游戏 首先,我们需要引入随机数模块和初始化一些变量。我们需要一副牌(52张),每张牌都有一个点数和花色。点数分别为1-10、J、Q、K,花色为红桃、方块、梅花、黑桃。我们使用一个列表来存储牌的点数和花色,再使用一个列表来存储已经发出的牌。 ```python import random suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'] ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] deck = [] for suit in suits: for rank in ranks: deck.append(f'{rank} of {suit}') random.shuffle(deck) player_hand = [] dealer_hand = [] player_score = 0 dealer_score = 0 ``` 2. 发牌 我们需要发两张牌给玩家和庄家。玩家可以看到自己的牌,但只能看到庄家的一张牌。 ```python for i in range(2): player_card = deck.pop() player_hand.append(player_card) player_score += get_card_value(player_card) dealer_card = deck.pop() dealer_hand.append(dealer_card) dealer_score += get_card_value(dealer_card) print(f'Player has: {player_hand} ({player_score})') print(f'Dealer has: {dealer_hand[0]}') ``` 3. 判断是否胜利 如果玩家发牌后就直接达到了21点,则玩家赢了游戏。如果庄家发牌后就直接达到了21点,则庄家赢了游戏。 ```python if player_score == 21: print('Player wins!') return if dealer_score == 21: print('Dealer wins!') return ``` 4. 玩家操作 如果玩家没有直接达到21点,则需要询问玩家是否要继续要牌。如果玩家要牌,则再发一张牌,并计算总点数。如果玩家点数超过21点,则玩家输了游戏。 ```python while True: action = input('Do you want to hit or stand? ') if action.lower() == 'hit': card = deck.pop() player_hand.append(card) player_score += get_card_value(card) print(f'Player has: {player_hand} ({player_score})') if player_score > 21: print('Player busts! Dealer wins!') return else: break ``` 5. 庄家操作 如果玩家不再要牌后,则庄家需要继续要牌,直到庄家的点数超过17点或者爆牌。 ```python while dealer_score < 17: card = deck.pop() dealer_hand.append(card) dealer_score += get_card_value(card) print(f'Dealer has: {dealer_hand} ({dealer_score})') if dealer_score > 21: print('Dealer busts! Player wins!') return ``` 6. 判断胜负 如果玩家和庄家都没有爆牌,则比较点数大小,点数更接近21点的一方获胜。 ```python if player_score > dealer_score: print('Player wins!') elif player_score < dealer_score: print('Dealer wins!') else: print('It\'s a tie!') ``` 7. 完整代码 ```python import random suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'] ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] deck = [] for suit in suits: for rank in ranks: deck.append(f'{rank} of {suit}') random.shuffle(deck) player_hand = [] dealer_hand = [] player_score = 0 dealer_score = 0 def get_card_value(card): if card.startswith('Ace'): return 11 elif card.startswith('King') or card.startswith('Queen') or card.startswith('Jack'): return 10 else: return int(card.split()[0]) for i in range(2): player_card = deck.pop() player_hand.append(player_card) player_score += get_card_value(player_card) dealer_card = deck.pop() dealer_hand.append(dealer_card) dealer_score += get_card_value(dealer_card) print(f'Player has: {player_hand} ({player_score})') print(f'Dealer has: {dealer_hand[0]}') if player_score == 21: print('Player wins!') elif dealer_score == 21: print('Dealer wins!') else: while True: action = input('Do you want to hit or stand? ') if action.lower() == 'hit': card = deck.pop() player_hand.append(card) player_score += get_card_value(card) print(f'Player has: {player_hand} ({player_score})') if player_score > 21: print('Player busts! Dealer wins!') break else: while dealer_score < 17: card = deck.pop() dealer_hand.append(card) dealer_score += get_card_value(card) print(f'Dealer has: {dealer_hand} ({dealer_score})') if dealer_score > 21: print('Dealer busts! Player wins!') break else: if player_score > dealer_score: print('Player wins!') elif player_score < dealer_score: print('Dealer wins!') else: print('It\'s a tie!') break ```

相关推荐

最新推荐

recommend-type

20行python代码的入门级小游戏的详解

主要介绍了python入门级小游戏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Python小游戏之300行代码实现俄罗斯方块

主要给大家介绍了关于Python小游戏之300行代码实现俄罗斯方块的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
recommend-type

Python实现打砖块小游戏代码实例

主要介绍了Python打砖块小游戏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Python语言编写智力问答小游戏功能

本篇博文将使用Python代码语言简单编写一个轻松益智的小游戏,效果如下所示: 1、设计思路 本项目使用SQLite建立问答题库,每道题包括4个选项答案(3个正确答案,1个错误答案)。每道题都有一定分值,根据用户的...
recommend-type

Python I/O与进程的详细讲解

今天小编就为大家分享一篇关于Python I/O与进程的详细讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

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