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

PHP 5/MySQL Programming- P31 docx

5 214 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 126,98 KB

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

Nội dung

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

Trang 1

g r

s o

l u

g in

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){

Trang 2

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 andoperator You

can read it as and.The condition is evaluated to TRUEonly 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 secondRollelement contains a TRUE

129

Trang 3

or FALSEvalue 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 forloops 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

g r

s o

l u

g in

e r

Trang 4

C H A L L E N G E S

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.

Trang 5

This page intentionally left blank

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

TỪ KHÓA LIÊN QUAN