Scrabble in PHP2

64
PSZ 19:16 (Pind. 1/07) I hereby declare that I have read this thesis and in my opinion this thesis is sufficient in terms of scope and quality for the award of the degree of Bachelor of Engineering (Electrical – Electronics) Signature : .................................................... Name of Supervisor : Prof. Madya Dr. Sulaiman bin Mohd Nor Date : .................................................... * Delete as necessary DECLARATION OF THESIS / UNDERGRADUATE PROJECT PAPER AND COPYRIGHT Author’s full name : Date of birth : 27/04/1989 Title : DESIGNING SCRABBLE USING PHP Academic Session: I declare that this thesis is classified as follows: I acknowledged that Universiti Teknologi Malaysia reserves the right as follows : 1. The thesis is the property of Universiti Teknologi Malaysia. 2. The Library of Universiti Teknologi Malaysia has the right to make copies for the purpose of research only. 3. The Library has the right to make copies of the thesis for academic exchange. Certified by: SIGNATURE SIGNATURE OF SUPERVISOR (NEW IC NO. /PASSPORT NO.) NAME OF SUPERVISOR Date : Date : NOTES : * If the thesis is CONFIDENTIAL or RESTRICTED, please attach with the letter from the organisation with period and reasons for confidentiality or restriction. UNIVERSITI TEKNOLOGI MALAYSIA CONFIDENTIAL (Contains confidential information under the Official Secret Act 1972)* RESTRICTED (Contains restricted information as specified by the organisation where research was done)* OPEN ACCESS I agree that my thesis to be published as online open access (full text) MUHAMMAD ‘AASIM ASYAFI’IE BIN AHMAD 2010/2011

description

Scrabble in PHP

Transcript of Scrabble in PHP2

PSZ 19:16 (Pind. 1/07)

I hereby declare that I have read this thesis and in my

opinion this thesis is sufficient in terms of scope and quality for the

award of the degree of Bachelor of Engineering (Electrical – Electronics)

Signature : ....................................................

Name of Supervisor : Prof. Madya Dr. Sulaiman bin Mohd Nor

Date : ....................................................

* Delete as necessary

DECLARATION OF THESIS / UNDERGRADUATE PROJECT PAPER AND COPYRIGHT

Author’s full name :

Date of birth : 27/04/1989

Title : DESIGNING SCRABBLE USING PHP

Academic Session:

I declare that this thesis is classified as follows:

I acknowledged that Universiti Teknologi Malaysia reserves the right as follows :

1. The thesis is the property of Universiti Teknologi Malaysia.

2. The Library of Universiti Teknologi Malaysia has the right to make copies for the purpose of research

only.

3. The Library has the right to make copies of the thesis for academic exchange.

Certified by:

SIGNATURE SIGNATURE OF SUPERVISOR

(NEW IC NO. /PASSPORT NO.) NAME OF SUPERVISOR

Date : Date :

NOTES : * If the thesis is CONFIDENTIAL or RESTRICTED, please attach with the letter from

the organisation with period and reasons for confidentiality or restriction.

UNIVERSITI TEKNOLOGI MALAYSIA

CONFIDENTIAL (Contains confidential information under the Official Secret

Act 1972)*

RESTRICTED (Contains restricted information as specified by the

organisation where research was done)*

OPEN ACCESS I agree that my thesis to be published as online open access

(full text)

MUHAMMAD ‘AASIM ASYAFI’IE BIN AHMAD

2010/2011

PSZ 19:16 (Pind. 1/07)

DESIGNING SCRABBLE USING PHP

MUHAMMAD ‘AASIM ASYAFI’IE BIN AHMAD

Submitted to Faculty of Electrical Engineering

in partial fulfilment of the requirement for the degree of

Bachelor of Engineering (Electrical – Electronics)

Faculty of Electrical Engineering

Universiti Teknologi Malaysia

JUN 2011

ii

I declare that this thesis entitled “Designing Scrabble Using PHP “is the result of my

own research except as cited in the references. The thesis has not been accepted for any

degree and is not concurrently submitted in candidature of any other degree.

Signature : ....................................................

Name : Muhammad ‘Aasim Asyafi’ie bin Ahmad

Date : ................................................

iiii

ACKNOWLEDGEMENT

First and foremost, I would like to thanks my supervisor, Dr. Sulaiman, for his

tolerance given throughout the period of the project.

I would like to appreciate my family for their support all this years. They have

been tolerant for many years despite my mysterious actions.

Last but not least, I would like to thanks the programmers on the web, novice or

professional who have directly or indirectly helped me with this project. Truly, the

Internet brings everyone closer than ever.

iv

ABSTRACT

PHP and the Internet go well with each other like bread and butter. The

practicality of PHP is undeniable and the world sees more and more website employ it to

greater length. Of course, PHP must be assisted by something else for it to work

beautifully. This project will use PHP, MySQL and JavaScript to attempt to create the

board game Scrabble. MySQL in this project is used to store the game configuration, the

player’s name, turn, score and all the letters with its scores. Simply put, its main job is

storing data. The data stored can be managed by using phpMyAdmin, the most

commonly used MySQL editor. What to do with the data will be determined by PHP

codes. JavaScript will be used to link between the player and the game. The moment the

player submit the letters, JavaScript will take all the data and link them to another PHP

page without the user refreshing their page. This PHP page will modify the data and

update them in the database. The project concludes that it is possible to create such

programme using PHP and its assistant.

v

ABSTRAK

Tidak dinafikan bahawa PHP dan Internet tidak boleh dipisahkan. Kesenangan

menggunakan PHP telah melahirkan banyak laman sesawang yang menggunakannya

dan angka ini masih bertambah. Akan tetapi, PHP perlukan bantuan software lain supaya

ia dapat berfungsi. Projek ini akan menggunakan PHP, MySQL dan JavaScript untuk

membuat permainan Sahibba. Dalam projek ini, MySQL akan digunakan untuk

menyimpan konfigurasi permainan, nama, giliran, markah pemain dan semua huruf-

huruf beserta dengan markahnya sekali. Ia berperanan sebagai penyimpan maklumat.

Maklumat yang disimpan boleh diubah-alih dengan menggunakan phpMyAdmin, yang

merupakan pengubah-alih MySQL yang paling dikenali. Apa yand akan dilakukan

terhadapa maklumat yang disimpan akan ditentukan oleh kod-kod PHP. JavaScript pula

akan menghubungkan pemain dengan permainan. Tatkala pemain menghantarkan huruf-

huruf yang telah disusun, JavaScript akan mengambil semua maklumat-maklumat ini

dan hantarkannya ke laman PHP yang lain. Laman PHP inin akan mengubah-alih

maklumat yang disampaikan dan akan mengubah maklumat dalam database MySQL.

Melalui projek ini boleh disimpulkan bahawa pembangunan software tersebut boleh

dilakukan dengan PHP dan software bantuannya.

vi

TABLE OF CONTENT

CHAPTER TITLE PAGE

DECLARATION OF THESIS ii

ACKNOWLEDGEMENT iii

ABSTRACT iv

ABSTRAK v

TABLE OF CONTENT vi

LIST OF FIGURES ix

1 INTRODUCTION

1.1 Background 1

1.2 Objective of Project 2

1.3 Scope of Project 2

1.4 Outline of Thesis 2

2 THEORY AND LITERATURE REVIEW

2.1 Introduction 3

2.2 PHP 3

vii

2.2.1 Strength of PHP 4

2.2.2 PHP Tools 5

2.3 MySQL 6

2.3.1 Strength of MySQL 7

2.3.2 MySQL Tool 7

2.4 JavaScript 8

2.4.1 JavaScript Uses 8

2.4.2 jQuery 9

2.5 Studies on Scrabble 9

2.5.1 Introduction 9

2.5.2 How to Play 10

3 METHODOLOGY

3.1 Introduction 12

3.2 Designing Index page 12

3.3 Designing Board 16

3.4 Designing the Letters 20

3.5 Designing the Rack 21

3.6 Designing the Rules 24

3.6.1 Starting From Middle 25

3.6.2 Adjacent Letter Rules 25

3.7 Identifying Words 35

3.8 Getting Scores 36

3.9 Updating Turns 39

4 RESULTS AND DISCUSSION

4.1 Introduction 41

4.2 Playing by Oneself 41

viii

4.2.1 Procedure 41

4.2.2 Experimental Results 42

4.2.2.1 Start the Game From Middle 42

4.2.2.2 Testing the Programme’s Ability to 44

detect Words and Scores

5 CONCLUSION AND RECOMMENDATION

5.1 Conclusion 47

5.2 Problems 48

5.3 Recommendation 48

6 REFERENCES 50

ix

LIST OF FIGURES

FIGURE TITLE PAGE

2.1 Starting game 12

3.1 Index page 13

3.2 Codes for index.php 14

3.3 Codes for showing game’s name 15

3.4 When there’s a game going 16

3.5 Picture of the Board 17

3.6 Designing board 18

3.7 The function SelectSquare(x,y,z) 18

3.8 Table ‘game_board1’ 19

3.9 Extracting data from ‘game_board1’ 20

3.10 Echoing letters to board 20

3.11 Table ‘total_letters’ 21

x

3.12 Picture of the rack with sample letters 22

3.13 Part of the table to store letters in the database 23

3.14 Code for calculating empty spaces 23

3.15 Code for picking random letters 24

3.16 The table that stores the chosen letter inside the database 24

3.17 The code that is responsible for detecting if the letter is in the 25

Middle

3.18 The function ‘get ( )’ 27

3.19 Code for getting variable from ‘get ( )’ 28

3.20 Code for arranging player’s letters 29

3.21 Codes for searching right adjacent letter (1) 30

3.22 Codes for searching right adjacent letter (2) 31

3.23 Codes for searching upper adjacent letter 32

3.24 Code for detecting adjacent letters (1) 34

3.25 Code for detecting adjacent letters (2) 34

3.26 Code for detecting adjacent letters (3) 35

3.27 Picture shows the score for each letter 36

xi

3.28 Picture shows the multiplier for each of the letter 37

3.29 Part of the code for calculating score 38

3.30 Part of the code for getting score 39

3.31 Tables from database showing score 39

3.32 Codes for deleting entries from database 40

3.33 Codes to update the turn 40

4.1 False play at the beginning 43

4.2 What happens if we play the wrong rule 43

4.3 Game records which is with the rule 44

4.4 Playing word from existing letters 45

4.5 Database was able to record conjugated words 45

4.6 Various combinations is added 45

4.7 Was able to record all of the words 46

Chapter 1

Introduction

1.1 Background

Scrabble is played throughout the world and it does not take long until the game

found itself turned into a computer programme. But the majority of the programmes are

not written in PHP. This is understandable because PHP does not give much interactivity

to the programme. But, recent development in PHP allows for such interactivity.

PHP as a programming language is indistinguishable from the internet. Its

application to the world of internet is virtually irreplaceable. However, PHP need to be

helped by its friends such as MySQL and JavaScript for it to function to its fullest

capacity.

1.2 Objective of the Project

The project is to design the word game Scrabble by using PHP, MySQL and

JavaScript.

2

1.3 Scope of the project

To design the Scrabble programme from PHP so that it can identify the rules of

the basic rules of the game.

1.4 Outline of Thesis

This thesis consist of 4 chapters, the first chapter discusses the objective and

scope of the project. Chapter 2 discusses the theory and literature review to be used in

the project.

Chapter 3 will be discussing on how the programme is arranged. Chapter 4 is

about results and discussion meanwhile chapter 5 will be on problems, conclusions and

recommendations.

Chapter 2

Theory and Literature Review

2.1 Introduction

This chapter will study PHP in general, its strength and some of it in detail, its

brief history. PHP will usually be accompanied by MySQL, so it will only be

appropriate to introduce it, and what it does. Also, the tools used to work with PHP and

MySQL will be discussed. It will also delve to some extent on JavaScript, though not as

in-depth because it plays a small part in the project.

2.2 PHP

Safe to say, one should be knowledgeable about PHP when it comes to coping

with project like this. We will only discuss some general aspect about PHP and why is

PHP chosen based on its strength.

4

PHP is a scripting language specifically designed for the Internet. It also worth

mentioning it is a server-side scripting language. One can embed a PHP code inside an

HTML page and it will run whenever a user visits the page.

It is important to know some of its history to understand its strength. But, a full

length history can fill several pages. So, what will be covered here is just a gist of it. It

started around 1995 when Rasmus Lerdorf developed a script made from Perl/CGI to

know how much people are viewing his resume. Lerdorf named his creation as PHP. It

will not be long until the second version came about because people are clamouring for

its toolset. This time however its written in C.

It wasnt until the year 2000 that the world see PHP 4.0 which many would argue

as the languages official debut within the enterprise development scene. PHP 5.0 would

follow suit which improve some existing functionalities.

2.2.1 Strength of PHP

Some of PHPs rivals are Perl, Microsoft ASP.NET, Java Server Page (JSP) and

ColdFusion. PHP topple its rival in term of performance, database integration, built-in

libraries, its free, language familiarity and it can work on many operating systems. I will

now go into details for each of these strengths.

PHP have high performance and is efficient. You can serve millions of hit by

using a single inexpensive server.

PHP have the ability to connect to a diverse number of databases. Apart from

the commonly used MySQL, it also can directly connect to PostgreSQL, mSQL, dbm,

FilePro, Hyperwave, Informix, InterBase, and Sybase databases.

5

PHP is designed specifically for the web. So when it come to designing web

sites, it can perform many useful task for the Web like generate GIF images, connect to

web services, work with cookies with few codes.

Over 180 libraries at developers’ disposal when they decide to use PHP. Plus, it

contains over 1,000 functions. PHP is most known for its ability to create pages

dynamically, interact with the databases, and modify form information. But PHP can

also manipulate AdobeFlash and Portable Document Format (PDF) file, check if the user

is the a particular person by comparing the data with the one from databases or from

stored flat files.

Still, others choose PHP because it is free. Now, free of charge is already a

tough price to compete with. Not only that, since its creation, users are allowed to

modify and redistribute without any restriction. Such software that met the previous

qualification is dubbed as open source software, and the Internet is practically a home

for such software. The Internet needs open source software to flourish and so is the other

way around. Its no wonder why PHP have garnered such popularity.

Practicality is one of the main reasons PHP was invented in the first place. PHP

is not created as a new language in of itself, but rather as a tool to solve unsolvable

problem during its time. The result is a language to build application with a minimum of

knowledge. Unlike C, there is no need to include some libraries before one is allowed to

programme. And PHP is loose type language so theres no need to typecast what a certain

variable is. For example, in C one must tell the programme whether a certain variable is

an integer, double, or string; but in PHP, one just create a variable and the programme

will employ a best-guess formula for typesetting the variable.

2.2.2 PHP Tools

6

For now, there’s no such thing as a GUI interface editor for PHP. The most we

can get is Dreamweaver. But even then, it mainly focuses on HTML and not PHP. There

exist several text editors for editing PHP. It works like a regular notepad but it will

recognise that the user is typing PHP and might highlight when a user a made mistake in

the syntax.

One of the best text editors is called Notepad++. It not only deals with PHP, but

will also work with JavaScript which used sometime to a lesser extent. But really though,

what one need is a text editor, even the regular one that comes with each computer can

be used.

2.3 MySQL

MySQL is a very fast, robust, relational management system (RDBMS). A

database will enable a user to store and manage data effectively. And let me tell you, this

project needs a lot of it. MySQL is available under a dual licensing scheme. It is free

under an open source license (GPL) and is free as long as you agree to comply with it.

MySQL is highly optimised but it lacks many of the features that are considered

standard.

This database system makes use of client-server architecture where it orbits

around the server, mysqld. And this server is responsible for managing the databases. On

the client side, it does not connect with the database immediately, rather it have to be

mediated through queries written in Structured Query Language (SQL). The client

programme or tools can be installed on a machine that one may wish to access MySQL

from, but the server can be conveniently installed elsewhere. Furthermore, since its

essentially a network database system, it can communicate with a server that is either

running on a local computer or one that is installed elsewhere.

7

2.3.1 Strength of MySQL

MySQL has it shares of competitors like PostgreSQL. MySQL have features that

are featured in many of its proprietary counterpart. Sure, its price is pretty much a dead

giveaway; its free for most people. But there are other reasons as to why it stuck out

amongst other products.

It is essentially flexible. No matter what operating system a user uses, MySQL most

probably got him or she covered. It is optimised for popular platforms such as Microsoft

Windows, Linux, Mac OS X, OpenBSD, Solaris, FreeBSD and even obscure ones such

as Compaq Tru64, DEC OSF, IBM AIX, HP-UX, Novell NetWare, QNX, SCO and SGI

IRIX. It also offers many ways of managing data.

MySQL main strength has been its power ever since the beginning of its

creation even at the loss of some features. Nowadays, its commitment to speed had not

changed at all, but instead it has gained many features. Its features are now on par with

many of its competitors.

It also has flexible licensing option and they come in two forms. One is the

usual Open Source License and the other one is the Commercial License. The former

works like any Open Source License, but the latter is for one who does not want to

redistribute their code or to build an application that is not under the GPL.

2.3.2 MySQL Tool

8

MySQL comes with no GUI to manage its database within. We can use

command-line tools but that process is tedious and time consuming. So, to cope with this

problem, various GUI have been introduced to manage the database.

This project will use phpMyAdmin to manage the database. phpMyAdmin is

installed in many Web Hosts making it the most commonly used MySQL GUI tools.

Other tools can also be used if one is knowledgeable, but it should be noted that

phpMyAdmin is the standard.

2.4 JavaScript

PHP working with MySQL can produce stupendous results but it will not help

much when it comes to users interactivity. Its purpose is to introduce interactivity to the

user, to let the pages responds to the user when the user made a specific event.

Nowadays, it is virtually impossible to find any Web Page that does not contain some

sort of JavaScript code. It is worth mentioning that JavaScript have nothing to do with

Java, a more complex language.

JavaScript is designed to work in the browser of your computer, as such; it is

properly called as a client-side language. Since it is associated with browser, it is tightly

integrated with HTML. Its possible to use JavaScript as a server-side language but its

rare.

2.4.1 JavaScript Uses

JavaScript is mainly used for detecting events such as a user who hover his or

her mouse over a picture. It can improve a website with navigational aids, dynamic

9

images and so on. It let you to validate what a user has submitted in his or her forms

before submitting the forms to the server.

2.4.2 jQuery

JavaScript however have some disadvantages. It is inconsistent to begin with,

some browser supports it, and some doesnt. And it has a monstrous syntax and can make

a beginner run away before scratching its surface. It is then several ambitious developers

decided to introduce JavaScript libraries to make it accessible for the new and existing

developers by simplifying the whole thing.

There are several JavaScript libraries but the one that is most useful is jQuery. It

has a small file size and the syntax used is ridiculously easy. The latter is very useful.

Using raw JavaScript, several dozen lines of codes for a single task will be the norm,

jQuery on the other hand, makes life much simpler and not only that, it can perform

more tasks. The developer can now do more stuff by typing less. jQuery has done to

JavaScript, what CSS had done to HTML.

2.5 Studies on Scrabble

2.5.1 Introduction

Scrabble is a board game designed for 2 to 4 players. It is played on a game

board marked by 15x15 grid and all the players have to form words on this game board

10

to score points. The words can only be arranged horizontally or vertically and the words

must be available in a standard dictionary.

2.5.2 How to Play

The steps on how to play Scrabble are as below:

i) Each of the players will take 8 letters from the sack.

ii) The player who starts the game will begin the game by forming a word in the

middle of the board either horizontally or vertically.

The next players will then put a letter anywhere near the words that had been formed

by previous player(s). This is better explained by referring to the figures below:

Figure 2.1 Starting game

11

After finishing their turn, each of the players must take new letters from the sack

to replace the letter used when forming the word. But the total number of letters in each

of the players hand must still be 8 at a time.

Each of the player can pass their turn if such a need arises. The game ends if all

the letters in the sack has been used or the game have been passed three times

consecutively.

Chapter 3

Methodology

3.1 Introduction

In this project, the main pages will be written in PHP. The Javascript file will be

stored elsewhere alongside the PHP files. The database for MySQL will be stored in the

database.

3.2 Designing Index Page

When designing any website, there must always be an index page. The index

page in this programme will be used to ask for the users name and the name of the title

for the name of the game that they have chosen. Its produced by drawing two tables

using simple HTML command.

13

Figure 3.1 Index page

The first table (the upper table), is designed by using the codes shown in figure

3.2. The code uses the method POST to send the required data to scrabble.php.

14

Figure 3.2 Codes for index.php

The lower table shown in figure 3.1 is to show if a certain game is playing the

moment a player reaches the index. This programme however only employs one match.

This is done by extracting from the database the data we require by using PHP

command; in this case its from the table gamename. Then we use another PHP command

to echo the name of the game. Figure 3.3 shows the code used. And figure 3.4 shows

what happens if a match is going on when were about to log in.

15

Figure 3.3 Codes for showing games name

16

Figure 3.4 When theres a game going

3.3 Designing Board

The game board is designed by using HTML codes. The game board is divided

into 225 squares as shown in figure 3.1. Each square have its own ids to allow the

JavaScript files to work.

17

Figure 3.5 Picture of the Board

Figure 3.6 below shows part of the codes used to design this. Notice each of the

squares is designated with an id in integer form. And the JavaScript codes will forward it

to the function SelectSquare(x,y,z) in java2.js as shown in figure 3.7.

Figure 3.6 Designing board

18

Figure 3.7 The function SelectSquare(x,y,z)

To display the characters onto the board, the programme will extract the current

game data from the database, this time from the table gameboard1. There exist 2

columns in this table. First is id and the second is value as shown in figure 3.8.

19

Figure 3.8 Table gameboard1

The id column designates the particular square on the board. And the value

column is to store what letters have been played on the particular id. We first extract the

data, store the data into a variable as shown in figure 3.9 and echo it out by using PHP as

shown in figure 3.10.

20

Figure 3.9 Extracting data from gameboard1

Figure 3.10 Echoing letters to board

3.4 Designing the letters

All the letters are stored inside the database within the table totalletters. The

letters does not stack to make things easier. Figure 3.11 shows the table totalletters.

Again, there are two columns. The column rack_letters is to store letters. The column

21

value is to indicate if the particular letter has been taken or not, ‘1’ means it have not

been taken while’ 0’ means it has.

Figure 3.11 Table total_letters

3.5 Designing the Rack

The letters given to the player to arrange on the board is stored in a rack. Each

player will receive exactly seven letters and is placed inside a table with seven columns

and one row as shown in figure 3.12. All of the codes used in this part are in fillrack.php.

22

Figure 3.12 Picture of the rack with sample letters

MySQL is used in order for the letters to appear inside the table. The process is

goes as follows:

i) All of the possible letters are stored inside the database using MySQL.

Figure 3.13 shows part of the table. Value 1 means the particular letter is

available while 0 means it have already been taken.

ii) Compute how many empty spaces a player has in his or her rack. If its the

beginning of the game, then the player has seven empty spaces. Figure

3.14 shows the code used.

iii) Take the letters from the letter table in the database randomly. The

amount of letters taken from the database is determined by the players

available empty spaces. Figure 3.15 shows the code used.

iv) The letters taken will be stored inside another table in the database that is

designed to store the players letter as shown in figure 3.16.

v) From the table that stores the players letters from the database, the

programme will extract each of the letters and put them into an array.

Each player will have their own array.

vi) These arrays will then be echoed into the table by using simple PHP

command.

vii) If all working accordingly. The database will delete the letters chosen

from it to prevent duplicates.

23

Figure 3.13 Part of the table to store letters in the database

Figure 3.14 Code for calculating empty spaces

24

Figure 3.15 Code for picking random letters

Figure 3.16 The table that stores the chosen letter inside the database

3.6 Designing the Rules

The rules of Scrabble are very simple. These rules are:

i) The game must start from the middle of the board.

25

ii) The letters arranged by the player must be adjacent to each other.

3.6.1 Starting From Middle

The id for the start square in the middle of the board is assigned as 113. So, the

programme can immediately know where the letters are placed based on its id. Figure

3.18 shows the part of the code used in this process. The code is from addword.php.

Figure 3.17 The code that is responsible for detecting if the letter is in the middle.

3.6.2 Adjacent Letters Rules

The letters arranged in Scrabble must be adjacent to each other and thus cannot

be placed haphazardly.

In order to detect the adjacent letters, the programme will need to know the id of

each of the squares used to assign the letters. And from there, the programme will

compute if the ids used are adjacent to each other. There are two ways for the letters to

26

be adjacent to each others; vertical and horizontal. Each has its own formula, although

they are only slightly different from each other.

In order to determine if the letters are arranged adjacent to each other

horizontally, the programme will first determine what squares are used. It then

determines its ids. The one with the lowest number as its id will be selected as reference

because the board id is assigned in such a way that the first letter will have the lowest

number and also due to the fact the English language is read from left to right. Now, the

programme will add the reference id by one, this is because the id of the square right

next to it is higher only by the value of one. Then the programme will check if this

particular id matches any of the squares id used by the player. If not, the programme will

conclude that the word is not arranged adjacent to each other horizontally. If yes, the

programme will then need to know if the letter it had just checked is the last one in the

letter. If the letter is not the last one in the word used, the programme will again add the

last used id by one and again will check if the new id match any of the squares used.

And thats the gist of it. All of the codes used are in addword.php.

Let’s take the process step by step. First, the player will click on the letters and

then click on the board. The programme will record the players move and store them

inside an array and then into the tableplayer1word. After the player finish arranging

words and clicked submit on the game board, the programme will run the function get( )

as from java2.js. The function is shown in figure 3.21.

27

Figure 3.18 The function get ( )

This function is done with the help of jQuery.js. The function $.post(x,y,z) is

used to send data to a specified page, in this case its addword.php. And it will take the

variable and use it on the designated page. After the page have processed the variable,

the programme will produce an output based on the command. In fact, the third

perimeter can be thrown away because what we really want out of this function is to

process the variable using addword.php. And, by using this function, the browser will

not refresh.

The variables to be given to addword.php are the boardid and letterx arrays.

Each array has seven elements. The boardid array is the array used to store the id of the

square used while the letterx array is used to store the letter played. Each array

correspond to each other which means boardid [1] will refer to the location of letterx[1]

and so on. Also, note that each of the array elements have to be an integer to be able to

28

process it. So, the element of boardid [1] will become id1, element boardid2 become id2

and so on.

After that, addword.php will get the entire variable passed by the function get

( ) and store them again into an array. This is so that we can simplify the next process.

The code used is shown in figure 3.22.

Figure 3.19 Code for getting variable from get( )

Next, the programme will put the entire variables we just got from get ( ) into the

table player1word. Not only that, it will arrange the variable according to the id of each

of the letters. Figure 3.23 is the codes for making it.

29

Figure 3.20 Code for arranging players letters

With that done, the programme will check if theres already a letter next to the

right side of each of the letters played. To do this, the programme will first increase the

id for each of the letters played by one. And by using these new ids, the programme will

check if theres any existing letter from the table gameboard1. Then, the number of these

existing letters is calculated. If its zero, then it will go out of the loop. But if theres one,

the programme will then take the existing letter and put it into the table arrangewordx.

Figure 3.24 shows the codes used for this process. Also, note that this process is used for

every letter played.

30

Figure 3.21 Codes for searching right adjacent letter (1)

But, the programme had only done checking the right side of the letters played

and it have not checked if theres a letter to the right side of the existing letters. Its

basically the mirror image of the previous step. Figure 3.25 shows the code for this

process. Note the difference is from what table are the data extracted. This time the

programme take them from player1wordinstead of gameboard1.

31

Figure 3.22 Codes for searching right adjacent letter (2)

An adjacent letter can exist in four different directions. So far, the programme

has only managed one of them. Next is to check if there are any adjacent letters to the

left. To do this, the programme needs to only do minor changes to the previous two steps.

Check the fourth line in figure 3.24 and figure 3.25, $idarray[$x] + 1 indicates that the

programme is checking the right hand side of the square. If we change it into

$idarray[$x] 1, the programme will now checking the left hand side.

After finish checking the horizontal side of the board, the programme now has to

check the vertical side of it. The process is, again, similar and what changes are the

variables. Figure 3.26 shows the code for checking if there are any adjacent letters at the

upper part of the letters.

32

Figure 3.23 Codes for searching upper adjacent letter

Instead of subtracting the id by the value of one, the programme subtracts it

with fifteen. This is because the id of the square directly above another square has a

difference of fifteen. And, instead of storing the adjacent letters to the table

arrangewordx, the programme stores them into the table arrangewordy. This is to

differentiate the letters that are arranged vertically and horizontally. The purpose for this

is to make calculating scores much easier.

The programme now needs to check if there any upper adjacent letters relative

to the existing letters and if there are any adjacent letters below relative to both the

played letters and existing letters.

The steps to detect the adjacent letters are:

i) Take from the game board all the squares used and get their ids. Note,

this does not include the letters used by the players.

ii) All the squares ids are then added by one. This is to detect if there are

letters at the right edge of the existing words. The modified ids are then

stored inside an array.

33

iii) All the unmodified squares ids are then added by fifteen, and that also

stored inside an array. This is to check if there are letters adjacent

vertically to the existing words.

iv) The unmodified squares ids are once again modified by subtracting them;

first with one and then stored inside an array. Then, the original squares

ids are again subtracted but by fifteen this time, and again stored inside

an array. The former is to check if there are adjacent letters on the left

while the latter is to check if there are adjacent letter above the existing

words.

v) The programme then checks the squares used by the player. If any of the

id used by the player matched with any of the elements inside any four of

the arrays that were previously created, then the programmes will know

that the player is linking their letters with those that are already exist on

the game board. The programme will then check to see the square the

player is trying to link with and get its id.

vi) The programme will then check if all the letters used by the player and

the existing letter on the board the player trying to link are adjacent to

each other either horizontally or vertically.

If all these steps are satisfied, the programme will acknowledge that the letters

are adjacent to each other.

After all that, the programme still has ways to go. The programme will take all

ids for each of the existing letter from the table gameboard1 and split them into four

different arrays. The first will have its entire id increase by the value of one. Another

one decreases them by the value of one. The third one will increase them by the value of

fifteen, and the fourth one will subtract them by fifteen. Figure 3.27 shows the code for

this process.

34

Figure 3.24 Code for detecting adjacent letters (1)

After that, the programme will check if any of the elements of each of the four

new arrays are overlapping with the ids from the letter played by the player. If any of

them overlap, the programme will let the programme pass through the later if statement

by assigning value to the variable $allow as shown in figure 3.28.

Figure 3.25 Code for detecting adjacent letters (2)

This code allows for limit but there are still some holes left. The last part of the

process is to take the id for all the letters played and subtract them with one another.

35

Since the programme had already arranged the id from lowest to highest, it will only

need slight addition. The code is shown in figure 3.29.

Figure 3.26 Code for detecting adjacent letters (3)

The reason to do this is due to the fact that, if each of the squares have its ids

separated by any number that can either be divisible by fifteen or by itself, then the

letters played are adjacent to each other.

3.7 Identifying Words

What the programme done so far is to identify if the player plays within the rules

but not to identify the word used. This step is obviously important because without it the

programme cannot calculate the score.

To accomplish this, the programme will use a lot of tables inside the database.

Each of the players will have two tables for them in the database. One is to store word

that happens to be arranged horizontally and the one for the vertical word.

After the programme determine that the letters are arranged adjacent to each

other. It then needs to know if the letters are arranged horizontally or vertically. If the

letters are arranged horizontally, the letters used by the players along with the letters the

36

player trying to link with will be placed inside a table to store horizontal words. If the

letters are arranged vertically, the letters will be placed in a table for vertical letters.

Next, the programme will then arrange the letters by id from lowest to highest.

Because this will arrange the letters from left to right or from top to bottom and from

this a word will be formed.

3.8 Getting Scores

Each of the letters will have its own intrinsic score and these scores are stored

inside a table in the database. The column score is self explanatory, it simply tells the

score of the particular letter. The column letter is showing just letters. Figure 3.9 shows

this.

Figure 3.27 Picture shows the score for each letter

In addition to the intrinsic scores of the letters, each of the squares on the board

will also have its own score multiplier as shown in figure 3.10. There are three columns

this time. The column id is, again, the id of a particular square on the board. The column

multiplier is how much the intrinsic is going to be multiplied if a particular letter is

played on a particular square. And the column range requires a bit more explanation. If

37

the value of range is W, which stands for word, then the score will first get the total

score of the word first and then multiply it with the appropriate multiplier. If, however,

the value is L, which stands for letters, the particular letter on the particular square will

be multiplied first and then added to the total score.

Figure 3.28 Picture shows the multiplier for each of the letter

Thats the part that requires MySQL. For calculating the scores, the codes are in

addword.php.

38

Figure 3.29 Part of the code for calculating score

The first step is to get all the letters from the word that has been identified in the

previous step. Next, identify the square of all the letters used. And identify the intrinsic

score of each of the letter used. From here the code can take one of two paths.

First, if one of the letters is played on a double or triple letter square. The

intrinsic score of the particular letter on that square will need to be multiplied by a

specified amount and that will become the letters new score. After all the score

modification on all the letters, the code will then identify the rest of the letter and get

their intrinsic score. And finally, the code will add them all up and store the score inside

a variable.

The second route is when one of the letters played is on a double or triple word

square. The code will first get all the letters intrinsic scores and add them all up and

store them inside a variable, and then it will multiply the total score with the specified

amount according to the square.

39

The above process is repeated twice to account for words that are arranged

vertically and horizontally.

The score will be stored inside a table in the database. The score the player

managed to get will then be stored inside the database along with the number of turn the

word was played. Figure 3.12 shows part of the code for this purpose and figure 3.13

shows the table that is used to store the score inside the database.

Figure 3.33 Part of the code for getting score

Figure 3.34 Tables from database showing score

3.9 Updating Turns

Some of the table in the database used inside this programme are just used

temporarily. Which means that those tables are used as mere convenience and after the

40

end of turn of a player, the data inside those temporary tables will be deleted. Figure

3.14 shows the codes used for such effect.

Figure 3.35 Codes for deleting entries from database

The letter used by the players will be deleted from their racks so to allow new

letters to be added and to avoid duplicates. Some of tables are used for arranging the

letters used by the player so to allow an English word to emerge.

Since the letters arranged on the board are done using MySQL, it is important

that before ending the turn, to update the table that is used for it by using a simple code

as shown in figure 3.15.

Figure 3.36 Codes to update the turn

Chapter 4

RESULTS AND DISCUSSION

4.1 Introduction

For this project, only two experiments are performed. One is to play the game

by using only one computer. And the other is to use several computers. Both

experiments is to start from the beginning until the end of the game.

4.2 Playing it By Oneself

Before the project can be used for multiplayer purposes, it first needs to be able

to satisfy the need for one player game.

4.2.1 Procedure

Now, to try out all these rules, we have to abandon the correctness of the English

language.

1) A simple index page is set up to register the player

42

2) See if the programme only allow for players to start in the middle (an

important rule).

3) Test if the programme will be able to detect the word right next to it.

4) Form random words and see if the programme is able to figure out what word

it needs to record.

5) From the previous words formed, check to see if the scores are calculated the

way they are meant to be.

4.2.2 Experimental Results

The results will be broken up into parts as below.

4.2.2.1 Start the Game from the Middle

If we were to start the game like in figure 4.1. The programme will not record

the letters after we pushed submit and will show only the previous board configuration

as in figure 4.2.

43

Figure 4.1 False play at the beginning

Figure 4.2 What happens if we play the wrong rule

If we start the game from the middle like in figure 4.3 however, the programme

will record the letters and will reconfigure the board.

44

Figure 4.3 Game records which is with the rule

4.2.2.2 Testing the Programmes Ability to Detect Words and Scores

We put a letter to an existing letter on the game board. For example, supposed

we have a game board as in figure 4.3. And we wish to add a letter at the bottom of L.

The result is as in figure 4.4 and the score table in the database is able to record the

specified word and scores as shown in figure 4.5.

45

Figure 4.4 Playing word from existing letters

Figure 4.5 Database was able to record conjugated words

We then add a whole bunch of letters as in figure 4.6 and the programme will be

able to record all the words intended to be arranged as shown in figure 4.7.

Figure 4.6 Various combinations is added

46

Figure 4.7 Was able to record all of the words

42

CHAPTER 5

CONCLUSION AND RECOMMENDATION

5.1 Conclusion

PHP unfortunately is not a prime language when it comes to designing games. So,

it will be tough for a programmer to design a game based solely on PHP.

The goal of this project is to design a Scrabble game by using PHP. Unfortunately,

PHP alone is not capable of doing it and must employ the help of MySQL to accomplish it.

Turns out, PHP does not play that much of a big role in this project against MySQL. The

PHP codes used in this programmes are basic and does not employ much need of web

interactivity anyways.

MySQL plays a big part in this project. From recording game configurations to

arranging words so that PHP can figure it out. Again, the codes for MySQL are also basic.

The real difficulty is designing the logic. Safe to say, one needs a high logic skill when it

comes to this project.

43

In conclusion, it is possible to create a fully functional Scrabble game by using

PHP and MySQL. But, really though, it is not a language you use if you want to make a

game.

5.2 Problems

Although the programme can identify most of the words, sometime it can make

mistakes, and with this mistakes come the wrong score.

PHP session () function cannot carry over to another PHP page and thats a big

problem. Essentially, one needs to use cookies when it comes to designing multiplayer

game using PHP.

The dictionary itself is a problem. There are a few English word list databases for

use with MySQL but one needs to pay several bucks to get them.

The document itself is a problem. Somehow forgot to do the backup for this file and

later it went corrupt without warning.

5.3 Recommendations

Simply put, one should not use PHP if one wishes to design games especially an

interactive one. Sure, simple online games that does not allow interactivity in real time

between players are possible using PHP. But, if one insists on making interactive PHP

games, here are a few recommendations:

44

i) MySQL is your best friend when it comes to designing online PHP games

Ive wasted hours labouring in vain when a simple use of MySQL can easily

obliterate many of my problems. Of course, MySQL is one of many database used with

PHP so pick one of your favourite if MySQL is not yours.

ii) Design the logic from the beginning

Programming is one of those easy to learn, hard to master type of skills. The codes

itself are not hard, but arranging those simple with one another is a whole different

story. I sometime get confused because I did not have a systematic logic design on

paper from the very beginning to let me navigate through the programmes easily.

45

REFERENCES

1) W. Jason Gilmore. Beginning PHP and MySQ: From Novice to Professional. 3rd

edition. United States of America: Apress. 2008.

2) Paul DuBois. MySQL Cookbook. United States of America: OReilly. 2002

3) Elli Quiggley. JavaScript by Example. 2nd edition. United States of America:

Prentice Hall. 2011.

4) Shelley Powers. JavaScript Cookbook. United States of America: OReilly. 2010

5) Jason Lengstrof. Pro PHP and jQuery. United States of America: Apress. 2010

6) Bear Bibeault, Yehuda Katz. jQuery in Action. United Kingdom of Great Britain

and Northern Ireland: Manning. 2008.

46

7) Matt Rutledge. PHP Game Programming. United States of America: Thompson

Course Technology. 2004.

8) Steve Suehring, Tim Converse, Joyce Park. PHP 6 and MySQL Bible. United States

of America: Wiley Publishing Inc. 2009.

9) Andi Gutsmans, Stig Saether Bakken, Derick Rethans. PHP 5 Power Programming.

United States of America: Prentice Hall. 2004.

10) Larry Ullman. PHP6 and MySQL 5.United States of America: Peachpit Press. 2008.

11) Luke Welling, Laura Thomson. PHP and MySQL Web Development. United States

of America: Sams Publishing. 2005.

12) Guy W. Lecky-Thompson. Just Enough Web Programming With XHTML, PHP,

and MySQL. United States of America: 2008.

13) Richard York. Beginning JavaScript and CSS Development With jQuery. United

States of America: Wiley Publishing Inc. 2009.

14) Jesus Castagnetto, harish Rawat, Sascha Schumann, Chris Scollo, Deepak Veliath.

Professional PHP Programming. United Kingdom of Great Britain and Northern

Ireland: Wrox Press. 1999.

15) Thomas A. Powell. HTML & CSS: The Complete Refrence. 5th edition. United

States of America: Mc Graw-Hill. 2010

47

16) Scott Duffy. How to Do Everything With JavaScript. United States of America: Mc

Graw-Hill/Osborne. 2003.