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

PHP 5/MySQL Programming- P40 pdf

5 246 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 133,08 KB

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

Nội dung

Row number doesn’t matter in an Eastward placement, because any row in the puzzle is legal—all letters are placed on the same row.. Each character is placed at the same row as the starti

Trang 1

l i

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

zero always works when you’re going right-to-left and the word is the same size

or smaller than the puzzle (which has been established) Row number doesn’t

matter in an Eastward placement, because any row in the puzzle is legal—all letters

are placed on the same row

Once I know the word’s largest and smallest legal starting places, I can randomly

generate that starting point knowing that the entire word can be placed there

legally as long as it doesn’t overlap any other

which is always one character long Each character is placed at the same row as

the starting character, but at a column offset by the position in the word Revisit

the elephant example: If the starting position chosen is column one, the

ele-phant, and 1 + 0 = 1 When the counter ($i) gets to the letter L, it has the value 1,

so it is placed in column two, and so on

If the formula for choosing the starting place and the plan for placing

subse-quent letters in place work correctly, you cannot add a letter outside the puzzle

board However, another bad thing could happen if a character from a previously

placed word is in a cell that the current word wants The code checks the current

it is empty and the new character can be freely placed there If the cell contains

the value that the current word wants to place in the cell, the program can

like-wise continue without interruption However, if the cell contains any other

char-acter, the loop must exit and the program must reset the board and try again Do

Printing in the Other Directions

Once you understand how to print words when the direction is East, you see that

the other directions are similar However, I need to figure out each direction’s

appropriate starting values and what cell to place each letter in Table 5.2

sum-marizes these values

A little explanation of Table 5.2 is in order Within the table, I identified the

min-imum and maxmin-imum column for each direction, as well as the minmin-imum and

maximum row This was easiest to figure out by writing some examples on graph

paper The placement of each letter is based on the starting row and column,

Trang 2

with i standing for the position of the current letter within the word In

left Work out the other examples on graph paper so you can see how they work

Making a Puzzle Board

words, the answer key is complete Each word is in place and any cell that does not contain one of the words still has a period The main program copies the

formatted into a form the user can use

174

g r

s o

l u

g in

e r

max Col board width – 1 board width – 1 board width – 1 board width – 1

– word width

max Row board height – 1 board height – 1 board height – 1 board height – 1

– word width

letter col start + i start – i start start

letter row start start start + i start – i

T A B L E 5 2 S U M M A R Y O F P L A C E M E N T D A T A

I N THE R EAL W ORLD This is exactly where computer programming becomes mystical for most people.

Up to now you’ve probably been following, but this business of placing the char-acters has a lot of math in it, and you didn’t get to see me struggle with it It might look to you as if I just knew what the right formulas were I didn’t I had to think about it carefully without the computer turned on I got out a white board (my favorite programming tool) and some graph paper and tried to figure out what

I meant mathematically when I said write the characters from bottom to top.

This is hard, but you can do it The main thing to remember? Turn off the com-puter Get some paper and figure out what it is you’re trying to tell the computer

to do Then you can start writing code You may get it wrong (at least I did) But

if you’ve written down your strategy, you can compare what you expected to happen with what did happen, and likely solve even this kind of somewhat math-ematical problem.

Trang 3

However, rather than writing one function to print the answer key and another to

print the finished puzzle, I wrote one function that takes the array as a parameter

and creates a long string of HTML code placing that puzzle in a table

function makeBoard($theBoard){

//given a board array, return an HTML table based on the array

global $boardData;

$puzzle = “”;

$puzzle = “<table border = 0>\n”;

for ($row = 0; $row < $boardData[“height”]; $row++){

$puzzle = “<tr>\n”;

for ($col = 0; $col < $boardData[“width”]; $col++){

$puzzle = “ <td width = 15>{$theBoard[$row][$col]}</td>\n”;

} // end col for loop

$puzzle = “</tr>\n”;

} // end row for loop

$puzzle = “</table>\n”;

return $puzzle;

} // end printBoard;

Most of the function deals with creating an HTML table, which is stored in the

a <td></td>pair for each table element

Sometimes PHP has trouble correctly interpolating two-dimensional arrays

If you find an array is not being correctly interpolated, try two things:

• Surround the array reference in braces as I did in the code in makeBoard()

• Forego interpolation and use concatenation instead For example, you could have built each cell with the following code:

$puzzle = “<td> width = 15>” $theBoard[$row][$col] “</td>\n”;

Adding the Foil Letters

The puzzle itself can be easily derived from the answer key Once the words in the

list are in place, all it takes to generate a puzzle is replacing the periods with

their job to foil the user This is actually quite easy compared to the process of

adding the words

function addFoils(){

//add random dummy characters to board

global $board, $boardData;

T R A P

175

l i

Trang 4

for ($row = 0; $row < $boardData[“height”]; $row++){

for ($col = 0; $col < $boardData[“width”]; $col++){

if ($board[$row][$col] == “.”){

$newLetter = rand(65, 90);

$board[$row][$col] = chr($newLetter);

} // end if } // end col for loop } // end row for loop

} // end addFoils

The function uses the standard pair of nested loops to cycle through each cell in the array For each cell that contains a period, the function generates a random

corresponds to that number and stored the new random letter in the array

Printing the Puzzle

The last step in the main program is to print results to the user So far, all the work has been done behind the scenes Now it is necessary to produce an HTML page

function has already formatted the actual puzzle and answer key tables as HTML

in $keyPuzzle

function printPuzzle(){

//print out page to user with puzzle on it

global $puzzle, $word, $keyPuzzle, $boardData;

//print puzzle itself

print <<<HERE

<center>

<h1>{$boardData[“name”]}</h1>

$puzzle

<h3>Word List</h3>

<table border = 0>

HERE;

//print word list

foreach ($word as $theWord){

176

g r

s o

l u

g in

e r

Trang 5

print “<tr><td>$theWord</td></tr>\n”;

} // end foreach

print “</table>\n”;

$puzzleName = $boardData[“name”];

//print form for requesting answer key.

//send answer key to that form (sneaky!)

print <<<HERE

<br><br><br><br><br><br><br><br>

<form action = “wordFindKey.php”

method = “post”>

<input type = “hidden”

name = “key”

value = “$keyPuzzle”>

<input type = “hidden”

name = “puzzleName”

value = “$puzzleName”>

<input type = “submit”

value = “show answer key”>

</form>

</center>

HERE;

?>

</body>

</html>

} // end printPuzzle

This function mainly deals with printing standard HTML from variables that

have been created during the program’s run The name of the puzzle is stored in

$boardData[“name”] The puzzle itself is simply the value of the $puzzlevariable

The trickiest part of the code is working with the answer key It is easy enough to

print the answer key directly on the same HTML page In fact, this is exactly what

I did as I was testing the program However, the puzzle won’t be much fun if the

answer is right there, so I allowed the user to press a button to get the answer key

The key is related only to the currently generated puzzle If the same word list

puzzle with a different answer

177

l i

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