You are on page 1of 92

Corpus-Based Scrabble

Tamara Sobrino López


B.A. (Mod.) CSLL
Final Year Project, May 2002
Supervisor: Dr. Carl Vogel
Declaration

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

I would like to thank the following people:

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.

My flatmates for putting up with me.

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

2 Architecture and Design 6


2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 What is Scrabble . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 Scrabble in Java . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.3 The Initial Plan . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 The Representation . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.1 Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

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

5 Servers and RMI 37


5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.1.1 Sockets and Streams . . . . . . . . . . . . . . . . . . . . . 38
5.2 RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.2.1 Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.2.2 Server Interface . . . . . . . . . . . . . . . . . . . . . . . . 39
5.2.3 rmiregistry . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.2.4 Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.2.5 Stubs and Skeletons . . . . . . . . . . . . . . . . . . . . . 40
5.3 Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.4 Client Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.4.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.4.2 The solution . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.4.3 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.4.4 Server callback . . . . . . . . . . . . . . . . . . . . . . . . 44
5.4.5 RMI and Applets . . . . . . . . . . . . . . . . . . . . . . . 44
5.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

6 Game and Server 47


6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.2 The Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.3 The Server Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.3.1 Operating Systems . . . . . . . . . . . . . . . . . . . . . . 49
6.3.2 The Server Class . . . . . . . . . . . . . . . . . . . . . . . 50
6.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
CONTENTS 6

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

B Corpus Trial Webpage 80

C Sample Run 82
List of Figures

2.1 Initial design for a Scrabble game . . . . . . . . . . . . . . . . . . 8


2.2 Examples of random letters . . . . . . . . . . . . . . . . . . . . . 11
2.3 A board full of squares - each with a different value . . . . . . . . 14

3.1 Bubble Sort Process . . . . . . . . . . . . . . . . . . . . . . . . . 22


3.2 Where user enters corpus webpage . . . . . . . . . . . . . . . . . . 24

4.1 Quicksort Example . . . . . . . . . . . . . . . . . . . . . . . . . . 31


4.2 Linked lists v’s Binary Search Trees . . . . . . . . . . . . . . . . . 32
4.3 Binary Search Example . . . . . . . . . . . . . . . . . . . . . . . . 33
4.4 Trie Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

5.1 Multi-tasking in an Operating System . . . . . . . . . . . . . . . . 43


5.2 Outline of RMI System and Call back . . . . . . . . . . . . . . . . 45

7.1 Where the user enters a blank letter . . . . . . . . . . . . . . . . . 58

C.1 A Scrabble Game in Process . . . . . . . . . . . . . . . . . . . . . 83

7
Abstract

This project describes a Java implementation of the world-known Scrabble game.


Gone are the days when the only way of playing a Scrabble was using the board
game. The use of such resources as Java, Applets and RMI provide the program-
mer with a whole word of possibilities. This project outlines how Java is used
to design the main game. Applets allow programmers to design interactive user
interfaces, and RMI provides the facilities for network communication.
Chapter 1

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

Object orientation is a natural way to think about the world, therefore it


seems like a natural way to program. In object oriented programming one thinks
of the world as if it was represented in terms of different pieces. These are called
classes. Classes consist of pieces called methods. These methods have the power
to do things associated with that object, and return information about what has
changed when a method is invoked.
Even though Java is a relatively new language, a lot of its concepts are drawn
from older programming languages such as Smalltalk and Lisp. Although Java’s
syntax has a lot in common with that of C and C++ they are extremely different
in a number of ways that will be described below.
Java is not as fast as C or C++ because it is an interpreted language, but it
is fast enough for interactive, network-based applications.
The way Java manages memory is another huge advantage compared to C or
C++. With Java you do not need to be concerned with pointers and garbage
collection. That is, Java can automatically detect what objects do not to be used
anymore, and can free up the memory space they were taking up. The lack of
garbage collection is known to be the largest source of errors for C and C++
programmers.(see Deitel & Deitel, 2002)

My choice was made - Java

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.

2. To design an user-friendly interface. This is why applets come in handy.


I wanted to follow as closely as possible the rules of the original Scrabble
game.

3. To use RMI for communication between different machines

4. To explore different possible dictionary representations

5. To make the Scrabble game corpus-driven

6. To prevent users from “breaking the rules”


CHAPTER 1. INTRODUCTION 5

Throughout the course of the project adaptations to my initial aims had to be


made. Problems to do mainly with Java Security prevented some of the usefulness
of Java network facilities to work to their full potential. Trinity licensing laws
do not allow users to access webpages outside Trinity College, unless they have
been granted permission. This meant that corpus based features could only work
with Trinity webpages. The problems and how I tackled most of them will be
described in later sections.
I had two options; either I made a one-player version of the game with all its
functionality closely following every rule, or I used the main Scrabble rules but
made it fully interactive. I chose the second option.

1.5 A Look Ahead


Chapter 2 describes the main architecture and design for any Scrabble game.
It describes the initial ideas when I first set out to design the game, and ends by
explaining the choices I made and algorithms used for each of the objects that
represent a game.
Chapter 3 discusses the feature that makes my Scrabble different. My Scrab-
ble is corpus based, that is, it has the capability to look at large amount of text,
and determine letter frequency based on the text.
Chapter 4 takes a look at the theory behind dictionary search strategies and
discusses the most optimal one. The possibilities are in abundance, from arrays
to tries, to linked lists and so on. Various are discussed, with reference to the
way I chose to implement my dictionary.
Chapter 5 takes a look at networking and sockets. I will explain why my
choice of using RMI (Remote Method Invocation) is vital to my project.
Chapter 6 discusses the Game and Server classes for the Scrabble game.
Chapter 7 brings everything together and describes the overall layout and de-
sign of my project. It looks at how the main application (the client class)together
with servers work side by side.
Chapter 8 describes conclusions from my project and what was learned. It
also takes a look at suggestions for future work.
Chapter 2

Architecture and Design

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.

2.1.1 What is Scrabble


Scrabble is a word game for 2, 3 or 4 players1 . The aim of the game is to form
interlocking words in the style of a crossword. Words can only be positioned
vertically or horizontally. Players take turns in drawing letters from a letter bag.
At each turn a player has seven letters and two options; either he or she makes
a word, or they skips a turn, in which case their seven letters can be replaced.
Each player competes for the highest score by using his or her letters in the most
clever way and taking advantage of letter values and premium squares on the
board.

2.1.2 Scrabble in Java


Gone are the days when if one wanted to play a game one had to look for someone
to sit next to for hours. The development of the Internet, which is a combination
of browsers, HTML, scripting languages such as Javascript, Java and so on is
bringing board games to a higher level. I made use of these developments and
implement a computer version of Scrabble.
The Java version of Scrabble is based on the board game version and imple-
ments its most important rules. Listed are some of the major rules and features
implemented in this version:

• Players are not allowed play out of turn.

• First word must cover center tile.

• Words are read from left to right and top to bottom.

• If a player skips their turn they get 7 new letters.

• 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

2.1.3 The Initial Plan


As was mentioned in the previous chapter Java is an Object Oriented program-
ming language. It seems like a natural way to talk about the world, because in
the world there are many objects of the same type. For example, there is a type
cat, but many instances of a that type, including Garfield, Felix and many more.
In a Scrabble game there is a type Letter, but 100 instances of that type. There
is a type Square, but 225 instances of that type. On top of that, each object
has charateristics of the same kind. A class or object allows the programmer
to make a prototype that defines variables and methods common to one certain
type. There are certain objects that come straight to mind when one thinks of a
Scrabble game. Now I had the basic idea in my head, I had to decide on a design,
which looked pretty much like that on Figure 2.1. (Note: The up arrow means
“is made up of”). It can be seen that:

• a letter bag object consists of letter objects

• a board object consists of square objects

• a rack object consists of tile objects

• a game object has player objects and a dictionary object

• a player object has a rack object

Letter Bag Players

Letter Rack
Scrabble
Game

Board Tiles

Squares Dictionary

Figure 2.1: Initial design for a Scrabble game


CHAPTER 2. ARCHITECTURE AND DESIGN 9

2.2 The Representation


In this section I will give a description of each of the objects, outlining the
attributes and methods that make them up. Each of the classes can be viewed in
Figure 2.1. Note that the outline of each of the classes here describes the object
by itself. It does not go into any detail of how they combine together to make a
game (will be described in Chapter 6) or how the Server and Client manage the
different objects (this will be discussed in Chapter 5).

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

1. Constructor 1: In an array are stored the 26 elements of type letter. Each


letter is of type Letter which as explained above contains a character for each
of the letters, a frequency and a value. After specifying the letters and their
frequencies, the letter bag must be created. A letter bag is stored as a vector,
which is like a dynamic array that can increase and decrease in size2 . This vector
is calculated from the frequency specification for each letter.

Example
The five first letters of the alphabet are stored in the first 5 positions in the
all bag array.

all_bag[0] = new Letter(‘A’,1,9);


all_bag[1] = new Letter(‘B’,3,2);
all_bag[2] = new Letter(‘C’,3,2);
all_bag[3] = new Letter(‘D’,2,4);
all_bag[4] = new Letter(‘E’,1,12);

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: []

- frequency for letter ‘A’ = 9. Add 9 As.

vector: [A][A][A][A][A][A][A][A][A]

- frequency for letter ‘B’ = 2. Add 2 Bs.

vector: [A][A][A][A][A][A][A][A][A][B][B]

-frequency for letter ‘C’ = 2. Add 2 Cs.

vector: [A][A][A][A][A][A][A][A][A][B][B][C][C]

-frequency for letter ‘D’ = 4. Add 4 Ds.

vector: [A][A][A][A][A][A][A][A][A][B][B][C][C]
[D][D]

-frequency for letter ‘E’ = 12


2
For the purpose of this Scrabble game, the vector is not allowed to have more than 100
elements, or a negative value
CHAPTER 2. ARCHITECTURE AND DESIGN 11

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.

2.Constructor 2: Letter frequencies assigned based on corpora the user chooses.


For further discussion consult section 3.5.1.

Generating Random Letters


Scrabble is a game where both luck and skill play a part. The element of luck is
introduced by the random letters the user gets.
I make use of the method random from class Math in the Java library. This
method generates a random number from 0.0 up to, but not including 1.0. (see
Niemeyer & Peck, 1997) Every value has an equal probability of being chosen
each time the method is called. I adapted this method to generate a number
from 1 to the size of the letter bag. Initially this will be a number between 1
and 100, but as the game proceeds, the upper bound will progressively become
smaller.
There is only one random letter generated at a time, because the number of
letters an user needs depends on the number of letters she or he has played.
The process to get random letter from the letter bag is the following:

Figure 2.2: Examples of random letters

1. generate a random number


2. retrieve the letter in the position in the letter bag corresponding to the
random number
CHAPTER 2. ARCHITECTURE AND DESIGN 12

3. decrement the size of the letter bag by 1


Method: Letter randomLetter() Returns: the random Letter.
/*Math.random generates a number between 0 and 1. This is multiplied
by the size of the letter bag to get a proper array position
store this in variable i */

int i = ((int)(Math.random() * bag.size()));

/*Access the given element inside the vector*/


Letter l = (Letter)bag.elementAt(i);

/*Remove it from the bag*/

bag.remove(i);

Putting letters back in the bag


As will be discussed later on in this section, the method to give letters to a player
belongs to the Player class. It is the player that will end up with the letters. On
the other hand, the method to return letters to a bag belongs in the LetterBag
class. It is inside the bag that the letters will end up. The only time that this
method needs to be used is when a player decides to skip his or her turn because
they don’t like the letters that are currently on their rack.
Method: putLetterBack(Letter[]3 rack) Returns: void - the result is the
seven letters of the rack are put back into the bag.
/*Go through every element in the player’s rack*/
for(int i = 0; i < rack.length; i++){

/*insert the element back into the end of the bag


the last position in the vector is defined by bag.size()
if there are 72 elements it will be inserted at position
72*/
bag.insertElementAt(rack[i], bag.size());
}

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

• taken - This is either “true” or “false” depending on whether there is a


letter positioned in that particular square.

• 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.

/*the squares that have double letter score (1)*/


grid[3][0] = new Square(false, 1, null); //[D][1]
grid[0][3] = new Square(false, 1, null); //[A][4]
...

/*the squares that have double word score(2) */


grid[1][1] = new Square(false, 2, null); //[B][2]
grid[2][2] = new Square(false, 2, null); //[C][3]
...

/*the squares that have triple letter score(3) */


grid[1][5] = new Square(false, 3, null); //[B][6]
grid[1][9] = new Square(false, 3, null); //[B][10]
CHAPTER 2. ARCHITECTURE AND DESIGN 14

...

/* the squares that have triple word score (4) */


grid[0][0] = new Square(false, 4, null); //[A][1]
grid[0][7] = new Square(false, 4, null); //[A][8]

Figure 2.3: A board full of squares - each with a different value

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:

• letter - An object of type Letter

• filled - Can be either “true” or “false” depending on whether there is a


letter in that position at any given time.

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:

/*Go through all 7 elements*/


for(int i = 0; i < 7; i++){
/*For each element check if it is filled*/
if (rack[i].filled == false){

/*If it is not filled (i.e. if filled is false)


if( server.getLetterBag().getSize() > 0){

/*count keeps track of the number of places we must replace*/


count++;
...
...
/*Get a letter from the server and give it to myPlayer
myPlayer is the player that asked for the letter.
The letter comes from the server (see chapter 6)
because all players share an uniqule letter bag*/
rack[i] = server.putPlayerLetters(myPlayer);
rack[i].filled = true;

/*Prints to the screen the number of letters the user needs*/

System.out.println("We must fill "+count+" positions");


}
}
}
CHAPTER 2. ARCHITECTURE AND DESIGN 16

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

• rack - an array of 7 Tiles.

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.

setPlayerLetters(LetterBag b) This method gives each player letters. It


is called when players start or join a game. It is also called when a space in
the rack is empty. The exception to this is towards the end of the game, when
there are not enough letters to fill the empty spaces in the rack. In this case the
player gets as many letters as possible. Where a letter can be taken from must
be specified, therefore it takes in the shared letter bag as a parameter. Returns:
A tile which is true when it is positioned in the rack, and has a random letter.
getPoints(Letter[] word) This methods calculates the points for a word
that is passed in as a parameter. A word is represented as a Letter array. Each
letter has a value associated with it. To calculate the score the values for each
letter are added up, and the extra points given for the position on the board.
This is added to the score a player already has from his or her previous turns.
Returns: The player’s score.

/*Go through every letter in the word*/


for(int i = 0; i < word.length; i++){

/*if there is a letter*/


if(word[i] != null)

/*add the score to the previous letters scores*/


score = score + word[i].getValue();
}

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.

3.2 What is a Corpus


A corpus is defined as

A collection of writings or recorded remarks used for linguistic anal-


ysis(dictionary.com, )

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:

1. Getting the corpus from the webpage

2. Counting the letters

3. Sorting the letters

4. Assigning the frequencies

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

CD-Rom Vowels Scrabble Express Vowels My Scrabble Vowels


YSYACLE 2 EJMRUZB 2 ETOEEAN 5
ORCCOIR 3 NQWZCGJ 0 I LERNP 2
EMLEES 3 ZBEILPS 2 OEDAOFT 4
VLMIEAF 3 U EHLOS 3 RNNOMWT 1
ODBVTAA 3 IMPU EH 3 IITVFLC 2
Total vowels 14 Total vowels 10 Total vowels 14

Table 3.1: Different vowel distribution in 3 different Scrabble games

only once in a standard Scrabble game, so the students would be impatiently


waiting to be the one to draw it out of the bag, and get the chance to learn a
word with ‘Q’.
It is easy to see how the project could be expanded for various language
teaching activities, not only in English, but in any other language. To take
an example, the Spanish version of Scrabble has a different letter distribution.
Firstly, the letter ‘K’ and ‘W’ do not exists, but there are new letters that are
only found in Spanish such as ‘Ll’, ‘Ch’, ‘Ñ’ and ‘RR’. The letter ‘Q’ is much
more frequent in the Spanish language, and is only worth 5 points. The number
of letters remains constant, at 100. Using the Corpus based features, and the
class Process which I designed, it is easy to imagine how it could be adapted for
foreign language teaching. There is the issue of representing letters that do not
appear in the corpus. This is discussed in section 3.4.4.
Last but not least, it makes a change from the standard Scrabble. Charles
Robinove (see Robinove, 2002), a retired geologist, carried out a study in which
he compared the Hasbro Scrabble Express handheld electronic game to a CD-
Rom Scrabble game. Robinove noticed that the tile draw was not statistically
random. He compared the first seven letters drawn from the hand-held game
with those of his PC version, 100 times each, and calculated the frequency of
drawing each letter. Experiment 1:
As a test, I decided to compare the first 5 runs of my Scrabble to those of Robi-
nove’s CD Rom and Scrabble Express.

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.

3.4.2 Counting the Letters


When scanning a corpus only lower case and upper case letters are relevant. The
count is stored in a 26 element array and every time a letter is encountered the
count for that particular element in the array is increased by one. Everything
that is not a letter is ignored. It is easy to imagine how in an extension of my
system this method could be adapted to recognise characters that appear in other
languages, and not English. It could have a list of punctuation signs, numbers,
and anything that is not a letter in English. Everything that is not to be found
in that list would probably be a character in another language.

3.4.3 Sorting the Letters


Now that we have processed all the file, and got the exact number of instances
for each letter, we need to give them values. But before this can be done, we
need to know which is the most frequent and the lest frequent letters. A bubble
sort function was applied to the letter array, until it was in ascending order of
number of occurrences.

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...

J 4 3 2,1 4 3,2 ...

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

Figure 3.1: Bubble Sort Process

3.4.4 Assigning the frequencies


The approach I took in assigning the frequencies was to keep the exact same
distribution that is used for the standard Scrabble, but applied to different let-
ters. For example, the letter ’K’ is the 22nd most frequent letter in the English
language. If you play a ’K’ you will add 5 points to your score. When creating
the corpus based letter bag, the 22nd more frequent letter will give you 5 points
and there will be 1 occurrence of it in the bag. The table below shows the dis-
tributions that will be applied to the sorted array. If a letter does not appear in
the corpus, it will be given very high points, but a small frequency. The method
to assign values uses all the letters in the English language to determine the
frequencies from Table 4.2, so it is not necessary that all letters appear in the
corpus.
CHAPTER 3. CORPUS BASED 23

Order Frequency Points


1st 12 1
2nd 9 1
3rd 9 1
4th 8 1
5th 6 1
6th 6 1
7th 6 1
8th 4 1
9th 4 1
10th 4 1
11th 4 2
12th 3 2
13th 2 3
14th 2 3
15th 2 3
16th 2 3
17th 2 4
18th 2 4
19th 2 4
20th 2 4
21st 2 4
22nd 1 5
23th 1 8
24th 1 8
25th 1 10
26th 1 10

Table 3.2: Scrabble Frequency Distribution


CHAPTER 3. CORPUS BASED 24

3.5 Example Corpus


When an user starts the game, a pop up window gives him or her the option to
enter a webpage. The user an option to enter a webpage, or to press Cancel, in
which case they will get the standard letter bag straight away.
To test the corpus based feature, and fully appreciate its features, I tested
various webpages with diverse letter distributions, and created a few webpages
in which the most frequent letters were Qs and Zs. One of them is found at:
matrix.netsoc.tcd.ie/∼tamara/corpus.html

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:

Figure 3.2: Where user enters corpus webpage

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

A:601 B:86 C:372 D:202 E:823 F:165 G:212 H:336 I:603

J:26 K:24 L:410 M:184 N:520 O:429 P:181 Q:12 R:476 S:540

T:625 U:267 V:47 W:80 X:11 Y:101 Z:1 _:0

Compare the count above with the webpage I created with many Qs, Zs
and Ws (see Appendix B).

A:11 B:4 C:1 D:12 E:2 F:5 G:1 H:2 I:7

J:9 K:46 L:11 M:3 N:3 O:9 P:6 Q:87 R:1 S:11

T:2 U:6 V:3 W:110 X:1 Y:3 Z:21 _:0

(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

• assignValues(Letter[] letter array) This method calls a sorting method


explained below. And then applies the frequencies from Table 4.2. By de-
fault a letter bag has 100 pieces. After applying this method there are 2
left. These are blank pieces that can be used as whatever the user chooses.
Return: void

• sort(Letter[] letter array) This method takes as a parameter the Letter


array that needs to be sorted. It sorts the array in ascending order after
applying insert sort to it.
Return: void

(E,1,12) (T,1,9) (I,1,9) (A,1,8) (S,1,6) (N,1,6)

(R,1,6) (O,1,4) (L,1,4) (C,1,4) (H,2,4) (U,2,3)

(G,3,2) (D,3,2) (M,3,2) (P,3,2) (F,4,2) (Y,4,2)

(B,4,2) (W,4,2) (V,4,2) (J,5,1) (K,8,1) (Q,8,1)

(X,10,1) (Z,10,1) (\_,0,2)


CHAPTER 3. CORPUS BASED 26

3.5.1 Letter Bag Corpus-Based Constructor


The letter bag constructor is called when the user decides to input a webpage. If
he or she does not input a web page, the ordinary letter bag constructor will be
used. The constructor goes through all the previous steps. It takes in an URL,
takes out all the tags, counts all the letters and makes a new letter bag with all
the different frequency assignments.

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.2 Searching Strategies


Information retrieval is one of the most important application of computers.
When given a word, or a key, associated features must be retrieved. For the
purpose of this dictionary representation a word is given as input, and the result
is a mapping to a boolean value. The value “true” is returned if the word does
exist in the dictionary, or “false” if the word was not found. In other dictionary
representations other information might be needed, such as its part of speech,
or the word definition. However, for the purpose of this project it is sufficient
to know whether a word was found. This is a binary decision, with a true or
false outcome. One could imagine a setting between ‘true’ and ‘false’, such as
‘probably true’. This setting could refer to a search for a word that uses, say, the
suffix ‘s’. The word “dogs” might not be in the dictionary, but “dog” is1 . Dog is
a noun, so its plural is made by adding ‘s’, and should be considered a word.
As mentioned above in a search a key is used, which is the piece of information
by which a record that contains other associated information is retrieved.
Searching through the keys is very time-consuming, so the choice of method
to search a structure makes a big difference is the program’s performance. The
easiest way to do a search is to do it sequentially, that is start at the beginning
and finish at the end. This is OK if the list has few elements, but if it is necessary
to deal with something as big as a dictionary other options must be considered.To
use other search strategies keys must be sorted in some way, so before discussing
other strategies, an outline of sorting is given.
1
the word “dogs” partially matches “dog”
CHAPTER 4. DICTIONARY STORING 29

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.

Two properties of sorting strategies are critical in choosing a sorting strategy:


1. Number of comparisons
2. Number of data movements
The efficiency of these two properties depends on the size of the data set.The
length of an average dictionary must be taken into account. The Oxford English
Dictionary has around 500,000 words. This is a rather large amount of data, and
the choice of sorting strategy needs to be considered carefully.
In chapter 3, Bubble sort was introduced. Bubble sort is one of the simplest
ways to sort an array, but unfortunately it is quite slow. An array is sorted in
O(n2 ) time, where n is the number of elements.(see Kruse et al., 1997) Because
bubble sort compares two neighbouring objects, and swaps them if they are in
the wrong order, the process is quite repetitive. Quicksort is an example of an
optimal, recursive sort. It is discussed in the following section.
CHAPTER 4. DICTIONARY STORING 30

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:

1. pick the partition point(pivot)

2. place pointers at the

• beginning
• pivot
• end

3. scan up from left and compare to pivot

4. stop when an element bigger than pivot is reached, or it comes to the end
pointer.

5. scan up from right until an element smaller than pivot

6. now swap!

7. quicksort the left and right separately(repeat steps 1 to 6)

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.

4.4 Storing and Searching


In this section various ways of storing and searching for a word in a dictionary
are outlined. There are 3 main options.
CHAPTER 4. DICTIONARY STORING 31

a) 4 2 3 5 1
pivot
start end

b) 1 2 3 5 4

c) 1 2 3 4 5

Figure 4.1: Quicksort Example

1. The dictionary can be stored as an array, an array of arrays, or a vector,


arranged in alphabetical order, and use different search strategies to find a
word.

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

Matt Ann Tom Kim

Figure 4.2: Linked lists v’s Binary Search Trees

option. For this reason, a dictionary tree representation will not be shown here,
but tries will be discussed in section 3.6.

4.5 Binary Search


There has been an outline of how to search through an array element by element.
This is not very realistic. To relate this to the dictionary representation problem,
when someone is looking for a word in a dictionary, they don’t start from page
one. They usually get the dictionary, open it around half way, decide whether the
word is left or right from the halfway mark, and keep on looking on the correct
half. This is the basic principle of binary search. This algorithm only works if
any element of the array can be accessed without having to traverse through all
of them; this ability is called random access. This is no problem with arrays in
Java.
In binary search, the array is constantly being halved. The element being
pointed at is first compared with the item in the middle position of the array. If
there’s a match, it returns it immediately. If the element is less than the middle
key, then the item being searched for must be in the lower half of the array; if
it’s greater then the item must be in the upper half of the array. The procedure
CHAPTER 4. DICTIONARY STORING 33

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

No. elements 9. 9 / 4 = 4 (.5)


Pivot = 4.
if (need > pivot) search right
if (need < pivot) search left
else need == pivot
7 < 19 = search left & ignore right
5 7 8 10

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 word in a trie, therefore not a word in the dictionary. A graph is searched in a


depth-first mechanism, each element being scanned from the root to the leaf.

4.6.1 Looking for a node in a trie


Scanning a trie is very efficient, because sharing word prefixes means that it
does not take up much memory, and search is fast. It is also possible to take
into account suffixes to make optimal use of memory space. However, most trie
implementations only deal with prefixes, therefore this section deals solely with
prefix tries. Section 3.6.3 discusses suffix tries.

a c

n
a u

t r t p

t #
# # #

Lexicon: ant, car, cart , cat, cup.

Figure 4.4: Trie Example

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.6.2 Making the trie


Adding a new key to a trie is similar to scanning the trie. The different levels are
searched through, until the appropriate point is reached. If there is no branch
or a NULL branch the new nodes are created and put into the trie in order to
complete the word. Each word is finished with ǫ.

4.6.3 Why a trie


The number of steps required to search a trie, or making a trie is proportional to
the number of letters making up the word. This means it is faster than searching
through a binary tree.
Example:
For example, taking 5 letter words, there can be a possible n = 265 =
11881376 nodes to search. A trie will find the word in 5 iterations. A binary
tree on the other hand will take a minimum of log n ≈ 23.5 comparisons.(see
Kruse et al., 1997)
It is possible to make a trie slightly more efficient by looking at suffixes as well
as prefixes. However there is a tradeoff between the time it takes to be stored in
memory, and its efficiency. Storing a trie in memory, dealing only with suffixes is
a gradual process. It is not necessary to store patterns of suffixes in order to know
which are the common ones. Prefixes are sorted alphabetically, therefore much
easier to implement. Thus, while a DAG4 encoding common suffixes as well
as common prefixes would have the search time advantages of tries, and even
greater space savings, the construction time is more complicated. Implementing
this trie was considered, but for expediency in delivering a prototype system, my
implementation makes use of the binary search function Java offers.

4.7 The Dictionary Class


Now that an outline has been given of various sorting and searching strategies,
it is time to discuss the implementation for the dictionary class in the Scrabble
program(see section 2.2.1). For the purpose of this project I implemented the
dictionary as a vector, loaded into memory from a file. The dictionary file consists
of an already ordered list of English words. A word is searched for using a binary
search method. The dictionary used contains just below 40000 words. (for source
see dictionary, )
setUpDictionary(String s, Vector v): This method takes in a file name,
reads the file, and stores every word in a vector. The size of the vector depends
on the number of words that are found in the dictionary.
4
Directed Acyclic Graph
CHAPTER 4. DICTIONARY STORING 36

lookUpWord(Vector v, String word): This method takes in the dictio-


nary as a parameter, and looks for a particular word. The word is the last word
that the player has made. The method return a boolean value specifying whether
the word has been found or not.
One of the reasons for choosing this method is that it is easy to implement
and the dictionary is small enough for binary search to be very efficient. A word
does not usually need to be looked up unless a player requests for it to be done,
hence this algorithm is not used very often. Making a big trie representation
does not seem necessary, but this option is left open for possible extensions of my
system.

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

Servers and RMI

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.

5.1.1 Sockets and Streams


The JavaTM Tutorial gives the following definition(see java.sun.com, 2002):

A socket is one endpoint of a two-way communication link between


two programs running on the network. A socket is bound to a port
number so that the TCP layer can identify the application that data
is destined to be sent.

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.

It provides a framework within which Java objects in distinct Java


virtual machines (JVMs) can interact.(Pitt & Kathleen, 2001)

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.2 Server Interface


The Server Interface is a list the methods that are defined in the server. All
the methods declared in the interface can be called from a client on the server,
and the client gets back their return values. If a method is not listed in the
ServerInterface class, it may not be called. Section 6.3.2 discusses some of the
methods found in the Server Interface for the Scrabble program.

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.

/*With the rebind method sun39 is added to the server list in


the rmiregistry*/

Naming.rebind("//sun39.cs.tcd.ie/myServer", server);

This is part of the code in the Client class that looks for a particular server.

/*The lookup method of class Naming looks for a particular


server in the registry*/

server = (ServerInterface) Naming.lookup("//sun39.cs.tcd.ie/myServer");


server.register(this);

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.

5.2.5 Stubs and Skeletons


A stub is defined as

a client-side object that represents a single server object inside the


client’s JVM3 . (Grosso, 2002)

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

a server-side object responsible for maintaining network connections


and marshalling and demarshalling data on the server side.(Grosso,
2002)

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.

5.4 Client Callback


5.4.1 The problem
One of the main problems I came across while doing the project was that of a
server talking to the client, without being asked to. If a player made a word,
CHAPTER 5. SERVERS AND RMI 42

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.2 The solution


One can imagine various solutions to this problem. I opted for using something
known as Callback, but first I will look at a possible solution using threads.

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

Application 1 Local Memory

Application 2 Local Memory

Application 3 Local Memory

Shared Memory

Figure 5.1: Multi-tasking in an Operating System

Threads
The JavaT M tutorial defines a thread as:

A single sequential flow of control within a program.(java.sun.com,


2002)

Thread is a shorthand for “thread of control”, which is a section of code exe-


cuted independently of other treads of control in a single program. Threads allow
a program running on a computer to perform more than one task at the same
time, in the same way as an operating system allows more than one action to
happen simultaneously. Threads are really important in programming, so many
features of the threaded system are built into the language itself. In Java pro-
grams threads are often used for adding time features to games, or any program
where time matters. Time using threads is discussed in chapter 8.
It is easy to see how threads would be useful for the the Scrabble application.
If someone wants to play a game while, for example, a counter keeps track of how
long and user takes in playing a word. This possibility is discussed in chapter 7.
For the moment I will focus on the possibility of a thread running, while users
CHAPTER 5. SERVERS AND RMI 44

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

5.4.4 Server callback


The other possible solution involves having an RMI server inside each client
application. The main server receives references to each of the client servers and
uses them to call back the appropriate clients. The steps are the following:

1. The client application (the Scrabble class) makes a remote invocation on


the server application. Along with this invocation it passes a reference
to the RMI server inside that application. This is stored in and array
called storeClients, because this program must keep track of more than one
clientServer. There will be as many clientServers as players there are in a
game. Each new client is given a different clientServer name.

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.

5.4.5 RMI and Applets


Appletviewers5 webpage(see and web browser impose various restrictions on ap-
plets. Here is a list of restrictions (see Pitt & Kathleen, 2001)

• it cannot loads libraries or define native6 methods

• it cannot read or write files on the host that is executing it


5
applet container for embedding applets that acts like a web browser(see Deitel & Deitel,
2002)
6
A method whose implementation is written in another programming language(see Deitel
& Deitel, 2002)
CHAPTER 5. SERVERS AND RMI 45

RMIREGISTRY

Server
Server Interface

Skeleton

Stub Stub

Client Server Client Server

ClientServer Interface ClientServer Interface

Client Client

Register and look up


Client Server Communication
Call back (Server Client Communication)

Figure 5.2: Outline of RMI System and Call back

• it cannot make network connections except to the host that it came from.

• it cannot start any program on the host that is executing it.

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;
};

Granting AllPermission saves the programmer from having to enumerate ev-


ery type of persmission one may wish to grant. However The JDK 1.3 docu-
mentation states that “This permission should be used only during testing, or
in extremely rare cases where an application or applet is completely trusted and
adding the necessary permissions to the policy is prohibitely cumbersome”(Pitt &
Kathleen, 2001) AllPermission was chosen because of time factors. I tried every
possible way to get the game working from different machines, but all methods
failed. When I learned about java policies, using AllPermission was my best bet.

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

Game and Server

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.

6.2 The Game


When thinking about a Scrabble game, it is easy to group actions into two groups;
those that belong to the players and those that belong to the game. For example,
making words is done by players individually, but giving the letters to players is
done by the game, which is in the server. Initially I set about designing a game
for one player, in which all the main methods were in the same class. Each main
class had:

• 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.

• Creating one of the two letter bags:

– standard letter bag


– corpus based letter bag
CHAPTER 6. GAME AND SERVER 49

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)

• Starting a game. When a game is started a new player is added to the


game.1 A game can only be started if no one else has already started it.
The Server controls the actions that can be done at a given time, and will
be discussed in the following section.

• 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.

6.3 The Server Class


This section starts by giving a brief outline of what an operating system is, and
how it works. There is an analogy between a feature of operating systems called
semaphores, and the way the server controls who can do what. This section
compares the common features in both processes.

6.3.1 Operating Systems


As was explained in the previous chapter, an operating system is a system pro-
gram which controls all the computer’s resources and provides the base upon
which the application programs can be written. There needs to be an order in
which things can happen in an OS, some form of order.
The analogy - Semaphores In a Scrabble game, players take turns in plac-
ing letters on the board. During a particular player’s turn, no other player can
interfere with any section of the game. While player A is deciding on a word,
player B cannot change her letters, or move the letters on the board. This is
1
Only if the game is not already full
CHAPTER 6. GAME AND SERVER 50

analogous to semaphores2 in operating systems. Two or more processes can op-


erate by means of signals, and a process can be forced to stop at a specified place
until it has received a specific signal. The variables used for this signaling are
semaphores.
To transmit a signal via semaphore s, a process executes the primitive sig-
nal(s). To receive a signal via semaphore s, a process executes the primitive
wait(s). There are three things that can be done with a semaphore:

1. It can be initialized to a negative value.

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.

6.3.2 The Server Class


The server class is responsible for the following control features:

1. Who can start a game.

2. Who can join a game.

3. Whose turn it is to play.

4. Which player gets the points.

5. Which player gets the letters.

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:

/*This method is called after a player takes his go*/


public void changeGoes(){
/*if it was the first player’s turn - now its the second player’s turn*/
if (currentPlayer == 0) currentPlayer = 1;
/*else its the first player*/
else if (currentPlayer == 1) currentPlayer = 0;

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.

7.2 Applet components


An applet works on a coordinate basis. What action has taken place or can take
place depends on the position of the cursor on the different buttons, or images.
Represented in the interface are all of the following

• Board: This is an image, that represents a 15 * 15 array corresponding to


the Scrabble board of squares.

• 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.

• Rack: image that represents a 7 element array


CHAPTER 7. SCRABBLE GAME 54

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.

7.2.2 Starting a game - The “start” button


When someone presses start, a new player is added to the game. He or she needs
7 letters from the server. The letters come from the same letterbag. It is not
the letter images that are sent, but the letter object. Once a Letter object is
received, the image file has to be retrieved. A Letter object is one consisting of
a character, points and frequency, as seen in the following example. An image is
a .gif file.
Example Imagine the following letter object
letter = (‘A’, 1, 9 )
To get the image the letter character must be obtained, and subsequently the
image corresponding to that characther. In this example the letter is ‘A’, so the
paint method2 must retrieve the image called “A.gif”. This process happens for
any letter taken out of the bag.
The following text is the written output of a sample run when start is pressed.

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

This is the player :0


This letter Y
The bag now has 99 letters
This is the player :0
This letter T
The bag now has 98 letters
This is the player :0
This letter E
The bag now has 97 letters
This is the player :0
This letter O
The bag now has 96 letters
This is the player :0
This letter N
The bag now has 95 letters
This is the player :0
This letter T
The bag now has 94 letters
This is the player :0
This letter A
The bag now has 93 letters

7.2.3 Joining a game - the “join” button


When a player joins a game, the process is pretty much the same, because all of
the positions in the rack need to be filled. There does not need to be any checking
for empty spaces, 7 letters must be retrieved and placed in every position of the
run. Here is an example run of when the second player joins a game. The bag
only has 93 letters, because the first player has already started. At the end it
will have 86 letters.
The following code is from a sample run when “join” is pressed.

Player number 1 is playing now


This is the player :1
This letter S
The bag now has 92 letters
This is the player :1
This letter D
The bag now has 91 letters
This is the player :1
This letter O
The bag now has 90 letters
CHAPTER 7. SCRABBLE GAME 56

This is the player :1


This letter G
The bag now has 89 letters
This is the player :1
This letter R
The bag now has 88 letters
This is the player :1
This letter I
The bag now has 87 letters
This is the player :1
This letter F
The bag now has 86 letters

7.2.4 Playing a word - the “play” button


When a word is played, the feautres associated with that word are stored in
a ‘Showplayer object. (Showplayer is described in Section 7.5.1, for the time
being it is only necessary to know that a showplayer is made up of letters and
coordinates). The position corresponding to the letter that is moved out of the
rack must be marked as false, so that it knows how many must be replaced. The
example below explains the step by step process for replacing letters after a word
is played.This example corresponds to an actual run of the program , and the
text below is the output of the run. The user has played the word “Dean”. (Note
that the output is the grid corresponding to the image board, and not the image
itself. Java offers multiple possibilities of creating user-friendly graphic interfaces.
I chose to exploit all it was offering and make the interface as user friendly and
colourful as possible)
Example:

This is the object we are sending to the other player:


0 D
1 E
2 A
3 N
The word you have made is :DEAN
Score for : DEAN is: 5
Player 0 you have 5 points for in total
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
CHAPTER 7. SCRABBLE GAME 57

[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][D][E][A][N][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

We must fill 4 positions

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.

7.2.5 The Rack


Two arrays, curX and curY hold the initial positions for the 7 letters that make
up a rack. Here is the code that sets the positions.

/*for the 7 elements in the rack*/


/*the x coordinates start at 85, and are incremented by 45 for
every letter that is placed on the rack*/
/*Each letter is 38 * 38 pixels*/

for (int i = 0,x = 85; i <7; x = x+45, i++){


curX[i] = x; //stores the x coordinates
curY[i] = 670; //stores the y coordinates
}

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:

[TRUE] [FALSE] [TRUE] [TRUE] [TRUE] [TRUE] [TRUE]

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.

7.3 Blank Letters


In a Scrabble game there are two empty tiles. These can be any letter the player
needs, it acts like a joker. Playing a blank tile does not give the player any points,
but does give them the opportunity to make a word that would otherwise have
not been possible in that turn. Even though the blank tile represents a letter
of the user’s choice, once the letter has been decided on, it cannot change. It is
necessary to inform the other users of the letters the blank represents.
When an user moves the blank piece, a pop up window like the one in Figure
7.1 appears.

Figure 7.1: Where the user enters a blank letter

The letter that the user inputs represents one of two blank letters. The
following steps are taken when dealing with blanks.

1. ask the user for the letter


CHAPTER 7. SCRABBLE GAME 59

2. if no blanks are filled this letter is stored as blank1

3. else the blank is stored as blank2

4. blank sent to the server

5. server communicates with clientServers(chapter 5).

6. all clients informed of letter

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:

• adding one or more tiles into words already on the board.

• 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)

7.4.2 Getting the words


Recognising the word and user plays is a big task in the program. Letters need to
be recognised for various reasons, such as getting the points, and checking that it
is a word in the dictionary. There are various factors to take into account when
recognising words. An user can mess up a game very easily. Here is a list of some
of the things that were taken into consideration:

• words can only go vertically or horizontally

• no letters can be placed outside the word

• no letters that were previously can be moved

• 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

7.4.3 Checking that words are in line


There are two options for a word, it can go from top to bottom, or from left to
right.
Each letter in the scrabble board is represented as a full position on a grid.
When an user moves a letter inside the applet, the x coordinate and y coordinate
are retrieved, and grid position is calculated, as is explained in section7.1. The
program must keep track of the movements an user makes. These are kept in an
array, let’s call it lastMoves that keeps track of:

• the x coordinates

• the y coordinates

• the letters

that the player uses in each go.

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

The y coordinates are equal


The word you have made is :NEWS
Score for : NEWS is: 7
Player 0 you have 7 points for in total
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][N][E][W][S][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

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

Letter1 X-coord1 Y-coord1 Letter2 X-coord2 Y-coord2


F 0 2 N 3 2
U 1 2 Y 4 2
N 2 2

Table 7.1: The last moves for player 1 and player 2

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 [ ][ ][ ][ ][ ]

Player 1: Points for Car


Player 2: Points for Cart and Run

The word Tin is represented in the following way:


T is at position (4,1)
I is at position (4,2)
CHAPTER 7. SCRABBLE GAME 63

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/

• Is there a letter at grid[3][1]? Yes, letter R

• Is there a letter at grid[2][1]? Yes, letter R

• Is there a letter at grid[1][1]? Yes, letter R

• Is there a letter at grid[0][1]? No

• Word starting from grid[1][1] until grid[x][1] = null where x is an integer


from 1 to the length horizontal length of the grid (1 to 5). The word is
“CART”. Keep on checking.

• Is there a letter at grid[3][2]? No

• Is there a letter at grid[5][2]? No

• Is there a letter at grid[3][3]? No

• Is there a letter at grid[5][3]? No

• ...

• until all directions have been checked.

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 [ ][ ][ ][ ][ ]

Player 1: Points for Car


Player 2: Points for Car but no points for Run

7.5 Client to Client


The previous chapter discussed the best solution for sending information between
players, and how the server can send information to a client without it being
requested. The answer to out problem was to use ClientCallback. There are two
reasons why the CallBack process was used.

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.

2. Another use is to inform players of blank letters. When a blank later is


placed on the board, it acts as a joker. Only the player using the blank
tile knows what letter he or she wants it to represent, but for the purpose
of making new words, it is important to tell everyone else what the blank
represents.

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

of sending words through the server. The features, or variables, of a showplayer


are:

• an array of Letter objects

• 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)

Once that problem was surpassed, I was satisfied.


Using the Java policy file discussed in section 5.4.5 also meant that the game
could run fast and effectively from outside the computer that the server was
running on. Being able to deal with the restrictions imposed by appletviewers on
applets using RMI (see section 5.4.5) was a great achievement.
The main aims for the Scrabble game discussed in section 1.4 were all achieved
with some exceptions on preventing users from breaking the rules. An user cannot
do anything out of turn, or move letters on the board, but he can sometimes place
letters incorrectly. The reason for this was that I chose as a main aim to get an
effective game running fast, instead of having a slow game with more player
control from a single computer.

8.3 Knowledge Acquired


Although I had basic Java knowledge, I had never done anything as major as
this. My programming skills have been greatly improved. Not only the basic
concepts of Java, but also those of Servers and Clients. I had never designed
CHAPTER 8. CONCLUSION 69

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.4.1 Adding Time


Java offers a class Timer. In future work it is conceivable that someone may want
to add time features to a game, so that players do not spend more than a given
time trying to make a word. A time is a thread(see section 5.4.3) that runs at
the same time as you are running your program.

8.4.2 Changing the number of players


The number of players is determined by the Player array(see section 6.2). It can
be adapted to accommodate more players by changing the size of the array.

8.4.3 Choice of Dictionary


In an ordinary Scrabble game the players decide on which dictionary they wish
to use. My implementation of the Scrabble game uses a text file with a list of
40000 words(see dictionary, ), but it is possible to imagine someone wanting to
give the players a choice of dictionary.

8.4.4 Expanding the System


The Scrabble game could be expanded in such a way that it could be used for
language teaching methods. Controlled access could be added as a feature, such
that only teachers could set the letter bag, and only students could use the game.

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.

dictionary. www.cs.odu.edu/˜wadaa/cs361/A5/distributions/dictionary.txt.. last


visited 2nd May 2002.

dictionary.com. Dictionary.com.. last visited 2nd May 2002.

Games, S. (1988). Scrabble Rules of Play..

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.

Robinove, C. (2002). Letter-frequency bias in an electronic Scrabble game.


Chance, 15 (1), 30–31.

Stallings, W. (2001). Operating Systems (4th edition). Prentice Hall Interna-


tional, Inc.

71
Appendix A

CSLL Webpage

72
APPENDIX A. CSLL WEBPAGE 73

<body text="#000000" bgcolor="#FDD0" link="#0000EE" vlink="#551A8B" alink="#FF0000"


<html><head><title>B.A. (Mod.) Degree in Computer Science, Linguistics
and a Language</title>
<link rev="made" href="Jeremy.Jones@cs.tcd.ie"></head>

<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>

If computers are to be really useful to people in general then it must


be possible to converse with them in a natural way. One of the major
objectives of this degree is to produce graduates who will be able to
design and implement software systems which are easy to use and which
have user interfaces which can accept different natural languages.
APPENDIX A. CSLL WEBPAGE 74

There are increasingly strong relations developing between the


artificial languages used by computers (i.e. programming languages)
and natural languages. Historically, natural language interfaces have
been the primary technology produced by researchers in computational
linguistics. However there is an ever increasing industry based on
natural language engineering -- personal translators, speech
synthesizers and the like. Computer programs need to be able to
reason intelligently in order to undertake complex tasks such as
natural language translation and automatic recognition of visual
scenes. For these reasons special emphasis is placed on artificial
intelligence and cognitive science.
<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

A representative sample of recent job offerings that CSLL graduates


are qualified for is available <A HREF="http://www.cs.tcd.ie/research_groups/clg/FY

<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

via web pages, from this link.


</A>

</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>

<h2> Computational Linguistics/Cognitive Science Offprint Library </h2>


<li> <A HREF="http://wilde.cs.tcd.ie:6677/index.html">Click Here</A>

<h2>Structure of Computer Science, Linguistics and a Language</h2>

<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.

<h3><a href="jfcsll.html">Junior Freshman</a></h3>


<ul> <a href="jfphotos.html">(meet the 2000/2001 class)</a>
<h4>Computer Science </h4>
<li> <a href="jfcsll.html#1BA1">1BA1</a>
Mathematics
APPENDIX A. CSLL WEBPAGE 77

<li> <a href="jfcsll.html#1BA2">1BA2</a>


Introduction to Programming
<li> <a href="jfcsll.html#1CSLL3">1CSLL3</a>
Introduction to Computing
<h4>Linguistics</h4>
<li> Introduction to the Study of Language (general linguistics)
<li> Introduction to Phonetics and Phonology
<li> Introduction to Syntax
<h4>Language</h4>
<li> French
<li> German
<li> Irish

</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>

Students develop a formal analysis in syntax or semantics of a


fragment of their host language using one of the theoretical
frameworks addressed in the degree. The exact topic is negotiated
individually, and it is jointly evaluated by the host and home
institutions. Students electing to take the fourth year option in
computational linguistics develop implementations of these grammars.
<a href="projects3.html">Previous students</a> have done a wide range
of interesting papers for this.

</ul>

<h3><a href="sscsll.html">Senior Sophister</A></h3>


<ul>
<h4>Computer Science</h4>
<li> <a href="http://www.cs.tcd.ie/courses/ba/ssba.html#4BA1">4BA1</a> Information
<li> <a href="sscsll.html#4CSLL4">4CSLL4</a> Artificial Intelligence
<h4>Linguistics</h4>
<li> Speech Science
<li> Computational Linguistics
<h4>Language</h4>
APPENDIX A. CSLL WEBPAGE 79

<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

Corpus Trial Webpage

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

This image shows a sample run of a Scrabble game.

Figure C.1: A Scrabble Game in Process

You might also like