【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()))

完整代码截图:


相关文章
|
9天前
|
存储 算法 数据库
Python 抽奖程序限定次数详解
构建Python抽奖程序,限定用户抽奖次数,使用字典存储用户ID及抽奖次数。`LotterySystem`类包含判断、记录和抽奖方法。当用户达到最大抽奖次数(默认3次)时,禁止继续。示例展示如何创建系统,模拟用户抽奖,并扩展功能如动态调整次数和多用户、多奖品池。性能优化可通过数据持久化和并发控制实现。
11 0
|
13天前
|
Shell Python
GitHub星标破千Star!Python游戏编程的初学者指南
Python 是一种高级程序设计语言,因其简洁、易读及可扩展性日渐成为程序设计领域备受推崇的语言。 目前的编程书籍大多分为两种类型。第一种,与其说是教编程的书,倒不如说是在教“游戏制作软件”,或教授使用一种呆板的语言,使得编程“简单”到不再是编程。而第二种,它们就像是教数学课一样教编程:所有的原理和概念都以小的应用程序的方式呈现给读者。
|
14天前
|
Python 索引
【Python字符串攻略】:玩转文字,编织程序的叙事艺术
【Python字符串攻略】:玩转文字,编织程序的叙事艺术
|
14天前
|
Python 存储 数据处理
【Python数据类型的奥秘】:构建程序基石,驾驭信息之海
【Python数据类型的奥秘】:构建程序基石,驾驭信息之海
|
14天前
|
Python
【Python的魅力】:利用Pygame实现游戏坦克大战——含完整源码
【Python的魅力】:利用Pygame实现游戏坦克大战——含完整源码
|
1天前
|
Web App开发 JSON 开发者
程序技术好文:用Python撸点视频背景音乐素材
程序技术好文:用Python撸点视频背景音乐素材
|
2天前
|
数据安全/隐私保护 Python
程序技术好文:猪圈密码python脚本实现
程序技术好文:猪圈密码python脚本实现
10 0
|
8天前
|
IDE Shell 程序员
[人间也值得] - Mryang带你快速入门第一个 Python 程序
[人间也值得] - Mryang带你快速入门第一个 Python 程序
6 0
|
14天前
|
XML 数据格式 Python
Python零基础入门-1 从一行代码开始运行Python程序(续)
Python零基础入门-1 从一行代码开始运行Python程序(续)
|
14天前
|
测试技术 Python
Python零基础入门-1 从一行代码开始运行Python程序
Python零基础入门-1 从一行代码开始运行Python程序