用Python实现斗地主人机对战

时间: 2023-11-05 17:51:28 浏览: 49
斗地主是一种比较常见的纸牌游戏,可以用Python编写一个基本的人机对战程序。 下面是一个简单的实现示例: ```python import random # 初始化牌组 cards = [] for i in range(3, 11): cards.extend([str(i)] * 4) cards.extend(['J'] * 4) cards.extend(['Q'] * 4) cards.extend(['K'] * 4) cards.extend(['A'] * 4) cards.extend(['2', '小王', '大王']) # 洗牌 random.shuffle(cards) # 定义牌型 card_types = { 'single': 1, 'pair': 2, 'triple': 3, 'triple_with_single': 4, 'triple_with_pair': 5, 'four_with_two_single': 6, 'four_with_two_pair': 7, 'straight': 8, 'straight_pair': 9, 'aircraft_with_wings': 10 } # 牌型判断函数 def get_card_type(card_list): card_len = len(card_list) if card_len == 1: return 'single' elif card_len == 2 and card_list[0] == card_list[1]: return 'pair' elif card_len == 3 and card_list[0] == card_list[1] and card_list[1] == card_list[2]: return 'triple' elif card_len == 4 and card_list[0] == card_list[1] and card_list[1] == card_list[2] and card_list[2] == card_list[3]: return 'four_with_two_single' elif card_len == 5 and card_list[0] == card_list[1] and card_list[1] == card_list[2] and card_list[2] == card_list[3] and card_list[3] != card_list[4]: return 'triple_with_single' elif card_len == 5 and card_list[0] != card_list[1] and card_list[1] == card_list[2] and card_list[2] == card_list[3] and card_list[3] == card_list[4]: return 'triple_with_single' elif card_len == 6 and card_list[0] == card_list[1] and card_list[1] == card_list[2] and card_list[2] == card_list[3] and card_list[3] == card_list[4] and card_list[4] == card_list[5]: return 'four_with_two_pair' elif card_len >= 5 and card_len % 2 == 1: return None elif card_len >= 6 and card_list[-1] == '2': return None elif card_len == 6 and card_list[0] == card_list[1] and card_list[1] == card_list[2] and card_list[3] == card_list[4] and card_list[4] == card_list[5]: return 'straight_pair' elif card_len >= 5 and card_list[-1] != '2': for i in range(card_len - 1): if int(card_list[i + 1]) - int(card_list[i]) != 1: return None return 'straight' else: return None # 定义出牌函数 def play_cards(hand_cards, last_cards=[]): if not last_cards: # 首次出牌,出单张牌 return [hand_cards.pop(0)] else: last_type = get_card_type(last_cards) hand_type = get_card_type(hand_cards) if hand_type is None or (last_type is not None and card_types[hand_type] < card_types[last_type]): # 不能出牌或者手牌牌型不大于上家 return [] elif hand_type != last_type: # 牌型不同,只能出炸弹或者火箭 if hand_type == 'four_with_two_single' or hand_type == 'four_with_two_pair': return hand_cards[:4] elif hand_type == 'aircraft_with_wings': triple_list = [] for i in range(len(hand_cards) - 2): if hand_cards[i] == hand_cards[i + 1] and hand_cards[i + 1] == hand_cards[i + 2]: triple_list.append(hand_cards[i]) return triple_list * 3 else: return [] else: # 牌型相同,出牌逻辑 if hand_type == 'single' or hand_type == 'pair' or hand_type == 'triple': for card in hand_cards: if card > last_cards[0]: return [card] return [] elif hand_type == 'triple_with_single': triple_list = [] for i in range(len(hand_cards) - 2): if hand_cards[i] == hand_cards[i + 1] and hand_cards[i + 1] == hand_cards[i + 2]: triple_list.append(hand_cards[i]) if len(triple_list) == 0: return [] for triple in triple_list: if triple > last_cards[0]: single_list = [card for card in hand_cards if card != triple] return [triple] + single_list[:1] return [] elif hand_type == 'triple_with_pair': triple_list = [] for i in range(len(hand_cards) - 2): if hand_cards[i] == hand_cards[i + 1] and hand_cards[i + 1] == hand_cards[i + 2]: triple_list.append(hand_cards[i]) if len(triple_list) == 0: return [] for triple in triple_list: if triple > last_cards[0]: pair_list = [card for card in hand_cards if card != triple] pair_list = [pair_list[i:i+2] for i in range(0, len(pair_list), 2)] pair_list = sorted(pair_list, key=lambda x: int(x[0])) return [triple] + pair_list[:1][0] return [] elif hand_type == 'straight': hand_len = len(hand_cards) last_len = len(last_cards) if hand_len != last_len: return [] if hand_cards[-1] <= last_cards[-1]: return [] for i in range(last_len): if hand_cards[i] <= last_cards[i]: return [] return hand_cards elif hand_type == 'straight_pair': hand_len = len(hand_cards) last_len = len(last_cards) if hand_len != last_len: return [] if hand_cards[-1] <= last_cards[-1]: return [] for i in range(last_len): if hand_cards[i] <= last_cards[i]: return [] pair_list = [hand_cards[i:i+2] for i in range(0, hand_len, 2)] return pair_list elif hand_type == 'four_with_two_single': if hand_cards[0] <= last_cards[0]: return [] return hand_cards[:4] + hand_cards[-2:] elif hand_type == 'four_with_two_pair': if hand_cards[0] <= last_cards[0]: return [] pair_list = [hand_cards[i:i+2] for i in range(0, len(hand_cards), 2)] pair_list = sorted(pair_list, key=lambda x: int(x[0])) return hand_cards[:4] + pair_list[:2][0] elif hand_type == 'aircraft_with_wings': hand_len = len(hand_cards) last_len = len(last_cards) if hand_len != last_len: return [] if hand_cards[-1] <= last_cards[-1]: return [] triple_list = [] for i in range(len(hand_cards) - 2): if hand_cards[i] == hand_cards[i + 1] and hand_cards[i + 1] == hand_cards[i + 2]: triple_list.append(hand_cards[i]) if len(triple_list) != last_len // 4: return [] triple_list = sorted(triple_list, key=lambda x: int(x)) wings_list = [card for card in hand_cards if card not in triple_list] if len(wings_list) != last_len // 2: return [] wings_list = [wings_list[i:i+(last_len // 4)] for i in range(0, len(wings_list), last_len // 4)] return triple_list * 3 + wings_list[:1][0] else: return [] # 定义游戏函数 def play_game(): # 初始化玩家手牌 player1_cards = cards[:17] player2_cards = cards[17:34] player3_cards = cards[34:] # 首次出牌 last_cards = [] turn = 1 while player1_cards and player2_cards and player3_cards: if turn == 1: print('玩家1出牌') print('上一家出牌:', last_cards) print('手牌:', player1_cards) play_cards_list = play_cards(player1_cards, last_cards) if not play_cards_list: print('玩家1不出') else: print('玩家1出牌:', play_cards_list) for card in play_cards_list: player1_cards.remove(card) last_cards = play_cards_list turn = 2 elif turn == 2: print('玩家2出牌') print('上一家出牌:', last_cards) print('手牌:', player2_cards) play_cards_list = play_cards(player2_cards, last_cards) if not play_cards_list: print('玩家2不出') else: print('玩家2出牌:', play_cards_list) for card in play_cards_list: player2_cards.remove(card) last_cards = play_cards_list turn = 3 elif turn == 3: print('玩家3出牌') print('上一家出牌:', last_cards) print('手牌:', player3_cards) play_cards_list = play_cards(player3_cards, last_cards) if not play_cards_list: print('玩家3不出') else: print('玩家3出牌:', play_cards_list) for card in play_cards_list: player3_cards.remove(card) last_cards = play_cards_list turn = 1 if not player1_cards: print('玩家1获胜') elif not player2_cards: print('玩家2获胜') else: print('玩家3获胜') # 运行游戏 play_game() ``` 这个程序实现了基本的斗地主人机对战逻辑,可以运行并进行游戏。

相关推荐

最新推荐

recommend-type

python实现录屏功能(亲测好用)

主要介绍了使python实现录屏功能(亲测好用),本文通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

员工考勤系统.docx

员工考勤系统.docx
recommend-type

基于STM32的调试模块的外设和时钟电路分析

基于STM32的调试模块的外设和时钟电路分析。回顾 CMSIS、LL、HAL 库
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/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

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

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB数据可视化黑科技:洞悉数据背后的秘密

![MATLAB数据可视化黑科技:洞悉数据背后的秘密](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB数据可视化的基本原理 MATLAB数据可视化是一种将数据转化为图形表示形式的技术,以帮助人们理解和分析数据。其基本原理包括: - **数据表示:**将原始数据转换为图形元素,如点、线和条形。 - **图形布局:**确定图形元素在绘图区域中的位置和大小。 - **图形美化:**添加标题、标签、颜色和纹理等元素,以增强图形的可读性和美观性。 - **交互性:**允