COMP 118 – Lab 4: President’s Word Game

Project Goal

The goal of this program is to create a game where the player guesses the more common word in President John F. Kennedy’s Inaugural address. The project includes the speech in the file “Kennedy1961.txt”. The program will show two words from the speech, and the user will guess which one is more common. Score is kept of their success or failure. Below are all (or almost all) inaugural addresses. You are to pick which one you wish to use.

In order to make the game more interesting, we want to rule out common words like “the”, “to”, “and”, etc. We call these “stopwords”, which means we ignore them. They are included in the file “StopWords.txt”. The program should ignore these words.

All the inaugural addresses

Learning Goals

  1. Reading from files
  2. Parsing lines into words
  3. Searching a list for a matching string
  4. Using a random number
  5. Using functions (including parameters and returning values)
  6. Checking for input errors

Example

Below is a sample game play:

In this game you choose the more popular word
in President Kennedy's 1961 presidential
inauguration speech.
The words are: 1. summons 2. go
Enter number of more common word, or 0 if the same. 2
summons occurs 1 times
go occurs 3 times
You got it right!
Score 1/1
Go again? ('y' or 'n'):y
The words are: 1. war 2. cost
Enter number of more common word, or 0 if the same. 1
war occurs 2 times
cost occurs 1 times
You got it right!
Score 2/2
Go again? ('y' or 'n'):y
The words are: 1. hard 2. loyalty
Enter number of more common word, or 0 if the same. 1
hard occurs 1 times
loyalty occurs 1 times
Better luck next time!
Score 2/3
Go again? ('y' or 'n'):n

Method

You are to write a program to play this game. This program will be broken down into several function, which will be defined for you. You are to write and test each funcition individually, and then assemble the functions in the final solution.

Steps

Please complete and submit each of the following codeboard.io projects. For 1-5 you are implementing and testing specific functions for the solution (in some cases using previous functions). You must complete these first, and submit them. Then assemble the functions into a complete solution in 6. Submit that as well.

  1. Write and test function loadStopList(): Link
    This reads a file of stopwords, and loads them into a list.
  2. Write and test function isStopWord(aWord,stopWords): Link
    This function is passed a word and a list of words. It returns true if the word is in the list of words.
  3. Write and test function loadWords(filename,stopList): Link
    This function is passed a filename and a stopword list.
    It opens the file and reads each the words, removing punctuation and converting it to lower case.
    If the word is not in the stop list (use the isStopWord function) add it to a list of words.
    Return the list of words.
  4. Write and test function countWord(wordList,aWord): Link
    Given a word aWord, count how many times the word is in the word list wordlist.
    Return the count
  5. Write and test function pickRandomWord(wordList): Link
    Use the random library to return a random word from wordlist.
  6. Complete the final program by assembling the other functions: Link

Turn in

  1. All links to all your completed projects (all 6). Submit all links to codeboard.io and Moodle.
  2. A text file with the output from playing the game with 5 turns

Grading

RequirementsGrading CommentsPointsScore
Fully functioning code that works exactly like game above (6)60
Completion of all functions in projects 1-520
Fully commented code for 6, with your name and date at the top.10
Text file with 5 runs10
Total100

Scroll to Top