Skip to content

Instantly share code, notes, and snippets.

@dwgebler

dwgebler/Slot.php

Created Feb 11, 2019
Embed
What would you like to do?
<?php
// Our slot will be 3 reels, one win line, with 3 symbols.
// X, BAR and 7.
// 0 = X, 1 = BAR, 2 = 7
// This is the layout of the reels, running vertically. There are 20
// symbols on each reel.
$reels = [
// reel 1 has 10 X's, 7 Bars and 3 7s
[0,1,0,1,0,1,0,1,2,2,1,0,1,0,0,0,0,2,1,0],
// reel 2 has 11 X's, 7 Bars and 2 7s
[1,1,0,0,0,1,0,2,0,1,1,0,0,2,0,1,0,0,1,0],
// reel 3 has 9 X's, 9 Bars and 2 7s
[0,0,0,1,1,1,0,1,2,0,0,1,1,2,0,0,1,1,0,1],
];
// Let's map to the symbols:
$symbols = [
0 => 'X',
1 => 'B',
2 => '7',
];
// Match 3 of the same symbol on the winline for a prize.
// Xs will be the lowest win at 2x, BARs will pay 10x, 7s will pay a whopping 100x
$paytable = [
'X' => 2,
'B' => 10,
'7' => 100,
];
// How many spins our game will run for.
$spins = 100000;
// Bet size in £/$/whatever
$bet = 1;
// How much money have we spun through in bets?
$totalBet = 0;
// How much money have we won in total?
$totalWin = 0;
// What is the RTP over the number of spins, as a percentage?
$rtp = 0;
// Placeholder for counting the occurrences of each of the 3 possible types
// of win.
$wins = [
'X' => 0,
'B' => 0,
'7' => 0,
];
// Game loop - run for $spins
for ($i=0; $i < $spins; $i++) {
// Let's take the money!
$totalBet = $totalBet + $bet;
// We start by selecting a genuinely random reel position for each of the 3
// reels. Remember - there are 20 possible positions on each reel.
// The positions selected randomly are the symbols which will appear on
// the win line.
$reelPositions = [random_int(0, 19), random_int(0, 19), random_int(0, 19)];
// What symbols do these positions map to? This creates the win line.
$reelSymbols = [
$symbols[$reels[0][$reelPositions[0]]], // first reel
$symbols[$reels[1][$reelPositions[1]]], // second
$symbols[$reels[2][$reelPositions[2]]], // third
];
// At this point, $symbols contains something like ['X', 'X', 'B'] or
// whatever our combination is. Let's display it in the form X-X-B.
//echo implode('-', $reelSymbols)."\n";
// Now let's calculate if it was a win. To be a win, it must be the same
// symbol on all 3 reels.
// One way we can do this is to count how many different symbols are
// in the win line - if it's 1, they must all be the same.
if (count(array_unique($reelSymbols)) === 1) {
// It's a win. Let's compare the symbol on the win line to the paytable
// and add that amount of money to the total wins.
$totalWin = $totalWin + $paytable[$reelSymbols[0]];
// Now let's add that symbol to our running count of how many wins
// of each type took place.
$wins[$reelSymbols[0]] = $wins[$reelSymbols[0]] + 1;
}
}
// Let's calculate RTP. This is the total wins divided by the total bet, then
// multiplied by 100 to give a percentage.
$rtp = ($totalWin / $totalBet) * 100;
// Let's see what the results were:
echo 'Spins: '.$spins."\n";
echo 'Total bet: '.$totalBet."\n";
echo 'Total wins: '.$totalWin."\n";
echo 'Number of X-X-X: '.$wins['X']."\n";
echo 'Number of B-B-B: '.$wins['B']."\n";
echo 'Number of 7-7-7: '.$wins['7']."\n";
echo 'RTP: '.$rtp."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment