CS 484 - Artificial Intelligence
Fall 2007
Loyola College >
Department of Computer Science >
CS 484 >
Labs & Projects >
Lab 0
Due
Thursday, September 13th at the beginning of class.
Lab will be penalized 20% for each class meeting after the due date.
THIS LAB WILL BE DONE INDIVIDUALLY.
Introduction
In this lab you will be writing a text based version of hangman. The purpose of this lab is introduce you to the language features of python, so I will include some design suggestions below if you would like to use them. For this lab, you will write a class Hangman which has class variables to keep track of the state of the game and a few methods to support playing the game. At the beginning of a game, a random word should be choosen from this dictionary. The user will begin with 6 lives, which are used when a user guesses a letter which is not part of the word. As the game progresses, letters in the word are filled in when the user guesses a letter that is in the word. The game ends when there are no more letters to guess or all of the lives have been used. What follows is a sample interaction with the game. The program you produce should be similiar to the following:
snoopy[~/CS484] python hangman.py
You have 6 lives remaining
The word is: ______
The following letters have already been guessed: []
Enter a letter: a
a is not in the word
You have 5 lives remaining
The word is: ______
The following letters have already been guessed: ['a']
Enter a letter: b
b is not in the word
You have 4 lives remaining
The word is: ______
The following letters have already been guessed: ['a', 'b']
Enter a letter: b
Letter has already been guessed.
Enter a new letter: c
c is not in the word
You have 3 lives remaining
The word is: ______
The following letters have already been guessed: ['a', 'b', 'c']
Enter a letter: d
d is not in the word
You have 2 lives remaining
The word is: ______
The following letters have already been guessed: ['a', 'b', 'c', 'd']
Enter a letter: e
e is not in the word
You have 1 life remaining
The word is: ______
The following letters have already been guessed: ['a', 'b', 'c', 'd', 'e']
Enter a letter: f
f is not in the word
Sorry you lose
Do you want to play again? (y/n) y
You have 6 lives remaining
The word is: ______
The following letters have already been guessed: []
Enter a letter: a
a is not in the word
You have 5 lives remaining
The word is: ______
The following letters have already been guessed: ['a']
Enter a letter: e
e is not in the word
You have 4 lives remaining
The word is: ______
The following letters have already been guessed: ['a', 'e']
Enter a letter: i
i is not in the word
You have 3 lives remaining
The word is: ______
The following letters have already been guessed: ['a', 'e', 'i']
Enter a letter: o
o is in the word
You have 3 lives remaining
The word is: ____o_
The following letters have already been guessed: ['a', 'e', 'i', 'o']
Enter a letter: t
t is in the word
You have 3 lives remaining
The word is: __t_o_
The following letters have already been guessed: ['a', 'e', 'i', 'o', 't']
Enter a letter: h
h is in the word
You have 3 lives remaining
The word is: __tho_
The following letters have already been guessed: ['a', 'e', 'i', 'o', 't', 'h']
Enter a letter: n
n is in the word
You have 3 lives remaining
The word is: __thon
The following letters have already been guessed: ['a', 'e', 'i', 'o', 't', 'h', 'n']
Enter a letter: p
p is in the word
You have 3 lives remaining
The word is: p_thon
The following letters have already been guessed: ['a', 'e', 'i', 'o', 't', 'h', 'n', 'p']
Enter a letter: y
y is in the word
You guessed it! The word is python
Do you want to play again? (y/n) n
snoopy[~/CS484]
Requirements
- Write a class called Hangman
- Constructor takes as an argument a string which is word that the user will try to guess
- Include class variables that keep track of the state of the game, including the number of lives that remain and the letters that have been guessed
- Methods that facilitate the game. You might consider a method that plays one round of the game, one that determines if the game is over, and one that outputs the results of the game
- Make sure that you make use of the list functionality in python. I don't want to see a Java or C++ program translated into python.
- Remember to identify a letter that has already been guessed and not penalize the user for guessing it again
- Write code that uses the Hangman class to play the game
- For each game choose a word randomly from the dictionary
- After the first game has been played, ask the user if they would like to continue, and do so until they want to quit.
- Remember that these lines of code will be written outside of any class or funtion. For simplicity put them in the same file as the Hangman class.
To run the program you will type "python <filename>"
What to turn in
- Email me the file containing the program
- Turn in a print out of the program