CPSC 231 Assignments 03 Hangman

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (8 votes)

Hangman is a classic two-player guessing game, played on pencil and paper that most of us probably
learned in elementary school. One player thinks of a “secret” word, and the other player’s goal is to try
to determine the secret word by guessing its letters one at a time.
Your main objective in this assignment is to write a computer program that will play a game of Hangman
with you. Your program will come up with the secret word and keep track of your guesses, and you, or
anyone playing your game on the computer, will be the one guessing the word.
The rules for our game of Hangman are as follows. The computer will first choose a random secret word
that is at least four letters long from a lexicon (A lexicon is like a dictionary, but without definitions for
the words) of the 4000 most frequently used English words. It shows the word first as blanks
(underscores or dashes), with one blank for each letter of the word. Then play proceeds as follows:
1. The player chooses a letter of the alphabet and indicates it to the computer as his or her guess.
a. If the secret word contains the letter, all occurrences of the letter in the word are
revealed.
b. Otherwise, the letter is written in the list of incorrect guesses
2. If all letters of the secret word are revealed, the game is won. Otherwise, play continues with
step 1.
Our Hangman game will allow the human player a total of eight guesses to determine the secret word.
The game is lost once all the guesses are finished and word is not guessed.
Problem 1: Word LookUp (4 marks)
Write a Python program that will read the provided lexicon file and perform a lookup for a userspecified word in the lexicon. The lexicon file, cpsc231-lexicon.txt, is a text file that contains the 4000
most frequent words used in contemporary American English with one word written per line in order of
their frequency of use. All words are written lowercase except for the word “I”.
Your program should first indicate at what rank position the query word appears in the list, or that the
query word is not contained in the lexicon. Then it should output, in alphabetical order and without
duplicates, a list of all the letters that occur in the query word.
Inputs: A lexicon file name specified as the first command line argument, and a query word as the
second command line argument. For example, you might run your program with the following
invocation:
$ python3 UCID-p1.py cpsc231-lexicon.txt color
Outputs: An indication of what frequency rank the query word appears in the lexicon, or that it was not
found in the provided file. Then print a list of all letters contained within the query word, in alphabetical
order. For example, your program’s output may look like those shown below:
Example Problem 1 output:
According to our lexicon, “color” is the 633rd most common word in contemporary American English.
It contains the following letters: c l o r
Another sample run:
According to our lexicon, “colour” is not in the 4000 most common words of contemporary American
English.
It contains the following letters: c l o r u
Problem 2: Console Hangman (10 marks)
Your goal is to create an interactive game of Hangman that can be played entirely through the console
window. Your program will play the role of “executioner”, selecting the secret word, keeping track of
letter guesses, and detecting when the game is won or lost.
Since this is a console version of the game, so it will suffice to keep track of the number of guesses
remaining before the game is lost. At minimum, your program must be able to perform the following
functions:
1. Select a random secret word that contains at least four letters from the provided lexicon.
2. Display the number of guesses remaining. The player starts the game with 8 guesses left.
3. Prompt the human player for successive letter guesses through the terminal until the game is
over.
a. If a guess is correct, reveal all occurrences of that letter in the appropriate positions in
the secret word, keeping the other letters hidden as underscore (_) or dash (-)
characters.
b. If the secret word does not contain the letter guessed, add the letter to the list of
incorrect guesses and deduct one from the guesses remaining.
c. If the letter has been guessed before, inform the player and ask for a new guess
4. Detect when the game is either won or lost, display a message accordingly, and reveal the secret
word at the end.
It’s completely up to you to decide how you want to organize your program, but we suggest you try
using top-down design and stepwise refinement to break it up into sub-problems. Identify the tasks that
need to be completed and write functions for them if you can.
Inputs: The name of the lexicon file specified as a command line argument. For example, you may run
your program as follows:
$ python3 UCID-p2.py cpsc231-lexicon.txt
Outputs: A guess-by-guess transcript of the game unfolding as you play Console Hangman with your
computer program.
Example Problem 3 output:
Welcome to CPSC 231 Console Hangman!
The secret word looks like: _ _ _ _ _ _ _
You have 8 guesses remaining.
What’s your next guess? e
Nice guess!
The secret word looks like: _ _ _ _ _ _ e
You have 8 guesses remaining.
What’s your next guess? a
Sorry, there is no “a”.
The secret word looks like: _ _ _ _ _ _ e
Your bad guesses so far: a
You have 7 guesses remaining.
What’s your next guess? o
Nice guess!
The secret word looks like: _ o _ _ _ _ e
Your bad guesses so far: a
You have 7 guesses remaining.
What’s your next guess? t
Sorry, there is no “t”.
The secret word looks like: _ o _ _ _ _ e
Your bad guesses so far: a t
You have 6 guesses remaining.
What’s your next guess? u
Nice guess!
The secret word looks like: _o_ _ u _ e
Your bad guesses so far: a t
You have 6 guesses remaining.
What’s your next guess? n
Nice guess!
The secret word looks like: _on_u_e
Your bad guesses so far: a t
You have 6 guesses remaining.
What’s your next guess? f
Nice guess!
The secret word looks like: _onfu_e
Your bad guesses so far: a t
You have 6 guesses remaining.
What’s your next guess? s
Nice guess!
The secret word looks like: _onfuse
Your bad guesses so far: a t
You have 6 guesses remaining.
What’s your next guess? c
Nice guess! Congratulations!
You guessed the secret word: confuse
Submission:
After you have completed this assignment, you should have written a total of two Python programs,
saved as .py files.
Please ensure that your files are named descriptively (e.g. UCID-P1.py), so that your TA can easily see
which program is associated with each problem.
Use the University of Calgary Desire2Learn system (http://d2l.ucalgary.ca) to submit your assignment
work online. Log in using your UofC eID and password, then find our course, cpsc 231 l01 and l02, in the
list. Then navigate to Assessments -> Dropbox Folders, and find the folder for Assignment #3 here.
Collaboration
Discussing the assignment requirements with others is a reasonable thing to do, and an excellent way to
learn. However, the work you hand-in must ultimately be your work. This is essential for you to benefit
from the learning experience, and for the instructors and TAs to grade you fairly. Handing in work that is
not your original work, but is represented as such, is plagiarism and academic misconduct. Penalties for
academic misconduct are outlined in the university calendar.
Here are some tips to avoid plagiarism in your programming assignments.
1. Cite all sources of code that you hand-in that are not your original work. You can put the citation
into comments in your program. For example, if you find and use code found on a web site, include
a comment that says, for example:
# the following code is from
https://www.quackit.com/python/tutorial/python_hello_world.cfm.
Use the complete URL so that the marker can check the source.
2. Citing sources avoids accusations of plagiarism and penalties for academic misconduct. However,
you may still get a low grade if you submit code that is not primarily developed by yourself.
3. Discuss and share ideas with other programmers as much as you like, but make sure that when you
write your code that it is your own. A good rule of thumb is to wait 20 minutes after talking with
somebody before writing your code. If you find yourself exchanging code by electronic means,
writing code while sitting and discussing with a fellow student, typing what you see on another
person’s console, then you can be sure that “your” code is not substantially your own, and your
sources must then be cited to avoid plagiarism.
4. Collaborative coding is strictly prohibited. Your assignment submission must be strictly your code.
Discussing anything beyond assignment requirements and ideas is a strictly forbidden form of
collaboration. This includes sharing code, discussing code itself, or modeling code after another
student’s algorithm. You cannot use (even with citation) another student’s code.
5. We will be looking for plagiarism in all code submissions, possibly using automated software
designed for the task. For example, see Measures of Software Similarity (MOSS –
https://theory.stanford.edu/~aiken/moss/).
Remember, if you are having trouble with an assignment, it is always better to go to your TA and/or
instructor to get help, than it is to plagiarize.
Credits:
The idea of the Hangman game as an assignment was shamelessly borrowed from the CS 106a course
taught at Stanford University. It was likely Eric Roberts who introduced this assignment there. The
lexicon of most frequently used words in contemporary American English was obtained from
https://www.wordfrequency.info.
The assignment description is taken from a course offered by Dr. Sonny Chen.
Grading:
There are total 14 marks for this assignment. If you have completed all the problems, you would get 14
marks.