Git Product home page Git Product logo

pfwtfp-pseudocode-implementing-dice-thrower-nyc-web-080519-mod-1-repeat's Introduction

Pseudocode for Dice Thrower

Learning Goals

  • Design solutions using pseudocode

Introduction

In the last lab, we created two methods, throw_die and generate_set, which allow us to simulate the roll of a single die and create an Array of roll results. With just these two methods, we can simulate rolling dice for all sorts of board games. Want to play Monopoly with our methods? All we'd need to do is load up IRB in our terminal, paste in our methods and then, whenever someone takes a turn, write:

2.5.1 :001 > generate_set(2) # calls throw_die twice and returns an `Array`
=> [1, 4] # you rolled a total of five and landed on Reading Railroad!

Our next task is to expand on what we've written. Right now we've got enough code to play Monopoly and Snakes and Ladders, but with a few more methods, we could simulate rolling dice for all sorts of games!

In this lesson, we will pseudocode out three new methods that build off of the throw_die and generate_set methods in the previous lab.

Keeping a Roll History

Some dice games involve keeping track of multiple rolls. A game of Liar's Dice, for instance, involves all players rolling their own set of five dice, then hiding the results. Players try to deceive other players about what dice they've rolled, revealing the results later on. Other games such as Greed involve multiple dice rolls per turn.

If we wanted to expand our dice throwing application to work with these sorts of games, we will need a way to store results over many rolls.

We could use an Array of Arrays like so:

roll_history = []

def add_to_history(roll)
  roll_history << roll
end

add_to_history(generate_set(5))
# => [[3,4,1,3,5]]
add_to_history(generate_set(5))
# => [[3,4,1,3,5], [6,3,2,2,4]]
add_to_history(generate_set(5))
# => [[3,4,1,3,5], [6,3,2,2,4], [1,5,3,4,4]]

But after a handful rolls, this could become pretty complicated to access. How would we store rolls in a Hash instead?

In 01_roll_history_pseudocode.html, pseudocode storing history in a Hash.

Special Roll Conditions

Many dice based games involve 'special' rolls. In Monopoly, if a player rolls doubles, that is, two dice with the same number, the player gets to roll and additional time. Roll too many doubles, however, and the player goes to jail.

While each game has different rules for what is considered a 'special' roll, let's focus on a single one for practice: a method that checks if a roll of one or more dice adds up to 7. The method should simply return true or false.

In 02_special_roll_conditions_pseudocode.html, follow the Flatiron Process steps and write out the pseudocode for how you would implement this method.

seven

Multi-Sided Dice

Finally, if we want to build a versatile dice throwing application, we can't forget our tabletop roleplaying friends! So far, we've focused on six sided dice, but what if we wanted to set the number of sides on the die that we roll?

3-sided dice

At minimum, a die can have 3 sides (any fewer and we would be flipping heads or tails):

3-sided dice

For simplicity, we will say that at maximum, a single die can have 100 sides:

3-sided dice

How would we write a method that allows us to simulate throwing these dice? In addition, how would we prevent our method from rolling a die that has less than 3 or more than 100 sides? What should the output be?

In 03_multisided_dice_pseudocode.html, follow the Flatiron Process steps and write out the pseudocode for how you would implement this method.

Submitting This Lab

When you're done providing the pseudocode, you'll need to submit this lab using learn submit. The tests will verify that you've filled out each section.

Conclusion

In the next lesson, we will be writing the code to implement these methods. Taking the time to pseudocode ensures that when we get to writing the code itself, we'll have a clearer idea of how to approach the problem.

pfwtfp-pseudocode-implementing-dice-thrower-nyc-web-080519-mod-1-repeat's People

Contributors

maxwellbenton avatar sgharms avatar jenmyers avatar

Watchers

Kevin McAlear avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.