25 Good PHP game programming script code sharing

Source: Internet
Author: User
Tags random name generator
This article introduces 25 good PHP game programming script codes, including a simple flipper, Random Name Generator, scenario generator, card builder, and shuffler) simple poker card generator, Hangman game, crossword puzzle assistant, meilibice, Lotto, etc. Hope to help your work. Simple throwing hacker "> <LINKhref =" http: // w

This article introduces 25 good PHP game programming script codes, including simple drop handler, Random Name Generator, scenario generator, card builder, and shuffler) simple poker card generator, Hangman game, crossword puzzle assistant, meilibice, Lotto, etc. Hope to help your work.

Simple flipper

Many games and game systems require dice. Let's start with the simple part: throw a six-sided dice. In fact, rolling a six-sided dice means selecting a random number from 1 to 6. In PHP, this is very simple: echo rand );.

In many cases, this is basically simple. However, when dealing with probability games, we need some better implementations. PHP provides a better random number generator: mt_rand (). Without in-depth research on the differences between the two, mt_rand can be considered as a faster and better random number generator: echo mt_rand );. If the random number generator is put into the function, the effect will be better.

Listing 1. Using the mt_rand () random number generator function

Function roll (){

Return mt_rand (1, 6 );

}

Echo roll ();

Then, you can pass the dice type to be rolled to the function as a parameter.

Listing 2. Passing the dice type as a parameter

Function roll ($ sides ){

Return mt_rand (1, $ sides );

}

Echo roll (6); // roll a six-sided die

Echo roll (10); // roll a ten-sided die

Echo roll (20); // roll a twenty-sided die

Starting from here, we can continue to scroll multiple dice at a time as needed and return an array of results. we can also scroll multiple different types of dice at a time. However, most tasks can use this simple script.

Random Name Generator

If you are running a game, writing a story, or creating a large number of characters at a time, you may sometimes get tired of new names. 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 the other for the last name.

Listing 3. two simple arrays of first and last names

$ Male = array (

"William ",

"Henry ",

"Filbert ",

"John ",

"Pat ",

);

$ Last = array (

"Smith ",

"Jones ",

"Winkler ",

"Cooper ",

"Cline ",

);

Then you can select a random element from each array: echo $ male [array_rand ($ male)]. ''. $ last [array_rand ($ last)];. To extract multiple names at a time, you only need to mix the array and extract the names as needed.

Listing 4. mixed name array

Shuffle ($ male );

Shuffle ($ last );

For ($ I = 0; $ I <= 3; $ I ++ ){

Echo $ male [$ I]. ''. $ last [$ I];

}

Based on this basic concept, we can create a text file that saves the first and last names. If you store a name in each line of a text file, you can easily use line breaks to separate the file content to build an array of source code.

Listing 5. create a text file with the name

$ Male = explode ('\ n', file_get_contents('names.female.txt '));

$ Last = explode ('\ n', file_get_contents('names.last.txt '));

After building or searching for some good name files (some files are included in the code archive), we will never need to worry about names.

Scenario generator

With the same basic principles used to build a name generator, we can build a scenario generator. This generator is useful not only in role-playing games, but also in scenarios where a set of pseudo-random environments (used for role-playing, impromptu creation, and writing) are required. One of my favorite games, Paranoia included "mission blender" in its GM Pack )". The task mixer can be used to integrate a complete task when rolling the dice quickly. Let's integrate our scenario generator.

Consider the following scenario: you wake up and find yourself lost in the jungle. You know you have to go to New York, but you don't know why. You can hear the sounds of dogs nearby and clear enemy searches. You are cold, trembling, and have no weapons. Each sentence in this scenario describes specific aspects of the scenario:

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

"You know you have to go to New York"-this sentence will describe the target.

"You can hear the call of a dog"-this sentence introduces the enemy.

"You are cold, trembling, and have no weapons"-this sentence adds complexity.

Just like creating a text file with the first name and last name, create a text file with the settings, Target, enemy, and complexity respectively. The sample file is included in the code archive. After you have these files, the generated scenario code is basically the same as the generated name code.

Listing 6. generation scenarios

$ Settings = explode ("\ n", file_get_contents('scenario.settings.txt '));

$ Objectives = explode ("\ n", file_get_contents('scenario.objectives.txt '));

$ Antagonists = explode ("\ n", file_get_contents('scenario.antagonists.txt '));

$ Complicati ***** = explode ("\ n", file_get_contents ('scenario. complicati *****. txt '));

Shuffle ($ settings );

Shuffle ($ objectives );

Shuffle ($ antagonists );

Shuffle ($ complicati ****);

Echo $ settings [0]. ''. $ objectives [0].''. $ antagonists [0].''

. $ Complicati ***** [0]. "\ n ";

You can add elements to a scenario by adding a new text file, or you may want to add multiple complexity levels. The more content you add to a basic text file, the more scenarios change over time.

Card Group creator (Deck builder) and equipment (shuffler)

If you want to play cards and process card-related scripts, we need to integrate a deck builder with tools in the equipment. First, let's build a pair of standard cards. You need to construct two arrays-one to save the cards of the same color, and the other to save the cards. If you need to add a new group card or card type later, doing so will provide great flexibility.

Listing 7. building a pair of standard playing cards

$ Suits = array (

"Spades", "Hearts", "Clubs", "Diamonds"

);

$ Faces = array (

"Two", "Three", "Four", "Five", "Six", "Seven", "Eight ",

"Nine", "Ten", "Jack", "Queen", "King", "Ace"

);

Create a deck array to save all card values. You only need to use a pair of foreach loops to complete this operation.

Listing 8. building a deck array

$ Deck = array ();

Foreach ($ suits as $ suit ){

Foreach ($ faces as $ face ){

$ Deck [] = array ("face" => $ face, "suit" => $ suit );

}

}

After building an array of playing cards, we can easily shuffles and randomly draw a card.

Listing 9. shuffling and randomly picking a card

Shuffle ($ deck );

$ Card = array_shift ($ deck );

Echo $ card ['face']. 'of'. $ card ['suit'];

Now, we have achieved a shortcut to extract multiple deck cards or build a multi-layer card box (multideck shoe.

Winning rate calculator: Licensing

Since the cards and colors of each card are tracked separately when building cards, you can use this card in programming to calculate the probability of a specific card. First, five cards are drawn for each hand.

Listing 10. five cards for each hand

$ Hands = array (1 => array (), 2 => array ());

For ($ I = 0; $ I <5; $ I ++ ){

$ Hands [1] [] = implode ("of", array_shift ($ deck ));

$ Hands [2] [] = implode ("of", array_shift ($ deck ));

}

Then you can check the deck to see how many cards are left and the probability of getting a specific card. It is easy to view the remaining number of cards. 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 probability of winning a specific card

Function calculate_odds ($ draw, $ deck ){

$ Remaining = count ($ deck );

$ Odds = 0;

Foreach ($ deck as $ card ){

If ($ draw ['face'] = $ card ['face'] & $ draw ['suit'] =

$ Card ['suit']) |

($ Draw ['face'] = ''& $ draw ['suit'] = $ card ['suit']) |

($ Draw ['face'] = $ card ['face'] & $ draw ['suit'] = '')){

$ Odds ++;

}

}

Return $ odds. 'in' $ remaining;

}

Now you can select the cards to be pulled. For simplicity, input an array that looks like a card. We can find a specific card.

Listing 12. search for a specified card

$ Draw = array ('face' => 'acs', 'suit' => 'spades ');

Echo implode ("of", $ draw). ':'. calculate_odds ($ draw, $ deck );

Alternatively, you can find a card with a specified card surface or color.

Listing 13. search for a card with a specified card surface or color

$ Draw = array ('face' => '', 'suit' => 'spades ');

$ Draw = array ('face' => 'Ace', 'suit' => '');

Simple poker poster

Now we have obtained the card group builder and some tools to help calculate the probability of getting a specific card. we can integrate a truly simple card generator for licensing. For the purpose of this example, we will build a poster that can draw five cards. The poster provides five cards from the entire deck. Use a number to specify which cards need to be abandoned, and the poster will replace these cards with other cards in one deck. We do not need to specify licensing limits or special rules, but you may find these are very useful personal experiences.

As shown in the previous section, generate and shuffles five cards for each hand. Display these cards by array index so that you can specify which cards are returned. You can use the check boxes indicating which cards to replace to complete this operation.

Listing 14. use the check box to indicate the cards to be replaced

Foreach ($ hand as $ index => $ card ){

Echo "<span =" ">. $ index."] '> <>

". $ Card ['face']. 'of'. $ card ['suit']." ";

}

Then, enter array $ _ POST ['card '] to check which cards have been selected for replacement.

Listing 15. calculation input

$ I = 0;

While ($ I <5 ){

If (isset ($ _ POST ['card '] [$ I]) {

$ Hand [$ I] = array_shift ($ deck );

}

}

With this script, you can try to find the best way to process a specific group of cards.

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.