1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| <?php
class RandomExamples { public function demonstrateRandomizers(): void { echo "=== 新随机扩展示例 ===\n"; $randomizer = new Random\Randomizer(); echo "随机整数 (1-100): " . $randomizer->getInt(1, 100) . "\n"; $randomBytes = $randomizer->getBytes(16); echo "随机字节 (hex): " . bin2hex($randomBytes) . "\n"; $array = ['apple', 'banana', 'cherry', 'date', 'elderberry']; $shuffled = $randomizer->shuffleArray($array); echo "打乱的数组: " . implode(', ', $shuffled) . "\n"; $picked = $randomizer->pickArrayKeys($array, 3); echo "随机选择的键: " . implode(', ', $picked) . "\n"; $this->demonstrateEngines(); } private function demonstrateEngines(): void { echo "\n=== 不同随机引擎示例 ===\n"; $mt19937 = new Random\Engine\Mt19937(12345); $randomizer1 = new Random\Randomizer($mt19937); echo "Mt19937 引擎结果:\n"; for ($i = 0; $i < 5; $i++) { echo " " . $randomizer1->getInt(1, 1000) . "\n"; } $pcg = new Random\Engine\PcgOneseq128XslRr64(12345); $randomizer2 = new Random\Randomizer($pcg); echo "\nPCG 引擎结果:\n"; for ($i = 0; $i < 5; $i++) { echo " " . $randomizer2->getInt(1, 1000) . "\n"; } $xoshiro = new Random\Engine\Xoshiro256StarStar( hash('sha256', 'seed', true) ); $randomizer3 = new Random\Randomizer($xoshiro); echo "\nXoshiro256** 引擎结果:\n"; for ($i = 0; $i < 5; $i++) { echo " " . $randomizer3->getInt(1, 1000) . "\n"; } } public function gameRandomSystem(): void { echo "\n=== 游戏随机系统示例 ===\n"; $gameRandomizer = new Random\Randomizer(); $equipment = $this->generateRandomEquipment($gameRandomizer); echo "随机装备: " . json_encode($equipment, JSON_PRETTY_PRINT) . "\n"; $battleResult = $this->simulateBattle($gameRandomizer); echo "战斗结果: " . json_encode($battleResult, JSON_PRETTY_PRINT) . "\n"; } private function generateRandomEquipment(Random\Randomizer $randomizer): array { $types = ['sword', 'shield', 'armor', 'helmet', 'boots']; $rarities = ['common', 'uncommon', 'rare', 'epic', 'legendary']; $attributes = ['strength', 'defense', 'agility', 'magic', 'luck']; return [ 'type' => $randomizer->pickArrayKeys($types, 1)[0], 'rarity' => $randomizer->pickArrayKeys($rarities, 1)[0], 'level' => $randomizer->getInt(1, 100), 'attributes' => array_combine( $randomizer->pickArrayKeys($attributes, 3), [ $randomizer->getInt(10, 100), $randomizer->getInt(10, 100), $randomizer->getInt(10, 100) ] ) ]; } private function simulateBattle(Random\Randomizer $randomizer): array { $playerHealth = 100; $enemyHealth = 100; $round = 0; $log = []; while ($playerHealth > 0 && $enemyHealth > 0 && $round < 10) { $round++; $playerDamage = $randomizer->getInt(15, 25); $criticalHit = $randomizer->getFloat(0, 1) < 0.2; if ($criticalHit) { $playerDamage *= 2; $log[] = "第{$round}回合: 玩家暴击造成 {$playerDamage} 伤害!"; } else { $log[] = "第{$round}回合: 玩家造成 {$playerDamage} 伤害"; } $enemyHealth -= $playerDamage; if ($enemyHealth <= 0) { $log[] = "玩家获胜!"; break; } $enemyDamage = $randomizer->getInt(10, 20); $playerHealth -= $enemyDamage; $log[] = "敌人造成 {$enemyDamage} 伤害"; if ($playerHealth <= 0) { $log[] = "敌人获胜!"; break; } } return [ 'rounds' => $round, 'player_health' => max(0, $playerHealth), 'enemy_health' => max(0, $enemyHealth), 'winner' => $playerHealth > 0 ? 'player' : 'enemy', 'battle_log' => $log ]; } }
$randomExamples = new RandomExamples(); $randomExamples->demonstrateRandomizers(); $randomExamples->gameRandomSystem(); ?>
|