Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bottom of Form Building a Simple Dice Game in Python: To learn more about how Python works and prepare for more advanced programming with graphics,

Bottom of Form

Building a Simple Dice Game in Python: To learn more about how Python works and prepare for more advanced programming with graphics, let's focus on game logic. In this tutorial, you'll also learn a bit about how computer programs are structured by making a text-based game in which the computer and the player roll a virtual die, and the one with the highest roll wins.

Planning your game: Before writing code, it's important to think about what you intend to write. Many programmers write simple documentation before they begin writing code, so they have a goal to program toward. Here's how the dice program might look if you shipped documentation along with the game:

Start the dice game and press Return or Enter to roll.

The results are printed out to your screen.

You are prompted to roll again or to quit.

It's a simple game, but the documentation tells you a lot about what you need to do. For example, it tells you that you need the following components to write this game:

Player: You need a human to play the game.

AI: The computer must roll a die, too, or else the player has no one to win or lose to.

Random number: A common six-sided die renders a random number between 1 and 6.

Operator: Simple math can compare one number to another to see which is higher.

A win or lose message.

A prompt to play again or quit.

Making dice game alpha: Few programs start with all of their features, so the first version will only implement the basics. Lets start with a couple of definitions:

A variable is a value that is subject to change, and they are used a lot in Python. Whenever you need your program to "remember" something, you use a variable. In fact, almost all the information that code works with is stored in variables. For example, in the math equation x + 5 = 20, the variable is x, because the letter x is a placeholder for a value.

An integer is a number; it can be positive or negative. For example, 1 and -1 are both integers. So are 14, 21, and even 10,947.

Variables in Python are easy to create and easy to work with. This initial version of the dice game uses two variables: player and ai.

Type the following code into a new text file called dice_alpha.py:

import random player = random.randint(1,6) ai = random.randint(1,6) if player > ai : print("You win") # notice indentation else: print("You lose")

Run your program. If you encounter an error, note the error encountered in your assignment, and correct the error. Tell me what you did to correct the error, run the game to ensure that it works, and continue with the assignment.

Make a screen shot of the game once it runs correctly, and include that in your submission for this assignment.

This basic version of your dice game works pretty well. It accomplishes the basic goals of the game, but it doesn't feel much like a game. The player never knows what they rolled or what the computer rolled, and the game ends even if the player would like to play again.

This is common in the first version of software (called an alpha version). Now that you are confident that you can accomplish the main part (rolling a die), it's time to add to the program.

Improving the game: In this second version (called a beta) of your game, a few improvements will make it feel more like a game.

1. Describe the results: Instead of just telling players whether they did or didn't win, it's more interesting if they know what they rolled. Try making these changes to your code:

player = random.randint(1,6) print("You rolled " + player) ai = random.randint(1,6) print("The computer rolled " + ai)

If you run the game now, it will crash because Python thinks you're trying to do math. It thinks you're trying to add the letters "You rolled" and whatever number is currently stored in the player variable.

You must tell Python to treat the numbers in the player and ai variables as if they were a word in a sentence (a string) rather than a number in a math equation (an integer).

Make these changes to your code:

player = random.randint(1,6) print("You rolled " + str(player) ) ai = random.randint(1,6) print("The computer rolled " + str(ai) )

Run your game now to see the result. Make a screen shot of the results at this point in your coding, and include the screen shot in your assignment.

2. Slow it down: Computers are fast. Humans sometimes can be fast, but in games, it's often better to build suspense. You can use Python's time function to slow your game down during the suspenseful parts.

import random import time player = random.randint(1,6) print("You rolled " + str(player) ) ai = random.randint(1,6) print("The computer rolls...." ) time.sleep(2) print("The computer has rolled a " + str(player) ) if player > ai : print("You win") # notice indentation else: print("You lose")

Launch your game to test your changes.

Once you have this version of your game working correctly, make a screen print of the results of the game being run, and include that in your submission for this assignment.

3. Detect ties: If you play your game enough, you'll discover that even though your game appears to be working correctly, it actually has a bug in it: It doesn't know what to do when the player and the computer roll the same number.

To check whether a value is equal to another value, Python uses ==. That's two equal signs, not just one. If you use only one, Python thinks you're trying to create a new variable, but you're actually trying to do math.

When you want to have more than just two options (i.e., win or lose), you can using Python's keyword elif, which means else if. This allows your code to check to see whether any one of some results are true, rather than just checking whether one thing is true.

Modify your code like this:

if player > ai : print("You win") # notice indentation elif player == ai: print("Tie game.") else: print("You lose")

Launch your game a few times to see if you can tie the computer's roll.

Once you have this version of your game working correctly, make a screen print of the results of the game being run, and include that in your submission for this assignment.

Programming the final release: The beta release of your dice game is functional and feels more like a game than the alpha. For the final release, create your first Python function.

A function is a collection of code that you can call upon as a distinct unit. Functions are important because most applications have a lot of code in them, but not all of that code has to run at once. Functions make it possible to start an application and control what happens and when.

Change your code to this:

import random import time def dice(): player = random.randint(1,6) print("You rolled " + str(player) ) ai = random.randint(1,6) print("The computer rolls...." ) time.sleep(2) print("The computer has rolled a " + str(player) ) if player > ai : print("You win") # notice indentation else: print("You lose") print("Quit? Y/N") cont = input() if cont == "Y" or cont == "y": exit() elif cont == "N" or cont == "n": pass else: print("I did not understand that. Playing again.")

This version of the game asks the player whether they want to quit the game after they play. If they respond with a Y or y, Python's exit function is called and the game quits.

More importantly, you've created your own function called dice. The dice function doesn't run right away. In fact, if you try your game at this stage, it won't crash, but it doesn't exactly run, either. To make the dice function actually do something, you have to call it in your code.

Add this loop to the bottom of your existing code. The first two lines are only for context and to emphasize what gets indented and what does not. Pay close attention to indentation.

else: print("I did not understand that. Playing again.") # main loop while True: print("Press return to roll your die.") roll = input() dice()

The while True code block runs first. Because True is always true by definition, this code block always runs until Python tells it to quit.

The while True code block is a loop. It first prompts the user to start the game, then it calls your dice function. That's how the game starts. When the dice function is over, your loop either runs again or it exits, depending on how the player answered the prompt.

Using a loop to run a program is the most common way to code an application. The loop ensures that the application stays open long enough for the computer user to use functions within the application.

Once you have this version of your game working correctly, make a screen print of the results of the game being run, and include that in your submission for this assignment.

Deliverables for this assignment are 5 screen prints, as noted above. You may work with any other student in the class, but each student must turn in their own assignment.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

ISBN: 0764532545, 978-0764532542

More Books

Students also viewed these Databases questions