Tag cloud
l4d html5 render glow after effects bullet time lighting reactor javascript kitchen demo reel ut3 wrc 2009 update car far cry 2 veyron texturing web mod template demo ball special effects anticipation dragon script skinning mysql animation character machines effects calendar physics legof1 progress interior camera match monkey robot mental ray cars directx match test eurofighter reaction
Categories
Featured work
Finished animations
Interactive features
Maps and mods
Work In progress
Character animation and loops
Images
Site news and updates
Tutorials
Random blog posts
Uncategorised
Finished animations
Interactive features
Maps and mods
Work In progress
Character animation and loops
Images
Site news and updates
Tutorials
Random blog posts
Uncategorised
Most popular
How to Build a Car Rig in 3ds Max
Car Rig Project - 3ds max - Updated
Unreal Tournament 3 - UT3 - City Project
Car Rig Tutorial - 3ds Max and Reactor
Motorway and Foliage - Grass
Car Rig Project - 3ds max - Updated
Unreal Tournament 3 - UT3 - City Project
Car Rig Tutorial - 3ds Max and Reactor
Motorway and Foliage - Grass
Archive
August 2010 [1]
July 2010 [3]
June 2010 [4]
May 2010 [3]
April 2010 [3]
March 2010 [3]
February 2010 [6]
January 2010 [1]
October 2009 [2]
September 2009 [3]
August 2009 [4]
July 2009 [2]
June 2009 [3]
May 2009 [1]
April 2009 [7]
March 2009 [2]
February 2009 [3]
January 2009 [3]
December 2008 [4]
November 2008 [1]
October 2008 [7]
September 2008 [3]
July 2008 [1]
July 2010 [3]
June 2010 [4]
May 2010 [3]
April 2010 [3]
March 2010 [3]
February 2010 [6]
January 2010 [1]
October 2009 [2]
September 2009 [3]
August 2009 [4]
July 2009 [2]
June 2009 [3]
May 2009 [1]
April 2009 [7]
March 2009 [2]
February 2009 [3]
January 2009 [3]
December 2008 [4]
November 2008 [1]
October 2008 [7]
September 2008 [3]
July 2008 [1]
Blog
Multiplayer Blackjack in PHP, Javascript, MySQL
JUN
13
2010
I've been working on this extension to my website for the last few days, and it's ready to be viewed and played around with. It's an online version of Blackjack, created with PHP, Javascript and MySQL, and can be played as single or multiplayer - to test out the multiplayer, just open another browser window and be another player. The aim is to get a hand as close to 21 without going over, and to beat the dealer (not the other players - each individual player just plays against the dealer).Aces count as one or eleven, whichever is better for you - this is taken account of in the game and adds the higher value as long as it won't make you go over 21. A 2 is worth 2, 3 worth 3, etc, and face cards are worth ten.
It requires cookies and obviously Javascript to be enabled. The Javascript is the main workings of the game, calling a PHP function to check the SQL database when updates have been made, then refreshing section of the screen in an Ajax style way, allowing other sections (such as the chat) to continue independently without interruption. An auto kick function is added, so that if any player leaves without clicking the leave button, the game doesn't just cripple to an indefinite halt - players can then return when they're back.
One of the most difficult challenges was working out how to accommodate new players, and also those that leave without destroying the game. I decided to add a value to the database when a new player joins, and have them wait until the next deal, then update them into the game. A player leaving caused more problems, but I resolved this by first adding the timer so they have to leave if they don't actually click the leave button, then if they do click to leave, it takes the player's cards with them. Initially, it meant that a leaving player transferred their cards onto the next player, which would be a bit harsh if they were a bad hand. So this player is still 'in' the game even after leaving, even though their cards and score aren't visible, a message is noted that a player has left, and they are then removed at the next round.
I then added a chat function, since the server was already calling a PHP script every couple of seconds, it may as well check the file size of the saved chat, then update if it's larger. A javascript function to then force the div to automatically scroll to the bottom (and so the updated messages) is also called when loading new data. The line required, for anyone that may find this useful (it took me quite a bit of Googling, hopefully you'll fare better), was:
$("#chat_div").attr({ scrollTop: $("#chat_div").attr("scrollHeight") });
Where chat_div is your div ID.
It doesn't include all of the rules of Blackjack just yet - but enough to form a functioning game. Features such as splitting a hand, betting and keeping track of high scores will come in the future.
Try it out, and let me know what you think, and if you find any errors. I'm aware of quite a few ways to cheat, but I left some of the ways I thought of in there as they were handy debuggers - I'll remove them when I come to finish everything off.
www.adamgleeson.co.uk/blackjack
Related posts
RegisterLogin