Wednesday, November 6, 2019

Text-based Hangman in Python

import random
from idlecolors import *

L = ['education', 'residence', 'conveying', 'Suppose', 'shyness', 'behaved', 'morning', 'unsatiable', 'assistance', 'compliment', 'occasional',
     'reasonably', 'advantages', 'Unpleasing', 'acceptance', 'partiality', 'alteration', 'understood', 'Worth', 'tiled', 'house', 'added', 'Married',
     'hearing', 'totally', 'removal', 'Remove', 'suffer', 'wanted', 'lively', 'length', 'Moonlight', 'applauded', 'conveying', 'direction',
     'principle', 'expenses', 'distance', 'weddings', 'perceive', 'strongly', 'domestic', 'Effects', 'present', 'letters', 'inquiry', 'removed',
     'friends', 'Desire', 'behind', 'latter', 'though', 'Supposing', 'shameless', 'engrossed', 'additions', 'possible', 'peculiar', 'together',
     'Desire', 'better', 'cannot', 'before', 'points', 'Remember', 'mistaken', 'opinions', 'pleasure', 'debating', 'Court', 'front', 'maids',
     'forty', 'aware', 'their', 'Chicken', 'pressed', 'removed', 'Sudden', 'looked', 'elinor', 'estate', 'silent', 'extent', 'entire', 'Curiosity',
     'remaining', 'repulsive', 'household', 'advantage', 'additions', 'Supposing', 'exquisite', 'daughters', 'eagerness', 'repulsive', 'Praise',
     'turned', 'lovers', 'warmly', 'Little', 'eldest', 'former', 'decisively', 'impression', 'attachment', 'friendship', 'everything', 'Whose',
     'enjoy', 'chief', 'young', 'Felicity', 'required', 'likewise', 'doubtful', 'attention', 'necessary', 'provision', 'otherwise', 'existence',
     'direction', 'Unpleasing', 'announcing', 'unpleasant', 'themselves', 'advantage', 'listening', 'belonging', 'supposing', 'Invitation',
     'excellence', 'imprudence', 'understood', 'continuing', 'Fifteen', 'winding', 'related', 'hearted', 'colonel', 'studied', 'County', 'suffer',
     'twenty', 'marked', 'moment', 'Valley', 'silent', 'cannot', 'things', 'remain', 'merits', 'season', 'better', 'tended', 'hunted', 'sometimes',
     'behaviour', 'contented', 'listening', 'eagerness', 'objection', 'collected', 'Together', 'feelings', 'continue', 'juvenile', 'Unknown',
     'service', 'subject', 'letters', 'Child', 'noise', 'forty', 'entrance', 'disposal', 'bachelor', 'remember', 'relation', 'Unpacked', 'declared',
     'confined', 'daughter', 'improved', 'Celebrated', 'imprudence', 'interested', 'especially', 'reasonable', 'Wonder', 'family', 'secure',
     'Depend', 'repair', 'before', 'admire', 'observe', 'covered', 'delight', 'hastily', 'message', 'ladyship', 'endeavor', 'settling',
     'Marianne', 'husbands', 'stronger', 'Considered', 'middletons', 'uncommonly', 'Promotion', 'perfectly', 'consisted', 'chatty', 'dining',
     'effect', 'ladies', 'active', 'Equally', 'journey', 'wishing', 'several', 'behaved', 'chapter', 'Deficient', 'procuring', 'favourite',
     'extensive', 'diminution', 'impossible', 'understood', 'stuff', 'think', 'jokes', 'Going', 'known', 'noise', 'wrote', 'round', 'leave',
     'Warmly', 'branch', 'people', 'narrow', 'Winding', 'waiting', 'parlors', 'married', 'feeling', 'Marry', 'fruit', 'spite', 'jokes', 'times',
     'Whether', 'unknown', 'warrant', 'herself', 'winding', 'Parties', 'brother', 'amongst', 'fortune', 'Twenty', 'behind', 'wicket', 'itself',
     'Consulted', 'perpetual', 'pronounce', 'delivered', 'months', 'change', 'relied', 'beauty', 'wishes', 'matter', 'Ignorant', 'dwelling',
     'occasion', 'thoughts', 'overcame', 'consider', 'Polite', 'depend', 'talked', 'effect', 'worthy', 'Household', 'shameless',
     'incommode', 'objection', 'behaviour', 'Especially', 'possession', 'insensible', 'sympathize', 'boisterous', 'Songs', 'widen', 'event',
     'truth', 'Certain', 'brother', 'sending', 'amongst', 'covered']

for i in range(len(L)):
  word = L[i].upper()
  del L[i]
  L.insert(i, word)

num = random.randint(0, len(L)-1)

word = L[num]
print(word)
length = len(word)
Word = word
word = list(word)
length = len(word)
guessword = ['_'] * length
misses = 0
available = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
played = ''
guess = ''

printc(red("Red ") + orange("Orange ") + green("Green ") + blue("Blue"))

while True:
  found = 'F'

  lineA = "                          _"
  lineB = "______                     "
  lineC = "                         |/"
  lineD = "      |                    " if misses > 0 else ""
  lineE = "                         | "
  lineF = "     (_)                   " if misses > 1 else ""
  lineG = "                         | "
  lineH = "     \|/                   " if misses > 2 else ""
  lineI = "                         | "
  lineJ = "      |                    " if misses > 3 else ""
  lineK = "                         | "
  lineL = "     / \\                  " if misses > 4 else ""

  print(lineA, end='');  print(lineB)
  print(lineC, end='');  printc(red(lineD))
  print(lineE, end='');  printc(red(lineF))
  print(lineG, end='');  printc(red(lineH))
  print(lineI, end='');  printc(red(lineJ))
  print(lineK, end='');  printc(red(lineL))

  print("                         |")
  print("                         |")
  print(" _                       |                     ")
  print("| |                      |                     ")
  print("| |__   __ _ _ __   ___  |_ __ __   __ _ _ __  ")
  print("| '_ \ / _` | '_ \ / _`\\ / '_ ` _ \/ _` | '_ \ ")
  print("| | | | (_| | | | | (_| | | | | | | (_| | | | |")
  print("|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|")
  print("                    __/ |                      ")
  print("                   |___/                     \n")
  
  if misses == 5:
    print("You have lost your 5 lives.")
    print("The word was '" + Word + "'")
    break

  if guess in available and guess not in played:
    played += guess
  played = sorted(played)
  played = str(played)
  #print(played)

  for i in available:
    if i not in played:
      print(i, end=' ')
    else:
      print(' ', end=' ')
  print('\n')
  
  for i in range(length):
    print(guessword[i] + " ", end='')
  guess = input("\n\nYou have " + str(5 - misses) + " lives left.\tEnter a character: ")
  guess = guess[0].upper()

  ctr = word.count(guess)
  for i in range(length):
    if word[i] == guess:
      guessword[i] = guess
      found = 'T'

  if found == 'F':
    misses += 1
  
  if guessword == word:
    for i in range(length):
      print(guessword[i] + " ", end='')
    print("\nGotcha")
    break

No comments:

Post a Comment