【Python】Yahtzee(掷骰子游戏)模拟程序【独一无二】

简介: 【Python】Yahtzee(掷骰子游戏)模拟程序【独一无二】


👉博__主👈:米码收割机

👉技__能👈:C++/Python语言

👉公众号👈:测试开发自动化【获取源码+商业合作】

👉荣__誉👈:阿里云博客专家博主、51CTO技术博主

👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术。



一、原文要求

Yahtzee Game Simulation. Imagine you have been tasked with extending an existing piece of software that was abandoned by its previous programmer. The program compiles and runs. It does not yet play a game of Yahtzee

between to human players. There are many helper methods to code to detect dice states such as three of a kind, four of a kind, full-house, small and large straights, and Yahtzee (all five dice the same). You have been tasked to make use of the existing code as much as possible. You will need to design the score sheet and be able to display it to the players and keep track of each player’s turn. You must implement it with the parallel list structures as given. You may add other lists to manage the score sheets. The main game loop must work to allow the users to play the game again.

Good luck!

Ministry Expectations

o Demonstrate the ability to use different data types, including arrays

o Demonstrate the ability to use subprograms

o Use proper code maintenance techniques

o Use a variety of problem-solving strategies

o Design algorithms according to specifications

o Apply a software development life-cycle model (design-code-test-repeat)

o Demonstrate an understanding of the software development process

Steps

  1. Run the code attached to this project: Unit4_Project_Yahtzee.py
  2. Understand the given skeleton program and how it works
  3. Plan your changes to the existing code – set milestones
  4. Generate flow-charts or pseudocode of your changes
  5. Use step-wise-refinement to test your code at each milestone
  6. Update your programmer’s journal at every stage
  7. Complete the Yahtzee project as a two human player game
  8. Ensure that existing code is preserved as much as possible
  9. Comment all internal blocks of code as required
  10. Submit your project file and programmer’s journal.


二、中文描述

描述了一个Yahtzee(掷骰子游戏)模拟程序的开发任务。你的任务是继续开发一个由前一名程序员遗弃的软件项目。虽然该程序能够编译和运行,但尚未实现两个人类玩家之间进行Yahtzee游戏的功能。项目中有许多用于检测骰子状态的辅助方法,例如三样、四样、满屋、小顺和大顺,以及Yahtzee(五个骰子都相同)。你需要尽量使用现有的代码,并设计记分表以显示给玩家并跟踪每个玩家的回合。你必须使用给定的并行列表结构来实现它。你可以添加其他列表来管理记分表。主游戏循环必须允许用户重新玩游戏。祝你好运!

期望

o 展示使用不同数据类型的能力,包括数组

o 展示使用子程序的能力

o 使用适当的代码维护技术

o 使用多种解决问题的策略

o 根据规格设计算法

o 应用软件开发生命周期模型(设计-编码-测试-重复)

o 展示对软件开发过程的理解

步骤

  1. 运行附加到这个项目的代码:Unit4_Project_Yahtzee.py
  2. 理解给定的骨架程序及其工作方式
  3. 计划对现有代码的更改 - 设置里程碑
  4. 生成你更改的流程图或伪代码
  5. 在每个里程碑处使用分步细化来测试你的代码
  6. 在每个阶段更新你的程序员日志
  7. 完成Yahtzee项目作为一个两个人类玩家的游戏
  8. 确保尽可能保留现有代码
  9. 根据需要注释所有内部代码块
  10. 提交你的项目文件和程序员日志。

三、代码详解

这个代码实现的游戏是一个简化版本的骰子游戏,其中包括两名玩家(标记为"A"和"B")。每位玩家都有一个得分表,该得分表有13个槽位。游戏的目标是通过掷骰子来填满这些得分槽位。

游戏流程:

  1. 开始游戏:首先,轮到玩家"A"。
  2. 掷骰子:每名玩家轮流掷5个骰子(存储在列表cup中)。系统会自动为当前玩家掷骰子并显示结果。
  3. 操作命令:在每个回合中,玩家有两次机会输入命令,这些命令包括:
  • sheet:查看当前玩家的得分表。
  • score 1-13:将当前骰子的总和储存在得分表的指定槽位(从1到13)。
  • rollDice:重新掷骰子(仅对没有被保留的骰子进行)。
  • displayDice:显示当前骰子的状态。
  • holdDie 1-5:保留一个指定位置(从1到5)的骰子。
  • releaseDie 1-5:释放一个指定位置(从1到5)的骰子。
  • help:显示可用命令列表。
  1. 轮换玩家:完成两次操作后,游戏切换到另一名玩家。
  2. 得分:玩家通过输入score 1-13来将当前骰子的总和记录到得分表的某个槽位。每个槽位只能被填充一次。
  3. 游戏结束条件:当两名玩家的得分表都被完全填满时,游戏结束。

注意

  • 每个骰子都有一个保留状态(held列表中的布尔值),表示玩家是否选择保留该骰子。被保留的骰子在下次掷骰子时不会改变。
  • 得分表(score_sheet_ascore_sheet_b)最初都是空的(即全为None)。当玩家选择一个槽位并记录得分时,该槽位会被填充。
  • 由于这是一个非常简化的版本,它没有实现像Yahtzee这样复杂的骰子游戏规则。所有得分都是基于当前骰子的总和。

这个游戏主要用于展示基本的掷骰子和得分机制,并没有高级的游戏逻辑或复杂的得分规则。它也没有错误处理或数据验证的全面实现。希望这能清晰地解释游戏的整体规则!

玩家可以输入以下命令:

  • sheet:查看得分表。
  • score 1-13:将当前骰子的总和作为得分储存到指定的得分槽(1-13)中。
  • rollDice:重新掷骰子。
  • displayDice:显示当前骰子的状态。
  • holdDie 1-5:保留指定位置(1-5)的骰子。
  • releaseDie 1-5:释放指定位置(1-5)的骰子。
  • help:查看可用的命令列表。

1. A掷骰子

使用score 1 将总和计入得分槽中,并使用sheet 查看。

使用 rollDice,重新掷骰子 并使用 displayDice 显示当前骰子的状态。

2. B掷骰子

使用holdDie 1保留指定位置1的骰子。score 1 将总和计入得分槽中,并使用sheet 查看。


四、代码展示

代码框架展示:

# End of Unit 4: Yahtzee Project
#
# Purpose: to create a two player text based game.
# 1. Complete the code -alone- using the template below.
# 2. Do not 're-write' the project, make use of it.
# 3. Rules of the Game: https://en.wikipedia.org/wiki/Yahtzee
#    A sample graphical game example to play: https://cardgames.io/yahtzee/
# 4. Two Human players play a game of Yahtzee together 
# 5. The game ends when both players fill their score sheet
# 6. A turn consists of one, two or three dice rolls.
#    A turn ends when (a) a player makes the third roll and scores
#    or (b) when a player stops rolling to record the dice on the score sheet.
#    The score-sheet consists of 13 slots.  Follow the rules for
#    filling in the score sheet.  
# 7. Note: you must create a Command Line Interface for your game
# 8. commands must include:
#    sheet - displaying a player's full score sheet
#    score (1-13) - selecting a spot to score the current dice
#    rollDice() - rolling all free dice
#    displayDice() - showing the player their current dice.
#    holdDie (1-5) - a method of holding / keeping dice not to be rolled
#    releaseDie(1-5) - a method of releasing dice so they can be rolled.
#    help() - prints out a list of all valid commands for your CLI 
# 9. Follow the Major Programming Rubric - Keep a Journal!
# 0. REMOVE TEMPLATE INSTRUCTIONS - REPLACE WITH YOUR COMMENTS
#
# Author: YOUR NAME HERE
#
# Date: DATE SUBMITTED HERE
#
# Setup the initial environment 
import random
# a dictionary of dice - a data structure to make the game pretty
# - in Thonny increase font size under view to see them properly
dice = { 1 : "⚀",
         2 : "⚁",
         3 : "⚂",
         4 : "⚃",
         5 : "⚄",
         6 : "⚅"
         }
# the dice cup which hold the dice (a list of dice) - starting with impossible values
# the parallel array boolean flags required to hold specific dice 
cup = [0,0,0,0,0]
held = [False,False,False,False,False]
# 。。。。。。。。。。。
# 。。。。。。。。。。。
# 。。。。。。。。。。。
# Your Main Program Here -- helpful hints follow and the methods given are simply tested below
# you'll need a game loop
# you'll need two score sheets, one for each player
# you'll need to keep track of which player (a or b) is playing.
# you'll need to keep track of which slots on the score sheet are still available for each player
# you'll need to protect the score sheet from incorrect selections
print("""\
 __   __    _   _              
 \ \ / /_ _| |_| |_ ______ ___ 
  \ V / _` | ' \  _|_ / -_) -_)
   |_|\__,_|_||_\__/__\___\___|
                               """)
rollDice()
displayDice()
holdDie(1)  # simulating a player holding onto dice
holdDie(2)
rollDice()
displayDice()
releaseDie(1) # simulating a player making new holding pattern choices
holdDie(5)
rollDice()
displayDice()
print('chance score is: {0}'.format(chanceScore()))

完整代码截图:


相关文章
|
5月前
|
人工智能 Linux 开发工具
Python从零到一:手把手带你写出第一个实用程序
Python语法简洁易懂,适合编程新手入门。它广泛应用于人工智能、自动化办公、Web开发等领域。学习Python可快速搭建项目,拥有丰富库支持和强大社区资源。通过本教程,你将掌握基础语法、环境搭建、程序逻辑控制及实战项目开发,开启编程之旅。
587 0
|
12月前
|
存储 人工智能 运维
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
627 48
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
|
4月前
|
设计模式 决策智能 Python
Python条件控制:让程序学会"思考"的魔法
本文深入浅出地讲解Python条件控制,从基础if语句到多分支、嵌套结构,再到简洁的三元表达式与Python 3.10新增的match-case模式匹配,结合电商折扣、会员等级、ATM系统等实战案例,全面掌握程序“智能决策”的核心逻辑。
436 0
|
4月前
|
小程序 PHP 图形学
热门小游戏源码(Python+PHP)下载-微信小程序游戏源码Unity发实战指南​
本文详解如何结合Python、PHP与Unity开发并部署小游戏至微信小程序。涵盖技术选型、Pygame实战、PHP后端对接、Unity转换适配及性能优化,提供从原型到发布的完整指南,助力开发者快速上手并发布游戏。
|
12月前
|
人工智能 Python
【02】做一个精美的打飞机小游戏,python开发小游戏-鹰击长空—优雅草央千澈-持续更新-分享源代码和游戏包供游玩-记录完整开发过程-用做好的素材来完善鹰击长空1.0.1版本
【02】做一个精美的打飞机小游戏,python开发小游戏-鹰击长空—优雅草央千澈-持续更新-分享源代码和游戏包供游玩-记录完整开发过程-用做好的素材来完善鹰击长空1.0.1版本
478 7
|
6月前
|
存储 算法 区块链
从零实现Python扫雷游戏:完整开发指南与深度解析
扫雷作为Windows经典游戏,承载了许多人的童年回忆。本文将详细介绍如何使用Python和Tkinter库从零开始构建一个功能完整的扫雷游戏,涵盖游戏设计、算法实现和界面开发的全过程。
493 1
|
8月前
|
人工智能 并行计算 开发者
CUDA重大更新:原生Python可直接编写高性能GPU程序
NVIDIA在2025年GTC大会上宣布CUDA并行计算平台正式支持原生Python编程,消除了Python开发者进入GPU加速领域的技术壁垒。这一突破通过重新设计CUDA开发模型,引入CUDA Core、cuPyNumeric、NVMath Python等核心组件,实现了Python与GPU加速的深度集成。开发者可直接用Python语法进行高性能并行计算,显著降低门槛,扩展CUDA生态,推动人工智能、科学计算等领域创新。此更新标志着CUDA向更包容的语言生态系统转型,未来还将支持Rust、Julia等语言。
594 3
CUDA重大更新:原生Python可直接编写高性能GPU程序
|
7月前
|
PyTorch 算法框架/工具 C++
人工智能算法python程序运行环境安装步骤整理
本教程详细介绍Python与AI开发环境的配置步骤,涵盖软件下载、VS2017安装、Anaconda配置、PyCharm设置及组件安装等内容,适用于Windows系统,助你快速搭建开发环境。

推荐镜像

更多