I'm new to Java, and just trying to create a simple, fun zombie survival game. One of the things I want to do is to create an array of things in the users backpack, but I'm wondering how I can put a certain quantity of something inside the backpack, so users can only use it so many times. An example would be arrows. Users would have arrows in their backpack, but how can I make it so the user can only use the arrows x amount of times before it's removed from the backpack?
#Survival Game Help
31 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @jovial mural! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Use a map.
Also, I’d recommend brushing up on data structures. https://youtube.com/playlist?list=PLBZBJbE_rGRV8D7XZ08LK6z-4zPoWzu5H
A map is data structure that corresponds variables with values, right?
so "item" : 1, or something like that?
there's a section on the collections api there ^
Gotcha gotcha, ty. I had a question regarding giving users lives in the game too. I've written plenty of programs like story games in Python, but Im wondering how I can give users, say, 2 lives, and while the user still has at least 1 life, the game can be played, but when they get to 0 lives, the game ends. Do you know how I could write that in Java?
are there multiple players at once? or is it singleplayer
singleplayer, this is just a simple console input game to get me familiar with Java syntax
right that would just be a field
you would have checks to the current life count whenever you lose a life
or any other method, really
there's no logical difference from python, just semantic difference
you could really just translate the python into java if you want to learn its syntax. it wouldn't necessarily be following java patterns, but it could be reasonably done.
Gotcha gotcha. You also can't remove variables from an array, right?
arrays don't really have variables
arrays have values, but yeah you can't remove them outright, since they're fixed size
Is that what arraylists are typically used for then?
list -> List
dict -> Map
set -> Set
tuple -> array
use this mapping for data types (for now)
well no, just Lists in general
Hm, ok. What're arraylists good for then?
List is an interface, it just describes the data structure
ArrayList is a class implementing List, it's an implementation of that data structure
usually the "main" implementations are ArrayList (sometimes LinkedList), HashMap, HashSet
In addition to what others have said here, you could use a more data-oriented approach (for info on what that is, google data-oriented design) and simply make two arrays, for example:
String[] items = new String[]{...};
int[] itemCount = new int[]{...};
And then access both using the same integer index. Whether you do it this way, or using more abstract data, is up to you and your design.
Oh, I do like that idea. I’m wondering if it’d end up being a little less readable (it might not be, I’d have to see the implementation), but regardless, it should be more efficient.
it does look a little messy, but it's also simpler to understand. It's almost definitely going to be more optimized (arrays take advantage of contiguous memory, which is nicer on the CPU during data iterations), but that probably doesn't matter if someone is just making a text game. If you're wondering what it would look like, here's some sample code for checking a player's inventory to see if they have enough items, given the above data setup:
static boolean checkInv (int selectedItem){
if (itemCount[selectedItem] > 0) {return true;}
else {return false;}
}
It's not too bad in terms of readability here, but data-oriented design can get messy if it's not implemented correctly. Obviously the big plus here is that you can now pass integers to methods instead of actual data entities, which is in my opinion pretty nice. I could go into a deeper discussion on its advantages and disadvantages but that might take a bit too much space.