Тестируем систему Фибоначчи на PHP

Счет по системе Фибоначчи ведется таким образом.

1 — 1 — 2 — 3 — 5 — 8 — 13 — 21 — 34 — 55 — 89 — 233 — 275

Тоесть каждое последующее число представляет собой сумму двух предыдущих чисел, счет ведется начиная с единицы.

Пример:

1+1 = 2
1+2 = 3
2+3 = 5
3+5 = 8
5+8 = 13 и т.д.


class FSystem
{
    public $startBalance;
    public $log = false;
 
    private $maximum;
    private $total;
    private $currentFibBet = 1;
 
    public function fibonacci($n)
    {
        if ($n == 1 || $n == 2)
            return 1;
        return $this->fibonacci($n - 1) + $this->fibonacci($n - 2);
    }
 
    public function runOnce()
    {
        $this->currentFibBet = 1;
        $this->total         = 0;
        $currentBalance      = $this->startBalance;
 
        while (true) {
            $win        = false;
            $currentBet = $this->fibonacci($this->currentFibBet);
            if ($currentBet > $currentBalance) {
                // проиграли
                if ($this->log)
                    echo "You lose!";
                return false;
            }
 
            if ($currentBalance >= ($this->startBalance * 2)) {
                // выиграли
                if ($this->log)
                    echo "You win!";
                return true;
            }
 
            $this->total = $this->total + 1;
            $currentBalance -= $currentBet;
 
            if (rand(1, 37) === 1) {
                $win = false;
            } else {
                // В идеальной рулетке шанс выпадения черного или красного = 50/50
                if (rand(1, 2) === 1)
                    $win = true;
            }
 
            if ($win) {
                if ($this->log)
                    echo "Число $currentBet выигрывает. Вычеркиваем $currentBet и " . $this->fibonacci(($this->currentFibBet - 1 < 1) ? 1 : $this->currentFibBet - 1);
                $this->currentFibBet = $this->currentFibBet - 2;
                if ($this->currentFibBet < 1)
                    $this->currentFibBet = 1;
                $currentBalance += $currentBet * 2;
                if ($this->log)
                    echo " Новая ставка:" . $this->fibonacci($this->currentFibBet) . "\r\n";
            } else {
                if ($this->log)
                    echo "Число $currentBet проигрывает.";
                $this->currentFibBet = $this->currentFibBet + 1;
                if ($this->log)
                    echo " Новая ставка:" . $this->fibonacci($this->currentFibBet) . "\r\n";
            }
 
            if ($this->maximum < $currentBalance)
                $this->maximum = $currentBalance;
        }
    }
 
    public function runTests($amount)
    {
        $wins  = 0;
        $loses = 0;
        for ($i = 0; $i < $amount; $i++) {
            if ($this->runOnce()) {
                $wins++;
            } else {
                $loses++;
            }
        }
        $winPercents  = ($wins / $amount) * 100;
        $losePercents = 100 - $winPercents;
        $percents     = array(
            'wins' => $winPercents,
            'loses' => $losePercents
        );
        return array(
            'wins' => $wins,
            'loses' => $loses,
            'percents' => $percents
        );
    }
}
 
$casino               = new FSystem();
$casino->startBalance = 100;
 
$result = $casino->runTests(1000);
 
echo 'Результаты: <br />';
echo '<ul>
        <li>Побед: ' . $result['wins'] . '</li>
        <li>Проигрышей: ' . $result['loses'] . '</li>
        <li>% побед: ' . $result['percents']['wins'] . '% </li>
        <li>% поражений: ' . $result['percents']['loses'] . '% </li>
      </ul>';


Результаты:
Побед: 316
Проигрышей: 684
% побед: 31.6%
% поражений: 68.4%



Просмотров: 1802
10.02.2015, 10:05 -

Категория: Статьи » Программирование » PHP