Search

Game of anagrams using perl

The game of anagrams using perl

The perl script below is for the game of anagram, i.e you will presented with a word with its letters jumbled and you have to guess the correct word.

The script by default uses the file /usr/share/dict/words, which has a list words of english.
Script explanation:

Variables :
$score = 0; To keep track of the score
$level = 5; To set the level
$file = "/usr/share/dict/words"; The default file to look into for words.
$max = 74000; Maximum number of words in the file being used.

Subroutines :

randomize :
This is the main subroutine that is the core of the game.
The file to be used is opened in read mode using



We will pick any random word from this file and shuffle it to generate the anagram. So for this we need a random integer number, which is generated using the rand function.



Where $max is the maximum number among random the numbers generated.
We will loop over the file incrementing a count on every word till count becomes equal to the random number.
The word at the specified count will have to be shuffled now. But before this we have look into the following points
1. The word is not a noun, hence we check if the first letter is capitalized /^[A-Z]/
2. The word is not of the kind her's , their's etc. We look for " ' " in the file using
index($_,"\'")
If this returns -1 then the word does not have a " ' ".
3. Make sure the number of letter in the word is as per the level set, i.e.
For easy number of letters is lesser than 5
For medium number of letter is lesser than 7
For hard number of letter is lesser than 10.
These three conditions are checked using


In case this if condition fails we generate a new random number and restart from the beginning of the file and
This is the else part

If the word is fine then the word will be read as a string, to shuffle the letters we will have to convert it to an array. This is done using split as follows.



To get the length of the string we use the function length () .
An array of flags is used to keep track of the letters that have been shuffled. The flag array is initialized to 0 using



Next we start a loop which runs as many times as the word length and in each iteration we select a random letter from the string again using the rand() function. This random letter is assigned to a new array called shuffled, whose index is incremented serially. Each time we select a letter from the array sting, we set the flag of the corresponding index to inform that the character at that index has been shuffled.
The above steps have been implemented in the following loop.



Once we have finished the above loop, there are chances that some characters might have been missed out as the characters were generated randomly. To account for this we loop over the array string again, but serially this time and check the flag corresponding to each index. If any of the flag is not set, we append that letter to the end of shuffled array.
This is done using the following loop.



Now that we have our shuffled word ready we will present it to the user, and ask the user to enter the answer.
The answer entered by the user is comapred with the original word, if its the correct answer then we increment the score and ask the user if he/she wants to continue playing. If the user enter the wrong answer the we ask the user to try again. If the user does not want to try again we will show the correct answer and continue to the next word.

set_level: This routine is for setting the level, it has three options of level
Easy, Medium and Hard. Depending on the selection, the variable level is set which is used in the randomize routine to set the number of letters.

set_file: The default file used to look for words is /usr/share/dict/words. In case we want to use some other file we can use this routine. We can enter the full path to the file which will be set to the variable "file".
We also have to set the variable "max" to the number of words in the file, which is done by looping over the file and incrementing a count on every word.
The file that we use should have only one word per line as we count one line a one word in the program.

Main menu :

print " Press: 1 to begin the game : Will call the randomize routine
2 to know about the game : Will give information about how to play and set levels etc
3 to select level : Will allow you to change the level by calling set_level routine.
4 to change the input file : Will allow you to change the input file using set_file.
5 to quit : Will exit the game.
Here is the full script, it has been tested on perl version 5.10 on linux.It might work in windows too but has not been tried.

Anagram.pl



Sample output


Hope you have fun... feel free to give any kind of feedback. :-)

No comments:

Post a Comment