The script described in this article is easy to understand, easy to use, and can be quickly mastered.
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 (, file_get_contents(names.female.txt ));
$ Last = explode (, 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 pseudo-random environment set (can be used for role-playing, impromptu creation, and writing) is 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 ("", file_get_contents(scenario.settings.txt ));
$ Objectives = explode ("", file_get_contents(scenario.objectives.txt ));
$ Antagonists = explode ("", file_get_contents(scenario.antagonists.txt ));
$ Complicati ***** = explode ("", file_get_contents (scenario. complicati *****. txt ));
Shuffle ($ settings );
Shuffle ($ objectives );
Shuffle ($ antagonists );
Shuffle ($ complicati ****);
Echo $ settings [0]... $ objectives [0]... $ antagonists [0].
. $ Complicati ***** [0]. "<br/> ";
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 => Ace, 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 use a deck