25 Good PHP Game programming script code sharing (1) _php tutorial

Source: Internet
Author: User
Tags shuffle vars random name generator
Simple Roll- dice

Many games and game systems require dice. Let's start with a simple section: Throw a six-sided dice. In fact, scrolling a six-sided dice selects a random number from 1 to 6. In PHP, this is very simple: echo rand (1,6);.

In many cases, this is basically simple. But in dealing with odds games, we need some better implementations. PHP provides a better random number generator: Mt_rand (). Without delving into the differences, you can think of Mt_rand as a faster, better random number generator: Echo Mt_rand (1,6);. If you put the random number generator in a function, the effect will be better.

Listing 1. Using the Mt_rand () Random number generator function

 
  
  
  1. function Roll () {
  2. return Mt_rand (1,6);
  3. }
  4. Echo

You can then pass the dice type that needs to be scrolled as a parameter to the function.

Listing 2. Passing the dice type as a parameter

 
  
  
  1. function Roll ($sides) {
  2. return Mt_rand (1,$sides);
  3. }
  4. Echo Roll (6);
  5. Echo Roll (ten);
  6. Echo Roll (); //Roll a twenty-sided die

From here on, we can continue to roll multiple dice at once, return an array of results, or roll several different types of dice at once. But most tasks can use this simple script.

Random Name Generator

If you're running a game, writing a story, or creating a large number of characters at once, you'll sometimes struggle to cope with new names that are constantly appearing. Let's take a look at a simple random name generator that can be used to solve this problem. First, let's create two simple arrays-one for the first name and one for the last.

Listing 3. Two simple arrays of first and last names

 
 
  1. $male = Array (
  2. "William" ,
  3. "Henry" ,
  4. "Filbert" ,
  5. "John" ,
  6. "Pat" ,
  7. );
  8. $last = Array (
  9. "Smith" ,
  10. "Jones" ,
  11. "Winkler" ,
  12. "Cooper" ,
  13. "Cline" ,
  14. );

You can then select a random element from each array: Echo $male [Array_rand ($male)]. ' ' . $last [Array_rand ($last)];. To extract multiple names at once, simply mix the arrays and extract them as needed.

Listing 4. Mixed name Array

 
  
  
  1. Shuffle ($male);
  2. Shuffle ($last);
  3. for ($i$i$i+ +) {
  4. echo$male[$i'$last [$i];

Based on this basic concept, we can create a text file that holds the first and last names. If you place a name in each line of a text file, you can easily separate the contents of the file with a newline character to build the source code array.

Listing 5. Create a text file with a name

 
  
  
  1. $maleexplode(' n 'file_get_contents( ' Names.female.txt ' ));
  2. $lastexplode(' n 'file_get_contents (' Names.last.txt '

Build or find some good name files (some files are included with the Code archive), and then we never have to worry about names anymore.

Scene Builder

Using the same rationale used to build the name generator, we can build the scene generator. This generator is useful not only in role-playing games, but also in situations where pseudo-random environments can be used, such as role-playing, improvisation, writing, and so on. One of my favorite games, paranoia includes the task mixer (mission Blender) in its GM Pack. Task mixer can be used to integrate complete tasks when rolling the dice quickly. Let's integrate our own scene generators.

Consider the following scenario: you wake up and find yourself lost in the jungle. You know you have to rush to New York, but you don't know why. You can hear the barking of nearby dogs and the sound of a clear enemy seeker. You are cold, shivering, and unarmed. Each sentence in the scenario describes a specific aspect of the scene:

"You wake up and find yourself lost in the jungle"-this sentence will be set up.

"You know you have to go to New York"-This sentence will describe the goal.

"You can hear the dog barking"-this sentence will introduce the enemy.

"You are cold, shivering, and unarmed"-this sentence adds complexity.

Just like creating a text file of first and last names, create a text file of settings, targets, enemies, and complexity, respectively. Sample files are included with the Code archive. After you have these files, the code that generates the scene is basically the same as the code that generated the name.

Listing 6. Build scene

 
 
  1. $settings = Explode ("n", file_get_contents(' scenario.settings.txt '));
  2. $objectives = Explode ("n", file_get_contents(' scenario.objectives.txt '));
  3. $antagonists = Explode ("n", file_get_contents(' Scenario.antagonists.txt ' ));
  4. $complicati **** = Explode ("n", file_get_contents(' scenario.complicati****. TXT '));
  5. Shuffle ($settings);
  6. Shuffle ($objectives);
  7. Shuffle ($antagonists);
  8. Shuffle ($complicati* * * *);
  9. Echo $settings[0]. ' ' . $objectives [0]. ' ' . $antagonists [0]. ' '
  10. . $complicati ****[0]. "
    n ";

We can add elements to the scene by adding new text files, or we might want to add multiple complexities. The more content you add to a basic text file, the more the scene changes over time.

Card Creator (Deck builder) and equipment (Shuffler)

If you want to play Solitaire and want to deal with solitaire-related scripts, we need to integrate a deck builder with the tools in the rig. First, let's build a standard deck of cards. You need to build two arrays-one for holding the same suit, and the other for holding the card. If you later need to add a new deck or card type, this will be a good flexibility.

Listing 7. Building a standard poker deck

 
 
  1. $suits = Array (
  2. "Spades" , "Hearts" , "Clubs" , "Diamonds"
  3. );
  4. $faces = Array (
  5. "both" , "three" , "Four" , "Five" , "Six" , "Seven" , "Eight" ,
  6. "Nine" , "Ten" , "Jack" , "Queen" , "King" , "Ace"
  7. );

Then build a deck array to hold all the card values. You can do this with just a couple of foreach loops.

Listing 8. Building a deck array

 
  
  
  1. $deck = array ();
  2. foreach ( $suits as $suit ) {
  3. Li class= "alt" > foreach ( $faces span class= "keyword" >as $face ) {
  4. $deck [] = array ( => $face , "Suit" => $suit );
  5. }
  6. }

After building a deck of poker arrays, we can easily shuffle and randomly draw a card.

Listing 9. Shuffle and randomly pull out a card

 
  
  
  1. Shuffle ($deck);
  2. $cardarray_shift($deck);
  3. echo$card[' face '' of '$ Card[' suit '

Now, we get a shortcut to draw multiple decks or build multi-layered card boxes (Multideck shoe).

Winning Calculator: Licensing

Since the cards are built to track the face and suit of each card separately, it is possible to use this card programmatically to calculate the probability of getting a particular card. First, five cards are drawn separately for each hand.

Listing 10. Five cards per hand

 
 
    1. $hands = Array (1 = array (), 2=> Array ());
    2. for ( $i = 0; $i < 5; $i + +) {
    3. $hands [1][] = implode ( , array_shift ( $deck ));
    4. $hands [2][] = implode ( "of" , array_shift ( $deck ));
    5. }

You can then look at the deck to see how many cards are left and what the odds are of drawing a particular card. It is easy to see the number of cards remaining. You only need to calculate the number of elements contained in the $deck array. To get the chance to draw a specific card, we need a function to traverse the entire deck and estimate the remaining cards to see if they match.

Listing 11. Calculate the chance to draw a specific card

 
 
  1. function Calculate_odds ($draw, $deck) {
  2. $remaining = Count ($deck);
  3. $odds = 0;
  4. foreach ( $deck as $card) {
  5. if ($draw[' face '] = = $card[' face '] && $draw[' suit '] = =
  6. $card [' suit ']) | |
  7. ($draw[' face '] = = ' && $draw[' suit '] = = $card[' suit ']) | |
  8. ($draw[' face '] = = $card[' face '] & & $draw[' suit '] = = ' )) {
  9. $odds ++;
  10. }
  11. }
  12. return $odds . ' in ' $remaining;
  13. }

You can now select the cards you are trying to pull out. For the sake of simplicity, pass in an array that looks like a card. We can look for a specific card.

Listing 12. Find a specified card

 
  
  
  1. $drawarray(' face '' Ace'suit ' ' Spades ');
  2. Echo implode ("of"$draw': ' . Calculate_odds ($draw$deck

Or you can find a card that specifies a face or suit.

Listing 13. Find a card for a specified face or suit

 
  
  
  1. $drawarray('face ''suit ' ' spades ');
  2. $drawarray(' face 'Ace ' ' Suit '

1

http://www.bkjia.com/PHPjc/445840.html www.bkjia.com true http://www.bkjia.com/PHPjc/445840.html techarticle simple craps many games and game systems require dice. Let's start with a simple section: Throw a six-sided dice. In fact, rolling a six-sided dice is from 1 to 6 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.