Theme: Copy&Paste all words from dictionary list to an array
If you want to make a word game then you should implement a dictionary list somehow and create also an engine to check if a specific word exists in the dictionary.
Here you can find my method of implementation dictionary list.
Note: To see code snippets please enable JavaScript!
In fact, there are two methods:
1) copy&paste all words from dictionary list to an array
2) load all words from an external file to an array
Today, I will explain the first method. Because this is only an example I will use only words which are beginning with “A” and “B” letters.
Later you can easily add all other words to your program.
Here is a link to the
Dictionary List from Sun which I am using in this example,
but you can use any other dictionary list you want. You must only convert it to an appropriate format by some text editor so that the words can be
added to an actionscript array of strings without errors.
Let’s start with a structure of program. There are four layers as it is shown on the picture below:
1. Layer “Word Engine” – includes an actionscript with the main engine
2. Layer “Word Class” - includes a class with all functions for manipulation with words
3. Layer “A words” – includes a global array with all words which are beginning with letter “A”
4. Layer “B words” – includes a global array with all words which are beginning with letter “B”
There should be still 24 more layers for words from “C” to “Z” but this could be easily implemented by you in the same way as it is done in layers for “A” and “B” words.
Instead of making a specific layer for each of word arrays you can also try to add all arrays with associated words to one layer but in that case you will get too big actionscript code for reading in that layer.
Let’s look now at the organisation of arrays and how they are created in the program:
The array _global.words_A is defined in layer “A words” as follows:
The array _global.words_B is defined in layer “B words” in the same way.
The class Words is defined in layer “Word Class” as follows:
// class that implements all words specifically defined with their first letter
Words = function(pointer_to_array_of_words_defined_with_its_first_letter){
// array list of all words defined with its first letter
this.words = pointer_to_array_of_words_defined_with_its_first_letter;
// to perform a faster searching of words ...
// ... at first, we must set pointers in the array list ...
// ... where words with a different second letter are beginning!
// There are 26 pointers in array for letters from "A" to "Z"...
// ... which are placed on the second position of the word:
this.pointer = [ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ];
// function to find and set pointers
this.setPointers = function(){
var currChar="";
for (var i=0; i<this.words.length; i++){
if (this.words[i].charAt(1)!=currChar){
currChar=this.words[i].charAt(1);
this.pointer[ord(currChar)-65]=i;
}
}
}
// return a starting pointer in the array list to all words ...
// ... which have a "second_letter" on the second position
this.getPointer = function(second_letter){
return this.pointer[second_letter];
}
// return the total number of words in the array list
this.getWordNum = function(){
return this.words.length;
}
// return a word from the specific position in the array list
this.getWord = function(word_position){
return this.words[word_position];
};
// add word to array
this.addWord = function(strWord){
this.words.push(strWord);
}
};
The main engine is defined in layer “Word Engine”. Here is the code to create, initialize and add words to the dictionary array:
// an array which contains all words in dictionary distributed by theirs first letter
dictionary = new Array();
// function for adding words in dictionary
function addWordsInDictionary(){
// include all words with first letter "A" in the dictionary
dictionary[0]=new Words(_global.words_A);
// include all words with first letter "B" in the dictionary
dictionary[1]=new Words(_global.words_B);
/*
// TODO: include here all other words with first letter from "C" to "Z" in the dictionary...
dictionary[2]=new Words(_global.words_C);
...
dictionary[25]=new Words(_global.words_Z);
*/
for (var i=0; i<dictionary.length; i++){
dictionary[i].setPointers();
}
}
And here is the code to check if a word exists in the dictionary:
// function for checking if a word is in dictionary
function checkWord(strWord){
// find a first letter of the word
// to know where to fast jump in the dictionary
var firstChar = ord(strWord.charAt(0))-65; // return value must be from 0 to 25
// find a second letter of the word
// to know where to fast jump in the specific word array
var secondChar = ord(strWord.charAt(1))-65; // return value must be from 0 to 25
// get a starting pointer for searching the word
// in the specific word array of the dictionary
var search_start = dictionary[firstChar].getPointer(secondChar);
// to know if the word is found in the dictionary
var isWordFound = false;
if (search_start != -1){ // check dictionary for words...
// get an ending pointer for searching the word
// in the specific word array of the dictionary
var search_end = dictionary[firstChar].getWordNum();
for (var i=secondChar+1; i<26; i++){
if (dictionary[firstChar].getPointer(i) != -1){
search_end = dictionary[firstChar].getPointer(i);
break;
}
}
// check if the word can be found in the dictionary
for (var j=search_start; j<search_end; j++){
if (strWord == dictionary[firstChar].getWord(j)){
// the word is found in the dictionary !!!
isWordFound = true;
break;
}
}
}
if (isWordFound)
ok_txt.text = "WORD IS OK";
else
not_txt.text = "NOT IN DICTIONARY";
}
Well, that’s all and here is the final result (please note again that there are only A and B words included in the program so you can try to add all other words for an exercise):
I hope the code is quite well commented so there is no need for more explanation, but if you have any question you can always contact me at ssusnic [at] gmail [dot] com .