This site applies only to CMPT 165 (Distance Ed) in Summer 2016. See the CMPT 165 sections page for other sections.

Dice in a hand In this assignment, you will create an interactive web page using Python web scripts. The pages you create will implement a simple “dice” game.

The game itself is very simple:

  1. The user guesses the total of the dice that will be thrown.

  2. Five dice are thrown, and the total added up.

  3. If the total is at least the value the user guessed, they win.

This “game” will be played online. If you're not clear on how the game is played (or of any other details later on), have a look at an example working dice game.

Be sure to test your (partially completed) game regularly: at least after each of these steps.

Step 1: The Form

Create an XHTML form that asks the user for their name and their guess for the total of the dice. It should also have a submit button.

The “action” of this form should be the Python program you create in the next step.

Step 2: Program Basics

Create a Python web script that reads the input from the form in the previous step. Start by generating these fragments of the output (replacing the values the user entered and wrapping it in appropriate XHTML markup, of course):

Thanks for playing, Name.
You bet the total would be at least value.

You can add the code for the later steps into this skeleton.

Step 3: Rolling the Dice

Rolling dice is basically a way to generate random numbers from 1 to 6. We can simulate this with a computer program by just coming up with some randomly generated numbers. Before you can do this, you will have to import the Python module that contains random number functions:

import random

Then to generate a random number from a to b, use its randint function:

die = random.randint(a,b)

Use this function to generate the values of the five dice. Just print out the numbers for now—we will worry about the dice images later. Add up the values of the dice and produce this part of the output:

The total rolled was total.

(You can do the next two steps in either order.)

Step 4: Winning and Losing

You should now have both the value the user guessed and the total of the dice in separate variables. If the total of the dice is greater than or equal to the user's guess, they win. Use an if statement to output whichever of these statements is appropriate:

You won!
Sorry, you lost.

Step 5: Dice Images

If you are generating the random numbers and just printing them (as suggested above), you should at this point be able to tell that your game is working: random numbers from 1 to 6 are generated, and they are added up to get the total. But, it doesn't look like a dice game yet.

Fortunately, once we have the random numbers, displaying the corresponding images is fairly easy. Remember: we're generating XHTML in the program, and all you have to do to display an image in XHTML is use the <img> tag.

You can download and use the provided dice images.

If the random number generated is 2, you would want to create XHTML like this:

<img src="dice-2.gif" alt="2" width="107" height="107" />

So, for each die, you need to generate the random number (which you should already be doing), and then print a line like the above with the generated number substituted in the file name and alt text.

Validation

The XHTML code that your program generates should be valid XHTML. You can check this in one of two ways.

When your game is online and functioning, you can copy and paste the URL with the query string to the validator. In the working example, the URL would be something like this:

http://cmpt165.csil.sfu.ca/~ggbaker/demos/dice-total/dice.py?name=Your%20Name&value=20

The other option is to play the game, view the page source in your browser, and copy and paste this into the “enter HTML code directly” option in a validator.

Submitting

Upload all of your files to the course web server: XHTML and Python, with all images and stylesheets. Test the game on the server to make sure it's working.

When you're done, submit the URL of the front XHTML page (with the form) to the CourSys.

The dice images have been converted from SVG images distributed with Gnome Games. They can be redistributed under the terms of version 2 of the GNU General Public License (and are for this assignment). The dice-in-hand image is used with permission.