25 Good PHP game programming scripting code sharing

Source: Internet
Author: User
Tags foreach array arrays explode generator shuffle random name generator

This article describes 25 good PHP game programming script code, including simple roll dice, random name generator, scene builder, license group creator (Deck builder) and equipment (shuffler), Simple poker licensing, hangman games, crossword helper, Mad Libs, Lotto machines and so on. Hope to be able to help you with your work.

A simple roll roll device

Many games and game systems need dice. Let's start with the simple part: roll a six-sided dice. In fact, scrolling a six-sided dice is 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 further study of the difference, Mt_rand is considered to be a faster and 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

function Roll () {

Return Mt_rand (1,6);

}

echo Roll ();

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

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

From here, we can continue to scroll multiple dice at once as needed, return an array of results, or scroll through multiple different types of dice at once. But most tasks can use this simple script.

Random Name Builder

If you are running a game, writing a story, or creating a large number of characters at once, you will sometimes be tired of dealing with new names that are constantly appearing. Let's take a look at a simple random name builder 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 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",

);

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

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 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 a source code array.

Listing 5. Create a text file with a name

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

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

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

Scene Builder

Using the same rationale used to build the name builder, we can build the scene builder. This builder is useful not only in role-playing games, but also in situations where you need to use a set of pseudo random environments (for role-playing, improvisation, writing, etc.). One of my favorite games, paranoia included the task Mixer (mission Blender) in its GM Pack. The task mixer can be used to consolidate complete tasks when rolling dice quickly. Let's integrate our own scene builder.

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 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 scenario:

"You wake up and find yourself lost in the jungle."-this sentence will 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, trembling, and unarmed"-this sentence adds complexity.

Just like creating a text file with a first and last name, create a text file of settings, targets, enemies, and complexity, respectively. A sample file is included in the Code archive. After owning these files, the code that generates the scenario is essentially the same as the code that generated the name.

Listing 6. Generating scenes

$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";

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

Licensing Group Builder (Deck builder) and equipment (Shuffler)

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

Listing 7. Building a standard deck of poker

$suits = Array (

"Spades", "Hearts", "clubs", "Diamonds"

);

$faces = Array (

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

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

);

Then build a deck of cards to hold all the card values. You can do this by using only a pair of foreach loops.

Listing 8. Build a deck of cards array

$deck = Array ();

foreach ($suits as $suit) {

foreach ($faces as $face) {

$deck [] = Array ("Face" => $face, "Suit" => $suit);

}

}

After building up an array of playing cards, we can easily shuffle and draw a random card.

Listing 9. Shuffle and draw a card randomly

Shuffle ($deck);

$card = Array_shift ($deck);

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

Now, we've got a shortcut to pull more than a deck of cards or to build a multi-layer box (Multideck shoe).

Winning Calculator: Licensing

As the cards are built to track the cards and colors of each card, you can use this card programmatically to calculate the probability of getting a particular card. First, draw five cards per hand, respectively.

Listing 10. Draw five cards from 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));

}

You can then look at the deck and see how many cards are left and what the odds are for a particular card to be drawn. It is easy to see the remaining number of cards. Only the number of elements contained in the $deck array needs to be computed. To get a 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 a particular card being drawn

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 pick out the cards you're trying to draw. For simplicity, pass in an array that looks like a card. We can look for a specific card.

Listing 12. Find the specified card

$draw = Array (' Face ' => ' Ace ', ' suit ' => ' spades ');

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 with a specified face or suit

$draw = Array (' Face ' => ', ' suit ' => ' spades ');

$draw = Array (' Face ' => ' Ace ', ' suit ' => ');

Simple Poker Licensing device

Now that we've got the deck builder and some tools that can help you figure out the odds of pulling out a specific card, we can integrate a really simple licensing device to do the licensing. For the purposes of this example, we will build a licensing device that can pull out five cards. The licensing device will provide five cards from the entire deck. Use numbers to specify which cards you want to discard, and the licensing device replaces them with other cards in a deck. We do not need to specify licensing restrictions or special rules, but you may find these to be very useful personal experiences.

As shown in the previous section, generate and shuffle, then five cards per hand. Displays these cards by array index so that you can specify which cards to return. You can do this by using the check boxes that represent which cards you want to replace.

Listing 14. Use check boxes to represent the cards you want to replace

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

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

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

}

Then compute the input array $_post[' card ' to see which cards have been selected for replacement.

Listing 15. Calculate 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 handle a particular set 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.