The script presented in this article is easy to understand, simple to use, and quick to master.
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
function Roll () {
Return Mt_rand (1,6);
}
echo Roll ();
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
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 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
$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 the source code array. Listing 5. Create a text file with a name
$male = Explode (, file_get_contents (Names.female.txt));
$last = Explode (, 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
$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]. "
";
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
$suits = Array (
"Spades", "Hearts", "Clubs", "Diamonds"
);
$faces = Array (
"The other", "three", "four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King", "Ace"
);
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
$deck = Array ();
foreach ($suits as $suit) {
foreach ($faces as $face) {
$deck [] = Array ("face" = = $face, "Suit" and "= $suit");
}
}
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
Shuffle ($deck);
$card = Array_shift ($deck);
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
$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 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
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;
}
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
$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 for a specified face or suit
$draw = Array (face =, suit = spades);
$draw = Array (face + Ace, suit = =);
A simple poker licensing device
Now that you've got a deck builder and some tools that can help calculate the odds of extracting a particular card, we can integrate a really simple token to do it. For the purposes of this example, we will build a card that can extract five cards. The card will provide five cards from the entire deck. Use numbers to specify which cards need to be discarded, and the card will be used in a deck of cards
http://www.bkjia.com/PHPjc/486435.html www.bkjia.com true http://www.bkjia.com/PHPjc/486435.html techarticle the script presented in this article is easy to understand, simple to use, and quick to master. Simple craps many games and game systems require dice. Let's start with a simple section: Throw a ...