Created
August 10, 2015 12:59
-
-
Save anabarasan/ef0dff953ab7807430f9 to your computer and use it in GitHub Desktop.
hangman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
CATEGORY_LIST = ["Animals", "Countries", "Fruits", "Vegetables"] | |
CATEGORIES = { | |
"Animals": ["alligator", "bear", "camel", "cat", "cheetah", "chimpanzee", "cow", "crocodile", "deer", "dog", | |
"dolphin", "duck", "elephant", "fish", "fox", "frog", "giraffe", "goat", "hamster", "hippopotamus", | |
"horse", "kangaroo", "lion", "monkey", "octopus", "panda", "pig", "rabbit", "seal", "sheep", "snake", | |
"spider", "squirrel", "tiger", "wolf", "zebra"], | |
"Countries": ["Afghanistan", "Albania", "Algeria", "Argentina", "Armenia", "Aruba", "Australia", "Austria", | |
"Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Bermuda", "Bhutan", | |
"Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "British Virgin Islands", "Brunei", | |
"Bulgaria", "Burkina Faso", "Burma", "Burundi", "Cambodia", "Cameroon", "Canada", "Cayman Islands", | |
"Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos Islands", "Colombia", | |
"Comoros", "Cook Islands", "Costa Rica", "Cote d Ivoire", "Croatia", "Cuba", "Curacao", "Cyprus", | |
"Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", | |
"Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", | |
"Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Federated States of Micronesia", "Fiji", | |
"Finland", "France", "French Guiana", "French Polynesia", "Gabon", "Georgia", "Germany", "Ghana", | |
"Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", | |
"Guinea Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", | |
"Indonesia", "Iran", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", | |
"Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", | |
"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", | |
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", | |
"Mauritania", "Mauritius", "Mayotte", "Mexico", "Moldova", "Monaco", "Mongolia", "Montenegro", | |
"Montserrat", "Morocco", "Mozambique", "Nagorno Karabakh", "Namibia", "Nauru", "Nepal", "Netherlands", | |
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", | |
"North Korea", "Northern Cyprus", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", | |
"Palestinian National Authority", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", | |
"Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Republic of the Congo", "Reunion", | |
"Romania", "Russia", "Rwanda", "Sahrawi Arab Democratic Republic", "Saint Barthelemy", | |
"Saint Helena Ascension and Tristan da Cunha", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin", | |
"Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", | |
"Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Sint Maarten", "Slovakia", "Slovenia", | |
"Solomon Islands", "Somalia", "Somaliland", "South Africa", "South Korea", "South Ossetia", | |
"South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden", | |
"Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", "The Gambia", | |
"Togo", "Tokelau", "Tonga", "Transnistria", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", | |
"Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", | |
"United States", "United States Virgin Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", | |
"Venezuela", "Vietnam", "Wallis and Futuna", "Yemen", "Zambia", "Zimbabwe"], | |
"Fruits": ["apple", "banana", "cherry", "grapefruit", "grapes", "lemon", "lime", "melon", "orange", "peach", "pear", | |
"pineapple", "plum", "strawberry", "watermelon"], | |
"Vegetables": ["asparagus", "beans", "broccoli", "cabbage", "carrot", "celery", "corn", "cucumber", "eggplant", | |
"green pepper", "lettuce", "onion", "peas", "potato", "pumpkin", "radish", "spinach", "sweet potato", | |
"tomato", "turnip"] | |
} | |
def get_category(): | |
return CATEGORY_LIST[random.randrange(0, len(CATEGORY_LIST))] | |
def get_word(category): | |
word_list = CATEGORIES[category] | |
return word_list[random.randrange(0, len(word_list))] | |
DISPLAY = [ | |
r''' | |
+---+ | |
| | | |
| | |
| | |
| | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''', | |
r''' | |
+---+ | |
| | | |
O | | |
| | |
| | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''', | |
r''' | |
+---+ | |
| | | |
O | | |
| | | |
| | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''', | |
r''' | |
+---+ | |
| | | |
O | | |
/| | | |
| | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''', | |
r''' | |
+---+ | |
| | | |
O | | |
/|\ | | |
| | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''', | |
r''' | |
+---+ | |
| | | |
O | | |
/|\ | | |
/ | | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''', | |
r''' | |
+---+ | |
| | | |
O | | |
/|\ | | |
/ \ | | |
| | |
============== | |
%s | |
%s | |
%s | |
%s | |
''' | |
] | |
class Hangman(object): | |
alphabets = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') | |
def __init__(self): | |
self.spaces = 0 | |
self.wrong_guesses = 0 | |
self.correct_guesses = 0 | |
self.word_category = get_category() | |
self.word_to_guess = get_word(self.word_category) | |
self.word_display = [] | |
self.game_over = False | |
self.result = "" | |
self.characters_guessed = [] | |
self.__generate_blanks__() | |
def __generate_blanks__(self): | |
for character in self.word_to_guess: | |
if character == ' ': | |
self.word_display.append(' ') | |
self.spaces += 1 | |
else: | |
self.word_display.append(r'-') | |
def __check_character__(self, guess): | |
wrong_guess = True | |
for index, character in enumerate(self.word_to_guess): | |
if character.lower() == guess.lower(): | |
wrong_guess = False | |
self.word_display[index] = character.upper() | |
self.correct_guesses += 1 | |
self.game_over = (self.correct_guesses + self.spaces == len(self.word_to_guess)) | |
if self.game_over: | |
self.result = 'You Win' | |
return wrong_guess | |
def update(self, guess): | |
if guess.upper() in self.alphabets and guess.upper() not in self.characters_guessed: | |
self.characters_guessed.append(guess.upper()) | |
if not self.game_over and self.__check_character__(guess.lower()): | |
self.wrong_guesses += 1 | |
self.game_over = (self.wrong_guesses == len(DISPLAY) - 1) | |
if self.game_over: | |
self.result = 'You Lose' | |
if self.game_over: | |
self.word_display = r'' | |
for character in self.word_to_guess: | |
if character == ' ': | |
self.word_display += r' ' | |
else: | |
self.word_display += character.upper() | |
def draw(self): | |
print(chr(27) + "[2J") # clear screen | |
print DISPLAY[self.wrong_guesses] % (self.word_category, r''.join(self.word_display), | |
', '.join(self.characters_guessed), self.result), | |
if __name__ == '__main__': | |
play_again = 'y' | |
while play_again.lower() != 'n': | |
hangman = Hangman() | |
while not hangman.game_over: | |
hangman.draw() | |
entry = raw_input() | |
hangman.update(entry) | |
hangman.draw() | |
play_again = raw_input("Try Again? (Y / n) ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment