1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P39 docx

5 199 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 107,48 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

If the addWordfunction fails to place a word, the $itWorkedvalue is changed to FALSE.. Adding a Word The fillBoardfunction handles the global process of adding the word list to the game

Trang 1

//print “failed to place $word[$counter]”;

$keepGoing = FALSE;

$itWorked = FALSE;

} // end if

$counter++;

if ($counter >= count($word)){

$keepGoing = FALSE;

} // end if } // end while

return $itWorked;

} // end fillBoard

The function begins by defining an array for directions At this point, I decided only to support placing words in the four cardinal directions, although it would

be easy enough to add diagonals (Hey, that sounds like a dandyend-of-chapter exercise!) The $direction array holds the initials of the four directions I have decided to support at this time The $itWorkedvariable is a Boolean which reports whether the board has been successfully filled It is initialized to TRUE If the addWord()function fails to place a word, the $itWorkedvalue is changed to FALSE The $countervariable counts which word I’m currently trying to place I incre-ment the value of $countereach time through the loop When $counteris larger than the $wordarray, the function has successfully added every word and can exit triumphantly

To choose a direction, I simply created a random value between 0 and 3 and referred to the associated value of the $directionarray

The last line of the function returns the value of $itWorked The fillBoard() func-tion is called by the main program until it succeeds This success or failure is reported to the main program by returning the value of $itWorked.

Adding a Word

The fillBoard()function handles the global process of adding the word list to the game board, but addWord() adds each word to the board This function expects two parameters: the word and a direction

The function cleans up the word and renders slightly different service based on which direction the word is placed It places each letter of the word in an appro-priate cell while preventing it from being placed outside the game board’s boundary It also checks to make sure that the cell does not currently house some

168

l u

Trang 2

other letter from another word (unless that letter happens to be the one the

func-tion is already trying to place) The funcfunc-tion may look long and complex at first,

but when you look at it more closely you find it’s extremely repetitive

function addWord($theWord, $dir){

//attempt to add a word to the board or return false if failed

global $board, $boardData;

//remove trailing characters if necessary

$theWord = rtrim($theWord);

$itWorked = TRUE;

switch ($dir){

case “E”:

//col from 0 to board width - word width

//row from 0 to board height

$newCol = rand(0, $boardData[“width”] - 1 - strlen($theWord));

$newRow = rand(0, $boardData[“height”]-1);

for ($i = 0; $i < strlen($theWord); $i++){

//new character same row, initial column + $i

$boardLetter = $board[$newRow][$newCol + $i];

$wordLetter = substr($theWord, $i, 1);

//check for legal values in current space on board

if (($boardLetter == $wordLetter) ||

($boardLetter == “.”)){

$board[$newRow][$newCol + $i] = $wordLetter;

} else {

$itWorked = FALSE;

} // end if

} // end for loop

break;

case “W”:

//col from word width to board width

//row from 0 to board height

$newCol = rand(strlen($theWord), $boardData[“width”] -1);

$newRow = rand(0, $boardData[“height”]-1);

//print “west:\tRow: $newRow\tCol: $newCol<br>\n”;

169

l i

Trang 3

for ($i = 0; $i < strlen($theWord); $i++){

//check for a legal move

$boardLetter = $board[$newRow][$newCol - $i];

$wordLetter = substr($theWord, $i, 1);

if (($boardLetter == wordLetter) ||

($boardLetter == “.”)){

$board[$newRow][$newCol - $i] = $wordLetter;

} else {

$itWorked = FALSE;

} // end if } // end for loop break;

case “S”:

//col from 0 to board width //row from 0 to board height - word length

$newCol = rand(0, $boardData[“width”] -1);

$newRow = rand(0, $boardData[“height”]-1 - strlen($theWord)); //print “south:\tRow: $newRow\tCol: $newCol<br>\n”;

for ($i = 0; $i < strlen($theWord); $i++){

//check for a legal move

$boardLetter = $board[$newRow + $i][$newCol];

$wordLetter = substr($theWord, $i, 1);

if (($boardLetter == $wordLetter) ||

($boardLetter == “.”)){

$board[$newRow + $i][$newCol] = $wordLetter;

} else {

$itWorked = FALSE;

} // end if } // end for loop break;

case “N”:

//col from 0 to board width //row from word length to board height

$newCol = rand(0, $boardData[“width”] -1);

$newRow = rand(strlen($theWord), $boardData[“height”]-1);

for ($i = 0; $i < strlen($theWord); $i++){

170

l u

Trang 4

l i

//check for a legal move

$boardLetter = $board[$newRow - $i][$newCol];

$wordLetter = substr($theWord, $i, 1);

if (($boardLetter == $wordLetter) ||

($boardLetter == “.”)){

$board[$newRow - $i][$newCol] = $wordLetter;

} else {

$itWorked = FALSE;

} // end if

} // end for loop

break;

} // end switch

return $itWorked;

} // end addWord

The addWord()function’s main focus is a switchstructure based on the word

direc-tion The code inside the switchbranches are similar in their general approach

Closely Examining the East Code

It’s customary in Western languages to write from left to right, so the code for E,

which indicates write towards the East,is probably the most natural to understand

I explain how that code works and then show you how the other directions differ

Here’s the code fragment that attempts to write a word in the Easterly direction:

case “E”:

//col from 0 to board width - word width

//row from 0 to board height

$newCol = rand(0,

$boardData[“width”] - 1 - strlen($theWord));

$newRow = rand(0, $boardData[“height”]-1);

for ($i = 0; $i < strlen($theWord); $i++){

//new character same row, initial column + $i

$boardLetter = $board[$newRow][$newCol + $i];

$wordLetter = substr($theWord, $i, 1);

//check for legal values in current space on board

if (($boardLetter = = $wordLetter) ||

($boardLetter == “.”)){

$board[$newRow][$newCol + $i] = $wordLetter;

Trang 5

} else {

$itWorked = FALSE;

} // end if } // end for loop break;

Determining Starting Values for the Characters

Essentially, the code steps through the word one letter at a time, placing each let-ter in the next cell to the right I could have chosen any random cell and checked

to see when the code got outside the board range, but this would have involved some complicated and clunky code

A more elegant solution is to carefully determine what the range of appropriate starting cells are and choose cells within that range For example, if I’m placing the word elephant(with eight letters) from left to right in a puzzle with a width

of 10, zero and one are the only legal columns (Remember, computers usually start counting at zero.) If I place elephantin the same puzzle but from right to left, the last two columns (eight and nine) are the only legal options Once I rec-ognized this fact, I had to figure out how to encode this idea so it could work with any size words in any size puzzle

I need a random value for the row and column to figure out where to place each word However, that random value must be within an appropriate range based on the word length and board width By trial and error and some sketches on a white board, I determined that $boardData[“width”] – 1is the largest column in the game board and that strlen($theWord)is the length of the current word in characters

If I subtract the word length from the board width, I get the largest legal starting value for a left-to-right placement That’s how I got this slightly scary formula:

172

l u

I N THE R EAL W ORLD

By far the most critical part of this code is the comments at the beginning Even though I’m a reasonably experienced programmer, it’s easy to get confused when you start solving problems of any reasonable complexity Just to remind myself, I placed these comments to explain exactly what the parameters of this chunk of code are.

I referred to these comments many times while I was writing and debugging the code If I hadn’t given myself clear guidance on what I was trying to do, I would have gotten so lost I probably wouldn’t have been able to write the program.

Ngày đăng: 07/07/2014, 02:20

TỪ KHÓA LIÊN QUAN