Home Articles My Account Messages Tools Join
Tech Blog, ASP.Net, VB.Net, C#.Net, Programming Help, Help Guide
StellarPC.com | How to code slot machine logic (ASP.Net)
How to code slot machine logic (ASP.Net)
Written by: Justin Rich

How to code slot machine logic

Slot machines seem simple at first, but to actually generate a working slot machine that has a higher rater of difficulty of winning large jackpots and lower rate on winning the lower jackpots, you'll need some complex logic.

If you code up a 3 reel slot with 18 possible items on the slot machine, and divide them out among cherries, diamonds, bar, double bar, and 7's, then you only put 1 7 on each reel, your odds of winning 3 7's in a row are actually 1:5832. In reality, if you're going to have a high volume of plays your ultimate jackpot of 3 sevens should be closer to 1 in 100,000 or 1 in 1,000,000 (depending on the type of application you're writing).

Most people tend to try to write logic that just uses random numbers from the Random class or if you're more of a classic ASP person, Randomize with the Rnd function. Now, you will need random numbers at some point to do the full logic, but you won't want to be doing random numbers over and over and over in order to decide whether they hit a 7 or not. Too many time's I've seen programmers say to do the regular random number (to choose 1 of the 18 spots on the reel) and then write an if statement to see if it's a big jackpot spot, send a second random number to lower their odds on whether it stays on that number or not. Well, to me that isn't fair. That's now how real slots work, so why should yours?

The best method would be to generate a logical reel that has more than 18 spots on it. You should use 128, 256, or 512 logical spots on your logical reels (yes, you'll need 3 - one for each real reel on your slot machine). Then, the logic is to match up spots on your logical reel to spots on your physical reel. This is actually how real slot machines work, though in the days of old, the logical reel too was a physical reel inside the machine that you couldn't see. Today, all slot machines run on a computer program even if they're physical reels or video slots.

In order to code it, each logical reel should be an array and when you write your spin function, you're going to be spinning your logical reels, and then showing the user the value that is mapped across to the physical reel. They way to make it challenging is that in the 512 logic spots, your 7's may only be 1 - 5 spots out of 512 while cherries may have closer to 50 - 100 spots on the logical reel. In doing this, you can actually add more than 1 7 spot on your physical reel, and just make sure that they're not tied to as many spots on the logical.

I'll write out the code for a simple way to do it:


Dim reel1(18) as String
Dim reel2(18) as String
Dim reel3(18) as String

'physically set all the reel items (this could be data driven)
reel1(0) = "D" 'diamond
reel1(1) = "C" 'cherry
reel1(2) = "B" 'bar
reel1(3) = "7" 'seven
reel1(4) = "BB" 'double bar
reel1(5) = "C"
'... 'You get the picture must assign all slots
reel1(18) = "D"

Dim virtualReel as Integer = (127 * Rnd) + 1
Dim intIndex as Integer

Select case virtualReel
case 1 thru 10
intIndex = 0
case 11 thru 30
intIndex = 1
case 31 thru 45
intIndex = 2
case 46 thru 50
intIndex = 3
'... continue to apply these through all 18 indeces
End Select



Then all there is to do is display the item that corresponds to that index. So if the 4 in 128 chance that it was index 3 comes up, you display the 7. If you make each of the 3 reels a 4 in 128 chance, then your actual jackpot odds are now 0.0030517578125% or 1/32768.

To make it 'tease' the users make 1 of your 7's have higher odds and make the items above and below the other 2 7's higher odds, but both 7's each only 1 in 128 chance. This will make one 7 come up and the other 2 above or below the selected item making it seem like they user was SOOOO close to getting a jackpot. In reality, they are just as far away as any other spin.

New Post Next 50 | Previous 50
Post# Subject: Posted By: Time:
1 This article is getting a lot of interest from coders a Justin Rich 7/16/2008 11:51:17 PM
New Post Next 50| Previous 50