A couple of years ago, when I was learning Java programming, I thought of testing myself and my programming skills by writing a game in Java. Now, I'm not going to call it 'game programming', since game programming is way more than what I did. In fact, what I did was just a test for me. So I decided to write a game I was playing on my old Nokia E50 phone, a Slot Game.
This slot game I was playing on my phone was really simple. It had only 3 slots with different items in each. You had to push the Spin button in order to spin the slots, and you would win a small amount of coins if two or three slots were alike. Of course, 3 slots were better than 2. It is not really hard to make a game like this, but for a beginner it is good to start with. As I remember, this was probably the first program that I could tell others: 'Look at what I just did!'
So I started working on it (I remember using NetBeans at that time), firstly as console-only, and then using GUI. The first thing I did, was deciding what kind of images (actually their names, not the images themselves) I would use. I wrote this line of code:
Code2web is a toolkit for converting source code (Java,JavaScript,XML,JSP,SQL and more) to HTML with a wide array of features, including CSS customization, outline generation, automatic documentation liking, selective highlighting and more. JDLabAgent 1.0.1 - Jdlabagent. Slot Machine Code Javascript - Build Own Java Slot Machine Source Code - Build own Java Script Slot Machine at reasonable rate with AIS Technolabs. Slot Machine Code Java Script will help your business to reach at next level. Contact us today to know more. In-house experts and professional coders at AIS Technolabs have mastery of developing Java slot machine source code and are known to handle complex coding solutions in almost all coding languages. We serve you the best possible solution as per your requirements. Attain the Wonderful Benefits by Our JavaScript Slot Machine Code for. I'm having issues with a homework problem for my CS class. I'm supposed to make a slot machine and I can't get the continue to work, I get that the scanner nextLine method skips over the line and keeps what was there but it won't let me enter anything and it just ends the program.
I also decided what would be the amount of 'money' that the user would win if he matched two or three symbols:
After that, I went on writing the code that was suposed to randomly choose one of the elements in the symbols
array. This could be done by using the Math.random()
method, or by calling the nextInt()
method at a Random
instance, or by the wrong way I used to do at the beginning:
Of course, I soon switched to calling Math.random()
, and in order to get a number that I could use as an index for my array, I wrote this block of code:
So the variable choice
was the random index that I could use to get a random item from the array (keep in mind that Math.random() returns a double between 0.0 and 1.0)
So after choosing 3 random items, I just printed them out at the console, saying whether there were none, two or three matches, and calculating the amount of money won, if there was any, with the given coefficient. It was a good start; I only had to think of the UI, and I suck at UI design. But for this one, all that I needed was a really simple design which I managed to code as I was planning.
For the items to show at slots, I just googled them and found 12 of them in a single sprite. I downloaded the sprite and started my old photo editing software which sometimes can really be magical; Microsoft Paint! I started cropping images from the sprite, paying attention to their dimensions that should be the same, 122px by 114px. Why these magical values? Just because!
What was left to do, was the UI. I could use the really-helpful drag-and-drop UI builder that ships with NetBeans, but I wanted to do it myself. I had a really hard time figuring out which kind of layout to use, since the only one I really knew was GridLayout
. Anyway beside that, I managed to use FlowLayout
and BorderLayout
. There is a difference between them, but I'm not really capable of pointing that out, so you can check the online JavaDoc for them.
I managed to build the game, and started to play it. I figured out that the coefficients for multiplying the bet were too damn high, but I didn't care as long as I knew that the game worked.
My bad practices
As you can see, the source code of this simple game is in only one file. This is something that I don't like to do anymore. A better way to do it is by making the code as modular as it can be. By building small modular elements, dividing the UI from the logic of the application, you help yourself during the testing and debugging phase. So the first thing that I would like to change, is dividing the whole class Slots extends JFrame
from the class that calls it.
This is done by firstly creating a file called Slots.java
that will contain only the code for the UI. Then, creating an ActionListener
that will listen to different button clicks (there are 5 different buttons). Finally, creating a class called App.java
that will only create a Slots
instance and make it run.
Basically, the App.java
would look like this:
As I remember, SwingUtilities.invokeLater()
is used to divide the UI thread from other threads, so if any UI changes are needed, they won't stall the application.
The Listener class, which might be called something like SlotButtonListener
, might be something like this:
And finally, in the Slots
class there would be only the code for defining the UI of the game. All the buttons would have SlotButtonListener
as action listener.
Anyone who wants to change the code following these advices is free to do it. You can fork it anytime you want.
Do you have any Java programming advice for me? Feel free to comment below
Hangman Flash GameSourceCode.You can easily install Hangman game on your webpage.The sourcecode of Hangman game includes an initial file *.fla which contains ActionScript code and graphics for this game. Tips on winning slot machines. High limit slots strategy. For adding or changing any of the puzzle. ..
- File Name:hangman_demo.zip
- Author:Fla-shop.com Inc.
- License:Demo ($24.90)
- File Size:37 Kb
- Runs on:Windows All
Use this cross-platform Net Chess to provide online chess game on your web site. Sourcecode is available to be able to modify to meet your needs.
- File Name:netchesstrial.zip
- Author:SoftView Technology
- License:Shareware ($99.95)
- File Size:105 Kb
- Runs on:Win95, Win98, WinME, WinNT 3.x, WinNT 4.x, WinXP, Windows2000, Windows CE, Unix, Linux
This is the sourcecode for chance statistics game for you to study and see how statistical mechanism works.
- File Name:roulette2.zip
- Author:VB Open Source
- License:Freeware (Free)
- File Size:199 Kb
- Runs on:Win95, Win98, WinME, WinNT 4.x, Windows2000, WinXP, Windows2003
SourceCode Organizer PS is your library for organizing and storing your sourcecode. It doesn't matter what type of language you program in, SourceCode Organizer PS will keep it safe and secure until you need to use it again. It has several easy t. ..
- File Name:sourcecodeorganizerps.exe
- Author:PinderSoft
- License:Shareware ($29.99)
- File Size:515 Kb
- Runs on:Win98, WinME, WinNT 4.x, Windows2000, WinXP
Don't you hate those webpages that try to prevent you from viewing their sourcecode or saving images by disabling Right-Click? Now you can view the source of any webpage, as well as save any image from a webpage, using this source browser software!
- File Name:RSSourceCodeBrowserDemo.msi
- Author:Rebrand Software, LLC
- License:Shareware ($14.95)
- File Size:2.71 Mb
- Runs on:Win95, Win98, WinME, WinXP, Windows2000
Please feel free to download and use MouseImp Pro Live! sourcecode. The source lacks MouseImp Pro's copy protection and some other trialware/evaluation stuff from the commercial version. The sourcecode can be compiled under Microsoft Visual C++ 6.0. ..
- File Name:mimppro_live_src.zip
- Author:FreeSoft411
- License:Freeware (Free)
- File Size:356 Kb
- Runs on:Win98, WinME, WinXP, WinNT 4.x, Windows2000, Windows2003
Free SourceCode Browser software. Aren't webpages that try to prevent you from viewing their sourcecode or saving images by disabling right-click bothersome? Who cares! View the sourcecode of any webpage, and save any image as well.
- File Name:FreeSourceCodeBrowser.zip
- Author:free. software downloads
- License:Freeware (Free)
- File Size:2.55 Mb
- Runs on:Win95, Win98, WinME, WinNT 3.x, WinNT 4.x, WinXP, Windows2000, Windows2003, Windows Vista
SourceCode Library is a sourcecode Library and clipboard extender shipped with 50,000+ lines of code, quick search engine, powerful printing capability, password protection, user notes & attachments handling, favorites & rating support and more!
- File Name:source_library.zip
- Author:OverZone Software
- License:Shareware ($79.99)
- File Size:14.23 Mb
- Runs on:WinXP, WinVista, WinVista x64, Win7 x32, Win7 x64, Windows2003, Windows Vista, Windows Media Center Edition 2005
Visual Basic SourceCode that demonstrates how to make VB Controls by using a Picturebox and pure VB Code. Current Visual Basic Components include: Scheduler SourceCode, Calendar SourceCode, Clock SourceCode, Slider SourceCode, Scheduling Program. ..
- File Name:VB Source Code Controls
- Author:WolfeByte Solutions
- License:Freeware (Free)
- File Size:152 Kb
- Runs on:Windows 2003, XP, 2000, 98, Me, NT
We are pleased to announce the launch of our partner program of iFap ERP 6.0 which will enable Software companies to own software with sourcecode and widen their business fast and easy.The iFap ERP partner program are for those companies who don't. ..
- File Name:iFapERP6setup.exe
- Author:Infygen USA
- License:Shareware ($2000.00)
- File Size:170.11 Mb
- Runs on:Win2000, Win7 x32, Win7 x64, WinServer, WinVista, WinVista x64, WinXP
JAVA to PDF SourceCode Converter is a simple Java-based software that can convert your Java sourcecode to PDF.The app will convert all the .JAVA files in the folder you select (and will look for .JAVA recursively in all child folders) and create. ..
- File Name:javasrc2pdf.jar
- Author:Gustavo
- License:Freeware (Free)
- File Size:
- Runs on:Windows All
CtrlCAD is a 2D sourcecode cad for C++.It is the starting point to build your own customized drawing applications. The library is furnished in sourcecode formats for c++More then 1000 functions,well documented with a lot of examples. CtrlCAD. ..
- File Name:ctrlcad.exe
- Author:Design&Software
- License:Shareware ($1800.00)
- File Size:2.3 Mb
- Runs on:All