Eric Roberts Handout #27 CSCI 121 October 3, Project #2 Snowman. This assignment is designed to build your skills in the following areas:

Similar documents
Assignment #4 Hangman Due: 3:15pm on Monday, November 3rd Your Early Assignment Help (YEAH) hours: 4:15pm-5:15pm, Friday, October 24th in Hewlett 200

Assignment 4 Hangman

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

Assignment #4 Hangman Due: 11am on Monday, May 14th This assignment may be done in pairs (which is optional, not required)

Assignment 2: Welcome to Java!

Assignment #5 The Enigma Machine

Assignment 3 Functions, Graphics, and Decomposition

Assignment #3 Breakout!

CMPSCI 187 / Spring 2015 Hangman

Assignment #6 Adventure

Practice Midterm Examination #1

Assignment #2: Simple Java Programs Due: 1:15pm on Friday, April 19th

Animations involving numbers

Assignment #3 Hangman Due: 11AM PST on Thursday, July 20th

YEAH Hours: Enigma. 11/6/18 Ryan Eberhardt

PowerPoint Basics: Create a Photo Slide Show

Assignment #1 Simple C++

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

Mehran Sahami Handout #39 CS 106A November 14, 2007 Assignment #6 NameSurfer Due: 3:15pm on Wednesday, November 28th

CS 106A, Lecture 27 Final Exam Review 1

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

CMSC 201 Spring 2018 Project 3 Minesweeper

1 Getting started with Processing

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

Assignment #6 NameSurfer

Practice Final Examination #2

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, Jan 29 th

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

Assignment A7 BREAKOUT CS1110 Spring 2011 Due Fri 6 May 1

Mehran Sahami Handout #7 CS 106A September 24, 2014

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

Math Dr. Miller - Constructing in Sketchpad (tm) - Due via by Friday, Mar. 18, 2016

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Excel Basics: Working with Spreadsheets

Basic Microsoft Word

Animations that make decisions

Creating a Presentation

Create Reflections with Images

Programming Project 1

CMSC 201 Spring 2018 Project 2 Battleship

Making a PowerPoint Accessible

In this lesson, you ll learn how to:

Word Tips (using Word but these work with Excel, PowerPoint, etc) Paul Davis Crosslake Communications

JS Lab 1: (Due Thurs, April 27)

CREATING CONTENT WITH MICROSOFT POWERPOINT

4. Use a loop to print the first 25 Fibonacci numbers. Do you need to store these values in a data structure such as an array or list?

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Interactive Powerpoint. Jessica Stenzel Hunter Singleton

Chapter One Modifying Your Fonts

CSE 143 Lecture 12 Inheritance

Text Input and Conditionals

Computational Complexity and Implications for Security DRAFT Notes on Infeasible Computation for MA/CS 109 Leo Reyzin with the help of Nick Benes

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

This chapter is intended to take you through the basic steps of using the Visual Basic

Tutor Handbook for WebCT

HOW TO. In this section, you will find. miscellaneous handouts that explain. HOW TO do various things.

SSEA Newbies Handout 09 Summer 2012 August 17 th, 2012 Assignment 4: Bouncing Ball and Bizz Bazz Buzz

CPSC 217 Assignment 3

Introduction to Programming

CS 134 Programming Exercise 9:

Your First Windows Form

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, April 23 rd

Beginning PowerPoint XP for Windows

INTRODUCTION TO COMPUTER CONCEPTS CSIT 100 LAB: MICROSOFT POWERPOINT (Part 2)

Figure 4.1: The evolution of a rooted tree.

-Using Excel- *The columns are marked by letters, the rows by numbers. For example, A1 designates row A, column 1.

BEGINNER PHP Table of Contents

Introduction. What is Max?

Click on any link on the left to jump to that page and learn more about the program.

Practice Midterm Exam #2

USING POWERPOINT IN THE CLASSROOM LESSON 1 POWERPOINT BASICS

B.Sc. VI SEM (CS+BIO)

Objects and Graphics

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

Lecture (06) Java Forms

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Download Free Pictures & Wallpaper from the Internet

InDesign Basics. Adobe

Use the Move tool to drag A around and see how the automatically constructed objects (like G or the perpendicular and parallel lines) are updated.

Spring CS Homework 3 p. 1. CS Homework 3

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Anjuli Kannan. Google Earth Driving Simulators (3:00-7:00)

Navigating and Managing Files and Folders in Windows XP

Drawing Hands, by M. C. Escher (lithograph, 1948)

Chapter 5: Character and Paragraph Formatting in MS Word

CS 106A, Lecture 27 Final Exam Review 1

Power Point Story Telling. Using Power Point to learn about using PowerPoint

CS 3 Midterm 1 Review

Accounts and Passwords

Keep Track of Your Passwords Easily

EXCEL + POWERPOINT. Analyzing, Visualizing, and Presenting Data-Rich Insights to Any Audience KNACK TRAINING

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Testing and Debugging

(Refer Slide Time: 00:02:00)

Assignment 0. Nothing here to hand in

Basic Concepts 1. For this workshop, select Template

Basic Concepts 1. Starting Powerpoint 2000 (Windows) For the Basics workshop, select Template. For this workshop, select Artsy

Transcription:

Eric Roberts Handout #27 CSCI 121 October 3, 2018 Due: Friday, October 11, 11:59 P.M. Project #2 Snowman For Project #2, your task is to write a Python program that plays the classic game of Hangman, with just a little updating to avoid seeming so macabre (and also to offer at least a bit of homage to Mary Shelley 200 years after the publication of Frankenstein). This version is called Snowman and draws its inspiration from the following cartoon from Calvin and Hobbes: The Attack of the Deranged Mutant Killer Monster Snow Goons: Knowing that Calvin is about to create the first in an army of snow goons that will threaten the entire world, your mission is to guess the magic word that will disable the creatures before Calvin has finished creating the first one. This assignment is designed to build your skills in the following areas: 1. String manipulation. The game requires you to update a string with every occurrence of a letter in a secret word. 2. New graphical objects. The game requires you to use the GPolygon and GCompound classes, so that you have some practice with those. 3. Modular development. The Snowman game is divided into four independent Python modules, two of which you have to write. Large projects are always subdivided into modules, and it is important to understand what information each module keeps private and what facilities it exports for use by other modules. Playing the Snowman game At the beginning of a game of Snowman, the computer chooses a secret word from a list of hard-to-guess English words stored in the PythonWords.py module, which is given to you. It then displays the word with every letter replaced by a hyphen. For example, if the secret word is FRUSTRATE, the computer would display a row of nine hyphens, like this: ---------

2 The computer then asks the user to choose a letter by clicking the mouse on one of the 26 letters displayed on the graphics window. If the user guesses a letter that is in the word, the word is redisplayed with all instances of that letter shown in the correct positions, along with any letters correctly guessed on previous turns. For example, if the user begins by guessing the letter E, the computer will update the word to show that the secret word has an E in the final character position, as follows: --------E If the user were then lucky enough to guess R, the computer would fill in both copies of that letter and update the display like this: -R---R--E If the letter does not appear in the word, the user is charged with an incorrect guess. The user keeps picking letters until either (1) the user has correctly guessed all the letters in the word or (2) the user has made eight incorrect guesses. To remind you of what s at stake, each of your incorrect guesses are recorded by drawing a new piece of the growing snowman in the following order: 1. The snowball (i.e., a GOval) that represents the base of the snowman 2. The slightly smaller snowball (GOval) for the body 3. The still smaller snowball (GOval) for the head 4. The twig (GLine) for the left arm 5. The symmetrically defined twig (GLine) for the right arm 6. The charcoal briquette (filled GOval) for the left eye 7. An identical charcoal briquette (filled GOval) for the right eye 8. A carrot (GPolygon) for the nose The finished snowman looks like this:

3 Snowman is played entirely on the graphics window using mouse events. The bottom of the graphics window shows 26 letters one for each letter in the alphabet each of which is implemented as a GLabel. These letters act as buttons. To guess a letter, the user clicks on one of these 26 buttons. If the guess is correct, Snowman updates the display of the secret word, which is located just above the selectable letters. If the guess is incorrect, Snowman adds the next body part to the top of the window. The program also changes the color of the letter to record the guess. Incorrect guesses are shown in red (specified by the constant INCORRECT_COLOR) and correct guesses are shown in green (specified by the constant CORRECT_COLOR). Figure 1 shows the graphics window after the user has correctly guessed the E and R in FRUSTRATE but has also incorrectly guessed I and N. As in any well-structured program, the location and sizes of the elements appearing in the window are specified as constants, which are shown in the SnowmanConstants.py file in Figure 2 at the top of the next page. Module structure for the Snowman game The Snowman game is implemented using four different Python modules: SnowmanGame.py. This module implements the game and is supplied to you with a stub implementation of the SnowmanGame function that creates the graphics window but does nothing else. Your job is to complete the implementation. SnowmanGraphics.py: This module is responsible for drawing the various pieces of the snowman figure on the graphics window. The version in the starter project

4 contains two functions createemptysnowman and addsnowmanpart along with the docstring comments that describe what those functions are supposed to do. You just need to implement them. SnowmanConstants.py. This module defines all the constants used in the Snowman game. These constants are needed in both the SnowmanGame and SnowmanGraphics modules, so it makes sense to put the constants in a separate module and import them into the namespaces for each of the modules you have to write. SnowmanWords.py. This module exports a constant called SNOWMAN_WORDS, which is a list of all the words that the computer can choose. The list does not include most English words but rather a collection of words that are hard to guess. As with the Breakout program, you should design, implement, and test your program in several parts, each of which represents an achievable milestone. The rest of this handout describes these milestones in more detail. Figure 2. The SnowmanConstants.py starter file

5 Milestone 1: Choose a random secret word and display it in its hidden form When I ve used Hangman as an exercise in past offerings of an introductory course, reading in the list of possible words from a data file and choosing a random one has been part of the assignment. Since I won t get to files in CSCI 121 until the week after the midterm, the easiest thing to do is to put all the possible words in a single list. That list of words is called SNOWMAN_WORDS, and it appears in the SnowmanWords.py module. You don t have to do anything to this file; all you have to do is import it. To complete this milestone, you need to do the following things: Figure out how to import the SnowmanWords variable into your SnowmanGame.py implementation. Choose a random element from the list to be the secret word. Create the mystery version of the word by assembling a string of hyphens exactly as long as the secret word. Create a GLabel that contains the mystery word and display it on the window. The word should be centered horizontally in the window at the baseline specified in the constants. Milestone 2: Display the letters at the bottom of the window Your next milestone is to display the 26 letters at the bottom of the window. Each one of these letters is stored in its own GLabel object. The baseline for these letters is given by the constant LETTER_BASE, and each letter should be displayed in a monospaced bold font given by the constant LETTER_FONT. What you have to do is figure out how to arrange these labels so that they show the 26 uppercase letters and are spaced uniformly across the bottom of the window as shown in Figure 2. Milestone 3: Detect mouse clicks on the letters The fact that each letter is a separate GLabel makes it possible to use the getelementat method to determine which letter the user has selected, in much the same way that you recognized collisions in the Breakout program. You need to add the code to detect mouse clicks and define a listener function that detects when the mouse is clicked on one of the letters. You first need to check whether the click is in the bottom portion of the window to ensure that none of the other GObject instances responds to the user action. As long as the click is in that region, any object returned by getelementat must be one of the 26 GLabel objects containing a letter. Once you have determined which GLabel was clicked, you can get the letter by calling the getlabel method, which returns the string the GLabel displays. At the moment, you don t have anything particularly useful to do with that information, but that fact shouldn t stop you from testing this milestone and making sure you have it working. You can, for example, call print to display the letter on the console. And since all letters are in some sense incorrect at this point, you might also change the color of the GLabel to the constant INCORRECT_COLOR. You can go back and modify the code for the clickaction listener when you have built the rest of the game.

6 Milestone 4: Implement the code that updates correctly guessed letters Your next task is to go back to the function that responds to mouse clicks and add whatever code you need to update the mystery word as the user guesses letters that appear in the word. To do so, it is useful to write a helper function that goes through the secret word and updates the corresponding position in the mystery word for every letter position in which the guess appears. If any such matches occur, your function that responds to the click action should change the color of the label to the shade of green represented by the constant CORRECT_COLOR. If no matches occur, the label should be set to the reddish color stored in INCORRECT_COLOR. Milestone 5: Draw successive body parts of the snowman for each incorrect letter In addition to changing the color of the letter to red, each incorrect guess has to display the next body part in the snowman diagram at the top of the window. You will need to maintain a variable that keeps track of the number of incorrect guesses and then write the code necessary to add the GObject necessary to display the next piece of the snowman s body, as shown in Figure 1. Effective decomposition is the key to success here. One of the requirements of the assignment is to make sure that the entire snowman figure is a GCompound object that has all of the various pieces in the right places. The function createemptysnowman function in SnowmanGraphics.py should create the GCompound, add it to the window, and set its reference point to the location at the bottom of the body circle. Your code for addsnowmanpart must then add each part at the appropriate position relative to the reference point. The implementations of addsnowmanpart and its helper functions consist almost entirely of code to ensure that everything is positioned correctly with respect to the reference point. The only minor complexity is in drawing the arms, where you will need to compute the starting and ending points of the line knowing that these lines run at 45 off the vertical. If you remember trigonometry, you can use the math.sin and math.cos functions here. If not, you can figure everything out using the Pythagorean Theorem. You might also notice that you can use the same helper function to draw each arm if you pass in an argument to show the direction. The same is true for the eyes. The reason for making the snowman a single GCompound is that doing so allows you to move the snowman as a unit. If you want to extend the program so that the completed snowman starts chasing a suitable proxy of Calvin across the screen, the can easily do so. All you have to do is animate the motion of the GCompound. Milestone 6: Determine when the game is over and display appropriate messages The final step in the process consists of determining when the game is over. The user wins when all letters have been guessed correctly, at which point you need to display the message You win! centered horizontally MESSAGE_BASE pixels above the bottom of the window. The user loses when the number of incorrect guesses reaches 8 (this value is not defined as a constant because it is built into the operation of SnowmanGraphics.py). At that point, your program should reveal the rest of the mystery word and display You lose! in the message area.

7 Once the game is finished, clicking in the window should restart the game by clearing the graphics window, reinitializing all the variables, choosing a new secret word, and letting the user play again. Extensions There are many things you could do with Snowman to make it more sophisticated. Once you get the basic structure working, you could try some of the following ideas: Check to make sure that the user has not already guessed the letter and display some message to that effect in the message area of the window. You will need to remove that message when the user enters a new guess. Spice up the display a little. Each of the body parts in the assignment is a single GObject, but you could add more detail. In particular, the arms in the assignment are single twigs. If you use the ideas from the brief introduction to Lindenmayer patterns that were part of the lecture on September 28 (see Handout #21), you could make them branching twigs, as they are in the Calvin and Hobbes cartoon. Animate the graphical display. Instead of having the body parts and letters merely appear on the screen, you could have them move in from offscreen, as they often do, for example, in PowerPoint slides. Similarly, you could animate the finished snow goon so that it moves menacingly around the screen. Change the story line to make it more socially relevant. A popular bumper sticker at the time of the Citizens United court decision reads I ll believe that corporations are people when Texas executes one. You could go back to a more traditional Hangman style of play using corporate icons instead of body parts. Expand the program to play something like Wheel of Fortune, in which the single word is replaced by a common phrase and in which you have to buy vowels. Use your imagination!