Simple Dice Roller
Many games and gaming systems require dice. Let's start with the easy part: rolling a six-sided die. Essentially, rolling a six-sided die is simply choosing a random number between 1 and 6. In PHP, this is very simple: echo rand(1,6);.
In many cases, this is basically simple. But when dealing with games of chance, we need some better implementation. PHP provides a better random number generator: mt_rand(). Without delving too deeply into the differences between the two, mt_rand can be thought of as a faster and better random number generator: echo mt_rand(1,6);. It would be even better if you put this random number generator into a function.
Listing 1. Using the mt_rand() random number generator function
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> roll () { </span></span></li> <li> <span class="keyword">return</span><span> mt_rand(1,6); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="func">echo</span><span> roll(); </span> </li> </ol>
Then you can pass the type of dice to be rolled as a parameter to the function.
Listing 2. Passing the dice type as a parameter
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> roll (</span><span class="vars">$sides</span><span>) { </span></span></li> <li> <span class="keyword">return</span><span> mt_rand(1,</span><span class="vars">$sides</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="func">echo</span><span> roll(6); </span><span class="comment">// roll a six-sided die </span><span> </span> </li> <li class="alt"> <span class="func">echo</span><span> roll(10); </span><span class="comment">// roll a ten-sided die </span><span> </span> </li> <li> <span class="func">echo</span><span> roll(20); </span><span class="comment">// roll a twenty-sided die</span><span> </span> </li> </ol>
From here, we can continue to roll as many dice at once as needed, returning an array of results; we can also roll multiple dice of different types at once. But most tasks can be done using this simple script.
Random Name Generator
If you're running a game, writing a story, or creating a large number of characters at once, it can sometimes be overwhelming to deal with the constant stream 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 first names and one for last names.
Listing 3. Two simple arrays of first name and last name
<ol class="dp-c"> <li class="alt"><span><span class="vars">$male</span><span> = </span><span class="keyword">array</span><span>( </span></span></li> <li> <span class="string">"William"</span><span>, </span> </li> <li class="alt"> <span class="string">"Henry"</span><span>, </span> </li> <li> <span class="string">"Filbert"</span><span>, </span> </li> <li class="alt"> <span class="string">"John"</span><span>, </span> </li> <li> <span class="string">"Pat"</span><span>, </span> </li> <li class="alt"><span>); </span></li> <li> <span class="vars">$last</span><span> = </span><span class="keyword">array</span><span>( </span> </li> <li class="alt"> <span class="string">"Smith"</span><span>, </span> </li> <li> <span class="string">"Jones"</span><span>, </span> </li> <li class="alt"> <span class="string">"Winkler"</span><span>, </span> </li> <li> <span class="string">"Cooper"</span><span>, </span> </li> <li class="alt"> <span class="string">"Cline"</span><span>, </span> </li> <li><span>); </span></li> </ol>
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, just mix the arrays and extract as needed.
Listing 4. Mixed Name Array
<ol class="dp-c"> <li class="alt"><span><span>shuffle(</span><span class="vars">$male</span><span>); </span></span></li> <li> <span>shuffle(</span><span class="vars">$last</span><span>); </span> </li> <li class="alt"> <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span> = 0; </span><span class="vars">$i</span><span> <span class="vars">$i</span><span>++) { </span></span> </li> <li> <span class="func">echo</span><span> </span><span class="vars">$male</span><span>[</span><span class="vars">$i</span><span>] . </span><span class="string">' '</span><span> . </span><span class="vars">$last</span><span>[</span><span class="vars">$i</span><span>]; </span> </li> <li class="alt"><span>} </span></li> </ol>
Based on this basic concept, we can create a text file that holds first and last names. If you store a name on each line of a text file, you can easily separate the file contents with newlines to build an array of source code.
Listing 5. Creating a text file of names
<ol class="dp-c"> <li class="alt"><span><span class="vars">$male</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">'n'</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'names.female.txt'</span><span>)); </span></span></li> <li> <span class="vars">$last</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">'n'</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'names.last.txt'</span><span>)); </span> </li> </ol>
Build or find some good name files (some are included in the code archive) and we'll never have to worry about names again.
Scenario Generator
Utilizing the same basic principles we used to build name generators, we can build scenario generators. This generator is useful not only in role-playing games, but also in situations where you need to use a collection of pseudo-random environments (which can be used for role-playing, improvisation, writing, etc.). One of my favorite games, Paranoia, includes a "mission blender" in its GM Pack. The Mission Mixer can be used to combine complete missions while rolling the dice quickly. Let's put together our own scene generator.
Consider the following scenario: You wake up and find yourself lost in the jungle. You know you have to get to New York, but you don’t know why. You can hear dogs barking nearby and the distinct sounds of enemy seekers. You're cold, shaking, and unarmed. Each sentence in the scene introduces a specific aspect of the scene:
“You wake up and find yourself lost in the jungle” — This sentence will establish the setting.
“You know you have to get to New York” — This sentence will describe the goal.
“You can hear the dogs barking” — This sentence will introduce the enemy.
“You’re cold, shaking, and unarmed” — this sentence will add complexity.
Just like you created the text files for First Name and Last Name, first create text files for Settings, Objectives, Enemies, and Complexity respectively. Sample files are included in the code archive. Once you have these files, the code to generate the scene is basically the same as the code to generate the name.
Listing 6. Generating the scene
<ol class="dp-c"> <li class="alt"><span><span class="vars">$settings</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.settings.txt'</span><span>)); </span></span></li> <li> <span class="vars">$objectives</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.objectives.txt'</span><span>)); </span> </li> <li class="alt"> <span class="vars">$antagonists</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.antagonists.txt'</span><span>)); </span> </li> <li> <span class="vars">$complicati</span><span>**** = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.complicati****.txt'</span><span>)); </span> </li> <li class="alt"> <span>shuffle(</span><span class="vars">$settings</span><span>); </span> </li> <li> <span>shuffle(</span><span class="vars">$objectives</span><span>); </span> </li> <li class="alt"> <span>shuffle(</span><span class="vars">$antagonists</span><span>); </span> </li> <li> <span>shuffle(</span><span class="vars">$complicati</span><span>****); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$settings</span><span>[0] . </span><span class="string">' '</span><span> . </span><span class="vars">$objectives</span><span>[0] . </span><span class="string">' '</span><span> . </span><span class="vars">$antagonists</span><span>[0] . </span><span class="string">' '</span><span> </span> </li> <li> <span>. </span><span class="vars">$complicati</span><span>****[0] . </span><span class="string">"<br>n"</span><span>; </span> </li> </ol>
We can add elements to the scene by adding new text files, and we may wish to add multiple levels of complexity. The more content you add to the basic text file, the more the scene changes over time.
Deck builder and shuffler
If you are going to play poker and deal with card-related scripts, we need to integrate a deck builder with the tools in the shuffler. First, let's build a standard deck of cards. Two arrays need to be constructed - one to hold the group of cards of the same suit, and another to hold the face of the card. This gives you great flexibility if you need to add new decks or card types later.
Listing 7. Build a standard deck of playing cards
<ol class="dp-c"> <li class="alt"><span><span class="vars">$suits</span><span> = </span><span class="keyword">array</span><span> ( </span></span></li> <li> <span class="string">"Spades"</span><span>, </span><span class="string">"Hearts"</span><span>, </span><span class="string">"Clubs"</span><span>, </span><span class="string">"Diamonds"</span><span> </span> </li> <li class="alt"><span>); </span></li> <li> <span class="vars">$faces</span><span> = </span><span class="keyword">array</span><span> ( </span> </li> <li class="alt"> <span class="string">"Two"</span><span>, </span><span class="string">"Three"</span><span>, </span><span class="string">"Four"</span><span>, </span><span class="string">"Five"</span><span>, </span><span class="string">"Six"</span><span>, </span><span class="string">"Seven"</span><span>, </span><span class="string">"Eight"</span><span>, </span> </li> <li> <span class="string">"Nine"</span><span>, </span><span class="string">"Ten"</span><span>, </span><span class="string">"Jack"</span><span>, </span><span class="string">"Queen"</span><span>, </span><span class="string">"King"</span><span>, </span><span class="string">"Ace"</span><span> </span> </li> <li class="alt"><span>); </span></li> </ol>
Then build a deck array to hold all card values. This can be done simply using a pair of foreach loops.
Listing 8. Constructing an array of playing cards
<ol class="dp-c"> <li class="alt"><span><span class="vars">$deck</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li> <li> <span class="keyword">foreach</span><span> (</span><span class="vars">$suits</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$suit</span><span>) { </span> </li> <li class="alt"> <span class="keyword">foreach</span><span> (</span><span class="vars">$faces</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$face</span><span>) { </span> </li> <li> <span class="vars">$deck</span><span>[] = </span><span class="keyword">array</span><span> (</span><span class="string">"face"</span><span>=></span><span class="vars">$face</span><span>, </span><span class="string">"suit"</span><span>=></span><span class="vars">$suit</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> </ol>
After constructing an array of playing cards, we can easily shuffle the deck and randomly draw a card.
List 9. Shuffle the deck and randomly draw a card
<ol class="dp-c"> <li class="alt"><span><span>shuffle(</span><span class="vars">$deck</span><span>); </span></span></li> <li> <span class="vars">$card</span><span> = </span><span class="func">array_shift</span><span>(</span><span class="vars">$deck</span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$card</span><span>[</span><span class="string">'face'</span><span>] . </span><span class="string">' of '</span><span> . </span><span class="vars">$card</span><span>[</span><span class="string">'suit'</span><span>]; </span> </li> </ol>
Now we have a shortcut to draw multiple decks of cards or build a multideck shoe.
Win Odds Calculator: Dealing Cards
Since the face and suit of each card are tracked separately when building a poker deck, the deck can be used programmatically to calculate the odds of getting a specific card. First draw five cards from each hand.
清单 10. 每只手抽出五张牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$hands</span><span> = </span><span class="keyword">array</span><span>(1 => </span><span class="keyword">array</span><span>(), 2=></span><span class="keyword">array</span><span>()); </span></span></li> <li> <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span> = 0; </span><span class="vars">$i</span><span> <span class="vars">$i</span><span>++) { </span></span> </li> <li class="alt"> <span class="vars">$hands</span><span>[1][] = implode(</span><span class="string">" of "</span><span>, </span><span class="func">array_shift</span><span>(</span><span class="vars">$deck</span><span>)); </span> </li> <li> <span class="vars">$hands</span><span>[2][] = implode(</span><span class="string">" of "</span><span>, </span><span class="func">array_shift</span><span>(</span><span class="vars">$deck</span><span>)); </span> </li> <li class="alt"><span>} </span></li> </ol>
然后可以查看这副牌,看看剩余多少张牌以及抽到特定牌的机率是多少。查看剩余的牌数十分简单。只需要计算 $deck 数组中包含的元素数。要获得抽到特定牌的机率,我们需要一个函数来遍历整副牌并估算其余牌以查看是否匹配。
清单 11. 计算抽到特定牌的几率
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> calculate_odds(</span><span class="vars">$draw</span><span>, </span><span class="vars">$deck</span><span>) { </span></span></li> <li> <span class="vars">$remaining</span><span> = </span><span class="func">count</span><span>(</span><span class="vars">$deck</span><span>); </span> </li> <li class="alt"> <span class="vars">$odds</span><span> = 0; </span> </li> <li> <span class="keyword">foreach</span><span> (</span><span class="vars">$deck</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$card</span><span>) { </span> </li> <li class="alt"> <span class="keyword">if</span><span> ( (</span><span class="vars">$draw</span><span>[</span><span class="string">'face'</span><span>] == </span><span class="vars">$card</span><span>[</span><span class="string">'face'</span><span>] && </span><span class="vars">$draw</span><span>[</span><span class="string">'suit'</span><span>] == </span> </li> <li> <span class="vars">$card</span><span>[</span><span class="string">'suit'</span><span>] ) || </span> </li> <li class="alt"> <span>(</span><span class="vars">$draw</span><span>[</span><span class="string">'face'</span><span>] == </span><span class="string">''</span><span> && </span><span class="vars">$draw</span><span>[</span><span class="string">'suit'</span><span>] == </span><span class="vars">$card</span><span>[</span><span class="string">'suit'</span><span>] ) || </span> </li> <li> <span>(</span><span class="vars">$draw</span><span>[</span><span class="string">'face'</span><span>] == </span><span class="vars">$card</span><span>[</span><span class="string">'face'</span><span>] && </span><span class="vars">$draw</span><span>[</span><span class="string">'suit'</span><span>] == </span><span class="string">''</span><span> ) ) { </span> </li> <li class="alt"> <span class="vars">$odds</span><span>++; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="keyword">return</span><span> </span><span class="vars">$odds</span><span> . </span><span class="string">' in '</span><span> </span><span class="vars">$remaining</span><span>; </span> </li> <li class="alt"><span>} </span></li> </ol>
现在可以选出尝试抽出的牌。为了简单起见,传入看上去类似某张牌的数组。我们可以查找特定的一张牌。
清单 12. 查找指定的一张牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$draw</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">'face'</span><span> => </span><span class="string">'Ace'</span><span>, </span><span class="string">'suit'</span><span> => </span><span class="string">'Spades'</span><span>); </span></span></li> <li> <span class="func">echo</span><span> implode(</span><span class="string">" of "</span><span>, </span><span class="vars">$draw</span><span>) . </span><span class="string">' : '</span><span> . calculate_odds(</span><span class="vars">$draw</span><span>, </span><span class="vars">$deck</span><span>); </span> </li> </ol>
或者可以查找指定牌面或花色的牌。
清单 13. 查找指定牌面或花色的牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$draw</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">'face'</span><span> => </span><span class="string">''</span><span>, </span><span class="string">'suit'</span><span> => </span><span class="string">'Spades'</span><span>); </span></span></li> <li> <span class="vars">$draw</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">'face'</span><span> => </span><span class="string">'Ace'</span><span>, </span><span class="string">'suit'</span><span> => </span><span class="string">''</span><span>); </span> </li> </ol>
1