有趣的终端骰子游戏

有趣的终端骰子游戏

这是早期项目之一。在编程方面,我仍在掌握各种元素。

这是一个有趣的骰子游戏,是我根据kindom come deliverence的骰子游戏制作的。我仅在终端中创建它。主要是因为我仍在尝试掌握 open gl 和其他图形输入。

非常欢迎任何反馈。

import random# menu to welcome the playerdef menu():    print("""    Welcome to dicen      Would you like to: n         1. Review the rule, n         2. play a new game n         3. review scoring of dice n         """)    try:        menu_choice = input("")    except EOFError:        print("No input received. Please run the program in an interactive environment.")        return    if menu_choice == "1":        print_rules()    elif menu_choice == "2":        new_game()    elif menu_choice == "3":        print_scroing_values()        second_meu()    else:        print("Invalid choice please choose again")        second_meu()#second menu to allow for a alteration of languagedef second_meu():    print("""  What would you like to do now?      Would you like to: n         1. Review the rule, n         2. play a new game n         3. review scoring of dice n         """)    menu_choice = input("Please enter your choice: ")    if menu_choice == "1":        print_rules()    elif menu_choice == "2":        new_game()    elif menu_choice == "3":        print_scroing_values()        second_meu()    else:        print("Invalid choice please choose again")        second_meu()#explantion of rulesdef print_rules():    print("""  A player's turn always begins by throwing all six dice. The player then selects and set aside scoring dice, and at least one die must always be set aside. Then the player can throw the remaining dice again and the situation repeats. n  Scoring combinations are counted only for the current throw, not the entire turn.n  The key element of the game is that if a throw does not produce a single scoring die, then the player's turn is over and all points scored up to that throw are forfeit. It is then the opposing player's turn to throw. n  For that reason, it's best to end your turn before the risk that not a single die will score gets too high. Sometimes it's better not to set aside all the scoring dice you you've thrown, so you stand a better chance of scoring higher on the next throw.nn  """)    second_meu()#and the scroing systemdef print_scroing_values():    print("""Scoring is as follows:    - a single 1 is worth 100 points; n    - a single 5 is worth 50 points; n    - three of a kind is worth 100 points multiplied by the given number, e.g. three 4s are worth 400 points; n    - three 1s are worth 1,000 points;n    - four or more of a kind is worth double the points of three of a kind, so four 4s are worth 800 points, five 4s are worth 1,600 points etc.n    - full straight 1-6 is worth 1500 points.n    - partial straight 1-5 is worth 500 points.n    - partial straight 2-6 is worth 750 points.nn """)# This die clas allows funtionality to roll a six sided dice and output the value.class die:    def __init__(self):        self.value = 0    def __repr__(self):        return f"{self.value}"    def roll(self):        self.value = random.randint(1, 6)#here is where the class objects are created and organised into a list for ease of use.die1 = die()die2 = die()die3 = die()die4 = die()die5 = die()die6 = die()dice = [die1, die2, die3, die4, die5, die6]#player class hold the dice values, the player name a method for rolling all 6 dice at one and rerolling specific dice.class player:    def __init__(self, name, dice_list, score=4000):        self.name = name        self.score = score        self.dice_list = dice_list    def deduct_score(self, deduction):        self.score -= deduction        return self.score    def roll_d6(self):        roll_string: str = ""                  #this funtion rolls all the dice coverts them to string and labels them 1 to 6 producing eg 1: 6, 2: 6, 3: 1, 4: 2, 5: 3, 6: 2        i = 1        for die in dice:            die.roll()            data = die.value            str_data = str(data)            str_i = str(i)            roll_string += str_i + ": " + str_data + ", "            i += 1        return roll_string    def print_d6(self):                                     #just print the values        roll_string: str = ""        i = 1        for die in dice:            data = die.value            str_data = str(data)            str_i = str(i)            roll_string += str_i + ": " + str_data + ", "            i += 1        return roll_string    def re_roll(self, index):                         #re rolls dice speficed        index-=1        dice[index].roll()        return dice[index].value#This is the main game loop it has a lot of moving parts. Take your time reviewing.def new_game():    print("Hi so what is your name?n")    human_name = input("")    human_player = player(human_name, dice, 4000)   #creating objects for both human and computer players in the player class    print("who do you wish to play against?")    computer_name = input("")    computer_player = player(computer_name, dice, 4000)    play = True    while (play):        print("""ok here is your roll:     you roll a: """)        print(human_player.roll_d6())     #use of the player class function roll_d6 to give a string of rolled dice        print("Time to score you dice")        total_dice_score = possible_to_score(human_player.dice_list)   #this function is below and check to see if any of the dice can score        print(total_dice_score)        print("Whould you like to re-roll you any dice? Y/N")  #allowing the player a chance to re roll dice        lroll = input("")        roll = lroll.upper()        if (roll == "Y"):            dice_choice(human_player)        #print(dice)        print("Time to score you dice")        total_dice_score = possible_to_score(dice)        print(total_dice_score)        human_player.deduct_score(total_dice_score)        print(f"Your score is now {human_player.score}")        print(f"Ok it's {computer_player.name} go they rolled")        print(computer_player.roll_d6())        print("They scored:")        total_dice_score = possible_to_score(dice)        print(total_dice_score)        computer_player.deduct_score(total_dice_score)        print(f"{computer_player.name} score is now {computer_player.score}")        input("")        if human_player.score <= 0 or computer_player.score <= 0:            if human_player.score  dice_score:            dice_score = temp_dice_score    if (isone_to_five == True):        temp_dice_score = 500        if temp_dice_score > dice_score:            dice_score = temp_dice_score    if (istwo_to_six == True):        temp_dice_score = 600        if temp_dice_score > dice_score:            dice_score = temp_dice_score    return dice_scoredef one(counts):    if counts[0] >= 1:        return True    else:        return Falsedef five(counts):    if counts[4] >= 1:        return True    else:        return Falsedef three_of_kind(counts):    if 3 in counts:        return True, counts.index(3)    else:        return False, Nonedef four_of_kind(counts):    if 4 in counts:        return True, counts.index    else:        return False, Nonedef five_of_kind(counts):    if 5 in counts:        return True, counts.index    else:        return False, Nonedef six_of_kind(counts):    if 6 in counts:        return True, counts.index    else:        return False, Nonedef full_straight(counts):    if all(value == 1 for value in counts):        return True    else:        return Falsedef one_to_five(counts):    if counts[0] <= 1 & counts[1] <= 1 & counts[2] <= 1 & counts[3] <= 1 & counts[4] <= 1:        return True    else:        return Falsedef two_to_six(counts):    if counts[1] <= 1 & counts[2] <= 1 & counts[3] <= 1 & counts[4] <= 1 & counts[5] <= 1:        return True    else:        return Falsedef count(dice):                   #dice is a alis for eaither human_player.dice_list or computer_player.dice_list which would look like [1, 2, 3, 4, 5, 6] with random numbers beteen 1 and 6 in each index.    value_counts = count_values(dice)    num_ones = value_counts[1]         #the job of this to take the 1: prefix to all the counts to leave behind only the count itself    num_twos = value_counts[2]    num_threes = value_counts[3]    num_fours = value_counts[4]    num_fives = value_counts[5]    num_sixes = value_counts[6]    numbers_list = [num_ones, num_twos, num_threes, num_fours, num_fives, num_sixes]    return numbers_list   #this goes back to new gamedef count_values(dice_list):    counts = {i: 0 for i in range(1, 7)}     #this created this {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}    for die in dice_list:        counts[die.value] += 1 #assins each of the dice to a value in numerical order eg of output {1: 1, 2: 1, 3: 2, 4: 1, 5: 1, 6: 0}    return countsdef dice_choice(player):    #alis for human_player    rolling = True    print("Please type the dice you want to re-roll after each choice press enter. When you finish type exit and press enter.")    while (rolling):        player_input = input("")        if player_input.isdigit():                     #checks is the input is a number            number = int(player_input)            if 1 <= number <= 6:                        #checks if it falls between 1 and 6                player.re_roll(number)                         #rolls the dice specified            else:                print("Invalid entry must be a value between 1 and 6")        elif player_input == "exit":            print(f"Your new values are: {player.print_d6()} .")              #outputs the results            rolling = False        else:            print("invalid entry must be a number or exit, please try again.")menu()

以上就是有趣的终端骰子游戏的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1352791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
如何获取 PySide6 源代码?
上一篇 2025年12月13日 17:07:59
如何利用 RAM 加速视频处理中的图像帧处理?
下一篇 2025年12月13日 17:08:13

相关推荐

  • 开源免费PHP工具 PHP开发效率提升利器

    推荐开源免费PHP开发工具以提升效率:VS Code、Sublime Text轻量高效,PhpStorm专业强大;调试用Xdebug、Kint、Ray;依赖管理选Composer;代码质量工具包括PHPStan、Psalm、PHP_CodeSniffer;数据库管理可用%ignore_a_1%MyA…

    2026年5月10日
    000
  • Golang JSON序列化:控制敏感字段暴露的最佳实践

    本教程探讨golang中如何高效控制结构体字段在json序列化时的可见性。当需要将包含敏感信息的结构体数组转换为json响应时,通过利用`encoding/json`包提供的结构体标签,特别是`json:”-“`,可以轻松实现对特定字段的忽略,从而避免敏感数据泄露,确保api…

    2026年5月10日
    000
  • 比特币新手教程 比特币交易平台有哪些

    比特币是一种去中心化的数字货币,基于区块链技术实现点对点交易,具有匿名性、有限发行和不可篡改等特点;新手可通过交易所购买,P2P交易获得比特币,常用平台包括Binance、OKX和Huobi;交易流程包括注册账户、实名认证、绑定支付方式、充值法币并下单购买,可选择市价单或限价单;比特币存储方式有交易…

    2026年5月10日
    000
  • c++中的SFINAE技术是什么_c++模板编程中的SFINAE原理与应用

    SFINAE 是“替换失败不是错误”的原则,指模板实例化时若参数替换导致错误,只要存在其他合法候选,编译器不报错而是继续重载决议。它用于条件启用模板、类型检测等场景,如通过 decltype 或 enable_if 控制函数重载,实现类型特征判断。尽管 C++20 引入 Concepts 简化了部分…

    2026年5月10日
    000
  • Go语言mgo查询构建:深入理解bson.M与日期范围查询的正确实践

    本文旨在解决go语言mgo库中构建复杂查询时,特别是涉及嵌套`bson.m`和日期范围筛选的常见错误。我们将深入剖析`bson.m`的类型特性,解释为何直接索引`interface{}`会导致“invalid operation”错误,并提供一种推荐的、结构清晰的代码重构方案,以确保查询条件能够正确…

    2026年5月10日
    100
  • Golang goroutine与channel调试技巧

    使用go run -race检测数据竞争,结合runtime.NumGoroutine监控协程数量,通过pprof分析阻塞调用栈,利用select超时避免永久阻塞,有效排查goroutine泄漏、死锁和数据竞争问题。 Go语言的goroutine和channel是并发编程的核心,但它们也带来了调试上…

    2026年5月10日
    000
  • 使用 Jupyter Notebook 进行探索性数据分析

    Jupyter Notebook通过单元格实现代码与Markdown结合,支持数据导入(pandas)、清洗(fillna)、探索(matplotlib/seaborn可视化)、统计分析(describe/corr)和特征工程,便于记录与分享分析过程。 Jupyter Notebook 是进行探索性…

    2026年5月10日
    000
  • 《魔兽世界》将于6月11日开启国服回归技术测试

    《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试

    《%ign%ignore_a_1%re_a_1%》官方宣布,将于6月11日开启国服回归技术测试,时间为7天,并称可以在6月内正式开服,玩家们可以访问官网下载战网客户端并预下载“巫妖王之怒”客户端,技术测试详情见下图。 WordAi WordAI是一个AI驱动的内容重写平台 53 查看详情 以上就是《…

    2026年5月10日 用户投稿
    200
  • 如何在HTML中插入表单元素_HTML表单控件与输入类型使用指南

    HTML表单通过标签构建,包含action和method属性定义数据提交目标与方式,常用input类型如text、password、email等适配不同输入需求,配合label、required、placeholder提升可用性,结合textarea、select、button等控件实现完整交互,是…

    2026年5月10日
    100
  • 创建指定大小并填充特定数据的Golang文件教程

    本文将介绍如何使用Golang创建一个指定大小的文件,并用特定数据填充它。我们将使用 `os` 包提供的函数来创建和截断文件,从而实现快速生成大文件的目的。示例代码展示了如何创建一个10MB的文件,并将其填充为全零数据。掌握这些方法,可以方便地在例如日志系统或磁盘队列等场景中,预先创建测试文件或初始…

    2026年5月10日
    000
  • Python命令怎样使用profile分析脚本性能 Python命令性能分析的基础教程

    使用Python的cProfile模块分析脚本性能最直接的方式是通过命令行执行python -m cProfile your_script.py,它会输出每个函数的调用次数、总耗时、累积耗时等关键指标,帮助定位性能瓶颈;为进一步分析,可将结果保存为文件python -m cProfile -o ou…

    2026年5月10日
    000
  • 如何插入查询结果数据_SQL插入Select查询结果方法

    如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法

    使用INSERT INTO…SELECT语句可高效插入数据,通过NOT EXISTS、LEFT JOIN、MERGE语句或唯一约束避免重复;表结构不一致时可通过别名、类型转换、默认值或计算字段处理;结合存储过程可提升可维护性,支持参数化与动态SQL。 将查询结果数据插入到另一个表中,可以…

    2026年5月10日 用户投稿
    000
  • 使用 WebCodecs VideoDecoder 实现精确逐帧回退

    本文档旨在解决在使用 WebCodecs VideoDecoder 进行视频解码时,实现精确逐帧回退的问题。通过比较帧的时间戳与目标帧的时间戳,可以避免渲染中间帧,从而提高用户体验。本文将提供详细的解决方案和示例代码,帮助开发者实现精确的视频帧控制。 在使用 WebCodecs VideoDecod…

    2026年5月10日
    000
  • Debian Copilot的社区活跃度如何

    debian copilot是codeberg社区维护的ai助手,旨在为debian用户提供服务。尽管搜索结果中没有直接提供关于debian copilot社区支持活跃度的具体数据,但我们可以通过debian社区的整体活跃度和特点来推断其活跃性。 Debian社区的一般情况: Debian拥有详尽的…

    2026年5月10日
    000
  • Discord.py 交互按钮超时与持久化解决方案

    本教程旨在解决Discord.py中交互按钮在一段时间后出现“This Interaction Failed”错误的问题。我们将深入探讨视图(View)的超时机制,并提供通过正确设置timeout参数以及利用bot.add_view()方法实现按钮持久化的具体方案,确保您的机器人交互功能稳定可靠,即…

    2026年5月10日
    000
  • JavaScript 动态菜单点击高亮效果实现教程

    本教程详细介绍了如何使用 JavaScript 实现动态菜单的点击高亮功能。通过事件委托和状态管理,当用户点击菜单项时,被点击项会高亮显示(绿色),同时其他菜单项恢复默认样式(白色)。这种方法避免了不必要的DOM操作,提高了性能和代码可维护性,确保了无论点击方向如何,功能都能稳定运行。 动态菜单高亮…

    2026年5月10日
    200
  • c++如何实现UDP通信_c++基于UDP的网络通信示例

    UDP通信基于套接字实现,适用于实时性要求高的场景。1. 流程包括创建套接字、绑定地址(接收方)、发送(sendto)与接收(recvfrom)数据、关闭套接字;2. 服务端监听指定端口,接收客户端消息并回传;3. 客户端发送消息至服务端并接收响应;4. 跨平台需处理Winsock初始化与库链接,编…

    2026年5月10日
    100
  • 谷歌浏览器如何截图 谷歌浏览器页面截图技巧

    谷歌浏览器如何截图 谷歌浏览器页面截图技巧谷歌浏览器如何截图 谷歌浏览器页面截图技巧谷歌浏览器如何截图 谷歌浏览器页面截图技巧谷歌浏览器如何截图 谷歌浏览器页面截图技巧

    使用谷歌浏览器的开发者工具截图步骤:1. 按ctrl+shift+i(windows/linux)或cmd+option+i(mac)打开开发者工具。2. 点击右上角三个点,选择”更多工具”,再选择”截图”。3. 选择截取整个页面。推荐的谷歌浏览器扩展…

    2026年5月10日 用户投稿
    100
  • JavaScript函数中插入加载动画(Spinner)的正确方法

    本文旨在解决在JavaScript函数中插入加载动画(Spinner)时遇到的异步问题。通过引入async/await和Promise.all,确保在数据处理完成前后正确显示和隐藏加载动画,提升用户体验。我们将提供两种实现方案,并详细解释其原理和优势。 在Web开发中,当执行耗时操作时,显示加载动画…

    2026年5月10日
    100
  • 使用 Pydantic v2 实现条件性必填字段

    本文介绍了如何在 Pydantic v2 模型中实现条件性必填字段。通过自定义验证器,可以根据模型中其他字段的值来动态地控制某些字段是否为必填项,从而满足 API 交互中数据验证的复杂需求。本文提供了一个具体的示例,展示了如何确保模型中至少有一个字段被赋值。 在 Pydantic v2 中,虽然没有…

    2026年5月10日
    000

发表回复

登录后才能评论
关注微信