Slot Machine

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Slot Machine

Post by Epiales »

Okay, found some code to work with in order to try and make a slot machine. I have it working where you click on spin the wheel and it shows all the images, but when I get three images in a row, it doesn't show how much they won. I'm new to using arrays, so I figured I'd get help here as usual. :) YAY Thanks

Code: Select all

<?php

    $Bells="<img src='bells.png'>";
    $Cherry="<img src='cherry.png'>";
    $Watermellon="<img src='watermellon.png'>";
    $Diamond="<img src='diamond1.png'>";
    $Seven="<img src='seven.png'>";
    $Orange="<img src='orange.png'>";
    
$faces = array ('<img src = "cherry.png" name = "Cherry">', '<img src = "bells.png" name = "Bells">', '<img src = "orange.png">', '<img src = "watermellon.png">', '<img src = "diamond1.png" name = "Diamond">', '<img src = "seven.png" name = "Seven">');

$payouts = array (
    '$Bells|$Bells|$Bells' => '5',
    '$Watermellon|$Watermellon|$Watermellon' => '10',
    '$Orange|$Orange|$Orange' => '15',
    '$Cherry|$Cherry|$Cherry' => '20',
    '$Seven|$Seven|$Seven' => '70',
    '$Diamond|$Diamond|$Diamond' => '100',
);


$wheel1 = array();
foreach ($faces as $face) {
    $wheel1[] = $face;
}
$wheel2 = array_reverse($wheel1);
$wheel3 = $wheel1;


if (isset($_POST['payline'])) {
    list($start1, $start2, $start3) = unserialize($_POST['payline']);
} else {
    list($start1, $start2, $start3) = array(0,0,0);
}

$stop1 = rand(count($wheel1)+$start1, 10*count($wheel1)) % count($wheel1);
$stop2 = rand(count($wheel1)+$start2, 10*count($wheel1)) % count($wheel1);
$stop3 = rand(count($wheel1)+$start3, 10*count($wheel1)) % count($wheel1);

$result1 = $wheel1[$stop1];
$result2 = $wheel2[$stop2];
$result3 = $wheel3[$stop3];

echo "Result: " . $result1 . ' ' . $result2 . ' ' . $result3 . "<br />";

if (isset($payouts[$result1.'|'.$result2.'|'.$result3])) {
    // give the payout
    echo "You won : " . $payouts[$result1.'|'.$result2.'|'.$result3];
}

?>
<br />
<form method='post'>
<input type='hidden' name='payline' value='<?php echo serialize(array($stop1, $stop2, $stop3)) ?>' />
<input type='submit' value='spin the wheel' />
</form>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Slot Machine

Post by Epiales »

Okay, I made these changes, and no difference, but it still works. Just not showing that you win when you match them.

Code: Select all

    $Bells="<img src=\"bells.png\" alt=\"Bells\" />";
    $Cherry="<img src=\"cherry.png\" alt=\"Cherry\" />";
    $Watermellon="<img src=\"watermellon.png\" alt=\"Watermellon\" />";
    $Diamond="<img src=\"diamond1.png\" alt=\"Diamond\" />";
    $Seven="<img src=\"seven.png\" alt=\"Seven\" />";
    $Orange="<img src=\"orange.png\" alt=\"Orange\" />";
    
$faces = array ("$Cherry", "$Bells", "$Watermellon", "$Diamond", "$Seven", "$Orange");

$payouts = array (
    'Bells|Bells|Bells' => '5',
    'Watermellon|Watermellon|Watermellon' => '10',
    'Orange|Orange|Orange' => '15',
    'Cherry|Cherry|Cherry' => '20',
    'Seven|Seven|Seven' => '70',
    'Diamond|Diamond|Diamond' => '100',
); 
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Slot Machine

Post by Epiales »

Okay, I was able to fix it. I went a little bit difference route. Thanks anyway :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
april927
Posts: 1
Joined: Mon Dec 22, 2014 6:42 pm

Re: Slot Machine

Post by april927 »

Im going to apply this, tnx !
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Slot Machine

Post by Epiales »

april927 wrote:Im going to apply this, tnx !
Here is what I finished with...

Code: Select all

<?php
 //////////////////////////////////BEGINNING OF THE CASINO GAME SLOTS////////////////////////////////

 if(isset($_GET['slots'])) { ?> 
 
<body id="slot">
  <div id="wrapper">

<div id="pics">

<?php

    $Bells="<img src=\"images/casino/slots/bells.png\" alt=\"Bells\" />";
    $Cherry="<img src=\"images/casino/slots/cherry.png\" alt=\"Cherry\" />";
    $Watermellon="<img src=\"images/casino/slots/watermellon.png\" alt=\"Watermellon\" />";
    $Diamond="<img src=\"images/casino/slots/diamond1.png\" alt=\"Diamond\" />";
    $Seven="<img src=\"images/casino/slots/seven.png\" alt=\"Seven\" />";
    $Orange="<img src=\"images/casino/slots/orange.png\" alt=\"Orange\" />";
    
$slots = array ("$Cherry", "$Bells", "$Watermellon", "$Diamond", "$Seven", "$Orange");

$one_t=rand(0,3);
$two_t=rand(0,3);
$three_t=rand(0,3);

$one=$slots[$one_t];
$two=$slots[$two_t];
$three=$slots[$three_t];

echo ("<div id=\"slots\">$one $two $three</div>"); ?>



<div id="spin">
<button onClick="javascript:history.go(0)">SPIN</button>
</div>
  
<div id="messages"> 


<?php

$one=$slots[$one_t];
$two=$slots[$two_t];
$three=$slots[$three_t];

if ($one == $two && $two == $three) {
echo "<br /><br />Congratulations, you won!";
} else { 
echo "<br /><br />Try Again";
}}
 //////////////////////////////////ENDING OF THE CASINO GAME SLOTS////////////////////////////////

?>
Hope it helps
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Slot Machine

Post by Sim »

Epiales wrote:
april927 wrote:Im going to apply this, tnx !
Here is what I finished with...

Code: Select all

<?php
 //////////////////////////////////BEGINNING OF THE CASINO GAME SLOTS////////////////////////////////

 if(isset($_GET['slots'])) { ?> 
 
<body id="slot">
  <div id="wrapper">

<div id="pics">

<?php

    $Bells="<img src=\"images/casino/slots/bells.png\" alt=\"Bells\" />";
    $Cherry="<img src=\"images/casino/slots/cherry.png\" alt=\"Cherry\" />";
    $Watermellon="<img src=\"images/casino/slots/watermellon.png\" alt=\"Watermellon\" />";
    $Diamond="<img src=\"images/casino/slots/diamond1.png\" alt=\"Diamond\" />";
    $Seven="<img src=\"images/casino/slots/seven.png\" alt=\"Seven\" />";
    $Orange="<img src=\"images/casino/slots/orange.png\" alt=\"Orange\" />";
    
$slots = array ("$Cherry", "$Bells", "$Watermellon", "$Diamond", "$Seven", "$Orange");

$one_t=rand(0,3);
$two_t=rand(0,3);
$three_t=rand(0,3);

$one=$slots[$one_t];
$two=$slots[$two_t];
$three=$slots[$three_t];

echo ("<div id=\"slots\">$one $two $three</div>"); ?>



<div id="spin">
<button onClick="javascript:history.go(0)">SPIN</button>
</div>
  
<div id="messages"> 


<?php

$one=$slots[$one_t];
$two=$slots[$two_t];
$three=$slots[$three_t];

if ($one == $two && $two == $three) {
echo "<br /><br />Congratulations, you won!";
} else { 
echo "<br /><br />Try Again";
}}
 //////////////////////////////////ENDING OF THE CASINO GAME SLOTS////////////////////////////////

?>
Hope it helps
Nice. This looks' better and functions better ;]

Code: Select all


// " is not needed on variables ;]
$slots = array ($Cherry, $Bells, $Watermellon, $Diamond, $Seven, $Orange);

//added count: You only showed first 4, leaving 2 out of the array.
//I also used count($slots) - 1 in case you ever add or remove additional img's, the code will not have to be modified
$count = (count($slots) - 1);


$one_t=rand(0,$count);
$two_t=rand(0,$count);
$three_t=rand(0,$count);

 
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Post Reply

Return to “Beginner Help and Support”