Jump to content

Making A Slot Machine - Help me!


Snooble

Recommended Posts

Hey everyone, I'm making the serverside code for a slot machine so far i have:

 

  <?php
////////USER VARIABLES//////////

$linebet = 5;
$numberoflines = 9;

////////USER VARIABLES//////////

//////////////////////////////////////////REELS///////////////////////////
function spin(&$number){

$number = rand(0, 100);
if ($number <= 20){$number = BAR;}
elseif($number < 45 && $number > 20){$number = X;}
else{$number = O;}
echo $number;

}


/////////////////////////////////////////REELS/////////////////////////////
?>
  <br />
  <br />
  <br />
  <table width="338" height="89" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="20%" height="40"><?php spin($aa); ?></td>
      <td width="20%" height="40"><?php spin($ba); ?></td>
      <td width="20%" height="40"><?php spin($ca); ?></td>
      <td width="20%" height="40"><?php spin($da); ?></td>
      <td width="20%" height="40"><?php spin($ea); ?></td>
    </tr>
    <tr>
      <td width="20%" height="40"><?php spin($ab); ?></td>
      <td width="20%" height="40"><?php spin($bb); ?></td>
      <td width="20%" height="40"><?php spin($cb); ?></td>
      <td width="20%" height="40"><?php spin($db); ?></td>
      <td width="20%" height="40"><?php spin($eb); ?></td>
    </tr>
    <tr>
      <td width="20%" height="40"><?php spin($ac); ?></td>
      <td width="20%" height="40"><?php spin($bc); ?></td>
      <td width="20%" height="40"><?php spin($cc); ?></td>
      <td width="20%" height="40"><?php spin($dc); ?></td>
      <td width="20%" height="40"><?php spin($ec); ?></td>
    </tr>
  </table>
  <br />
  PAYOUT - LINES BET - PER LINE - <a href="index.php">SPIN</a><br />
  <?php 
/////////////WINS/////////////////
function linecheck(&$lineone, &$linetwo, &$linethree, &$linefour, &$linefive, &$result, &$symbol){

if($lineone == $linetwo && $linetwo == $linethree && $linethree != $linefour){
			if ($lineone == "BAR" && $linetwo == "BAR" && $linethree == "BAR"){
				$result = 3 * 1;
				$symbol = "BAR";
				}
			if ($lineone == "X" && $linetwo == "X" && $linethree == "X"){
				$result = 2 * 1;
				$symbol = "X";
				}
			if ($lineone == "O" && $linetwo == "O" && $linethree == "O"){
				$result = 1 * 1;
				$symbol = "O";
				}
}
if($lineone == $linetwo && $linetwo == $linethree && $linethree == $linefour && $linefour != $linefive){
			if ($lineone == "BAR" && $linetwo == "BAR" && $linethree == "BAR" && $linefour == "BAR"){
				$result = 3 * 4;
				$symbol = "BAR";
				}
			if ($lineone == "X" && $linetwo == "X" && $linethree == "X" && $linefour == "X"){
				$result = 2 * 4;
				$symbol = "X";
				}
			if ($lineone == "O" && $linetwo == "O" && $linethree == "O" && $linefour == "O"){
				$result = 1 * 4;
				$symbol = "O";
				}
}
if($lineone == $linetwo && $linetwo == $linethree && $linethree == $linefour && $linefour == $linefive){
			if ($lineone == "BAR" && $linetwo == "BAR" && $linethree == "BAR" && $linefour == "BAR" && $linefive == "BAR"){
				$result = 3 * 5;
				$symbol = "BAR";
				}
			if ($lineone == "X" && $linetwo == "X" && $linethree == "X" && $linefour == "X" && $linefive == "X"){
				$result = 2 * 5;
				$symbol = "X";
				}
			if ($lineone == "O" && $linetwo == "O" && $linethree == "O" && $linefour == "O" && $linefive == "O"){
				$result = 1 * 5;
				$symbol = "O";
				}
}

}

linecheck($ab, $bb, $cb, $db, $eb, $one, $onesymbol);
linecheck($aa, $ba, $ca, $da, $ea, $two, $twosymbol);
linecheck($ac, $bc, $cc, $dc, $ec, $three, $threesymbol);
linecheck($aa, $bb, $cc, $db, $ea, $four, $foursymbol);
linecheck($ac, $bb, $ca, $db, $ec, $five, $fivesymbol);
linecheck($ac, $bc, $cb, $da, $ea, $six, $sixsymbol);
linecheck($aa, $ba, $cb, $dc, $ec, $seven, $sevensymbol);
linecheck($ab, $bc, $cc, $dc, $eb, $eight, $eightsymbol);
linecheck($ab, $ba, $ca, $da, $eb, $nine, $ninesymbol);



/////////////////////WINNING LINES////////////////////////////
function ifwin(&$line, &$start, &$linenumber, $linebet){
if($line != ""){
			switch($start){
				case "BAR":
				$line = $linebet * $line;
				break;
				case "X":
				$line = $linebet * $line;
				break;
				case "O":
				$line = $linebet * $line;
				break;
			}
echo "<br><br>You've won: ".$line." on line ".$linenumber."!";
}
}
////////////////////////WINNING LINES/////////////////////////
$no1 = 1;
$no2 = 2;
$no3 = 3;
$no4 = 4;
$no5 = 5;
$no6 = 6;
$no7 = 7;
$no8 = 8;
$no9 = 9;

switch($numberoflines){
case 1:
ifwin($one, $onesymbol, $no1, $linebet);
break;
case 2:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
break;
case 3:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
break;
case 4:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
ifwin($four, $foursymbol, $no4, $linebet);
break;
case 5:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
ifwin($four, $foursymbol, $no4, $linebet);
ifwin($five, $fivesymbol, $no5, $linebet);
break;
case 6:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
ifwin($four, $foursymbol, $no4, $linebet);
ifwin($five, $fivesymbol, $no5, $linebet);
ifwin($six, $sixsymbol, $no6, $linebet);
break;
case 7:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
ifwin($four, $foursymbol, $no4, $linebet);
ifwin($five, $fivesymbol, $no5, $linebet);
ifwin($six, $sixsymbol, $no6, $linebet);
ifwin($seven, $sevensymbol, $no7, $linebet);
break;
case 8:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
ifwin($four, $foursymbol, $no4, $linebet);
ifwin($five, $fivesymbol, $no5, $linebet);
ifwin($six, $sixsymbol, $no6, $linebet);
ifwin($seven, $sevensymbol, $no7, $linebet);
ifwin($eight, $eightsymbol, $no8, $linebet);
break;
case 9:
ifwin($one, $onesymbol, $no1, $linebet);
ifwin($two, $twosymbol, $no2, $linebet);
ifwin($three, $threesymbol, $no3, $linebet);
ifwin($four, $foursymbol, $no4, $linebet);
ifwin($five, $fivesymbol, $no5, $linebet);
ifwin($six, $sixsymbol, $no6, $linebet);
ifwin($seven, $sevensymbol, $no7, $linebet);
ifwin($eight, $eightsymbol, $no8, $linebet);
ifwin($nine, $ninesymbol, $no9, $linebet);
break;

}

/////////////WINS/////////////////
?>

 

There must be a nicer way to do all this. It shouldn't be too hard to understand, but I don't think I need a lot of the parts I have.

 

The thing is, I want to add up all the wins from each line and output them as a total to the user!

 

Thanks in advance,

 

Sam

 

Link to comment
https://forums.phpfreaks.com/topic/126052-making-a-slot-machine-help-me/
Share on other sites

This seems to do the trick. I simplified a lot of your code and hopefully made it a bit easier to read. The output is just going to do total winnings. If you want for each individual line, you just need to output the $results array for that particular line.

 

Example:

for ($x = 0; $x <= sizeof($results); $x++) {
   if ($results[$x] > 0) {
         echo 'You won $' . ($results[$x] * $linebet) . ' on line ' . ($x+1) . '.';
   }
}

 

Here is the code!

<?php
////////USER VARIABLES//////////

$linebet = 5;

////////USER VARIABLES//////////

//////////////////////////////////////////REELS///////////////////////////
function spin(){
$number = rand(0, 100);
if ($number <= 20) {
	$number = BAR;
} elseif ($number < 45 && $number > 20) {
	$number = X;
} else {
	$number = O;
}
return $number;
}

/////////////WINS/////////////////
function linecheck($slots) {
$result = 0;
if ($slots[0] == $slots[1] && $slots[1] == $slots[2]) {
if ($slots[2] != $slots[3]) {
	$amt = 3;
}
if ($slots[2] == $slots[3] && $slots[3] != $slots[4]) {
	$amt = 4;
} elseif ($slots[2] == $slots[3] && $slots[3] == $slots[4]) {
	$amt = 5;
}

switch($slots[0]) {
	case "BAR": $result = 3 * $amt; break;
	case "X": $result = 2 * $amt; break;
	case "O": $result = 1 * $amt; break;
}
}

return $result;
}

////// Getting Slots Values ///////
for ($x = 0; $x <= 14; $x++) {
$slots[] = spin();
}

////// Sending Values to Check Winnings ///////
$line[0] = array($slots[0], $slots[1], $slots[2], $slots[3], $slots[4]);
$line[1] = array($slots[5], $slots[6], $slots[7], $slots[8], $slots[9]);
$line[2] = array($slots[10], $slots[11], $slots[12], $slots[13], $slots[14]);
$line[3] = array($slots[0], $slots[6], $slots[12], $slots[8], $slots[4]);
$line[4] = array($slots[10], $slots[6], $slots[2], $slots[8], $slots[14]);
$line[5] = array($slots[5], $slots[1], $slots[2], $slots[3], $slots[9]);
$line[6] = array($slots[5], $slots[11], $slots[12], $slots[13], $slots[9]);
$line[7] = array($slots[10], $slots[11], $slots[7], $slots[3], $slots[4]);
$line[8] = array($slots[0], $slots[1], $slots[7], $slots[13], $slots[14]);

////// Calculate Winnings Amount ///////
$winnings = 0;
for ($x = 0; $x <= sizeof($line); $x++) {
$results[$x] = linecheck($line[$x]);
$winnings += ($results[$x] * $linebet);
}


////// Output ///////
echo '<br /><br /><br />
<table width="338" height="89" border="0" cellpadding="0" cellspacing="0">
<tr>';
for ($x = 0; $x <= 14; $x++) {
echo '<td width="20%" height="40">' . $slots[$x] . '</td>';
if ((($x+1) % 5) == 0) {
	echo '</tr><tr>';
}
}
echo '</table><br />
PAYOUT - LINES BET - PER LINE - <a href="slots.php">SPIN</a><br />
You\'ve won a total of $' . $winnings . ' with your bet of $' . $linebet . '.';
?>

I made a small feature addon... allows me to choose how many lines are active. just with if statements around the function calls.

 

Thanks ever so much, now i make the flash shell and symbols!

 

I'll pm you a link at the end so you can see your code being used!

 

Sam

Great, I'm glad to hear it's working out for you. I almost pulled my hair out trying to figure out what exactly was all being done haha.

 

One quick tip - Group the functions together at the top, and do the calls and output at the bottom of the page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.