Created
October 24, 2020 05:04
-
-
Save harshildarji/35ff02e7d34029748a5eea8052aa1cb4 to your computer and use it in GitHub Desktop.
Word Chain Game
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, json, requests, string, sys | |
def game(): | |
chain = 0 | |
usedWords = [] | |
answer_Word = None | |
chainGood = True | |
min_length = 3 | |
while chainGood: | |
# if chain != 0 and chain % 5 == 0: min_length += 1 | |
if answer_Word != None: | |
last_letter = answer_Word[-1:] | |
else: | |
last_letter = rand_letter | |
turn = playersNames[((chain+1)%numPlayers)-1] | |
print('\n> %s\'s turn' % turn) | |
rand_word = random.choice(wordTypes) | |
article = 'an' if rand_word == 'adjective' else 'a' | |
answer_Word = input('Enter {} {} (of length > {}) beginning with the letter "{}": '.format(article, rand_word, min_length, last_letter)).lower() | |
url = 'https://api.wordnik.com/v4/word.json/' + answer_Word + '/definitions?limit=200&includeRelated=false&useCanonical=false&includeTags=false&api_key=<--- Wordnik API Key --->' | |
response = json.loads(requests.get(url).text) | |
correct_type = response[0]['partOfSpeech'] if len(response) > 0 else '' | |
if answer_Word.isalpha() == True and answer_Word.startswith(last_letter) and answer_Word not in usedWords and len(answer_Word) >= min_length and rand_word in correct_type: | |
print ("Good job {}!".format(turn)) | |
usedWords.append(answer_Word) | |
chain += 1 | |
# print(' - Definition: ' + response[0]['text']) | |
print("The chain is now {} link long!".format(chain) if chain == 1 else "The chain is now {} links long!".format(chain)) | |
else : | |
print('\n' + '-' * 50 + '\nWord chain broken! Why?') | |
if answer_Word.isalpha() == False: print(' - Only alphabets were allowed!') | |
elif answer_Word.startswith(last_letter) == False: print(' - "{}" does not start with "{}"'.format(answer_Word, last_letter)) | |
elif answer_Word in usedWords: print(' - "{}" is already used'.format(answer_Word)) | |
elif len(answer_Word) < min_length: print(' - Minimum word length requirement: {} (Word length of "{}" is {})'.format(min_length, answer_Word, len(answer_Word))) | |
elif rand_word != correct_type: | |
if correct_type == '': print(' - "{}" is not a genuine word!'.format(answer_Word)) | |
else: | |
c_article = 'an' if correct_type == 'adjective' else 'a' | |
print(' - "{}" is {} {}, not {} {}'.format(answer_Word, c_article, correct_type, article, rand_word)) | |
chainGood = False | |
again = input(('\n' + '-' * 50 + '\nStart again? [y/n]: ')).strip() | |
if again.lower().startswith('y'): game() | |
else: | |
print('Thank you for playing WordChain, Goodbye!') | |
sys.exit() | |
wordTypes = ['noun', 'adjective', 'verb'] | |
playersNames = [] | |
print('-' * 50) | |
print(' ' * 10 + 'Welcome to WordChain!') | |
print('-' * 50) | |
numPlayers = 0 | |
while True: | |
try: | |
numPlayers = int(input('How many players (minimum of 2) ? : ')) | |
if numPlayers < 2: | |
print('value must be a positive integer > 2') | |
continue | |
break | |
except ValueError: | |
print(" value must be a positive integer equal or greater then 2") | |
count = 1 | |
while count <= numPlayers: | |
playersname = input('{} {} {}'.format('Enter name of player', count , ': ')) | |
if playersname.isalpha() == False: | |
print("enter valid characters from A-Z and a-z") | |
elif playersname in playersNames : | |
print("name already exists enter different name") | |
else: | |
playersNames.append(playersname) | |
count +=1 | |
rand_letter = random.choice("abcdefghijklmnopqrstuvwxyz") | |
game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment