Hangman

 A simple game involving two players. One person thinks of a word or words, and the other tries to guess the letters that make up that word or words. The game I made requires two human players, one to type in the word or words, and the second to guess the letters.

How I went about making the game is simple. Receive the words and separate them, letter by letter into an array, then create another array that's the exact same, but replaces all the letters with underscores, ignoring spaces. For example:

word_array = ['T', 'H', 'I', 'S', ' ', 'T', 'E', 'S', 'T']

masked_array = ['_', '_', '_', '_', ' ', '_', '_', '_', '_']

Then when the letters are being guessed, you take the letter and see if it is in the word_array, if it is, then find the indexes the letters are in, then fill in the masked_array with the right letters. Continue doing this until masked_array is identical to word_array.

There was one problem, which is the fact that I need to clear the screen regularly, so that I can (1) remove the typed in word from being visible, and (2) remove the previous guesses so that it doesn't become confusing. This just means I have to define the clear(), and that I can't run it through the PyCharm window, but rather the PyCharm terminal instead.

import os


def clear():
os.system('cls')


word = input("Enter your word/sentence to be guessed: ").upper()
clear()
word_array = []
for x in range(0, len(word)):
word_array.append(word[x])

masked_array = []
for x in word_array:
if x != " ":
masked_array.append("_")
else:
masked_array.append(" ")
print(masked_array)
guess_array = []
while masked_array != word_array:
guess = input("Enter your guess: ").upper()
if guess[0] in word_array:
for x in range(0, len(masked_array)):
if guess == word_array[x]:
masked_array[x] = word_array[x]
guess_array.append(guess)
clear()
print(masked_array)
print("Guessed letters: ", guess_array)
print("The word was correctly guessed.")

Comments