25 Good PHP game programming script code sharing (1 ). A simple flipper many games and game systems need dice. Let's start with the simple part: throw a six-sided dice. In fact, rolling a six-sided dice is from 1 to 6.
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 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("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' => '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' => '');
1
Dice is required for many games and game systems. Let's start with the simple part: throw a six-sided dice. In fact, rolling a six-sided dice is from 1 to 6...