0

I am a beginner IT student and doing a project for my programming logic and design class. I need to create a psuedocode for a dice game that allows you 2 rolls with 5 dice. On the first roll you get to pick 1 die to keep. The computer then rolls the other 4 dice and calculates you're score based on what you rolled. There are 3 rolls per game and the total score is displayed. Rolling nothing takes points away. The scoring is: 2 of a kind=50 points, 3 of a kind=75 points, 4 of a kind=100 points and nothing subtracts 50 points.

我的整个问题是我什至不知道从哪里开始。我想我需要重复 3 次,但是设置了哪些变量?请有人帮助我,我真的不能问我的导师,因为他在整个班级都在外面抽烟,而我对这门课的了解主要来自互联网和阅读书籍。我不想在这门课上不及格……有人请帮我解决这个问题???

4

5 回答 5

2

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.

于 2011-04-28T14:01:14.713 回答
0

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

一旦你有了它,你需要为每个骰子的每一卷调用它。如果你掷 5 个骰子,你会叫它 5 次。你会再叫它 5 次以获得第二次滚动。

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

于 2011-04-28T13:40:35.960 回答
0

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

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

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

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

于 2011-04-28T13:41:04.493 回答
0

只需在此处写下来,您就已经几乎回答了这个问题。什么是伪代码没有严格的定义。你为什么不从重写你在这里描述的一系列步骤开始。然后,对于每个步骤,只需进一步完善该步骤,直到您认为您已将其设置为您喜欢的细粒度。

你可以从这样的事情开始:

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

觉得问 SO 比问你的导师更容易,这很奇怪!:)

于 2011-04-28T13:45:23.053 回答
0

嗯,根据我的经验,伪代码最好在你假装你正在为其他人编写工作时编写:

我们需要的东西

  • 骰子
  • 球员
  • 分数

我们追踪的东西

  • 掷骰子
  • 球员得分

我们知道的事情

(这些也称为常量)

  • 无(-50)
  • 2种(+50)
  • 3 种 (+75)
  • 4种(+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"""

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

祝你好运!

于 2011-09-30T00:41:23.587 回答