PHP 5/MySQL Programming- P39 docx

5 199 0
PHP 5/MySQL Programming- P39 docx

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

Thông tin tài liệu

//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 dandy end-of-chapter exercise!) The $direction array holds the initials of the four directions I have decided to support at this time. The $itWorked variable 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 $itWorked value is changed to FALSE. The $counter variable counts which word I’m currently trying to place. I incre- ment the value of $counter each time through the loop. When $counter is larger than the $word array, 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 $direction array. 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 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r other letter from another word (unless that letter happens to be the one the func- tion is already trying to place). The function 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 C h a p t e r 5 B e t t e r A r r a y s a n d S t r i n g H a n d l i n g 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 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r 171 C h a p t e r 5 B e t t e r A r r a y s a n d S t r i n g H a n d l i n g //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 switch structure based on the word direc- tion. The code inside the switch branches 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; } 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 elephant in 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”] – 1 is 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 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r IN THE REAL WORLD 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

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Contents

    • Introduction

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 11: Data Normalization

    • Chapter 12: Building a Three-Tiered Data Application

    • Index

    • Team DDU

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan