About scrabble game
Hi,Developers:
Does any one know how the following array work? Is oL long data type?(I think this array is used to locate the 26 letters in checkDictionary class )
private long pointers[] = {0L, 31484L, 60934L, 0x1c917L, 0x2613cL, 0x2c13aL, 0x31d2bL, 0x35d78L, 0x3a2a3L, 0x41292L, 0x4233fL, 0x42efaL, 0x46acbL, 0x4d98bL, 0x4fdb6L, 0x539c1L, 0x5faf7L, 0x60713L, 0x68904L, 0x78c8cL, 0x7fa30L, 0x83e47L, 0x85eb6L, 0x89214L, 0x89236L, 0x896c8L, 0x898f7L };
And what does this mean(s1 is String): s1.length() ? 0 : 2
Thanks for any explanation!
Cai
[588 byte] By [
0004871j] at [2007-9-27 15:12:58]

> Hi,Developers:
> Does any one know how the following array work? Is oL
> long data type?(I think this array is used to locate
> the 26 letters in checkDictionary class )
>
> private long pointers[] = {0L, 31484L, 60934L,
> 0x1c917L, 0x2613cL, 0x2c13aL, 0x31d2bL, 0x35d78L,
> 0x3a2a3L, 0x41292L, 0x4233fL, 0x42efaL, 0x46acbL,
> 0x4d98bL, 0x4fdb6L, 0x539c1L, 0x5faf7L, 0x60713L,
> 0x68904L, 0x78c8cL, 0x7fa30L, 0x83e47L, 0x85eb6L,
> 0x89214L, 0x89236L, 0x896c8L, 0x898f7L };
>
if I had the source......
> And what does this mean(s1 is String): s1.length() ? 0
> : 2
s1.length()?0:2
is not a valid statement in Java (however, in C[++] it would be a valid statement, equivalent to the following)
s1.length()==0?0:2
would be a valid statement in Java.
>
> Thanks for any explanation!
>
> Cai
rob,
Abuse at 2007-7-5 23:12:50 >

Hi Rob,
Here is the source code and questions:
/*
this class is used to check if a word is valid.
*/
import java.io.*;
class CheckDictionary
{
private RandomAccessFile dictionary;
//I think the pointers are
//used to located the 26 letters. But how they work?
private long pointers[] = { 0L, 31484L, 60934L, 0x1c917L, 0x2613cL, 0x2c13aL, 0x31d2bL, 0x35d78L, 0x3a2a3L, 0x41292L,0x4233fL, 0x42efaL, 0x46acbL, 0x4d98bL, 0x4fdb6L, 0x539c1L, 0x5faf7L, 0x60713L, 0x68904L, 0x78c8cL, 0x7fa30L, 0x83e47L, 0x85eb6L, 0x89214L, 0x89236L, 0x896c8L, 0x898f7L};
public CheckDictionary()
{
try
{//It is a DAT file
dictionary = new RandomAccessFile("c:"+ File.separator + "Dictionary" + File.separator + "dictionary.dat", "r");
return;
}
catch(IOException _ex)
{
return;
}
}
public boolean searchWord(String s)
{
if(s.length() == 1)
return false;
String s1 = new String("");
//What does the following two lines mean?
long l1 = pointers[s.charAt(0) - 97];
long l2 = pointers[(s.charAt(0) - 97) + 1];
try
{
while(l2 - l1 > 1L) //once again: 1L,2L?
{
long l = (l1 + l2) / 2L;
dictionary.seek(l);
dictionary.readLine();
String s2 = dictionary.readLine();
s2.toLowerCase();
if(s2.equals(s))
return true;
if(beforeOrAfter(s, s2) == 0)
l1 = l;
else
l2 = l;
}
return false;
}
catch(IOException _ex)
{
return false;
}
}
//can I use String.compareTo(String anotherString) to replace this method?
private int beforeOrAfter(String s, String s1)
{
if(s.length() < s1.length())
{
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) - s1.charAt(i) > 0)
return 0;
if(s.charAt(i) - s1.charAt(i) < 0)
return 1;
}
return 1;
}
for(int j = 0; j < s1.length(); j++)
{
if(s.charAt(j) - s1.charAt(j) > 0)
return 0;
if(s.charAt(j) - s1.charAt(j) < 0)
return 1;
}
return s.length() != s1.length() ? 0 : 2;
//Sorry Rob, I still not clear about the above statement.
}
public void closeDictionary()
{
try
{
dictionary.close();
return;
}
catch(IOException _ex)
{
return;
}
}
}
> Hi Rob,
> Here is the source code and questions:
>
>
> /*
> this class is used to check if a word is valid.
> */
> import java.io.*;
>
> class CheckDictionary
> {
>private RandomAccessFile dictionary;
> //I think the pointers are
> //used to located the 26 letters. But how they
> y work?
> private long pointers[] = { 0L, 31484L, 60934L,
> 4L, 0x1c917L, 0x2613cL, 0x2c13aL, 0x31d2bL, 0x35d78L,
> 0x3a2a3L, 0x41292L,0x4233fL, 0x42efaL, 0x46acbL,
> 0x4d98bL, 0x4fdb6L, 0x539c1L, 0x5faf7L, 0x60713L,
> 0x68904L, 0x78c8cL, 0x7fa30L, 0x83e47L, 0x85eb6L,
> 0x89214L, 0x89236L, 0x896c8L, 0x898f7L};
ok, these are the address index's into the dictionary file, where the 1st word starting with a particular letter is located. e.g. all words beginning with the letter 'a' lie between address 0 and address 31484.
im not sure why there are 28 entries ;]
>public CheckDictionary()
>{
> try
> {//It is a DAT file
> dictionary = new RandomAccessFile("c:"+
> File("c:"+ File.separator + "Dictionary" +
> File.separator + "dictionary.dat", "r");
> return;
> }
> catch(IOException _ex)
> {
> return;
> }
>}
>
>public boolean searchWord(String s)
>{
> if(s.length() == 1)
> return false;
> String s1 = new String("");
>//What does the following two lines mean?
> long l1 = pointers[s.charAt(0) - 97];
> long l2 = pointers[(s.charAt(0) - 97) + 1];
these 2 lines take the 1st character( charAt(0) ) in the searchWord ( s ) and deduct 97( the ascii value of 'a' ). This gives an index between 0 and 25 for letters in the alphabet.
> try
> {
> while(l2 - l1 > 1L) //once again: 1L,2L?
> {
this loop, does a binary chop search on the dictionary
>long l = (l1 + l2) / 2L;
>dictionary.seek(l);
>dictionary.readLine();
>String s2 = dictionary.readLine();
>s2.toLowerCase();
>if(s2.equals(s))
> return true;
>if(beforeOrAfter(s, s2) == 0)
> l1 = l;
>else
> l2 = l;
> }
> return false;
> }
> catch(IOException _ex)
> {
> return false;
> }
>}
> //can I use String.compareTo(String anotherString) to
> replace this method?
you can, but you'll have to modify either the return value from this method, or the code above in the binary chop loop. (the code expects this method to return 0, 1 or 2 (before, after, equalTo) - where as String.compareTo returns (-1, 0, 1) (before, equalTo, after).
>private int beforeOrAfter(String s, String s1)
>{
> if(s.length() < s1.length())
> {
> for(int i = 0; i < s.length(); i++)
> {
>if(s.charAt(i) - s1.charAt(i) > 0)
> return 0;
>if(s.charAt(i) - s1.charAt(i) < 0)
> return 1;
> }
>
> return 1;
> }
> for(int j = 0; j < s1.length(); j++)
> {
> if(s.charAt(j) - s1.charAt(j) > 0)
>return 0;
> if(s.charAt(j) - s1.charAt(j) < 0)
>return 1;
> }
>
> return s.length() != s1.length() ? 0 : 2;
this line is a shortcut, it can be replaced with the following.
if(s.length != s1.length())
{
return 0;
}
else
{
return 2;
}
> //Sorry Rob, I still not clear about the above
> statement.
>}
>
>public void closeDictionary()
>{
> try
> {
> dictionary.close();
> return;
> }
> catch(IOException _ex)
> {
> return;
> }
>}
>
>
> }
rob,
Abuse at 2007-7-5 23:12:50 >

Hi Rob,Thanks a lot for your help!And I have some more questions to ask you:Why does the pointers need a "L" at the end? Are "0L","1L" and "2L" long data type?Is there any easier way to replace the searchWord(String s)?regards!Cai
> you can, but you'll have to modify either the return value from this method, or the code above in the binary
> chop loop. (the code expects this method to return 0, 1 or 2 (before, after, equalTo) - where as
> String.compareTo returns (-1, 0, 1) (before, equalTo, after).
That may be true (although I think not); it certainly can't be relied upon. String.compareTo returns a negative number for lt; 0 for eq; a positive number for gt.
chapter 31, http://www.amazon.com/exec/obidos/tg/stores/detail/-/books/0072130849/reader/23/103-1147013-1707008#reader-link
> > you can, but you'll have to modify either the return
> value from this method, or the code above in the
> binary
> > chop loop. (the code expects this method to return
> 0, 1 or 2 (before, after, equalTo) - where as
> > String.compareTo returns (-1, 0, 1) (before,
> equalTo, after).
>
> That may be true (although I think not); it certainly
> can't be relied upon. String.compareTo returns a
> negative number for lt; 0 for eq; a positive number
> for gt.
oops, sorry 'bout that - its been a while since I used String.compareTo ;]
as to the L, yes, L means the number preceding the L is a long.
im not sure why he bothered with the index's to each letters - seems abit of a hack to improve performance, (but having to change the sourcecode, to update the dictionary, is definitly NOT a desirable 'feature')
rob,
Abuse at 2007-7-5 23:12:50 >

I worked out why there are 27 entries (not 28, i miscounted)27 makes a little more sense as well, its the old fence to post relationship (you need 27 posts for a 26 segment fence :)
Abuse at 2007-7-5 23:12:50 >

So Why does the pointers need a "L" at the end? Are "0L","1L" and "2L" long data type?regards!Cai
Hi Rob,And do you know how the pointer are worked out? For example, why words begin with "a" is from 0 to 31484? Just add all the ascii value together? And must I put the "L" at the end?Regards,Cai
this is pretty shitty code to be used as an example in a reference book