public function __invoke() { $hitPoints = 103; $damagePoints = 9; $armourPoints = 2; $boss = new Boss($hitPoints, $damagePoints, $armourPoints); $game = new Game(new TurnTaker()); $weapons = $this->getWeaponCollection(); $armours = $this->getArmourCollection(); $rings = $this->getRingCollection(); $lowestCost = null; foreach ($weapons as $w => $weapon) { foreach ($armours as $a => $armour) { $permutations = $this->getRingPermutations(array_keys($rings)); foreach ($permutations as $permutation) { $boss->resurrect($hitPoints); $r1 = $permutation[0]; $r2 = $permutation[1]; $player = new Player($weapon, $armour, $rings[$r1], $rings[$r2], 100); /** * @var $winner PlayerInterface */ $winner = $game->play($player, $boss); if (!$winner->isBoss()) { $cost = $weapon->getCost() + $armour->getCost() + $rings[$r1]->getCost() + $rings[$r2]->getCost(); if (!$lowestCost || $cost < $lowestCost) { $lowestCost = $cost; } } } } } $this->write("Lowest amount of gold to win: " . $lowestCost); }
} public abstract function Update(); } /** * 观察者 */ class StockObserver extends Observer { public function __construct($name, $sub) { parent::__construct($name, $sub); } public function Update() { echo " 你赶快跑..."; } } $huhansan = new Boss(); //被观察者 $gongshil = new StockObserver("三毛", $huhansan); //初始化观察者 $gongshil2 = new StockObserver("si毛", $huhansan); //初始化观察者 $huhansan->Attach($gongshil); //添加一个观察者 $huhansan->Attach($gongshil2); //添加一个相同的观察者 $huhansan->Detach($gongshil); //踢出基中一个观察者 $huhansan->Notify(); //通过所有有效的观察者
<?php require_once 'Character.php'; require_once 'Boss.php'; require_once 'Player.php'; require_once 'Item.php'; /* * Create shop */ $weapons = [new Item('Dagger', 8, 4, 0), new Item('Shortsword', 10, 5, 0), new Item('Warhammer', 25, 6, 0), new Item('Longsword', 40, 7, 0), new Item('Greataxe', 74, 8, 0)]; $armors = [new Item('Leather', 13, 0, 1), new Item('Chainmail', 31, 0, 2), new Item('Splintmail', 53, 0, 3), new Item('Bandedmail', 75, 0, 4), new Item('Platemail', 102, 0, 5)]; $rings = [new Item('Damage +1', 25, 1, 0), new Item('Damage +2', 50, 2, 0), new Item('Damage +3', 100, 3, 0), new Item('Defense +1', 20, 0, 1), new Item('Defense +2', 40, 0, 2), new Item('Defense +3', 80, 0, 3)]; $leastSpent = null; $mostSpent = null; while (true) { $boss = new Boss(100, 8, 2); $player = new Player(100, 0, 0); // buy equipment for player // buy 1 weapon $weaponIndex = array_rand($weapons, 1); $player->buy($weapons[$weaponIndex]); // buy 0 or 1 armor $numArmors = rand(0, 1); if ($numArmors == 1) { $armorIndex = array_rand($armors, 1); $player->buy($armors[$armorIndex]); } // buy 0, 1 or 2 rings $numRings = rand(0, 2); for ($i = 0; $i < $numRings; $i++) { $ringIndex = array_rand($rings, 1);
} abstract function update(); } class AObserver extends Observer { function AObserver($name, $subject) { parent::Observer($name, $subject); } function update() { echo $this->name . "收到通知"; } } class BObserver extends Observer { function BObserver($name, $subject) { parent::Observer($name, $subject); } function update() { echo $this->name . "收到通知"; } } $boss = new Boss(); $a = new AObserver('a', $boss); $b = new BObserver('b', $boss); $boss->attach($a); $boss->attach($b); $boss->notify();
<?php spl_autoload_register(function ($class) { require_once $class . '.php'; }); $spells = ['MagicMissile', 'Drain', 'Poison', 'Shield', 'Recharge']; $leastSpent = null; $count = 0; $combinations = []; while (true) { $count++; if ($count % 100000 == 0) { echo 'Iteration ' . number_format($count, 0, '.', ' ') . ", used combinations " . number_format(count($combinations), 0, '.', ' ') . PHP_EOL; } $boss = new Boss(71, 10); $player = new Player(50, 500); $playersTurn = true; $usedSpells = []; $spentMana = 0; // spells $poison = false; $poisonTimer = 0; $shield = false; $shieldTimer = 0; $recharge = false; $rechargeTimer = 0; while ($boss->isAlive() && $player->isAlive()) { // hard mode (part 2) if ($playersTurn) { $player->decreaseHitpoints(1); if (!$player->isAlive()) {
} public abstract function Update(); } /** * 观察者 */ class StockObserver extends Observer { public function __construct($name, $sub) { parent::__construct($name, $sub); } public function Update() { echo $this->_Sub->_action . $this->_UserName . " 你赶快跑..."; } } $huhansan = new Boss(); //被观察者 $gongshil = new StockObserver("三毛", $huhansan); //初始化观察者 $huhansan->Attach($gongshil); //添加一个观察者 $huhansan->Attach($gongshil); //添加一个相同的观察者 $huhansan->Detach($gongshil); //踢出基中一个观察者 $huhansan->SubjectState("警察来了"); //达到满足的条件 $huhansan->Notify(); //通过所有有效的观察者
<?php header('Content-Type:text/html;charset=utf-8'); /* * 观察者模式【通知】 * Author: Kaysen */ define('ROOT_PATH', dirname(__FILE__)); require_once ROOT_PATH . '/../../Loader.php'; // 老板胡汉三 $huhansan = new Boss(); // 看股票的同事 $tongshi1 = new StockObserver('魏关姹', $huhansan); // 看NBA的同事 $tongshi2 = new NBAObserver('易管查', $huhansan); $huhansan->attach($tongshi1); $huhansan->attach($tongshi2); //$huhansan->detach($tongshi1); // 老板回来 $huhansan->setSubjectStatus('我胡汉三回来了!'); // 发出通知 $huhansan->notify();