Professional Documents
Culture Documents
I hereby declare that this thesis is entirely my own work and that it has not been
submitted as an exercise for a degree at any other university.
May 7, 2002
Tamara Sobrino López
1
Acknowledgements
My supervisor Carl Vogel for helping throughout the year at any time that
was needed, and for being so enthusiastic about the whole project.
My mum for all her love and support during the last four college years, and
during all the years before that.
Jimmy, for helping me with some problems encountered with the program.
2
3
All programmers are playwrights and all computers are lousy actors.
-Anonymous
Contents
1 Introduction 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.2 Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.3 RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Aims . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 A Look Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Corpus Based 18
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 What is a Corpus . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4 Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.1 Getting the Corpus . . . . . . . . . . . . . . . . . . . . . . 21
3.4.2 Counting the Letters . . . . . . . . . . . . . . . . . . . . . 21
3.4.3 Sorting the Letters . . . . . . . . . . . . . . . . . . . . . . 21
3.4.4 Assigning the frequencies . . . . . . . . . . . . . . . . . . . 22
3.5 Example Corpus . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5.1 Letter Bag Corpus-Based Constructor . . . . . . . . . . . 26
3.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4
CONTENTS 5
4 Dictionary Storing 27
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2 Searching Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.3.1 Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.4 Storing and Searching . . . . . . . . . . . . . . . . . . . . . . . . 30
4.4.1 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.5 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.6 Tries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.6.1 Looking for a node in a trie . . . . . . . . . . . . . . . . . 34
4.6.2 Making the trie . . . . . . . . . . . . . . . . . . . . . . . . 35
4.6.3 Why a trie . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.7 The Dictionary Class . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7 Scrabble Game 52
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2 Applet components . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2.1 Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.2.2 Starting a game - The “start” button . . . . . . . . . . . . 54
7.2.3 Joining a game - the “join” button . . . . . . . . . . . . . 55
7.2.4 Playing a word - the “play” button . . . . . . . . . . . . . 56
7.2.5 The Rack . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
7.3 Blank Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7.4 Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.4.1 Making new words . . . . . . . . . . . . . . . . . . . . . . 59
7.4.2 Getting the words . . . . . . . . . . . . . . . . . . . . . . . 59
7.4.3 Checking that words are in line . . . . . . . . . . . . . . . 60
7.5 Client to Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.5.1 ShowPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8 Conclusion 67
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.2 Achievements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.3 Knowledge Acquired . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.4 Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
8.4.1 Adding Time . . . . . . . . . . . . . . . . . . . . . . . . . 69
8.4.2 Changing the number of players . . . . . . . . . . . . . . . 69
8.4.3 Choice of Dictionary . . . . . . . . . . . . . . . . . . . . . 69
8.4.4 Expanding the System . . . . . . . . . . . . . . . . . . . . 69
8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Bibliography 71
A CSLL Webpage 72
C Sample Run 82
List of Figures
7
Abstract
Introduction
1
CHAPTER 1. INTRODUCTION 2
1.1 Introduction
The aim of my project was to design a corpus-based Scrabble. In this chapter
the reader will be introduced to the basic aims, the motivation and the resources
used for designing a Scrabble game. An overview of the project outline is also
given.
1.2 Motivation
There were various reasons that motivated me towards choosing to design a Scrab-
ble game.
First of all, games are exciting, and the idea of creating a computer version
of such a popular and world-known game was an appealing one. I liked the idea
of watching people playing a game I designed, or playing against them.
Secondly, although I did not initially appreciate the amount of resources, and
to what depth they needed to be used, having to improve my Java knowledge
seemed like a great idea. Ever since I took the 1BA1 “Introduction to Program-
ming” course in first year, I have been very interested in the language and wanted
an opportunity to learn the language properly and use it for something like this.
Finally, my supervisor, Carl Vogel is a huge fan of the game, and can be
quoted as always saying “Can I interest you in a game of Scrabble?”
1.3 Resources
In this section I will discuss the resources that I finally opted for to implement
the program, contrasting them to other options. After choosing the title of my
project, the next main task was to choose the right languages to implement it in.
1.3.1 Java
There are various languages that could be used to design the game, such as C,
C++, Java, and some algorithms could be implemented in Prolog. In this section
I describe the Object Oriented language Java, and why I chose it above others.
First a bit of history: Java was designed by Sun Microsystems and is very well
adapted to network programming. Bill Joy believed that “The Network is the
computer” and together with James Gosling developed the Java language, which
was formally announced at a major conference in 1995. It immediately attracted
the attention of mainly the business community, because of its closeness and
functionality with the World Wide Web.(see Deitel & Deitel, 2002)
CHAPTER 1. INTRODUCTION 3
1.3.2 Applets
An applet is a Java program that can be embedded in HTML, therefore can be
run from any web browser.1 The web browser executes the Java program if there
is a HTML document containing the applet. Applets allow the programmer to
do all sorts of things with an image. It allows the user to communicate with the
Java Program, through a “pretty” interface, or Graphical User Interface(GUI).It
provides facilities for the use of buttons, labels, text areas, menu bars and so on.
Other options were JavaScript or CGI. CGI(Common Gate-way Interface) is a
method for using programming languages on the web. A CGI program can be
written in any language that allows it to be executed within the system,such as
C, Fortran, Java but is mostly used with Perl. I considered this option, as it is
fast and effective, but Applets belong to Java itself and are easier to program and
run. My other choice, JavaScript was discarded based on its lack of portability2 .
JavaScript was developed by Netscape and Sun with the sole purpose of enhancing
webpages. The developers were hoping that this new “scripting”3 language would
be simpler to understand than Java and allow web designers to add interactive
elements to their webpages without the need to use a complicated programming
1
This is different if applets need to interact through a Server. This is discussed in chapter 6
2
Portability is defined as the capability of transferring one component or system from one
hardware or software environment to another
3
A scripting language is a language that consists of a sequence of statements
CHAPTER 1. INTRODUCTION 4
language. JavaScript does not allow as much portability as Java. It can only be
fully implemented on Netscape browsers that are version 3 or higher.
Again, mainly because of security and portability reasons, my best option was
to stick to Applets and Java.
1.3.3 RMI
I have already explained how the most interesting feature of Java is how well
adapted to the network it is. But how it does this is another question. I want my
Scrabble game to be interactive, and work through computers. Servers are needed
for this. RMI (Remote Method Invocation) is a powerful mechanism designed to
make communication between two Java programs running on separate machines.
In RMI an application is broken up into two separate programs: a server and a
client. A server waits for clients to invoke methods. A server contains all the
methods that need to be shared by all the clients. RMI makes use of the Java
Server and Socket classes in a more user-friendly framework. Because this is such
a big area of my project, RMI deserves a chapter of discussion, so the reader is
directed to chapter 5.(for reference see Grosso, 2002)
1.4 Aims
Once I decided on the title and on how to implement my project, the next step
was to divide my big aim of designing a web-based, corpus-based Scrabble into
smaller sub-aims.
1. What exactly constitutes a Scrabble game? What are the objects that need
to be represented? To get each “piece” of the game working separately and
also to make it combine with all the other “pieces” to make a full Scrabble
game was my main aim.
6
CHAPTER 2. ARCHITECTURE AND DESIGN 7
2.1 Introduction
In this chapter I give a brief description of the ScrabbleT M board game, and
compare it to the Java version of Scrabble. An outline of my initial design is
given, finishing up with how the classes were implemented. The main algorithms
are described, relating them to runs of the game, and with figures to show how
they work.
• If a word does not exist, the player’s turn is skipped and letters are returned
to the rack.
• Choice of corpus.
1
The “Rules of Play” from the Scrabble board game give an option for Solitaire Scrabble. It
suggests that a player can try to beat his or her previous scores, using only one rack. Otherwise
they can play as if they were two players. My Scrabble doesn’t give that option.
CHAPTER 2. ARCHITECTURE AND DESIGN 8
Letter Rack
Scrabble
Game
Board Tiles
Squares Dictionary
Letters
Scrabble board games refer to themselves as “The world’s leading word game”.(see
Games, 1988) To make words, letters are needed. Each letter is an object in Java.
There are three features that identify the Letter object.
1. A character - this is the letter itself.
2. A value - the points got for playing that letter.
3. A frequency - how often the letter is found in the letter bag.
Each time a letter is created, values for the three attributes are given. A letter
is created when the user starts the game. In fact, 100 letter objects are created,
one for each tile in the bag.
Example
Letter l = new Letter(’Z’, 10, 1);
LetterBag
In a Scrabble game there is one letter bag shared by all the players. It contains
100 elements. The main things that are done in the LetterBag class are the
following:
• Create a letter bag
– Standard English Scrabble
– Corpus Based Scrabble
• Generate random letters
• Put letters back in the bag
Creating a Letter Bag
In this section I will concentrate on how the standard letter bag is created.
An explanation of creating a corpus based letter bag is given in section 3.4.
CHAPTER 2. ARCHITECTURE AND DESIGN 10
Example
The five first letters of the alphabet are stored in the first 5 positions in the
all bag array.
This example shows that there are 9 As, 2 Bs, 2 Cs and so on. The method to
create the letter bag goes through each letter and sees how many of it there must
be inside the letter bag(vector). This is done in the Arr to Vector method. An
outline of the process for the first five elements would look like this:
- empty vector:
vector: []
vector: [A][A][A][A][A][A][A][A][A]
vector: [A][A][A][A][A][A][A][A][A][B][B]
vector: [A][A][A][A][A][A][A][A][A][B][B][C][C]
vector: [A][A][A][A][A][A][A][A][A][B][B][C][C]
[D][D]
vector: [A][A][A][A][A][A][A][A][A][B][B][C][C][D]
[D][E][E][E][E][E][E][E][E][E][E][E][E]
This process continues until all the letters are inside the bag. When all the
letters are inside 2 blank pieces are added.
bag.remove(i);
Square
This class represent each of the squares that are found on a board. There are
225 of these on a board. It is necessary that they should be a class of their own
because they have attributes that belong only to them. Each square is defined
by:
3
denotes an array in Java
CHAPTER 2. ARCHITECTURE AND DESIGN 13
• score - This represents the type of square. It is a number value that must
be one of the following:
– 0 = it is a normal score square
– 1 = double letter score square
– 2 = double word score square
– 3 = triple letter score square
– 4 = triple word score square
Board
In a Scrabble game players take turns forming words and placing them on a board
either vertically or horizontally. After a player decides on a word, his or her score
is calculated by addidng the points for each letter, but where on the board the
letter is placed is also taken into account. The board consists of a 15 * 15 grid
of squares. In class Board the grid is initialised with the different score values
for each square.
If no specific value is given for a square then it is taken that it is a plain
square. All other squares are initialised to one of the values of Square discussed
in the section above.
The algorithms used for ensuring that a player places letters correctly, and
receives proper points will be discussed in section 7.4
Example
This shows the initialisation for some squares for the 4 types of scores depending
on where a letter is placed on the board. After each line of code, the position it
represents on the board (Figure 2.3) is given.
...
Rack (Tile)
Each player owns one rack. This is the place where he or she places their letters.
I initially thought a Rack would be a group of 7 letters and made a class called
CHAPTER 2. ARCHITECTURE AND DESIGN 15
Rack, which consisted of 7 letters. This made perfect sense, until I found prob-
lems in trying to replace letters after an user makes a word. A rack is not always
made up of 7 letters. The number of letters that need to be replaced at a time
depends on the length of the word the user has made. My solution to this was
to make a rack as a group of Tiles. Each tile has as attributes:
The amount of “false” positions are counted when letters need to be replaced.
Example
A player makes the word “duck”. If none of the letters were already on the board,
then for each of the letters moved filled is set to false. In this section of code, it
goes through each of the 7 positions in the rack and checks how many of them
are filled:
Players
Now that there is a Scrabble way, there needs to be an object to represent a
player. A player’s features or attributes are:
• name
• score
There are various actions associated with a player. The main ones are those
that set a players letters, and the one that gives a player a score once a word has
been played. These two main methods are summarized below.
2.2.1 Dictionary
It is often the case that players try to get away with playing any random word
that comes into their heads. Any words listed in a standard English dictionary are
permitted except for proper nouns or abbreviations.(Games, 1988) A Dictionary
can be represented in varioius ways, and these will be discussed in Chapter 4.
CHAPTER 2. ARCHITECTURE AND DESIGN 17
2.3 Summary
This section has outlined the basic architecture and design behind a Scrabble
game. It has allowed the reader to appreciate the features associated with each
object and allowed him or her to relate it to the Scrabble program. In the
following chapters I will discuss the utilities of all the objects outlined here in
reference to the Scrabble game.
Chapter 3
Corpus Based
18
CHAPTER 3. CORPUS BASED 19
3.1 Introduction
A Scrabble game has a letter bag, of type LetterBag, (see section 2.2) with one
hundred pieces. The number of occurrences and points given for each letter
depends on how frequently they are found in the English language. This chapter
explores the benefits of being able to manipulate frequencies and points based on
different corpora. It describes the benefits of a corpus-based feature, and looks
at letter distribution in various Scrabble games. It concludes by examining the
CSLL webpage and applying its letter distribution to a Scrabble game.
Even though corpora have been around for centuries, computers have made their
use much easier. Linguists have used texts and corpora in many fields, including
syntax and semantics. With computers, a corpus can be analyzed using statistical
techniques to derive information about word frequency, co-ocurrence and so on.
For the purpose of this project the corpus analysed is that found in any webpage
supplied by the user to derive letter-frequency. The letters are counted and the
frequencies are distributed according to the number of occurrences. The most
common letter will be worth the least points, but will appear the most times in
the bag. The process is divided into four steps:
Section 3.4 discusses each of these steps in more detail, but before it is useful
to understand the purpose of using corpus based features. This is discussed in
the following section.
3.3 Purpose
Imagine yourself in a situation where you want to teach a group of students
learning English how to make words with the letter ‘Q’. The letter ‘Q’ occurs
CHAPTER 3. CORPUS BASED 20
Table 4.1 summarises the results from the experiment. The 7 letter sequences
represent the letters drawn from the bag into the rack for each of the three
Scrabble games. From the previous experiment, it can be seen that the total
number of vowels from the first five runs is exactly the same as the Scrabble
CD-Rom game. It would be very convenient to offer people like Robinove the
choice to input a corpus. In this way, and using the same random features, he
would not get a chance to write a paper about vowel-distribution.
CHAPTER 3. CORPUS BASED 21
3.4 Process
3.4.1 Getting the Corpus
Before a game is started the player has two choices. Either she wants to play the
standard Scrabble game, or wants a change, and decides to make it corpus based.
I will focus here on the latter choice, when the user enters an URL when asked.
Java provides a class URL (Uniform Resource Locator), which as the name
suggests represents a pointer to a resource on the Internet. When the methods
to read the webpage are used, what is actually being read is the html source file.
The source file is the html document that dictates what a page looks like, and
includes all the html and javascript tags. However, for corpus based purposes
only the plain text is necessary.
To get rid of html tags such as <head>, <html>, <body>, everything that
is found between “<” and “>” is ignored when a line is read. All the code inside
an opening <head> and a closing </head> is ignored. This gets rid of all the
Javascript, php1 or any additional features the page may have. Now the corpus
that is needed has been retrieved.
Bubble Sort
It is easier to understand bubble sort if we think of a vertical column, such
as those in Figure 3.1, where the largest elements need to be at the top and
1
PHP stands for Personal HomePages, and is a scripting language
CHAPTER 3. CORPUS BASED 22
the smallest at the bottom. The array is scanned from top to bottom and two
adjacent elements are interchanged when they are out of order, that is when the
one below is bigger than the one above. The big elements are ‘bubbled’ to the
top. (see Kruse, Tondo, & Leung, 1997)
In the first round, the item array[n -1] and array[n-2] are compared. Then
array[n-2] and array[n-3] are compared, until array[1] is compared to array[0]. At
the end of the first round the largest element is on top, so we do not need to
do anything more to it. In the second round elements are compared from the
bottom, until array[2] is compared to array[1]. When there are no more elements
to compare, the array will be sorted. This sorting algorithm is quite slow if we
are dealing with big arrays, but works well for small arrays.
I 0 1 2 3...
3 3 3 8 8 8
8 8 8 3 3 5
1 1 5 5 5 3
2 5 1 1 2 2
5 2 2 2 1 1
For the purpose of this example, I will use the CSLL course webpage.(see
Appendix A)
Step 1
The game is started, and the person that starts the game is asked:
The user decides to play the corpus based game and enters the following:
http://www.cs.tcd.ie/courses/csll/CSLLcourse.html
Step 2
The URL is input to the letter bag constructor, which was mentioned in section
2.2.
• calculateCount(String str) This method takes a string as a parameter.
Converts it to an URL format. Reads in line by line taking away the tags.
Everything that is a letter and is not within <head> and </head> or
inside angle brackets it counts. Return: A 26 element Letter array with the
counts.
• After it has counted, the program lets the user see how many instances of
each letter there are. For the CSLL webpage this is what it found:
CHAPTER 3. CORPUS BASED 25
J:26 K:24 L:410 M:184 N:520 O:429 P:181 Q:12 R:476 S:540
Compare the count above with the webpage I created with many Qs, Zs
and Ws (see Appendix B).
J:9 K:46 L:11 M:3 N:3 O:9 P:6 Q:87 R:1 S:11
(Note: represents the blanks. They are not counted, because no matter
how many blanks are found, there are always going to be 2 in the letter
bag)
Step 3
3.6 Conclusion
In this section I have explained why it is good to have an option of changing
letter frequency in Scrabble. I have explained the algorithms and methods I
used to make my Scrabble different from any other Scrabble; how I made it
corpus based.We have seen how statistical techniques are useful for measuring
relationships between letters. Choosing the CSLL webpage as my example seemed
like the obvious choice, and the step by step guide shows exactly how it was done.
Chapter 4
Dictionary Storing
27
CHAPTER 4. DICTIONARY STORING 28
4.1 Introduction
A Scrabble game needs to allow the players the possibility of using a dictionary.
Scrabble rules permit any words listed in a standard English dictionary, except
those spelt with an initial capital letter, abbreviations or suffixes(see Games,
1988). When using the ScrabbleT M board game, players decide on the dictionary
that can be used. In the Scrabble program the dictionary is represented as a text
file with as list of just under 40000 words.(for source see dictionary, )
There are many possible ways of storing a dictionary, and searching for a
word. This chapter looks at various storing and searching strategies. Careful
consideration needs to be taken in order to decide on the most optimal dictionary
implementation. The chapter concludes by giving a brief outline of how the
dictionary was implemented for the purpose of this project.
4.3 Sorting
Searching through data structures is much more efficient if the data is sorted
according to some criteria or order. It would be nearly impossible to look for a
word in a dictionary had it not been sorted in some way. Although computers
would deal much more efficiently than humans if they had to search through an
unordered collection of data it would be an extremely inefficient use of time and
memory. An ordered list is defined as
a list in which each entry contains a key, such that they keys are in
order. That is, if entry i comes before entry j in the list, then the
key of entry i is less than or equal to the key of entry j.(Kruse et al.,
1997)
The criteria for sorting a dictionary is obvious: alphabetical order. Ohter
types of dictionary sorting, such as sorting by lexical category or topic need not
concern us. This method majorly reduces search time. Primarily, even if the
most basic search is done through an ordered list, that is, if the search goes from
the beginning to the end element by element, when a word is not found past a
certain point, it cannot be there.
Example:
Imagine the following list:
bull — cat — cow — dog — duck — goose — hen — pig — rabbit
— sheep
Suppose the word “horse” needs to be found in this list, the search process needs
only continue until just before the word “pig”, because the list is ordered, and
the letter ‘h’ occurs before ‘p’ in the alphabet.
4.3.1 Quicksort
This is the quickest method for sorting that will be dealt with here. It is a
recursive method, which was designed by C.A.R Hoare back in 1962. The basic
idea is to choose a point, around the middle, so that there will be as many
elements to the right as to the left. The worst case is when the pivot fails to split
the list evenly, so that one sublist has n-1 entries, and the other is empty.In this
case the search takes O(n2 ), where n is the number of elements. The best case
is when Quicksort’s partitioning produces two parts of equal length. It occurs in
O(n log(n)) time, where n is the number of elements. (see Kruse et al., 1997)
The quicksort steps are the following:
• beginning
• pivot
• end
4. stop when an element bigger than pivot is reached, or it comes to the end
pointer.
6. now swap!
In the example on Figure 4.1, the middle element is chosen as the pivot.
Two pointers scan the array: one from the beginning and one from the end,
comparing both to the pivot. Number ‘4’ and number ‘1’ are out of order so are
swapped. Moving both pointers towards the middle, it finds that ‘2’ and ‘5’ are
correctly placed, hence are not swapped. Once both pointers reach the middle,
the recursion starts and quicksort is applied to both parts, as is shown by the
inverted curly braces. In the first half, ‘1’ and ‘2’ are sorted, but in the second
half ‘5’ and ‘4’ were swapped. Now the array is sorted.
a) 4 2 3 5 1
pivot
start end
b) 1 2 3 5 4
c) 1 2 3 4 5
2. The dictionary can be made into a tree and nodes and branches can be
searched through in the most efficient way. To appreciate its efficiency, a
tree implementation is compared to that of a linked list.
3. The dictionary can also be made into a trie. Tries will prove to be the most
efficient, as they allow multiple node sharing. They will be discussed in
section 3.6.
4.4.1 Trees
The most basic type of tree is a Binary Search Tree. It is more efficient than
searching through a linked list, because it is not necessary to move a node at a
time, hence having to search through every single node. A binary tree makes the
entries of an ordered list into the nodes of a binary tree, thus making it possible
to look for a particular key in O(log n) steps, where n is the number of nodes.(see
Kruse et al., 1997)
In a binary tree, whether a daughter node is positioned to the left or the right
of the parent node depends on what relationship it has to it. In Figure 4.2 a
possible relationship is “is younger than”, such that Matt is younger than Ann,
who are both younger than Mary and so on. Trees are efficient for looking up, for
example, lists of numbers, but for implementing a dictionary, tries are a better
CHAPTER 4. DICTIONARY STORING 32
Linked List
W
element next E
Type: Type:
char node R null
Binary Tree
John
Mary
Peter
option. For this reason, a dictionary tree representation will not be shown here,
but tries will be discussed in section 3.6.
keeps on being repeated on the half of the array that the element is in until the
element is found.
This algorithm has O(log2 n)performance, compared to O(n) for sequential
search, where where n is the number of element in the array2 . For the dictionary,
the number of nodes represents the number of words.
Need: 7
5 7 8 10 19 23 34 78 89
5 7
Need = pivot
Figure 4.3: Binary Search Example
4.6 Tries
A trie is a multi-way tree structure that is used for storing strings from an alpha-
bet. It is a very popular method for storing large dictionaries, and is used mainly
for spell-checking programs and has uses in NLP(Natural Language Processing).
The basic principle of this method of storing data is that all strings sharing a
common stem or prefix stream off from a common node. Each node has a max-
imum of 27 nodes.3 branching off(one for each letter of the alphabet, and ǫ to
signal the end of a word). A null information pointer means that the string is not
2
in this case n represents the words in the dictionary
3
This is the case for the English language, whose alphabet has 26 letters
CHAPTER 4. DICTIONARY STORING 34
a c
n
a u
t r t p
t #
# # #
Searching for a particular word, or key, begins at the root node. The branch
to take is determined by looking at the first letter of the word and choosing that
path, or branch. If there is an empty branch rooting from the node it is currently
are looking at, the word does not exist or is not represented. The second letter
in the word represents the next level in the tree, the third determines the third
level and so on. The end of a word is represented by ǫ, so when the last letter
of a word is reached, there must be an arc that leads to ǫ(represented as # in
Figure 4.4).
CHAPTER 4. DICTIONARY STORING 35
4.8 Conclusion
This chapter has served as an introduction to the importance of storing and
searching algorithms in computers, and real life situations. Various search strate-
gies and sorting algorithms have been outlined. The importance of choosing an
efficient strategy depends on the size of the data we are dealing with, and how
fast it needs to be. It can be concluded that a trie is the most efficient way of
storing a sorted dictionary. Search is extremely fast, as it is proportional to the
number of letters in the word being searched, rather than to the length of the
wordlist.
Chapter 5
37
CHAPTER 5. SERVERS AND RMI 38
5.1 Introduction
This chapter provides an outline of Java’s Remote Method Invocation (RMI)
features. The chapter starts by describing sockets and streams, which are the
basic features of network programming. RMI hides the difficulties in implement-
ing sockets, because you do not need to create socket instances every time one
is needed; it knows where and when to create sockets. The chapter ends by
introducing callback methods. Callback is a feature used to allow two-way com-
munication between servers and clients. This chapter focuses on RMI’s features,
referring occasionally to the Scrabble program. The architecture and implemen-
tation of my game requires a server that contains some aspects of the game, and
a client that contains others. Communication between the two must be possible,
hence requires RMI or something equally as powerful. RMI in relation to the
Scrabble Server is discussed in chapter 6, while RMI for the Scrabble Client is
explained in chapter 7.
Usually, for communication between different machines sockets and streams are
necessary. Java has Socket classes which allow servers to listen for a client
socket to make a connection. Clients and Servers talk to each other via con-
nections through sockets. Streams are ordered sequence of bytes, which send
information from a client to a server. RMI allows the programmer to hide the
difficulties involved with socket classes. The following section is dedicated to RMI
implementation. (For further information consult Grosso, 2002)
5.2 RMI
RMI adds power and flexibility to the Java language, while still preserving its
object-oriented features.
Because RMI takes it upon itself to implement the Java Server and Socket fea-
tures, it reduces the complexities of distributed computing. RMI applications
are made up of two separate programs: a server and a client. In the following
CHAPTER 5. SERVERS AND RMI 39
sections, both servers and clients will be discussed. Other components are vital
in making RMI work. These are the rmiregistry that keeps track of all the servers
running and stubs and the skeleton, which allow communication between a server
and a client. To send information though the network, it must be encoded in an
adequate way. This will be explained in section 5.2.4.
5.2.1 Server
A server creates remote objects, and makes a reference to them, such that they are
accessible to the client. Basically, it defines all the method implementations that
need to be accessed from some other machine. The Server class for the purpose
of the Scrabble project is described in the following chapter. It holds everything
that is particular to a Scrabble game, and not to each individual player.
5.2.3 rmiregistry
The rmiregistry is the standard naming registry service provided with Java. It
is easier to think of the registry in terms of a phone book, which is used to look
up a phone number through a person’s name. The client needs to look for the
server somewhere. The server will be listed in the registry, along with all the
other servers that may be running. Each server is identified by a different name.
When a client wants to find a server, it constructs a special URL with the RMI
protocol, the hostname and the object name.(see Grosso, 2002)
The Java Naming class, which provides methods for storing and obtaining
references to remote objects in the remote object registry is used here. The
Naming class’s methods take, as one of their arguments, a name that is an URL
formatted java.lang.String of the form:
//host:port/name
The lookup is then performed, and the remote object is bound to the corre-
sponding server. Binding a name is associating or registering a name for a remote
object that can be used at a later time to look up that remote object. A remote
object can be associated with a name using the Naming class’s bind1 or rebind2
methods. (For further information consult Pitt & Kathleen, 2001)
1
“Binds the specified name to a remote object”(java.sun.com, 2002)
2
“Rebinds the specified name to a new remote object”(java.sun.com, 2002)
CHAPTER 5. SERVERS AND RMI 40
The server is created and given a name in the Server class(in this case it is
myServer ). For this program the server is run in sun39. This is part of the code
that stores the reference to the server.
Naming.rebind("//sun39.cs.tcd.ie/myServer", server);
This is part of the code in the Client class that looks for a particular server.
5.2.4 Serialization
In order to execute a remote method, RMI has to transmit the methods arguments
from the client to the server, and transmit the result back. Arguments need to be
encoded in such a way that they can be sent through the network. Marshalling an
object refers to the process by which they are encoded and ready for transmission.
Unmarshalling refers to the process of decoding them once they are received. This
is where serialization comes in. If one refers to any of the classes in the Scrabble
program, the keyword “Serializable” appears in any object that is sent through
the server. Serialization is the action of encoding an object into a stream of
bytes. RMI requires that any object that needs to be sent through the network
to be marshalled or unmarshalled be serializable.(see Pitt & Kathleen, 2001)
In a Scrabble program, multiple clients access the Server simultaneously.
Some actions aren’t possible for a client, if other actions are taking place. For
example, 2 players cannot place letters on the board at the same time. This flow
of control process is discussed in section 6.3.2.
A skeleton is defined as
3
Java Virtual Machine: This is a computer that runs compiled java code, for example
Netscape
CHAPTER 5. SERVERS AND RMI 41
Basically, a server does not communicate directly with a client. There are things
that need to be done before communication can go on, and this is done by these
two other applications. A stub is created when the code is compiled with the RMI
compiler(rmic). It acts as a client’s local representative or proxy for the remote
object. All the communication that needs to be done is carried out between the
stub and the skeleton. The server and the client never talk directly to each other.
The steps in their communication are the following:
1. The stub either creates a socket connection to the skeleton on the server,
or uses an already existing connection.
2. The skeleton marshalls all the information associated to the method call,
and sends the information over the socket connection to the skeleton.
3. The skeleton then demarshalls the data and makes the method call on the
actual server object.
4. It then gets the return value from the method, marshalls the value and
sends it over to the stub.
5. The stub demarshalls the return value and uses it for whatever purpose it
needed it.
(For further information refer to Grosso, 2002). Figure 5.2 shows the representa-
tion of stubs and skeletons, along with other RMI components.
5.3 Client
An RMI client is nearly the same as any other Java class, with the only exception
that it calls remote methods from the Server. The design for the Client program in
the Scrabble game represents each player. It defines all the features and methods
related to an individual player, such as his rack, his score and so on. Chapter 7
discusses the Client application in the Scrabble game.
I needed a way to show everyone else that word. A server waits for methods
to be invoked by the client on the remote objects the server created. In other
words, a server listens, and a client talks. In Java, for something to happened,
it needs to be asked so that it happens. This is done with method invocation.
Suppose there is a rectangle, and a method called getArea(), and the user wants
to know its area, he or she must call the method, and then as a result the area
will be given. Section 7.5 deals with callback methods in relation to the Scrabble
program.
When a player makes a word, puts it on the board and presses play, the server
automatically knows there is a word, but it must tell all the other users. Only
the player who has the turn can see the events on the board until he decides to
play the word.
How is the server going to tell all the players that there is a new word, when
no one has asked if there is a new word?
5.4.3 Threads
Multitasking
For clarification purposes I will use an analogy to Operating Systems.Operating
systems are easy to relate to, and their multitasking ability is closely related to
the concept of Java Threads.
What is an operating System: An operating system can be defined as
a system program which controls all the computer’s resources and provides the
base upon which the application programs can be written. (see Stallings, 2001)
In an Operating System, like Unix,4 more than one program is allowed to
run simultaneously. When different applications or processes are opened in an
OS they don’t necessarily know about each other; each process uses a different
“thread”. However, it is possible for processes to interact and communicate with
each other. Imagine a text editor and a file manager are running at the same
time. These are both separate processes, and have variables or properties that
are particular to them, but it is possible for example to drag a file into a text
editor. There is some space for shared memory, which more than one process can
access at the same time.
In the following figure it can be seen how operating systems manage applica-
tions. This is analogous to the problem of running more than one method at the
4
Other OS also allow this; even Microsoft has recently accomodated Multitasking
CHAPTER 5. SERVERS AND RMI 43
same time. To discuss the problem, the following sections explain the concept of
threads, as well as server callback.
The following figure shows how each application has its own space of local
memory, where its variables and particular features are kept. All applications
however have some shared memory, where processes interact. (For further reading
refer to Oaks & Wong, 1997)
Operating System
Shared Memory
Threads
The JavaT M tutorial defines a thread as:
are playing, that keeps an eye for the word played. So the two processes needed
are:
1. Scrabble program
2. Thread that watches for words and draws them on everyone’s board
2. When the server application has received the word, it makes a remote
method invocation on the RMI server inside each of the client applications.
This method proves to be much more advantageous than using threads. Mem-
ory does not need to be wasted, making the program slower by continuously
invoking a method to ask whether the server has a word to show everyone.
Figure 5.2 shows all the components of RMI that have been discussed. The
dotted arrows show the process carried out by the Server to register in the rmireg-
istry. The client then binds to a particular server. Server-Client communication
is through the stubs and skeleton. Callback is represented by the thick straight
arrows. The Server is allowed to talk to the servers that reside inside each of the
clients.
RMIREGISTRY
Server
Server Interface
Skeleton
Stub Stub
Client Client
• it cannot make network connections except to the host that it came from.
The Java Security manager imposes these restrictions to prevent any form of
attacks such as viruses.
The fact that network connections cannot be made, as is stated above,(see
Pitt & Kathleen, 2001) except from the host that the server is running on created
a few problems during program execution. Java policies, which will be discussed
in the following section, can fix this. These applet restrictions meant that the
only way the Scrabble program could be tested was if it was running all from the
same machine. The server resides in sun39, so then the clients had to be either
directly in sun39, or accessing sun39 via telnet or ssh. Although the game was
played using this method, it proved to be quite slow, because of network traffic.
However, there is a possible solution so that it is possible to have a server run-
ning from one machine, and different clients from other machines.This is possible
if permission is granted. In Java permission is granted via policy files. Policy
files will be discussed in the following section. This Scrabble program works for
2 clients. Chapter 8 discusses player number expansion.
CHAPTER 5. SERVERS AND RMI 46
Java Policy
A policy is a normal text file, which is by default named “java.policy”. A policy
file consists of different protection domains.7 For the purpose of this project I
have granted permission on all domains. I want to allow my applets to transmit
everything that is necessary through different machines.
Here is the java.policy file.
grant {
// Allow everything for now
permission java.security.AllPermission;
};
5.5 Conclusion
This chapter has served to outline RMI’s features. Chapter 6 takes a look at
how the Server application for the Scrabble program was implemented. Chapter
7 gives an outline of the Client class, along with the ClientServer classes.
7
set of permission entries granted to a code source
Chapter 6
47
CHAPTER 6. GAME AND SERVER 48
6.1 Introduction
In the previous chapter, the concepts of server, clients and RMI were explained.
This chapter further expands on Server concepts, but in relation to the Scrabble
Server class. The Server class is responsible for the methods that are particular
to a game, and serve a purpose for the clients. A Game class was created, which
represented the collection of all the possible general actions in a Scrabble game.
The flow of control algorithms are discussed in relation to Operating Systems
because of their closeness in the way they deal with flow of control.
• a bag
• four racks
• four players
• a board
This did not work because all players cannot have access to everyone else’s
rack. A player cannot look at someone else’s word until they have finished playing.
The solution was to create a game , but not a client(player), class that grouped
all the methods relevant to a game together. The Game class represents the game
that the server possesses. The Server uses the Game class and its methods to
make them accessible to clients. The Game class is responsible for the following
actions:
• Keeping a record of the number of players, which are of type Player (section
2.2). The maximum number of players a game can have is four. This
number can be changed, simply by increasing the size of the array players.
The letter bag is created when an user starts a game. It uses one of the
two constructors defined in class Letter. (section 2.2)
• Setting the players letters. The server knows which player has to be given
the letters at each time. (section 2.2)
• Putting the letters back in the bag. This method calls the method from
the Letter Bag class. It is called when an user wants to swap her letters.
(section 2.2)
• Joining a game. Similarly, a game can only be joined if there are already
players. Joining a game adds an extra player to the game. It would also be
an option to allow an use to be the first player, if they press “Join”, instead
of “Start”, however, I chose to implement it such that a game is started
before anyone can join.
• Getting and setting players’ scores. This is only done when a word exists
in the dictionary. The Server knows which player must be given the points.
2. The wait operation decrements the semaphore value. If the value becomes
negative, then the process executing the wait is blocked.
3. The signal operation increments the semaphore value. If the value is not
positive, then a process blocked by a wait operation is unblocked.
The main type of semaphore used is a binary semaphore. It has the same
expressive power as an ordinary semaphore, but only using the values 0 and 1. It
uses a FIFO (First In First Out) policy, that is, the process blocked the longest
is the one that will go first.(see Stallings, 2001)
The way semaphores were implemented for purpose of control in the Scrabble
program is discussed in section 6.3.2.
Who can start and who can join: Each player in the Scrabble game is
assigned a number, like an identification number. The Server class has a variable
called currentPlayer. It is a number which is initialised to 0. The player who
presses the “Start” button first is player 0. Once a game is started a flag is ticked
that does not allow anyone else to start. The boolean variable alreadyPressed
2
French for “traffic light”. Traffic lights control what is going on in the roads in the same
way that semaphores control the events in an operating system.
CHAPTER 6. GAME AND SERVER 51
which is initially set to false, becomes true when anyone presses start. When
anyone else attemps to press started they are not allowed, because it has already
been pressed once.
The case for joining the game is similar. A boolean variable(alreadyJoined )
can be pressed as many times as players are allowed join.3
This is similar to the FIFO policy. Whoever starts first is player 0. The
next one is player 1 and so on. The players number is stored in a variable called
myPlayer.
Which player gets the points and the letters When myPlayer corre-
sponds to the currentPlayer number in the Server, that particular player gets the
letters and the points for the words.
Changing goes: Turns are changed when a player makes a word, or changes
her letters. The following code from the Server class is for a 2 player game:
6.4 Conclusion
This chapter has described the methods relevant to a Scrabble game. The main
methods are kept in a Game object, and the Server dictates when the methods
can be called. The following chapter explains the Client Class, which uses the
methods found in the Server Interface4 .
3
The Scrabble game works for 2 players, but that can be changed by changing the binary
semaphore mechanism to a counting semaphore, but the same player cannot join twice. As
players join they are given a different number.
4
The Server Interface is the list of methods from the server that can be invoked by a client
Chapter 7
Scrabble Game
52
CHAPTER 7. SCRABBLE GAME 53
7.1 Introduction
The previous chapter explained how the Server works. This chapter looks at the
client implementation. The client is represented as a Graphical User Interface
(or Applet). An Applet is based on a coordinate system, wherby the actions
that take place are determined on the position of the cursor on the image. The
possible events from the Client can be summaried as follows:
1. start a game
2. join a game
3. play a word
4. move letters
Before discussing the possible events, an outline of all the components that
make the Scrabble applet is given.
• A start button, which can only be pressed once. This is controlled by the
server. The semaphore approach that I took to control the possibility of
pressing buttons out of turn is explained in section 6.3.2.
• A join button. It can only be pressed once by the second player, and only
if the start button has been pressed.How this is controlled is explained in
section 6.3.2.
• A play button. It can only be pressed once and only if it is your turn.
7.2.1 Board
In the Graphical User Interface the board is an just an image, but what is of
interest is the 2 dimensional 15 * 15 array that it represented. Because an applet
is based on coordinates, the position of the cursor on the board image determines
the particular position pointed at in the grid.
A letter can only be placed on the board if there are no other letters in that
position. Imagine how repetitive, and time-wasting it would be to have to search
through every element in the array. It would mean that anything from 1 to 256
(15 * 15) positions would have to be checked to find out if there is already a
letter there. There is a much better solution. Each square on the board is 38 *
38 pixels1 wide.
Example: Imagine a letter is placed at x = 348 and y = 466
Q: What position on the grid must be marked as taken?
A: Each letter is 38 by 38 pixels. If the x and y coordinates are divided by 38,
the result must be the position in the grid it occupies.
348 / 38 = 9
466 / 38 = 12
The position on the board which is represented by grid[9][12] can now be marked
as taken.
This algorithm is used for marking when a letter is on the board, for checking
if a position is taken, for checking if a letter is outside the board and so on.
initial size:100
1
A pixel (“picture element”) is the unit of display for the computer’s screen (Deitel & Deitel,
2002)
2
This is the method in charge of drawing all the graphics in the applet
CHAPTER 7. SCRABBLE GAME 55
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][D][E][A][N][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
In the previous example the object that is sent to the player is printed out to
the screen. The coordinates are also sent, so that the other players know where
to draw the word. At the end of this run, the program has noticed that it must
replace four letters.
The first letter is positioned at (85, 670) in the board. The next letter is at
((85 + 45), 670) and so on.
Example:
When a letter is moved out of position (130, 670), the second position of the rack
is marked as false:
When the letter needs to be drawn on the 2nd position, the coordinate arrays
are accessed, and the next letter drawn out of the bag is drawn at position x =
130, and y = 670.
Other considerations:
CHAPTER 7. SCRABBLE GAME 58
• If a player attemps to move a letter from the rack onto the board, when a
letter is already at that position in the board, it is automatically returned
to the rack. This position remains filled.
• If a player does not move any letter at all and skips turn, all the letters are
replaced. This is an option in scrabble that is taken when an user does not
like his or her letters. (Games, 1988) As in the real Scrabble, the letters are
replaced to the bag, and the player has as much chance of retrieving those
leteters as any others in the bag.
• If a letter is moved onto the board, and then back to the rack, this position
does not have to be replaced.
• If letters are placed outside the board and outside the rack, the player loses
his turn.
The letter that the user inputs represents one of two blank letters. The
following steps are taken when dealing with blanks.
7.4 Words
7.4.1 Making new words
The first player in the game must combine two or more letters and place them
in such a way that one of them is on top of the “star” tile in the middle of the
board. Otherwise new words can be made in the following ways:
• placing a word at right angles from a word that is already on the board.
• placing a word parallel to a word which is already there (see Games, 1988)
• all the letters around the word have to be checked to get adjacent words
• the new words formed, and adjancent words must all exist. The dictionary
class discussed in chapter 3 is used to check the validity of a word.
CHAPTER 7. SCRABBLE GAME 60
• the x coordinates
• the y coordinates
• the letters
Top to Bottom
For a word to be top to bottom, all the x coordinates must be the same. The
y coordinates are all different, but they must be consecutive once ordered. The
following example is a step by step process using the algorithm.
Example:
Imagine an user playing the word “fire” on a 5 * 5 grid, and placing it in the
following manner:
0 1 2 3 4
0 [ ][ ][ ][ ][ ]
1 [ ][F][ ][ ][ ]
2 [ ][I][ ][ ][ ]
3 [ ][R][ ][ ][ ]
4 [ ][E][ ][ ][ ]
Depending on the order a player moves his or her letters, the lastMoves array
may need to be sorted. Strictly speaking it is not actually sorted. When the
smallest y coordinate, its sucessors are checked. If all the remaining elements are
consecutive, then the user has played a the word correctly. Else the letters are
returned to the rack and the player loses their go.
Left to Right
This method is the same as top to bottom, the only difference being that
in this case the y coordinates are identical, and the x coodinates have to be
consecutive.
This is the text output of a sample run recognising a left to right word:
CHAPTER 7. SCRABBLE GAME 61
Other considerations
It is possible that an user has not played a “full” word. You can add a suffix
or prefix to an already existing word, or add extra letters to make a completely
unrelated word.
The array lastMoves, is keeping track of all the moves in the players turn, and
looked for the smallest x or smallest y coordinate. The search continues either
top and bottom from last moves, or left and right from last moves. The following
example describes a situation when an user plays the letters “ny” horizontally on
a pre-existing word.
Example Imagine a game with 2 players, again on a 5 * 5 board. The grids
below show each of the players last moves. Player 1 has made the word “Fun”,
and the second player has added “ny” to make “Funny”.
Player 1: Player 2:
0 1 2 3 4 0 1 2 3 4
0 [ ][ ][ ][ ][ ] 0 [ ][ ][ ][ ][ ]
1 [ ][ ][ ][ ][ ] 1 [ ][ ][ ][ ][ ]
2 [F][U][N][ ][ ] 2 [F][U][N][N][Y]
3 [ ][ ][ ][ ][ ] 3 [ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ] 4 [ ][ ][ ][ ][ ]
The following table represents the last moves by player 1 and player 2, and how
they are represented in the lastMoves array.
CHAPTER 7. SCRABBLE GAME 62
It can be seen from the table, that the lastMoves array for player 2 only holds
the letters ‘n’ and‘y’. This is not a word, so it has to check if there are any letters
before n and after y that make a word. This must be done for every word an user
plays.
In the above example, the smallest x-coordinate for player 2’s last move is 3.
Is there a letter at grid[2][2]? Yes, the letter is N
Is there a letter at grid[1][2]? Yes, the letter is U
Is there a letter at grid[0][2]? Yes, the letter is F
The word obtained is FUNNY
Adjacent words The Scrabble program is also capable of recognising adja-
cent words. Careful consideration had to be taken when determining the extra
words that a player gets credit for. Normally words must be separated from each
other by a vacant square, in the same style as cross-words. The exception to the
rule is when both words together make a new word.
So when exactly does an adjacent word count as a new word? In the following
example, the first player makes the word “Car”, and gets the appropriate points.
The second player places the next word (“Tin”) in such a way that there is the
extra word “Cart”. Example:
Player 1: Player 2:
0 1 2 3 4 0 1 2 3 4
0 [ ][ ][ ][ ][ ] 0 [ ][ ][ ][ ][ ]
1 [ ][C][A][R][ ] 1 [ ][C][A][R][T]
2 [ ][ ][ ][ ][ ] 2 [ ][ ][ ][ ][I]
3 [ ][ ][ ][ ][ ] 3 [ ][ ][ ][ ][N]
4 [ ][ ][ ][ ][ ] 4 [ ][ ][ ][ ][ ]
N is at position (4,3)
The word is placed vertically, so the x coordinates are identical. To check for
adjacent words it is slightly different. If the word is vertical, it must search for
horizontal words:
1. For every y position, decrement the x coordinate until grid position is blank
2. If all were not blank, store word until grid position is blank.
3. For every y position, increment the x coordinate until grid position is blank
4. If all were not blank, store word until grid position is blank.
In the first run of the example above, the first letter of the word played is T
and is at position (4,1).X must be decremented until it reaches a blank position/
• ...
The following example presents a different case. The user has made the word
“Run”, but she has only played the letters “U” and “N”. Even though there are
still two words made in this example (Car and Run), only points for “Run” are
given to the second player.
The way to deal with this is to not look around every letter. It can only look
around the last letters played, that is the letters found in the array lastMoves
CHAPTER 7. SCRABBLE GAME 64
Player 1: Player 2:
0 1 2 3 4 0 1 2 3 4
0 [ ][ ][ ][ ][ ] 0 [ ][ ][ ][ ][ ]
1 [ ][C][A][R][ ] 1 [ ][C][A][R][ ]
2 [ ][ ][ ][ ][ ] 2 [ ][ ][ ][U][ ]
3 [ ][ ][ ][ ][ ] 3 [ ][ ][ ][N][ ]
4 [ ][ ][ ][ ][ ] 4 [ ][ ][ ][ ][ ]
1. The main reason is to show the other players when a word has been played.
This method is only called when an user presses “Play”, after he or she have
decided on the word. If the word is not on the board, nothing is shown.
7.5.1 ShowPlayer
This section focuses mainly on how the server talks to the client.
Remember that when a word is sent through the server, it is not images that
are being sent, but the letter objects. Remember that a letter object consisted
of three things: a letter character, the point value and the frequency(see section
2.2). This is all that the player needs to know. The option of sending the image
was considered. Having sent the image straight, would mean that all the clients
would have it straight away and could just put them on the board. However, this
turned out to be inefficient, because sending an image takes up more memory, as
each file is 8KB.
The solution was to send a Letter object. In fact, it is not exactly a letter
object, it is a Showplayer object. Showplayer is a class I created for the purpose
CHAPTER 7. SCRABBLE GAME 65
• an array of X coordinates
• an array of Y coordinates
This is all the information that is necessary. Once a client has this information,
the letter image can be retrieved, and drawn on the positions given by the X and
Y coordinates.
ShowPlayer Example
Imagine an user plays the word “COOKIE” like it is positioned in this 6 * 6 grid:
0 1 2 3 4 5
0 [ ][ ][ ][ ][ ][ ]
1 [ ][ ][ ][ ][ ][ ]
2 [C][O][O][K][I][E]
3 [ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ]
The showplayer object sent by the server to all the clients would be represented
like this.
Letters: [C][O][O][K][I][E]
X coordinates: [0][1][2][3][4][5]
Y coordinates: [2][2][2][2][2][2]
In this example, I have only shown the letter character, but not the points or
frequency that are part of the Letter object. The reason to send the whole letter
object, and not just the character is that the point value for a letter needs to be
available to calculate points for adjacent words.
When the client receives this object, it knows that it must look for the image
with the letter “C” and place it at position (0,2). The 2 Os will be drawn at
positions (1,2), and (2,2) and so on.
CHAPTER 7. SCRABBLE GAME 66
7.6 Conclusion
This chapter has provided an outline of the main applet, from where the Scrabble
game runs. It served to describe each of the components individually, and outlined
their way of working. The chapter finishes by giving a call back example, which
allows players to interact. If callback was not availabe it would be very difficult
for players to see everyone’s words on the board.
Chapter 8
Conclusion
67
CHAPTER 8. CONCLUSION 68
8.1 Introduction
Throughout the last seven chapters I discussed all the features of the Scrabble
game and its many possible applications. Its other possible uses will be discussed
in this chapter, and possible ways of expanding the System are suggested. Some
of the problems encountered will be dealt with here, and suggestions for possible
solutions are outlined. This chapter concludes with a report of what was learned
while doing the project.
8.2 Achievements
I can truly say that the goals I set to myself before initialising the project have
been surpassed. The aims that I set to myself have all been completed. There
were unexpected difficulties encountered throughout the project. To get RMI
features working took up most of my time. Servers and Clients was a totally new
concept to me. Client call back, which was discussed in chapter 5.4 was the main
problem. Designing a solution for a two-way communication between servers and
clients is not very frequently used in Java, as any method needs to be called, so
that it is invoked. Information on this mechanism was also hard to find, because
programs don’t usually require a server to talk to a client. When information
was found it was described as follows:
The downside to server-side callbacks is that the code itself (and its
deployment) becomes slightly more complex (Grosso, 2002)
an applet before, so having to design this applet made me learn all about the
facilities that Java provides for Graphical User Interfaces (GUIs). Servers were
a totally new concept, but having used RMI gives me the knowledge to write
efficient distributed applications, which will be very useful.
8.4 Suggestions
The list of possible ways of expanding a Scrabble game could be infinite. This
section outlines some of the ways that the Scrabble game could be expanded.
8.5 Summary
This chapter has described what my achievements and knowledge acquired in
doing this project has been. I have outlined only a few of the many features
that could be added to the Scrabble program. Many possibilities are left open
to programmers for further expansion. The old maxim “It’s easy once you know
CHAPTER 8. CONCLUSION 70
how to” was applicable to some areas of this project”, but overall I am confident
to say that now I do “know how to”.
Bibliography
Deitel & Deitel (2002). Java, How to Program (4th edition). Prentice Hall
International, Inc.
Grosso, W. (2002). Java RMI (1st edition). O’Reilly and Associates, Inc.
java.sun.com (2002). Java Web Page.. last visited 1st May 2002.
Kruse, R., Tondo, C., & Leung, B. (1997). Data Structures and Program Design
in C (2nd edition). Prentice Hall International, Inc.
Niemeyer, P. & Peck, J. (1997). Exploring Java (2nd edition). O’Reilly and
Associates, Inc.
Oaks, S. & Wong, H. (1997). Java Threads (2nd edition). O’Reilly and Associates.
Pitt, E. & Kathleen, M. (2001). java.rmi: The Remote Method Invocation Guide
(1st edition). Addison Wesley.
71
Appendix A
CSLL Webpage
72
APPENDIX A. CSLL WEBPAGE 73
<body>
<IMG SRC="http://www.cs.tcd.ie/wwwresources/tcdcrest.gif" ALIGN=left>
<font COLOR="#FF0000">
<h1>B.A. (Mod.) Degree in Computer Science, Linguistics and a Language</h1>
<HR>
</font>
<BR>
C.A.O. Course Reference Number: German: TR10 <a href="http://www.cs.tcd
<BR>
Academic Year 2000/2001
<p>
The Departments of Computer Science, French, German, Irish and the Centre
for Language and Communication Studies offer a four year honors degree course.
</p>
<p>
This degree program has computational linguistics as its core, with
computer science, linguistics, and language study as constituent
disciplines. As an inherently interdisciplinary program, combining
arts and sciences, it is diverse and stimulating in its subject
matter. This makes the degree challenging, but quite rewarding.
Graduates are competent for employment or postgraduate study in labs
for research and development in language technology. They are also
competent for further study or direct employment in the software and
consulting industries (notably on an international scale), as well as
for further work in their language of choice, and linguistics.
Linguistics is the science of human language; computational
linguistics is the science of language with particular attention given
to the processing complexity constraints dictated by the human
cognitive architecture. Like most sciences, computational linguistics
also has engineering applications.
<p>
In each of the first three years, there will be two software courses,
a mathematics course and the equivalent of two courses in the chosen
language as well as in linguistics (syntax, semantics, applied
linguistics, phonetics, speech science). Students must also spend a
year in a country in which their chosen language is a primary
language. Virtually all students on the degree spend the third year of
the course attending another European University, where they take
courses in Computer Science, Linguistics and Language. While abroad,
students are expected to do a project on the syntax of their language,
essentially developing an analysis that can be turned into an
implemented grammar during the final year if they take the
computational linguistics options course. In the fourth year there
are core courses in the three components of the course and students
select one option from the range offered in any one year. In addition
each student undertakes a substantial final year
project. Interdisciplinary projects will be strongly encouraged.
<p>
The courses in French, German and Irish aim to give students
sufficient competence in their language of focus to be able to operate
in that language in in their future careers. The first two years
concentrate in particular on preparing students for their Junior
Sophister year abroad, where they will study at a university in a
country of their chosen language (students of Irish are prepared for
Scots Gaelic, and spend the Junior Sophister year in Scotland). It
should be stressed that this requires a real commitment to the study
of the language, at a level well beyond the honours leaving
certificate requirement. The end result, however, is a truly
interdisciplinary qualification that is remarkable in its own right,
and which is furthermore highly valued by prospective employers.
APPENDIX A. CSLL WEBPAGE 75
<p>
We aim to be in a position to offer paid summer
<a href="internships.html"> internships</a>
after each year
of study to those students who achieve at least an upper second-class rating
for the year.
</p>
<h2>Admission Requirements:</h2>
<p>
In addition to the College entrance requirements, applicants must have
at least grade C3 in Leaving Certificate Honours Mathematics or
equivalent and grade C1 in French or German, or grade B3 in Irish,
whichever is the chosen language.
</p>
<h3>Application literature can be obtained from:</h3>
<ul>
<li> Central Applications Office,
Tower House,
Eglinton Street,
Galway.
<p>
<li>
<a href="http://www.tcd.ie/Senior.Lecturer/Admissions/home/p0adnhom.html">
Admissions Office, Trinity College, Dublin 2.
</a>
<p>
<li>
<A HREF="http://www.cs.tcd.ie/courses/csll/handbook/hmain0102.ps">
The course handbook for 2001/2002 can be downloaded electronically,
as a postscript file, from this link.
</A>
<p>
<li>
<A HREF="http://www.cs.tcd.ie/courses/csll/handbook/hmain0102">
The course handbook for 2001/2002 can be accessed electronically,
APPENDIX A. CSLL WEBPAGE 76
</ul>
<h2>Timetables 2001-2002</h2>
<p>
<A HREF="http://www.cs.tcd.ie/courses/csll/handbook/hmain0102/node173.html">
The timetables, included in the course handbook for 2001/2002 can be accessed elect
via web pages, from this link.
</A>
<h2>Course Director</h2>
Dr. Carl Vogel
<br>Centre for Computing & Language Studies,
<br>Room 3042, Arts Building,
<br>Trinity College
<br>University of Dublin
<br>D2 Ireland
<br>Telephone +353 (1) 608-2659.
<br>Facsimile +353 (1) 608-3454
<br>e-mail:<A HREF="mailto:vogel@tcd.ie">vogel@tcd.ie</A>
<p>
The CSLL course is made up of the mathematics and software streams of
the Computer Science course combined with streams in Linguistics and a
Language. Courses in the computing science represent 50% of the
degree; linguistics is another 25% and the language of choice a final
25%. Students remain in the same language stream for all 4 years.
</ul>
<h3><a href="sfcsll.html">Senior Freshman</a></h3>
<ul> <a href="sfphotos.html">(meet the 2000/2001 class)</a>
<h4>Computer Science</h4>
<li> <a href="sfcsll.html#2BA1">2BA1</a> Discrete and Continuous Mathematics
<li> <a href="sfcsll.html#2BA2">2BA2</a> Programming Techniques
<li> <a href="sfcsll.html#2CSLL3">2CSLL3</a>
Systems Programming and Natural Language Processing
<h4>Linguistics</h4>
<li> Syntactic Theory
<li> Phonetics/Phonology
<li> Formal Semantics
<li> Experimental Phonetics
<h4>Language</h4>
<li> French
<li> German
<li> Irish
</ul>
<h3><a href="jscsll.html">Junior Sophister</a></h3>
<ul> <a href="jsphotos.html">(meet the 2000/2001 class)</a>
<p> It is a
requirement to spend at least two months in another country with the
primary language of choice. Students of Irish go to Scotland.
Virtually all students to spend the entire Junior Sophister year
abroad at another <a href="http://www.cs.tcd.ie/courses/csll/socrates/csll-socrates
Computer Science, Linguistics and Language.
APPENDIX A. CSLL WEBPAGE 78
</p>
<ul>
<h4>Computer Science</h4>
<li><a href="jscsll.html#3BA1">3BA1</a>
Statistics and Numerical Analysis
<li><a href="jscsll.html#3BA2">3BA2</a>
Artificial Intelligence
<li><a
href="jscsll.html#3BA7">3BA7</a> Software Engineering and Compiler Design
<h4>Linguistics and a Language</h4>
<p>Students take classes in language fluency, the linguistic study
of their chosen language, and courses in theoretical and applied linguistics
as follows:
</p>
<li>Text Linguistics
<li>Language Learning
<li>Sociolinguistics
<li>Lexicology
</ul>
<ul>
<h4>Project</h4>
</ul>
<li> French
<li> German
<li> Irish
</ul>
<p>
In addition they take one option and write a jointly supervised <a href="projects4.
Options may be offered in such areas as:
</p>
<ul>
<li>Structured Design and Database Management <a href="http://www.cs.tcd.ie/courses
<li>Computer Graphics <a href="http://www.cs.tcd.ie/courses/ba/ssba.html#4BA6">4BA6
<li>Compiler Design II <a href="http://www.cs.tcd.ie/courses/ba/ssba.html#4BA7">4BA
<li>Computer Vision <a href="http://www.cs.tcd.ie/courses/ba/ssba.html#4BA10">4BA10
<li>Advanced Computational Linguistics <a href="sscsll.html#4CSLL5">4CSLL5</a>
<li><a href="sscsll.html#CALL">Computer Applications in Language Learning</a>
<li><a href="sscsll.html#GERM">Historical Linguistics</a>
<li>Issues in French Applied Linguistics.
<li>History of the Irish Language
<li><a href="sscsll.html#SLA">Second language acquisition</a>
</ul>
<hr>
Last Modified: Tue Mar 26 19:49:52 2002 (vogel)
</body>
</html>
Appendix B
80
APPENDIX B. CORPUS TRIAL WEBPAGE 81
<html>
<head>
<title> Trial Corpus for Scrabble </title>
</head>
<body>
lkjasdlkjasfdoiuqwpoiueopiasdklnv alskjdslkjasdflkjalkjds
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzz
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
asdjfklkasdjfopiuvbopiunm *************** lakasdwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqq
pouiqwertyuioplkjhgfdsazxcvbnm
</body>
</html>
Appendix C
Sample Run
82
APPENDIX C. SAMPLE RUN 83