需要帮助对骰子游戏进行伪编码,但不知道从哪里开始

发布于 2024-11-03 17:33:53 字数 1435 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弱骨蛰伏 2024-11-10 17:33:54

开始这一点的最简单方法是不要严格地将自己束缚于特定语言的约束,甚至不要束缚于伪代码。简单地,用自然的英语写出你将如何做到这一点。想象一下,您是计算机,有人想和您一起玩游戏。想象一下,在非常具体的细节中,您在每个可能的步骤中会做什么,即

  1. 给用户 5 个骰子,
  2. 要求用户滚动它们
  3. ,允许用户从该骰子中选择一个骰子保留

......等等。一旦你完成了这个,并且你确定它是正确的,开始通过思考计算机需要做什么来解决这个问题,将其转换为伪代码。例如,您需要一个变量来跟踪用户的点数以及总共发生了多少次。如果您对问题的英文描述非常具体,这应该意味着您基本上只需要将伪代码插入已有的几个句子中 - 换句话说,您只需用一种类型的伪代码替换另一种类型的伪代码。

我很想提供帮助,但直接提供伪代码对您没有多大帮助。开始编程最困难的步骤之一是学习将问题分解为其组成元素。这种细粒度的思维一开始并不直观,但你花的时间越多,就会变得越容易。

The easiest way to get started on this is to not rigorously bind yourself to the constraint of a specific language, or even to pseudocode. Simply, in natural English, write out how you would do this. Imagine that YOU are the computer, and somebody wants to play the game with you. Just imagine, in very specific detail, what you would do at each potential step, i.e.

  1. Give the user 5 dice
  2. Ask the user to roll them
  3. From that roll, allow the user to pick one die to keep

...etc. Once you have done this, and you are sure it is correct, start transforming it into pseudo code by thinking about what a computer would need to do to solve this problem. For instance, you'll need a variable keeping track of how many points the user as, as well as how many total rolls have occurred. If you were very specific in your English description of the problem, this should mean you basically only need to plug pseudo code into a few sentences you already have - in other words, you're just substituting one type of pseudo code for another.

I'd like to help, but straight-up providing the pseudo code wouldn't be very helpful to you. One of the hardest steps in beginning programming is learning to break a problem down into its constituent elements. That type of granular thinking is unintuitive at first, but gets easier the more time you spend on it.

煮酒 2024-11-10 17:33:54

好吧,根据我的经验,当你假装自己正在为别人编写工作时,最好编写伪代码:

我们需要的东西

  • 骰子
  • 玩家
  • 得分

我们跟踪的东西

  • 骰子掷骰
  • 玩家得分

我们知道的事情

(这些也称为常量)

  • Nothing (-50)
  • 2 of a kind (+50)
  • 3 of a kind (+75)
  • 4 of a kind (+100)

所有这些都是入门的重要工具。还有……好吧,在 stackoverflow 上提问。

接下来,定义您的“行动”(我们所做的事情),它利用我们需要的上述已知事物。

我会从我一贯做的地方开始:创造我们的东西。

def player():
    """Create a new player"""

def dice():
    """Creates 4 new, 6 sided dice"""

def welcome():
    """Welcome player by name, give option to quit"""

def game():
    """Initialize number of turns (start at 0)"""

def humanturn():
    """Roll dice, display, ask which one they'll keep"""

def compturn():
    """Roll four dice"""

def check():
    """Check for any matches in the dice"""

def score():
    """Tally up the score for any matches"""

def endturn():
    """Update turn(s), update total score"""

def gameover():
    """Display name, total score, ask for retry"""

def quit():
    """Quit the game"""

这些是你的组件,全部以非常程序化的方式充实起来。还有许多其他更好的方法可以做到这一点,但现在您只是编写一个想法的框架。当您准备开始编码时,您可能会想将其中许多方法组合在一起,但最好将所有内容分开,直到您确信自己不会在追查错误时迷失方向。

祝你好运!

Well, pseudo-code, in my experience, is best drawn up when you pretend you're writing up the work for someone else to do:

THINGS WE NEED

  • Dice
  • Players
  • Score

THINGS WE TRACK

  • Dice rolls
  • Player score

THINGS WE KNOW

(These are also called constants)

  • Nothing (-50)
  • 2 of a kind (+50)
  • 3 of a kind (+75)
  • 4 of a kind (+100)

All of these are vital tools to getting started. And...well, asking questions on stackoverflow.

Next, define your "actions" (things we do), which utilizes the above known things that we will need.

I would start the same place I always do: creating our things.

def player():
    """Create a new player"""

def dice():
    """Creates 4 new, 6 sided dice"""

def welcome():
    """Welcome player by name, give option to quit"""

def game():
    """Initialize number of turns (start at 0)"""

def humanturn():
    """Roll dice, display, ask which one they'll keep"""

def compturn():
    """Roll four dice"""

def check():
    """Check for any matches in the dice"""

def score():
    """Tally up the score for any matches"""

def endturn():
    """Update turn(s), update total score"""

def gameover():
    """Display name, total score, ask for retry"""

def quit():
    """Quit the game"""

Those are your components, all fleshed out in a very procedural manner. There are many other ways to do this that are much better, but for now you're just writing the skeleton of an idea. You may be tempted to combine many of these methods together when you're ready to start coding, but it's a good idea to separate everything until you're confident you won't get lost chasing down a bug.

Good luck!

心碎无痕… 2024-11-10 17:33:53

首先不要惊慌。你要做的就是把任务分解成小步骤。
伪代码并不是真正的代码——你不能直接将它用作一种语言,而是用简单的英语来描述你正在做什么以及事件的流程。

那么,开始的初始步骤是什么?
问问自己事实是什么,你预先知道什么存在。这些是您所做的“声明”。

你有五个骰子。每个都是一个单独的对象,因此每个都有自己的变量声明

dice_1
dice_2
dice_3
dice_4
dice_5

接下来决定每个骰子是否有初始值

dice_1 initial value = 0
etc...

接下来您知道必须多次掷骰子。 Throwing 是一个具有初始值的变量

turns initial value = 2
turns_counter initial value = 2

您现在应该明白了。还有其他需要提前申报的事情吗?我想是的!

接下来你必须一步步决定你要做什么。这只是一系列事件还是重复发生?如果重复的话如何让它停止?

While turns_counter is less than 2

重复以下操作:

turns_counter = turns_counter + 1
if turns_counter = 2 
   Throw. Collect_result. Sum_result.
else
   Throw. Collect_result. Sum_result. Remove_a_dice.
endif.

也许您必须告诉可重用代码它们将使用哪些对象?这些是您传递给可重用代码 Throw(dice_1) 的参数,也许您还需要更新您创建的一些变量?在可重用代码中执行此操作,无论是否将它们作为参数传递。

这绝不是完整或完美的,但您应该了解正在发生的事情以及如何分解它。这可能需要相当长的时间才能完成。

First of all don't panic. What you are about to do is break the task down into small steps.
Pseudo-code is not really code - you can't use it directly as a language, but instead it is just plain english to describe what it is you are doing and the flow of events.

So what are the initial steps to get you started?
Ask yourself what are the facts, what do you know exist in advance. These are the "declarations" that you make.

You have five dice. Each is a seperate object so each gets it's own variable declaration

dice_1
dice_2
dice_3
dice_4
dice_5

Next decide if each die has an initial value

dice_1 initial value = 0
etc...

Next you know that you have to throw the dice a number of times. Throwing is a variable with an initial value

turns initial value = 2
turns_counter initial value = 2

You should be getting the idea now. Are there other things you should declare in advance? I think so!

Next you have to decide what it is you are doing step by step. Is it just a sequence of events or is it repeating? If it's repeating how do you get it to stop?

While turns_counter is less than 2

Repeat the following:

turns_counter = turns_counter + 1
if turns_counter = 2 
   Throw. Collect_result. Sum_result.
else
   Throw. Collect_result. Sum_result. Remove_a_dice.
endif.

perhaps you have to tell the reusable code which objects they are going to be working with? These are parameters that you pass to the reusable code Throw(dice_1) perhaps also you need to update some variables that you created? do it in the reusable code with or without passing them as parameters.

This is by no means complete or perfect, but you should get the idea about what's going on and how to break it down. It could take quite a while to do.

归途 2024-11-10 17:33:53

大多数语言都提供伪随机数生成器函数,返回一定范围内的随机数。我首先要弄清楚您将使用哪种语言以及它提供哪些功能。

一旦你有了它,你需要为每个骰子的每一次掷骰子调用它。如果你掷 5 个骰子,你就会跟注 5 次。第二次掷骰时,您可以再叫 5 次。

无论如何,这就是一个开始。

Most languages provide a pseudo-random number generator function that returns a random number within a certain range. I would start by figuring out which language you'll use and which function it provides.

Once you have that, you will need to call it for each roll of each dice. If you are rolling 5 dice, you would call it 5 times. And you would call it 5 more times for a second roll.

That's a start anyway.

血之狂魔 2024-11-10 17:33:53

只需将其写在这里,您就已经几乎回答了这个问题。什么是伪代码没有严格的定义。为什么不首先将此处描述的内容重写为一系列步骤。然后,对于每个步骤,只需进一步完善该步骤,直到您认为您已将其做得像您喜欢的那样细粒度。

你可以这样开始:

Roll 5 dice.
Pick 1 die to keep. 
Rolls the other 4 dice
Calculate the score.
// etc...

觉得问这样的问题比你的老师更容易,这很奇怪! :)

You have already almost answered the question by simply writing it down here. There is no strict definition of what pseudocode is. Why don't you start by re-writing what you've described here as a sequence of steps. Then, for each step simply refine that step further until you think you've made it as fine-grain as you like.

You could start with something like this:

Roll 5 dice.
Pick 1 die to keep. 
Rolls the other 4 dice
Calculate the score.
// etc...

Quite weird to think that it's easier to ask SO than your instructor! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文