CSc 7003, Programming for Data Science (Summer 2021)

Program 4 - Hangman

Write a Python program to implement the "Hangman" game of guessing phrases. A sample session follows:

This program plays the game of hangman.
The computer will pick a random phrase.
After 5 wrong guesses you lose.

I am thinking of a MOVIE ...

WHOS AFRAID OF VIRGINIA WOOLF
The current phrase is **** ****** ** ******** *****
The letters you have not guessed yet are:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
Enter your next guess: w

You guessed W
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is W*** ****** ** ******** W****
The letters you have not guessed yet are:
A B C D E F G H I J K L M N O P Q R S T U V X Y Z 
Enter your next guess: o

You guessed O
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is W*O* ****** O* ******** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L M N P Q R S T U V X Y Z 
Enter your next guess: o

O is not a valid guess.
The letters you have not guessed yet are:
A B C D E F G H I J K L M N P Q R S T U V X Y Z 

Enter your next guess: 3

3 is not a valid guess.
The letters you have not guessed yet are:
A B C D E F G H I J K L M N P Q R S T U V X Y Z 

Enter your next guess: m

You guessed M
That is not present in the secret phrase.
You have made 1 wrong guess.

The current phrase is W*O* ****** O* ******** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L N P Q R S T U V X Y Z 
Enter your next guess: n

You guessed N
That is present in the secret phrase.
You have made 1 wrong guess.

The current phrase is W*O* ****** O* *****N** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L P Q R S T U V X Y Z 
Enter your next guess: q

You guessed Q
That is not present in the secret phrase.
You have made 2 wrong guesses.

The current phrase is W*O* ****** O* *****N** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L P R S T U V X Y Z 
Enter your next guess: z

You guessed Z
That is not present in the secret phrase.
You have made 3 wrong guesses.

The current phrase is W*O* ****** O* *****N** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L P R S T U V X Y 
Enter your next guess: v

You guessed V
That is present in the secret phrase.
You have made 3 wrong guesses.

The current phrase is W*O* ****** O* V****N** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L P R S T U X Y 
Enter your next guess: x

You guessed X
That is not present in the secret phrase.
You have made 4 wrong guesses.

The current phrase is W*O* ****** O* V****N** WOO**
The letters you have not guessed yet are:
A B C D E F G H I J K L P R S T U Y 
Enter your next guess: t

You guessed T
That is not present in the secret phrase.
You lose. The secret phrase was WHOS AFRAID OF VIRGINIA WOOLF

Do you want to play again? (y/n): y
I am thinking of a MOVIE ...

THE PRESTIGE
The current phrase is *** ********
The letters you have not guessed yet are:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
Enter your next guess: T

You guessed T
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is T** ****T***
The letters you have not guessed yet are:
A B C D E F G H I J K L M N O P Q R S U V W X Y Z 
Enter your next guess: H

You guessed H
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is TH* ****T***
The letters you have not guessed yet are:
A B C D E F G I J K L M N O P Q R S U V W X Y Z 
Enter your next guess: e

You guessed E
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is THE **E*T**E
The letters you have not guessed yet are:
A B C D F G I J K L M N O P Q R S U V W X Y Z 
Enter your next guess: p

You guessed P
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is THE P*E*T**E
The letters you have not guessed yet are:
A B C D F G I J K L M N O Q R S U V W X Y Z 
Enter your next guess: r

You guessed R
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is THE PRE*T**E
The letters you have not guessed yet are:
A B C D F G I J K L M N O Q S U V W X Y Z 
Enter your next guess: s

You guessed S
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is THE PREST**E
The letters you have not guessed yet are:
A B C D F G I J K L M N O Q U V W X Y Z 
Enter your next guess: i

You guessed I
That is present in the secret phrase.
You have made 0 wrong guesses.

The current phrase is THE PRESTI*E
The letters you have not guessed yet are:
A B C D F G J K L M N O Q U V W X Y Z 
Enter your next guess: g

You guessed G
That is present in the secret phrase.
The phrase is THE PRESTIGE
YOU WIN!!!!

Do you want to play again? (y/n): n
This program will use the following Python Class (PhraseBank.py) to initialize a phrase bank and retrieve the next phrase as well as retrieve the topic of the phrases.
import random

class PhraseBank:

  def __init__(self,fname):
    with open(fname) as f:
      p = f.readlines()
      ps = [x.strip().upper() for x in p]
      self._topic = ps[0]
      pss = ps[1:]
      random.shuffle(pss)
      self._phrases = pss
      self._current_index = 0

  def next_phrase(self):
    self._current_index = (self._current_index + 1)%len(self._phrases)
    return self._phrases[self._current_index]
 
  def topic(self):
    return self._topic
You should use a variable:
remaining_letters = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
which will keep track of the remaining letters of the alphabet that have not been used by the user.

The template for your Hangman.py program should be the following:

import sys
from PhraseBank import *

NUM_WRONG_LIMIT = 5

def main():
  intro()
  phrases = PhraseBank(sys.argv[1])
  while True:
    play_one_round(phrases)
    response = input("Do you want to play again? (y/n): ").upper().strip()
    if response != "Y":
      break

def intro():
  print("\nThis program plays the game of hangman.")
  print("The computer will pick a random phrase.")
  print("After "+str(NUM_WRONG_LIMIT)+" wrong guesses you lose.\n")

def play_one_round(phrases):
# implements the code for one round of the game
# keep playing while the player has not won and not lost the game.
# loss would be indicated by: number of wrong guesses is less than NUM_WRONG_LIMIT
# win would be indicated when all letters have been revealed.

def current_phrase(phrase, remaining_letters):
# This function takes as input the phrase the user is trying to guess and
# remaining_letters and returns the phrase with letters that appear in remaining_letters
# as * and leaving other letters as they are.

def test_if_win_loss(num_wrong, result, phrase):
# this function prints the message for WIN or LOSS or number of wrong guesses

def get_valid_guess(remaining_letters):
# this function repeatedly prints the input message asking the user for the next guess
# and returns a valid guess