File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with my game. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with my game." Watch "Help with my game." New topic
Author

Help with my game.

John Michaels
Greenhorn

Joined: Jun 01, 2009
Posts: 7
Hello. I'm creating a game where people can set up an account. Their registration information includes but is not limited to a login and a password. The login and the password are used to sign in. I store the sign in information in a player object, and each player object is stored in an Array List called players. How do I prevent the player objects from being deleted when I compile the program. Am I doing this the right way? Should I be storing the players sign in information in a file? If so, I'm not sure how to do this. Please help. Thank you. My code:

[edit]Newlines because lines too long. CR[/edit]
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Dude, please, use the code tags. Edit your message.
Do this: click the Code button. Paste your code. Click do Code button again.

This way people can read your code in a better way and the chances that someone help you is bigger
Sorry my english.
John Michaels
Greenhorn

Joined: Jun 01, 2009
Posts: 7
Thank you dude. Sorry I didn't do that before. That was my first post.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
If you only want to have a few "pretend" users to work with while you develop the game, then consider having calls to the player constructor in your main method, where the player info such as name, etc. is hard coded into the constructor call. This will allow you to continue with developing the game without having to enter the user info each time.

This is a temporary solution that is useful if you don't want to deal with files yet. If you ever reach the point where real people will be using the game without your intervention, then you can deal with creating files at that time.

keep in mind as you develop the game that you may wish to store additional info with each user, such as the player's high score. You may also wish to have a way to store the highest score among all the users.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
As well as code tags, please make sure your lines are a reasonable length. I ahve been back and "broken" the lines so your whole code fits onto the width of the screen.

Never say if (x == true) . . . nor if (x == false) . . . That is poor style and also liable to serious errors if you mistakenly write = for ==. You write if (x) . . . or if (!x) . . .
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
John, the simple answer to your question is yes, you do want to store that data in some type of file or DB. If you are saving that information inside an object that is only in memory than every time your server resets or you have to start the application over again you will lose all of your data. Personally I would recommend using a lightweight DB program like MySQL to store the info. This makes it easy to check for existing players, and other data gathering without having to reinvent the wheel.

GL and hope to see you on the JavaRanch Game Forums as well.

SCJA
~Currently preparing for SCJP6
John Michaels
Greenhorn

Joined: Jun 01, 2009
Posts: 7
Thank you guys. BTW, what is my sql exactly? Is it what I need if I want to keep track of multiple users simultaneously?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
John Michaels wrote:Thank you guys. BTW, what is my sql exactly? Is it what I need if I want to keep track of multiple users simultaneously?


mySQL is a very popular server based database application. A lot of web hosting providers have it on their servers. It is used by many big websites to store user registration info.

If you intend design your game so that users run it from a website, then mySQL is a logical choice for storing user info. You would probably need to learn some html and some php in order to make use of it to validate users of your java application. So you do have your work cut out for you.

http://www.mysql.com/


p.s. it is appropriate for multiple simultaneous users. In your case it would be used for storing registration information about users. and maybe also game history information, if a game extends past one session. If you design things right, mySQL will not care how many users you have at any one time.
John Michaels
Greenhorn

Joined: Jun 01, 2009
Posts: 7
Thank you Fred. But what if I plan on making a very small website, one that might only have a few people logged on at any given time. Also, I don't plan to have that many registered users. It's just something basic, something my friends and I can use. You register, you place a bet, people can accept, and one party wins. The program does this and also keeps track of who owes who money. Is my sql necessary? Or would it make more sense to do something else, something a bit simpler? Should I just learn it regardless, as it would be beneficial to know for future projects? And do you have to pay?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
John Michaels wrote:Thank you Fred. But what if I plan on making a very small website, one that might only have a few people logged on at any given time. Also, I don't plan to have that many registered users. It's just something basic, something my friends and I can use. You register, you place a bet, people can accept, and one party wins. The program does this and also keeps track of who owes who money. Is my sql necessary? Or would it make more sense to do something else, something a bit simpler? Should I just learn it regardless, as it would be beneficial to know for future projects? And do you have to pay?


I use x10hosting.com, they have a free service that includes mySQL, php, etc.

You kind of answered your own question. If you want to learn mySQL, then go ahead and use it. But it is not a necessity. If the program is only gonna be used by you and a few of your friends, then you don't have to use files at all if you don't want, you're calling the shots.

Did my other post about have constructor calls with hard coded values make sense to you? That will work and eliminate the need for files. You would just need to update your source code when you added a new user.

edit: ok, I just noticed your comment about keeping track of bets and who owes who. AS I think about it more. this sort of thing might be rather tricky to do without files.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

John Michaels wrote:Thank you Fred. But what if I plan on making a very small website, one that might only have a few people logged on at any given time.


If you thinking of a web site where people can log in and place bets - you'll need something more than Java -- Servlets, JSp maybe Applets - but a plain Java class won't work!

The current program reads input data of the console - so it will only work on one PC at a time - You could get different people to play it from all over - If you get an online database - Reading and writing into local Files won't do the trick here.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Sam Mercs wrote:
John Michaels wrote:Thank you Fred. But what if I plan on making a very small website, one that might only have a few people logged on at any given time.


If you thinking of a web site where people can log in and place bets - you'll need something more than Java -- Servlets, JSp maybe Applets - but a plain Java class won't work!

The current program reads input data of the console - so it will only work on one PC at a time - You could get different people to play it from all over - If you get an online database - Reading and writing into local Files won't do the trick here.


agreed with regard to console input and one pc at a time. All in good time. Also, keep in mind that it's not a real betting site, I rather doubt credit cards will be involved.
 
Don't get me started about those stupid light bulbs.
 
subject: Help with my game.