A Game of Anagrams in Python

Project idea:

The aim of this project is to create a game in python in which the user is presented with an anagram of a word and has to guess the right word within a limited number of attempts.

Features of Project:

  1. The user is given a fixed number of attempts to guess the correct word. The number of attempts is dependent on the length of the word.
  2. After each incorrect attempt the user is provided with a hint of the correct word.
  3. If the user is not able to guess the right word within the fixed number of attempt the correct word is displayed and the game moves on to the next word.
  4. Ctrl+C or Ctrl+D exits the game.

Implementation:

This tutorial is valid only for Linux based systems. This tutorial is written on a Linux Mint 17.1 system. For implementing on other Linux systems (Redhat,Arch) see special note at the end of this tutorial.



In almost all the Linux based systems there is a file located at directory location “/usr/share/dict/” under different names like “cracklib-small”(Ubuntu based systems),”words”(Redhat,Arch) which contains words from dictionary and are often used by many applications to implement features such as “spell-check”.

In this project, I will be using the same file to create a game of anagrams.

Reading the file can provide us with all the words required for the game. The words in the file are separated with a new-line so while reading the file we need to split the words based on the new-line character to get individual words. The code for the same would look like:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

loc='/usr/share/dict/cracklib-small'
with open(loc) as f:
content=f.read().split('\n')
f.close()

chevron_right


The file also contains words like “zoo’s” but we do not want such words in our game so we can omit them. To avoid making the game too simple I decided to also ommit words of length less than 5 but this step is optional and can be skipped. The code for the same looks like:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

l=len(content)
words=[]
for i in range(0,l):
    if '\'' in content[i] or len(content[i])<5:
    continue
words.append(content[i])

chevron_right


The file also contains words like “2nd,3rd” at the start of the file. To prevent them from appearing in our game we omit them by:

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

words=words[1:]
d=len(words)
words=words[:d]

chevron_right


Finally “words” contain all the words we need for the game to proceed.

We can choose a specific word for a particular round of the game by:

word=words[random.randint(0,d)]

The word would be randomly chosen from the list of words created.

To create the anagram of the word we need to shuffle the characters.This can be done by:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

shuffle=list(word)
random.shuffle(shuffle)

chevron_right


If the length of the word chosen for the round is more than 7 the user gets 7 attempts else the number of attempts is 5.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

if len(word)>7:
    chances=7
else:
    chances=5
    tries=0

chevron_right


The variable “tries” keeps track of the number of attempts taken by the user. We initialize it to zero.
During each user attempt, we take their input and compare it with the correct word. If they match we congratulate the user and present the next anagram else we provide them with a hint.

To generate hint for a particular word we choose two random integers between 0 and the length of the word.

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

t1=random.randint(0,len(word))
t2=random.randint(0,len(word))

chevron_right


We display the correct characters at these two positions t1 and t2 and at all the other positions we display “.”.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

hint=""
for i in range(0,len(word)):
    if i==t1 or i==t2:
        hint=hint+word[i]
    else:
        hint=hint+"."
print (hint)

chevron_right


If the number of tries= number of chances, we display the corect word and the game continues.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

if tries==chances:
    print ("The answer was "+word)

chevron_right


Special Note:To make the code as platform independant as possible we can choose the location of the file dynamically using the platform module of python.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

os=platform.dist()[0]
if os=='LinuxMint' or os=='Ubuntu':
    loc='/usr/share/dict/cracklib-small'
else:
    loc='/usr/share/dict/words'

chevron_right


Software Tools Required:The game can be implemented in Python using modules platform and random.

 

This idea is contributed by Subham.If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.