开发者

Need help psuedocoding a dice game and dont know where to even start [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 3 years ago.

Improve this question

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.

The whole problem I have is I dont even know where to start. I think I need this to repeat 3 times, but what variables do set? Please som开发者_Go百科eone help me, I cant really ask my instructor because he is outside smoking the whole class and everything I have learned about this class mostly came from the internet and reading the book. I dont want to fail this class...someone please help me through this???


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.


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.


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! :)


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜