PHP Slots game

By Jonesy44 on Apr 23, 2008

Fairly useless slots game. hehe. can be used on any named webpage, the script will retrieve the name and re-direct blaaa. not much to it really. i was learning functions, and came up with it :P

Example: http://wfs.myartsonline.com/function/

<?php

// PHP Slots Script by Jonesy44 //
// http://www.hawkee.com        //

session_start();

echo '<style>
      #slots {
        border: 1px solid green;
        background: lightgreen url(\'slots.jpg\'); 
        padding: 10px;
        width: 200px; 
        text-align:left;
      }
      #gborder {
        border: 1px solid green;
        background: lightgreen; 
        padding: 10px;
        width: 200px; 
        text-align:center;
      }
      #rborder {
        border: 1px solid red;
        background: pink; 
        padding: 10px;
        width: 200px; 
        text-align:center;
      }
      #border {
        border: 1px solid orange;
        background: white; 
        padding: 10px;
        width: 200px; 
        text-align:center;
      }
      </style>';

function playSlots() {
  $r1 = rand(0,9);
  $r2 = rand(0,9);
  $r3 = rand(0,9);
  echo '<div id="slots"><h1>&nbsp;&nbsp;&nbsp;' .$r1. ' &nbsp;&nbsp;&nbsp;&nbsp; ' .$r2. ' &nbsp;&nbsp;&nbsp; ' .$r3. '</h1></div>';
  if ($r1 == $r2) {
    if ($r2 == $r3) {
      echo '<div id="gborder">Complete win!<br><b> + $2000</b></div>';
      $_SESSION["slots"] = $_SESSION["slots"] + 2000;
    }
    else {
      echo '<div id="gborder">First two!<br><b> + $250</b></div>';
      $_SESSION["slots"] = $_SESSION["slots"] + 250;
    }
  }
  elseif ($r2 == $r3) {
    if ($r1 != $r2) {
      echo '<div id="gborder">Second two!<br><b> + $250</b></div>';
      $_SESSION["slots"] = $_SESSION["slots"] + 250;
    }
  }
  elseif ($r1 == $r3) {
    echo '<div id="gborder">First and last!<br><b> + $250</b></div>';
    $_SESSION["slots"] = $_SESSION["slots"] + 250;
  }
  else {
    echo '<div id="rborder">You Lose!<br><i> - $100</i></div>';
    $_SESSION["slots"] = $_SESSION["slots"] - 100;
  }
}

if ($_GET["play"]) {
  if (!isset($_SESSION["slots"])) {
    $_SESSION["slots"] = 1000;
  }
  echo '<center>';
  playSlots();
  echo '<br><form action=' .$_SERVER['PHP_SELF']. ' method=get>';
  echo '<input type=hidden name=play value=slots>
        <input type=submit value="Spin Wheels!">
        </form>';
}
else {
  echo '<form action=' .$_SERVER['PHP_SELF']. ' method=get>';
  echo '<input type=hidden name=play value=slots>
        <input type=submit value="Play Slots!">
        </form>';
  exit();
}
echo '<div id="border">Cash: $' .$_SESSION["slots"]. '<hr><a href=http://wfs.myartsonline.com><i>PHP Slots Script By Jonesy44</a> - <a href=http://www.hawkee.com>Hawkee.com</a><hr></div>';
echo '</center>';

?>

Comments

Sign in to comment.
Hawkee   -  Aug 25, 2008

This would be neat as an OpenSocial app. You can run it through our Hawkee container to come up with a high score rank with your friends. Here is an example, http://www.hawkee.com/snippet/4818/ - Just create an XML document containing your code and paste the URL into the Test Script box. You can even store the scores on our database.

 Respond  
Hawkee   -  Aug 25, 2008

Your example doesn't seem to be working anymore.

 Respond  
Jonesy44   -  Apr 24, 2008

FIXED

The ordering wasnt working as i set it as a varchar rather than int, it\'s now sorted :-)

So it should be ready to be updated on here by tomorow :-)

 Respond  
Jonesy44   -  Apr 24, 2008

haha, not at all. I\'m hopin to code an arcade style thing soon, so it will have a login system and all. :P

im looking into to seeing if i can actually grab a completely unique ident for a computer, like a mac address or something. im no genius as server side stuff, apart from i can do some php/sql xD

 Respond  
Bouncer   -  Apr 24, 2008

Wasn\'t aware I had a dynamic IP jonesy44 but that would explain it wouldn\'t it, lol. Sorry about the confusion on my part then.

 Respond  
Jonesy44   -  Apr 24, 2008

Your IP has changed, Bouncer :-s
Perhaps a username and pass would be better ..

okay, i will try when i get back from school, furby :P

 Respond  
F*U*R*B*Y*   -  Apr 23, 2008

i dunno how your doing the scores, in to the database, haven\'t really looked, but if you insert them as 1234567 and 134 then just do ORDER BY score DESC then when echo\'ing use, number_format($something[score]);

should solve the problem

 Respond  
Bouncer   -  Apr 23, 2008

Another problem I just noticed. It saves your score, but if you ever disconnect from the page and go back, even if you use the same nick, you start over on your score. Your old one is still listed there, but you start a new one when you re-connect. :S

 Respond  
Jonesy44   -  Apr 23, 2008

haha, go nuts. the message system isn\'t 100% working atm, i cant flush the headers which is an ass. anyways. have fun, and invite friends :P

haha

if it goes down again, just wait untill the new hour, it will reset itself, (it\'s a shit server) so it cocks up xD

 Respond  
Bouncer   -  Apr 23, 2008

And when I finally decide to say something, it clears up, lol. Oh well, at least I can play again :P

 Respond  
Bouncer   -  Apr 23, 2008

Yeah, addicting it is, especially with other users to go up against :P Also a short time ago I got an error on the webpage it is on. A whole lot of warning going down the screen and it hasn\'t cleared up yet. It\'s been that way for about 10 minutes or more now. I don\'t know if you are resetting or not, but figured it was worth mentioning.

 Respond  
Jonesy44   -  Apr 23, 2008

also, possible updates, to make it more realistic, a \"nudge\" button :P

 Respond  
Jonesy44   -  Apr 23, 2008

thanks, i see you\'ve been winning a bit too hehe ! ti\'s addictive. but my server maxxes out at 1,500 connections / hour. so it\'ll reset on the hour. your score is saved :P

im looking into sorting that problem. and when i do, i wil update the script on here. which now runs soeley on sql, rather than the sessions you see here.

 Respond  
Bouncer   -  Apr 23, 2008

To expand a bit further on that, at 1,000 you go to the bottom, then again at 10,000 and I am betting again at 100,000. I know nothing of php and how to fix that, just decided to help test this out and give some advice.

 Respond  
Bouncer   -  Apr 23, 2008

Once you hit the 10,000 mark it starts you over at the bottom of the ranks list.

 Respond  
Jonesy44   -  Apr 23, 2008

hmmkay. also. i\'ve added an sql db to record scores;

function slotsScores() {
  $pos = 1;
  $ip = ($_SERVER[\'HTTP_X_FORWARDED_FOR\'])
      ?  $_SERVER[\'HTTP_X_FORWARDED_FOR\']
      :  $_SERVER[\'REMOTE_ADDR\'];
  $sql = mysql_query(\"SELECT * FROM `slots` ORDER BY `cash` DESC\");
  while ($c = mysql_fetch_array($sql)) {
    if ($c[\"ip\"] == $ip) {
      echo \'<b>@ \';
    }
    echo $pos. \' - $\' .$c[\"cash\"]. \'</b><br>\';
    $pos = $pos + 1;
  }
}

however;

Top Scores;

1 - $8100
2 - $4750
3 - $300
@ 4 - $10800

it orders correctly, apart from my score :s

 Respond  
Hawkee   -  Apr 23, 2008

Still some spacing issues, but it\'s much better. Maybe give each div padding-left: 20px. I don\'t know why you\'ve got 25 then 45, should all be the same. Then give your #slots div a height to match the background image\'s height.

 Respond  
Jonesy44   -  Apr 23, 2008

that is the closest i can get using div\'s :s

 Respond  
guest598594   -  Apr 23, 2008

Now the bottom of the slot image is cut off.

 Respond  
Jonesy44   -  Apr 23, 2008

Is that better Hawkee?

 Respond  
Jonesy44   -  Apr 23, 2008

i\'ve never used AJAX LoL!
i know there will be some errors with the spacing, i used   xD lazy. but i\'ll pop some div\'s in and post when it\'s updated. i just fancied trying to make a few php games.

 Respond  
Hawkee   -  Apr 23, 2008

The numbers don\'t line up properly for me. They\'re kinda all over the place. You should just do something like:

<div style=\"float: left;\">number 1</div>
<div style=\"float: left;\">number 2</div>
<div style=\"float: left;\">number 3</div>
<div style=\"clear: left;\"></div>

Maybe add some padding-right to each of the div\'s to space things out evenly.

Now if you really wanted to take this up a notch you could use AJAX and only refresh the reels upon spinning, not the entire page. In fact you should try this as an Opensocial snippet.

 Respond  
Jonesy44   -  Apr 23, 2008

Updated: Using my wild paint skillz, i made an image :P uploading it now, if u wanna use. hehe. it\'s on the test web.

@Dew, doing it now :D (Done it :P)

 Respond  
guest598594   -  Apr 23, 2008

Cool, but I think you should add a button/link that says \"Spin wheels\" or \"Play again\" or something, instead of refreshing the page.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.