Lesson # 10 - Final Project

# 10 Final Project

# 10 Review Questions Back to Main Menu

A Dice Game

Here is where we get to put it all together to make something fun - a game of chance!!

On the counter are six squares like this:

1
2
3
4
5
6

You start with a pocketfull of dollars, and you bet by placing a dollar (or more) on one of the squares.

Then ... three dice are rolled. If your number comes up on one die, you get your money back plus the amount of your bet.

If your number comes up on two dice, you get your bet back plus double that amount.

If your number comes up on all three dice, you get your bet back plus triple that amount. Otherwise you lose your bet. I first saw this game described on http://thinks.com/puzzles/loyd/puzzle1a.htm. It is rather fun to think about whether you stand to generally win or lose on this game, but it is also fun to plan it out and create a working game.

Design from the Top Down

So what are the BIG pieces to this game? Get them solidly in mind first, then the details will come later.

Let's think in general terms about

  1. Start the game
  2. Play for a while
  3. End game

So lets look at how this works, in smaller details.

Create the Details

Start the Game

Before going on, try to imagine what the playing screen should look like. You can click here to see a partial version, and one person's interpretation of what the game might look like. Some of the graphic images you might want to use are there, and you can re-use those images, as well as the ones on this page.

 

We need a text box for the player to enter their bet, and another box where they can view how much money they have at each roll of the dice. Then a place where the dice get displayed, and a prompt that will ask they want to quit playing after each roll. Click here to see an example, but you can probably improve on this screen.

 

First we need to decide how much money is in the player's pocket when they start. Any money in the pocket can be bet, and lost, so they have to start with SOME money. You can decide that everyone starts with $20, or you could create a random number generator that calculates a formula for a random amount somewhere between $20 and $80 bucks. Then the player can click a button to show the start screen and begin this awesome game.

Play a While

Here is where most of the detail and complexity is, as you have probably figured out. The player can choose to play once, or many times as long as they have money. They will always play at least once, or they would never have gotten this far. So we need a loop that checks for 'money > 0' and that the player wantsa to continue. For this we use a compound condition with the 'and' ( && ) operator. Then the player must bet, or make two choices. First, how much, and second, what number 1 - 6? For the part about how much, they can never bet more than they have, and can never bet 0 or negative money. An alert box should pop up if they try to be so cute with their betting. Then rolling three dice means we calculate three random whole numbers from one to six, and to make the game fun, we display the three dice as images. The gif images are provided for you at the top of this page. Then we check the dice to see if 0, 1, 2, or 3 or their number came up. This would be easier if the dice values were stored in an array of three whole numbers. Based on that count, we either add to or subtract from the player's money, and see if they get to play again.

Quit Playing

Here is just a little bye-bye screen where we check to see if the player ended with more money or less money than they started with, and either congratulate the winners or console the loosers. Invite them back to your site to play again.

 

Here are the details listed in pseudocode form:

  1. Start the Game
    1. Open the game window
    2. Set player's money - always at least $20 to start
    3. Store the starting amount in a var named 'startAmount' for later use
    4. Player clicks button to Start Game or Exit
  2. Play a While
    1. Player bets.
    2. Player clicks a button to roll three dice.
    3. Check array of numbers using 'for' loop for numbers that match player's bet.
      1. If 0 matches, subtract bet from money.
      2. If 1 match, add bet to money.
      3. If 2 matches, add 2 x bet to money.
      4. If 3 matches, add 3 x bet to money.
    4. Show player how much money he has now.
    5. If money = 0, alert player that game is now over and exit loop.
    6. (Otherwise) ask if player wants to quit or Play again.
  3. End Game
    1. If amount > startAmount then congratulate player.
    2. If amount <= startAmount then console the loser.
    3. Invite player to come back again.
    4. End game (close window).

 

Test Your Game

This is actually the part that is both fun and frustrating. Be sure you only worry about testing one small part at a time.

For example, the first thing that needs to work is just opening the game screen window. Just test that part at first. Then try to set the player's money equal to 20. When that works, move on.

Return to Top of Page | Back to Main Menu