12
MOM! Phineas and Ferb are … Aims: Use random numbers to simulate dice games. Objectives: All: Try out the programming techniques and make a simple dice game. Most: Make a simple two-player dice game. Some: Complete all tasks, including “Guess the Number.”

MOM! Phineas and Ferb are … Aims: Use random numbers to simulate dice games. Objectives: All:Try out the programming techniques and make a simple dice

Embed Size (px)

Citation preview

MOM!Phineas and Ferb are …

Aims:Use random numbers to simulate dice games.

Objectives:

All: Try out the programming techniques and make a simple dice game.

Most: Make a simple two-player dice game.

Some: Complete all tasks, including “Guess the Number.”

Computers cannot generate truly random numbers.

If you want some truly random numbers, visit: http://www.random.org/The numbers there are derived from atmospheric noise.

The ones supplied by Python are created by a mathematical process. They are properly known as “pseudo-random” numbers. They are good enough for most programming uses.

This import statement means: “get me all the modules from random”.Once we have imported them, we can use them.

The randint() function is great for returning a random number between two numbers (including both endpoints).

randint(1,6) simulates a die.

It's quite easy to write a function that will return a number from 1 to 6.

This function simulates the toss of a coin.

TaskWrite a program that simulates 1000 rolls of a dice.Count how many times each possible throw occurs.Print the results.

Lists

Did you use a list to complete the last task?It makes the code a lot simpler. Let's look at an example.

This is just our standard template, adapted for the die test idea.

Indexing

If we want to find the contents of a particular cell in a list, we use indexing. In Python the indices start at zero.

Index 0 1 2 3

Value apple pear plum orange

To get the item at a certain index, just type the list's name and the number in square brackets.

Once you are used to the idea that indices start at zero, you'll have no problems with this.

First we make a list with the same number of elements as there are sides on the die we are testing. Each element in the list is intialised to zero.

They will be used to count how many times each number gets rolled.

Then we use a for loop. Each time we use the random number that's generated to choose one of the cells in our list. Then that element gets incremented.

At the end of the loop, the list will hold the results of the test.

This function takes the list of results and turns it into a string.This makes the results look a bit nicer.

As an example, here's the output of a million throws of a six-sided die.

Task

Use what you have learned about the random module to make a simple game.

It is a two-player game. To begin with, both players should enter their names. The winner of the game is the first to roll a double six.

At each round, the program should display the two players' dice scores.If a player wins, the game should announce the winner and exit.If it is a draw, the game should give the option to continue or exit.

The output should therefore look something like this.

We are now going to implement a slightly more complex dice game now, it is called “Drop Dead”.

The game is simple. You start with five dice. You roll them all together.If any come up as “2” or “5”, you score zero for the round and lose those dice.

If you do not get any “2”s or “5”s, then you add the sum of all your dice onto your total.

When all your dice have “dropped dead”, it's game over.

Here's a sample run.

List Comprehensions

We have looked at these before, briefly. We are going to use one in “Drop Dead”.List comprehensions are ways of making a list without using a “for” loop.

Task: Use a list comprehension to create a list of odd numbers from 1 to 50.

Use count() to count how many “2”s and “5”s there are in the list of dice throws.

List comprehension.

Tasks

1: Make “Drop Dead” into a two-player game.Like we did with the double-six game, ask for player names.Display the status at the end of each round.When both players have “dropped dead”, announce the winner.

2:Make a guess the number game. The program should pick a random number (from 1-100).The user should get several chances to guess it.The program should tell the user to guess “higher” or “lower” if he or she does not get the number right.