百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

我让DeepSeek写程序,有懂的看看写的对不对?

itomcoil 2025-05-23 17:47 14 浏览

他写的时候就像教学生解方程一样,解释一段写一段,因为中间太长,我就截了最后的结果,应该是手机版本复制字数有限,可能也没复制全,有没有懂的看看写的对不对?

下面是他写的最后结果。

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("

相关推荐

最强聚类模型,层次聚类 !!_层次聚类的优缺点

哈喽,我是小白~咱们今天聊聊层次聚类,这种聚类方法在后面的使用,也是非常频繁的~首先,聚类很好理解,聚类(Clustering)就是把一堆“东西”自动分组。这些“东西”可以是人、...

python决策树用于分类和回归问题实际应用案例

决策树(DecisionTrees)通过树状结构进行决策,在每个节点上根据特征进行分支。用于分类和回归问题。实际应用案例:预测一个顾客是否会流失。决策树是一种基于树状结构的机器学习算法,用于解决分类...

Python教程(四十五):推荐系统-个性化推荐算法

今日目标o理解推荐系统的基本概念和类型o掌握协同过滤算法(用户和物品)o学会基于内容的推荐方法o了解矩阵分解和深度学习推荐o掌握推荐系统评估和优化技术推荐系统概述推荐系统是信息过滤系统,用于...

简单学Python——NumPy库7——排序和去重

NumPy数组排序主要用sort方法,sort方法只能将数值按升充排列(可以用[::-1]的切片方式实现降序排序),并且不改变原数组。例如:importnumpyasnpa=np.array(...

PyTorch实战:TorchVision目标检测模型微调完

PyTorch实战:TorchVision目标检测模型微调完整教程一、什么是微调(Finetuning)?微调(Finetuning)是指在已经预训练好的模型基础上,使用自己的数据对模型进行进一步训练...

C4.5算法解释_简述c4.5算法的基本思想

C4.5算法是ID3算法的改进版,它在特征选择上采用了信息增益比来解决ID3算法对取值较多的特征有偏好的问题。C4.5算法也是一种用于决策树构建的算法,它同样基于信息熵的概念。C4.5算法的步骤如下:...

Python中的数据聚类及可视化分析实践

探索如何通过聚类分析揭露糖尿病预测数据集的特征!我们将运用Python的强力工具,深入挖掘数据,以直观的可视化揭示不同特征间的关系。一同探索聚类分析在糖尿病预测中的实践!所有这些可视化都可以通过数据操...

用Python来统计大乐透号码的概率分布

用Python来统计大乐透号码的概率分布,可以按照以下步骤进行:导入所需的库:使用Python中的numpy库生成数字序列,使用matplotlib库生成概率分布图。读取大乐透历史数据:从网络上找到大...

python:支持向量机监督学习算法用于二分类和多分类问题示例

监督学习-支持向量机(SVM)支持向量机(SupportVectorMachine,简称SVM)是一种常用的监督学习算法,用于解决分类和回归问题。SVM的目标是找到一个最优的超平面,将不同类别的...

25个例子学会Pandas Groupby 操作

groupby是Pandas在数据分析中最常用的函数之一。它用于根据给定列中的不同值对数据点(即行)进行分组,分组后的数据可以计算生成组的聚合值。如果我们有一个包含汽车品牌和价格信息的数据集,那么可以...

数据挖掘流程_数据挖掘流程主要有哪些步骤

数据挖掘流程1.了解需求,确认目标说一下几点思考方法:做什么?目的是什么?目标是什么?为什么要做?有什么价值和意义?如何去做?完整解决方案是什么?2.获取数据pandas读取数据pd.read.c...

使用Python寻找图像最常见的颜色_python 以图找图

如果我们知道图像或对象最常见的是哪种颜色,那么可以解决图像处理中的几个用例,例如在农业领域,我们可能需要确定水果的成熟度。我们可以简单地检查一下水果的颜色是否在预定的范围内,看看它是成熟的,腐烂的,还...

财务预算分析全网最佳实践:从每月分析到每天分析

原文链接如下:「链接」掌握本文的方法,你就掌握了企业预算精细化分析的能力,全网首发。数据模拟稍微有点问题,不要在意数据细节,先看下最终效果。在编制财务预算或业务预算的过程中,通常预算的所有数据都是按月...

常用数据工具去重方法_数据去重公式

在数据处理中,去除重复数据是确保数据质量和分析准确性的关键步骤。特别是在处理多列数据时,保留唯一值组合能够有效清理数据集,避免冗余信息对分析结果的干扰。不同的工具和编程语言提供了多种方法来实现多列去重...

Python教程(四十):PyTorch深度学习-动态计算图

今日目标o理解PyTorch的基本概念和动态计算图o掌握PyTorch张量操作和自动求导o学会构建神经网络模型o了解PyTorch的高级特性o掌握模型训练和部署PyTorch概述PyTorc...