CMSC104, Spring 2010

Programming Project 3

Polar Bears


Out: Wednesday, April 26, 2010
Due Date: Wednesday, May 12, 2010 before 11:59 p.m.

The Objective

This project is designed to give you practice working with functions and arrays in JavaScript.

The Task

This program will allow you to play the "Polar Bears Game". See the course schedule for a detailed explanation of the game. PowerPoint and PDF files are available containing a detailed decription of the game.

Polar Bears is a dice rolling game. To make this a nice presentation, we will visually roll the dice on screen. You will find six images of a die on the internet which represent the face values of the dice from 1 to 6 at the following web site:
http://www.csee.umbc.edu/courses/undergraduate/104/fall09/ordonez/projects/die-images/.
Copy these images and save them to your proj3 directory. The images should be stored in that location unless you want to use a relative or absolute path to access them.

To get you started, code for loading pictures onto your web page is listed below. This code will be discussed in class in detail next week. There are many components to be added to the code below, before Polar Bears can be played. Howver, the code below can serve as the starting point for your third and final project. Copy the code below and save it as: proj3.html and place it in your proj3 folder.



		/* variable declaration section */
		var numDice = 5; /* the number of dice is a constant number 5 */
		var maxSide = 6; /* the die has 6 sides, a constant number */
		
		var i; /* a simple counter */
		var randomNumber; /* a random number */
		
		var diceImages = new Array(maxSide); /* An array to hold the dice images */
		var diceRolls = new Array(numDice); /* An array to hold the dice rolls */
		
		/* main program */

		/* load the images into the dice array */
		for(i = 0; i < maxSide; i++)
		{
			/* Place the image in the array using HTML code */
			diceImages[i] = "<img src=\"Die-" + (i+1) + ".gif\" />";
		}
			
		/* roll the number of dice */
		for(i = 0; i < numDice; i++)
		{
			/* Pick a random number */
			randomNumber = Math.floor(Math.random() * maxSide);
			
			/* store the result in the dice rolls array */
			diceRolls[i] = randomNumber;
			
			/* and put display the corresponding image in the dice image array */
			document.write(diceImages[diceRolls[i]]);	
		}

More Details

You are REQUIRED to use the following functions exactly as they are given:

  1. function PrintMessage(message)
    This function should display a message to the user. This function should print the message using an alert(). The function should not return anything but has one parameter, which is the message that you would like displayed.
  2. function CountPolarBears(diceRolls, numDice)
    This function compute the number of polar bears for a given dice roll. There are two input parameters, the diceRolls array and the number of dice. It should return the total number of polar bears for the five dice.
  3. function CountFish(diceRolls, numDice)
    This function will compute the number of fish for a given dice roll. There are two input parameters, the diceRolls array and the number of dice. It should return the total number of fish for the five dice.
  4. function CountPlankton(diceRolls, numDice)
    This function will compute the number of plankton for a given dice roll. There are two input parameters, the diceRolls array and the number of dice. It should return the total number of plankton for the five dice.
  5. function GetValidInput()
    This function will return a valid number (a non-negative number). It has no input parameters. It returns a valid integer.

Submitting the Program

You do not have to do anything to submit the program. It should be in your pub/www/cs104/proj3 directory. There is no extra credit associated with this project. This project will count double towards your final grade. It replaces the original project 3 and 4 which were designed for this course.