Simplest way to save data when Serialization is no option - java

I am working on a game. It has a Game class where everything is created and other objects are stored, a Player class with an Inventory of Items, and then there are Rooms where the Player can go and in these rooms we have other Items and Enemys and so on. Its a fairly simple programm.
I have to implement an Option to save and load the current game state. I tried using the Serializable interface, but I encountered the problem that all of my Item classes store StringPropertys. Saving those objects obviously doesnt work scince StringProperty doesnt implement the interface.
Then I thought about writing everything in a simple text file. But loading that will be pretty elaborate.
Persisting the data with an Database would be an asolute overkill for that project. I also know there are other possibilities like XML, but I have never done that before.
What I basically want is the easiest (does not have to be the prettiest/cleanest) option to save my game state, so when someone wants to load the game, I can simply create a new Game and Player and just do something like newPlayer.setCurrentLocation(savedLocation) and so on.

Related

Is there a way I can have two instances of an object with the same name and use one or another?

I'm a beginner and I'm stuck with this.
The thing is that I'm doing kind of a game (text game)
I made and if statement which lets the player choose whether they wanna be a warrior or a mage, the thing is that I don't wanna create both at the same time, so only want one to be created at a time with the name "player", the problem is that it gives me an error and I doesn't work as I've imagined it to work.
I know I can just create them with different names but then when the player attacks or gets attacked the name for that is "player" and I think making if statements every time I wanna it to attack with different names wouldn't be so efficient.
Instead of new Mage() or new Warrior() as you are doing you can try to create a higher level class.
For example, you can do new playerClass(Mage).
Also I suggest you to use enum for the classes.

How to organize configuration values in java project

Let's say I'm programming a small RPG game. I have several instances of the Enemy class. They share common properties (they all have a name, an amount of life/strength/dexterity/a weapon, etc), but all these values are different for each enemy. I'm looking for an appropriate way to initiate these instances.
I'm new to programming and Java, so I'm looking for the best practice to properly organize information in my project.
My first idea was, when creating the instance of the game, to instantiate all the occurrences of the required enemies in the constructor of the Game object, and put everything in a vector. Something like Enemy e1 = new Enemy("goblin", 10, 14, 10, a_weapon, ...). But this can get very tedious if there are a lot of enemies, a lot of properties, it gets very hard to maintain, and I don't find very "logical" to put that in the constructor of the Game object.
I just discovered XML files, and it looks promising. So maybe I could put everything in an XML file, and parse it in my program to extract the data and create all the enemies from it. It could look like
<Enemies>
<Enemy>
<Name>"Goblin"</Name>
<Strength>20</Strength>
<Agility>20</Agility>
<Life>20</Life>
<Weapon>
<Name>"Sword"</Name>
<Damage>3</Damage>
</Weapon>
<Enemy>
<Enemy>
...
</Enemy>
</Enemies>
I guess I could write a function that parses the XML file, extract the data and create the vector of enemies automatically, so I just have to edit the XML file to modify the values.
However, and before I dig into this solution, I want to ask if this is the preferred method, and if not, what would be the most common way of managing this kind of situation.

Handling data on an android app

I'm working on a school project and I'm having trouble coming to a conclusion on how to handle the data it involves.
Basically my app lets you create recipes (cooking) that are stored on the phone.
I don't really have any usable code just yet, so far the structure I thought of is as following:
A class Recipe that represents an individual recipe for manipulation via the app (has all the properties)
A class RecipeList that has a HashMap with all the Recipe objects in it that lets you get a certain recipe by an Id RecipeList.GetRecipe("id") and save Recipes via RecipeList.SaveRecipe(Recipe), it also handles saving to the SD card
The thing is though that eventually I want to outsource saving to a web space (using PHP) which is part of my project assignment, that's why I decided on using a HashMap so I could utilize an Id.
What's the best way to go about it given my current structure? Or is there a better structure for what I'm doing?
This will tell you everything you need to know.

How can I design this concept in a object oriented manner?

I am having a bit of issues with design. Maybe I am thinking about this all wrong, but it seems that what I am designing only works well in a procedural manner.
The Game
I am working on a game, this game has about 10-20 players inside of it, and consists of 3 rounds. When players start up the game, the server loads their data from a database, stores it in a java class, and sends data that is requested to the client. Lets call this Player.java. This class is persistent in between the rounds of the game.
Every player in the game also has a role. This role changes in-between each round and I need this role to be linked with Player.java in some way. Right now I have 3 roles: Hunter, Seeker, and Spectator. Each role has the same basic things: onDeath(), onRespawn(), onKill(KillEvent event). I also need to be able to easily check what role a player is. (For example linking the roles with a enum).
The Problem
The problem I am running into now is how do I implement this in a OOP way? Originally, I had each role as a class that implements Role.java, and every time a role is created, it takes a Player in the constructor. This is all fine and dandy until I start changing people's roles in the middle of the rounds and after the end of each round. It seems like bad practice to me if I am consistently setting the players role to a new object.
Summary
To sum this up (since I am terrible at describing things), it seems like all of this would work perfectly in a procedural manner, but I can't figure out for the life of me a good way to implement this concept using OOP. The way I have it implemented now, each Player has a enum stating what role they are, and to change that role I simply change the enum in Player. With that being said though, once I get to the game logic, I end up with a TON of nested if statements, something that I feel could be greatly reduced with some polymorphism.
So to end with a question, what would be a good plan of attack to implement this (or a slightly modified version of this system) in a object oriented way without having to consistently create new objects that take in data from old objects? (I don't have a ton of experience with OOP, but to me this just seems like a bad idea)
I think I would go for this solution:
Make Player an Interface
Create a Proxy-Class for it (a class that has only one property, which is of type Player, and redirects all methods to this object). Lets call it ConcretePlayer
Add a setRole method, taking a Role to ConcretePlayer.
Make Role implement Player
Create Subclasses of Role like you did, each takes a ConcretePlayer in their constructor.
Store the stats that are shared among all Roles in the ConcretePlayer
Externally use Player or ConcretePlayer to access everything.
It's not fleshed out perfectly, but I think you get the idea. You may find that Role and Player shouldn't share the same interface, or that you want to create an own interface for the callbacks, but that depends on the rest of your code and usecases.

I accidentally serialized an object containing a non-serializable field; now I need to get it back

I am making a game. The game designer creates a Game object, then saves it to the hard drive; to play the game, the user loads the Game object from the hard drive. The Game object is Serializable. The Game object contains thousands of other objects (Map, Person, Tiles, etc.), all of which are also Serializable.
Unfortunately, I accidentally added a new feature to my game: a class called Large. I forgot to implement the Serializable interface on Large. Then I added a Large object to an existing Game file that I had spent a considerable amount of time working on. When I saved my Game file, a NotSerializableException exception occurred.
I am trying to recover from this error and retrieve my Game file from the hard drive in a usable form, so that I can continue to design my game without having to create it all over again. Unfortunately, when I try to load the file, I get a NotSerializableException. I have tried the following strategies, without success:
implementing Serializable on my Large class
making the 'large' variable transient
Do you have any other solutions?
Are you sure the game was actually saved? If the exception occurred when you were saving the file I don't think it managed to serialize the object.
The problem you are facing in fact is the major flaw of using serialized objects for these kinds of things. The moment you do changes to the class such as one of the following the old serialized objects become incompatible.
Remove a field
Change the object's superclass
Change a non-static field to static (or vice versa)
Change a type of a field.
You should use your own data format (while you're still in time). You can choose a data format of your choice, or even use XML or JSON (the latter is becoming more popular these days since its more lightweight) and there are APIs available to save objects quickly and easily.

Categories