PHP 5/MySQL Programming- P31 docx

5 214 0
PHP 5/MySQL Programming- P31 docx

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

Thông tin tài liệu

128 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 //check for two pairs if ($numPairs = = 2){ print “You have two pairs!<br>\n”; $payoff = 1; } // end if Of course, you’re welcome to change the payoffs. As it stands, this game is incred- ibly generous, but that makes it fun for the user. Looking for Three of a Kind and a Full House I combine the checks for three of a kind and full house (which is three of a kind and a pair). The code first checks for three of a kind by looking at $numThrees. If the user has three of a kind, it then checks for a pair. If both these conditions are true, it’s a full house and the user is rewarded appropriately. If there isn’t a pair, the user gets a meager reward for the three of a kind. //check for three of a kind and full house if ($numThrees = = 1){ if ($numPairs = = 1){ //three of a kind and a pair is a full house print “You have a full house!<br>\n”; $payoff = 5; } else { print “You have three of a kind!<br>\n”; $payoff = 2; } // end ‘pair’ if } // end ‘three’ if Checking for Four of a Kind and Five of a Kind Checking for four- and five of a kind is trivial. Looking at the appropriate vari- ables is the only necessity. //check for four of a kind if ($numFours = = 1){ print “You have four of a kind!<br>\n”; $payoff = 5; } // end if //check for five of a kind if ($numFives = = 1){ print “You got five of a kind!<br>\n”; $payoff = 10; } // end if Checking for Straights Straights are a little trickier, because two are possible. The player could have the values 1-5 or 2-6. To check these situations, I used two compound conditions. A compound condition is made of a number of ordinary conditions combined with special logical operators. Look at the straight-checking code to see an example: //check for straights if (($numVals[1] = = 1) && ($numVals[2] = = 1) && ($numVals[3] = = 1) && ($numVals[4] = = 1) && ($numVals[5] = = 1)){ print “You have a straight!<br>\n”; $payoff = 10; } // end if if (($numVals[2] = = 1) && ($numVals[3] = = 1) && ($numVals[4] = = 1) && ($numVals[5] = = 1) && ($numVals[6] = = 1)){ print “You have a straight!<br>\n”; $payoff = 10; Notice how each if statement has a condition made of several subconditions joined by the && operator? The && operator is called a Boolean and operator. You can read it as and. The condition is evaluated to TRUE only if all the subconditions are true. The two conditions are similar to each other, simply checking the two possible straight situations. Printing the Results The program’s last function prints variable information to the user. The $cash value describes the user’s current wealth. Two hidden elements store informa- tion the program needs on the next run. The secondRoll element contains a TRUE 129 C h a p t e r 4 L o o p s a n d A r r a y s or FALSE value indicating whether the next run should be considered the second roll. The cash element describes how much cash should be attributed to the player on the next turn. function printStuff(){ global $cash, $secondRoll; print “Cash: $cash\n”; //store variables in hidden fields print <<<HERE <input type = “hidden” name = “secondRoll” value = “$secondRoll”> <input type = “hidden” name = “cash” value = “$cash”> HERE; } // end printStuff ?> </form> </html> Summary You are rounding out your basic training as a programmer, adding rudimentary looping behavior to your bag of tricks. Your programs can repeat based on con- ditions you establish. You know how to build for loops that work forwards, back- wards, and by skipping values. You also know how to create while loops. You know the guidelines for creating a well-behaved loop and how to form arrays manually and with the array() directive. Stepping through all elements of an array using a loop is possible, and your program can keep track of persistent vari- ables by storing them in form fields in your output pages. You put all these skills together to build an interesting game. In chapter 5 you extend your ability to work with arrays and loops by building more-powerful arrays and using special- ized looping structures. 130 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 131 C h a p t e r 4 L o o p s a n d A r r a y s CHALLENGES 1. Modify the Poker Dice game in some way. Add a custom background, change the die images, or modify the payoffs to balance the game to your liking. 2. Write the classic I’m Thinking of a Number game. Have the computer ran- domly generate a number and let the user guess its value. Tell the user if he is too high, too low, or correct. When he guesses correctly, tell how many turns it took. No arrays are necessary for this game, but you must store values in hidden form elements. 3. Write I’m Thinking of a Number in reverse. This time the user generates a random number between 1 and 100 and the computer guesses the number. Let the user choose from too high, too low, or correct. Your algorithm should always be able to guess the number in seven turns or fewer. 4. Write a program that deals a random poker hand. Use playing card images from http://waste.org/~oxymoron/cards/ or another source. Your program does not need to score the hand. It simply needs to deal out a hand of five random cards. Use an array to handle the deck. This page intentionally left blank

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