簡單的擲骰器
許多遊戲和遊戲系統都需要骰子。讓我們先從簡單的部分開始:擲一個六面骰。實際上,滾動一個六面骰子就是從 1 到 6 之間選擇一個隨機數字。在 PHP 中,這十分簡單:echo rand(1,6);。
在許多情況下,這基本上很簡單。但是在處理機率遊戲時,我們需要一些更好的實現。 PHP 提供了更好的隨機數字產生器:mt_rand()。在不深入研究兩者差異的情況下,可以認為 mt_rand 是更快、更好的隨機數字產生器:echo mt_rand(1,6);。如果把該隨機數字產生器放入函數中,則效果會更好。
清單 1. 使用 mt_rand() 隨機數字產生器函數
<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>
然後可以把需要滾動的骰子類型作為參數傳遞給函數。
清單2. 將骰子類型作為參數傳遞
<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>
從這裡開始,我們可以繼續根據需要一次滾動多個骰子,返回結果數組;也可以一次滾動多個不同類型的骰子。但是大多數任務都可以使用這個簡單的腳本。
隨機名稱產生器
如果正在運行遊戲、編寫故事或一次性創建大批字符,有時會疲於應付不斷出現的新名字。讓我們來看看可用於解決此問題的簡單隨機名稱產生器。首先,讓我們建立兩個簡單陣列 — 一個用於名字,一個用於姓氏。
清單3. 名字和姓氏的兩個簡單數組
<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>
然後就可以從每個數組中選擇一個隨機元素:echo $male[array_rand($male)] . ' ' . $last[array_rand($last)];。要一次提取多個名稱,只需混合數組並根據需要提取。
清單 4. 混合名稱陣列
<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>
基於此基本概念,我們可以建立保存名字和姓氏的文字檔案。如果在文字檔案的每一行中存放一個名字,則可以輕鬆地用換行符號分隔檔案內容以建立原始程式碼陣列。
清單5. 創建名稱的文本文件
<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>
構建或查找一些好的名字文件(代碼歸檔中附帶了一些文件),此後我們絕不再需要為名字煩惱。
場景產生器
利用建構名字產生器所使用的相同基本原理,我們可以建構場景產生器。此生成器不但在角色扮演遊戲中十分有用,而且在需要用到偽隨機環境集合(可用於角色扮演、即興創作、寫作等情況)的情況下也十分有用。我最喜歡的遊戲之一,Paranoia 在其 GM Pack 中包含了 “任務混合器(mission blender)”。任務混合器可用於在快速滾動骰子時整合完整任務。讓我們整合自己的場景產生器。
考慮以下場景:您醒來後發現自己迷失於叢林中。您知道自己必須趕去紐約,但不知道原因。您可以聽到附近的狗叫聲及清晰的敵方搜尋者的聲音。您渾身發冷、不住顫抖,而且沒有武器。該場景中的每一句話都介紹場景的特定方面:
「您醒來後發現自己迷失於叢林中」 — 這句話將建立設定。
「您知道自己必須趕去紐約」 — 這句話將描述目標。
「您可以聽到狗叫聲」 — 這句話將介紹敵人。
「您渾身發冷、不住顫抖,而且沒有武器」 — 這句話將添加複雜度。
就像建立名字和姓氏的文字檔案一樣,首先分別建立設定、目標、敵人和複雜度的文字檔案。程式碼歸檔中附帶了範例檔。在擁有這些檔案後,產生場景的程式碼與產生名稱的程式碼基本上相同。
清單 6. 產生場景
<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>
我們可以透過新增文字檔案為場景中新增元素,也可能希望新增多重複雜度。新增到基本文字檔案的內容越多,場景隨時間的變化就越多。
牌組創建器(Deck builder)和裝備(shuffler)
如果您要玩紙牌並且要處理與紙牌相關的腳本,我們需要用裝備中的工具整合一副牌組建構器。首先,讓我們建立一副標準紙牌。需要建立兩個陣列 — 一個用於保存同花色的組牌,而另一個用於保存牌面。如果稍後需要添加新組牌或牌類型,則這樣做將獲得良好的靈活性。
清單 7. 建立一副標準撲克牌
<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>
然後建立一副牌數組來保存所有紙牌值。只需使用一對 foreach 循環即可完成此操作。
清單 8. 建立一副牌數組
<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>
在構建了一副撲克牌數組後,我們可以輕鬆地洗牌並隨機抽出一張牌。
清單 9. 洗牌並隨機抽出一張牌
<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>
現在,我們就獲得了抽取多副牌或構建多層牌盒(multideck shoe)的捷徑。
勝率計算器:發牌
由於構建撲克牌時會分別追蹤每張牌的牌面和花色,因此可以透過程式設計方式利用這副牌來計算得到特定牌的幾率。首先每隻手分別抽出五張牌。
清单 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