我让DeepSeek写程序,有懂的看看写的对不对?
itomcoil 2025-05-23 17:47 3 浏览
他写的时候就像教学生解方程一样,解释一段写一段,因为中间太长,我就截了最后的结果,应该是手机版本复制字数有限,可能也没复制全,有没有懂的看看写的对不对?
下面是他写的最后结果。
import random
from collections import defaultdict
def create_deck():
"""创建一副扑克牌"""
deck = []
# 3-14 代表 3到A, 15代表2, 16小王,17大王
for point in range(3, 16):
deck.extend([point] * 4)
deck.extend([16, 17]) # 添加大小王
return deck
def validate_play(cards):
"""验证牌型有效性"""
if not cards:
return None
sorted_cards = sorted(cards)
count = len(sorted_cards)
freq = defaultdict(int)
for card in sorted_cards:
freq[card] += 1
# 单张
if count == 1:
return {'type': 'single', 'rank': sorted_cards[0]}
# 对子或王炸
if count == 2:
if sorted_cards[0] == sorted_cards[1]:
return {'type': 'pair', 'rank': sorted_cards[0]}
if sorted_cards == [16, 17]:
return {'type': 'rocket', 'rank': 17}
return None
# 三张
if count == 3 and len(set(sorted_cards)) == 1:
return {'type': 'triple', 'rank': sorted_cards[0]}
# 三带一或炸弹
if count == 4:
if len(set(sorted_cards)) == 1:
return {'type': 'bomb', 'rank': sorted_cards[0]}
triple = [k for k, v in freq.items() if v == 3]
if triple and len(freq) == 2:
return {'type': 'triple_plus_one', 'rank': triple[0]}
# 炸弹(四张)
if count == 4 and len(set(sorted_cards)) == 1:
return {'type': 'bomb', 'rank': sorted_cards[0]}
return None
def compare_plays(last, current):
"""比较两次出牌的大小"""
if not current:
return False
# 王炸最大
if current['type'] == 'rocket':
return True
if last and last['type'] == 'rocket':
return False
# 炸弹压制非炸弹
if current['type'] == 'bomb' and last['type'] != 'bomb':
return True
if last and last['type'] == 'bomb' and current['type'] != 'bomb':
return False
# 同类牌比较
if current['type'] == last['type']:
return current['rank'] > last['rank']
return False
def computer_play(hand, last_play):
"""电脑玩家出牌策略"""
freq = defaultdict(int)
for card in hand:
freq[card] += 1
# 尝试出牌
if not last_play: # 首出
return [min(hand)] if hand else []
# 跟牌逻辑
candidates = []
if last_play['type'] == 'single':
candidates = [[c] for c in hand if c > last_play['rank']]
elif last_play['type'] == 'pair':
pairs = [k for k, v in freq.items() if v >= 2 and k > last_play['rank']]
candidates = [[p, p] for p in pairs]
elif last_play['type'] == 'triple':
triples = [k for k, v in freq.items() if v >= 3 and k > last_play['rank']]
candidates = [[t, t, t] for t in triples]
elif last_play['type'] == 'bomb':
bombs = [k for k, v in freq.items() if v == 4 and k > last_play['rank']]
candidates = [[b]*4 for b in bombs]
if 16 in hand and 17 in hand:
candidates.append([16, 17])
# 选择最小的可用牌
if candidates:
return min(candidates, key=lambda x: (x[0], len(x)))
# 尝试出炸弹
bombs = [k for k, v in freq.items() if v == 4]
if bombs:
return [bombs[0]]*4
if 16 in hand and 17 in hand:
return [16, 17]
return []
# 初始化游戏
deck = create_deck()
random.shuffle(deck)
players = [[], [], []]
for i in range(51):
players[i % 3].append(deck[i])
landlord_cards = deck[51:]
# 随机选择地主
landlord = random.randint(0, 2)
players[landlord].extend(landlord_cards)
players[landlord].sort()
current_player = landlord
last_play = None
passed = 0
while True:
print(f"\n玩家{current_player} 的手牌:{sorted(players[current_player])}")
if current_player == 0: # 人类玩家
while True:
try:
choice = input("出牌(用,分隔)或输入pass: ").strip()
if choice.lower() == 'pass':
play = []
break
play = list(map(int, choice.split(',')))
temp = players[current_player].copy()
for card in play:
temp.remove(card)
if validate_play(play):
break
print("无效出牌!")
except:
print("输入错误!")
else: # 电脑玩家
play = computer_play(players[current_player], last_play)
print(f"电脑出牌:{play}" if play else "电脑选择pass")
# 处理出牌
if play:
play_type = validate_play(play)
if last_play and not compare_plays(last_play, play_type):
print("出牌不符合规则!")
continue
# 移除手牌
for card in play:
players[current_player].remove(card)
last_play = play_type
passed = 0
print(f"玩家{current_player} 出牌:{play} ({play_type['type']})")
# 胜利判定
if not players[current_player]:
print(f"玩家{current_player} 获胜!")
exit()
else:
passed += 1
print(f"玩家{current_player} 选择pass")
# 轮次结束判断
if passed == 2:
last_play = None
passed = 0
print("
相关推荐
- Python 最常用的语句、函数有哪些?
-
1.#coding=utf-8①代码中有中文字符,最好在代码前面加#coding=utf-8②pycharm不加可能不会报错,但是代码最终是会放到服务器上,放到服务器上的时候运行可能会报错。③...
- PyYAML 实用的使用技巧
-
作者:Reorx’sForge中文版:https://reorx.com/blog/python-yaml-tips-zh英文版:Tipsthatmaysaveyoufromthehe...
- 学习编程第127天 python中字符串与数值中的巧妙应用
-
今天学习的刘金玉老师零基础Python教程第10期,主要内容是python中字符串与数值中的巧妙应用。一、新建一个工程如图,新建一个工程。二、字符串与数值的区别变量只有为数值的时候,才能进行数学运算。...
- Python 必学!12 个 “开挂级” 内置函数深度解析(小白也能秒懂)
-
干货来了以下是Python中12个强大内置函数的深度解析,涵盖数据处理、代码优化和高级场景,助你写出更简洁高效的代码:一、数据处理三剑客1.map(function,iterable)作用:...
- Python浮点数保留两位小数的方法
-
技术背景在Python编程中,经常会遇到需要将浮点数保留特定小数位数的情况,比如在处理货币、统计数据等场景。然而,由于浮点数在计算机中采用二进制表示,存在精度问题,导致直接使用round函数有时无法得...
- DAY4-step5 Python示例说明 round()函数
-
Round()Round()是python提供的内置函数。它将返回一个浮点数,该浮点数将四舍五入到指定的精度。如果未指定要舍入的小数位,则将其视为0,并将舍入到最接近的整数。语法:round(flo...
- 第五个测试版本了,iOS 9 又有了什么变化?
-
今天的早些时候苹果发布了iOS9beta5,除去修复BUG和提升系统的稳定性外,苹果还带来了一些新功能。本次更新包括了对Carplay,WiFi以及Siri等功能的优化,Mac...
- 如何在 Python 中随机排列列表元素
-
在本教程中,我们将学习在Python中如何打乱列表元素顺序,随机排列列表元素。如何随机排列列表是一项非常有用的技能。它在开发需要选择随机结果游戏中非常有用。它还适用于数据相关的工作中,可能需要提取...
- Python获取随机数方法汇总
-
1.random.random()作用:随机生成一个[0,1)之间的浮点数importrandomprint(f'随机生成一个[0,1)之间的浮点数={random.random()}&...
- Python程序开发之简单小程序实例(11)小游戏-跳动的小球
-
Python程序开发之简单小程序实例(11)小游戏-跳动的小球一、项目功能用户控制挡板来阻挡跳动的小球。二、项目分析根据项目功能自定义两个类,一个用于控制小球在窗体中的运动,一个用于接收用户按下左右键...
- Keras人工智能神经网络 Regressor 回归 神经网络搭建
-
前期分享了使用tensorflow来进行神经网络的回归,tensorflow构建神经网络本期我们来使用Keras来搭建一个简单的神经网络Keras神经网络可以用来模拟回归问题(regression)...
- 我让DeepSeek写程序,有懂的看看写的对不对?
-
他写的时候就像教学生解方程一样,解释一段写一段,因为中间太长,我就截了最后的结果,应该是手机版本复制字数有限,可能也没复制全,有没有懂的看看写的对不对?下面是他写的最后结果。importrandom...
- Picker Wheel 线上随机抽签轮盘
-
#头条创作挑战赛#办公室经常会碰到「中午要吃什么?」、「要订哪家饮料店?」或「谁要去帮大家跑腿?」等各种情境,为了公平起见,我们可以使用随机方式进行抽签,这样一来就能确保公平公正性,其他人也就不...
- 思维链COT原理探究
-
TEXTANDPATTERNS:FOREFFECTIVECHAINOFTHOUGHTITTAKESTWOTOTANGO测试模型:PaLM-62B,GPT3,CODEXCOT元素...
- 永别了iPod!系列产品回顾,你用过几款?
-
中关村在线消息:就在本周,苹果官方宣布iPod系列将不再生产新品,现货售完即止。作为一个偏向音乐播放的系列,iPod系列想必陪伴了很多朋友的学生年代。近日有外媒总结了iPod系列的全部产品,来看看你用...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)