I have a more generic code theory question. I have a program I am writing that is a companion program for a board game. As the board game develops, more pieces are added to the board. The board initially has no pieces on it and by the end of the first few turns will have 8 pieces. That number will go up as the game progresses. In my current design those pieces are individual objects within the companion program that have their own attributes. Those attributes will be tracked for various in-game mechanics (total player points, distribution of player resources, etc).
When you start my program, you open on an empty board and then customize it for the game you want to review. All objects that will be placed are placed after the initial program setup() runs.
How do I deal with objects that will be created at a time after the initial setup() runs? Is it possible to create new objects without initializing them in the setup() function and if so how is this done?
I have two solutions that I think work but they seem a little crude.
I could create all of the objects that could potentially be in the game, make them “empty”, and then just fill in each object with the needed attributes when it is called. This would I think solve the problem but create a lot of objects at the initial running of the program that simply aren’t ever needed. I don’t have the exact answer, but I think you would only ever realistically see about half of those objects put in play before the game ended.
The spots are also objects in my current design, but are always in the same location every single time the game is played and will be in the same location every time the program runs. Rather than create objects for these pieces, I can create a boolean for each of the spots those pieces would go on. If there is a game piece on that spot, the boolean is true and the attributes of the space are tracked through the spot on the board itself and not as individual objects.
Is there a more standard way to go about solving this problem?
If you know how many objects you will ever have you could use an array, if the size is undefined and may grow to any size use lists or arraylist.
Arrays and arraylists are containers which store objects. Arrays are static have to be initialised with a size whereas lists and arraylists are dynamic meaning you can add objects after initialisation.
I figured as much. From a resource allocation standpoint it doesn’t make sense to need all of the objects to be setup at the very beginning. My question is how, or what tutorial goes into detail about this topic?
I am only familiar with standard object creation. Create the slot for the object, initialize in setup, manipulate it in draw. I am not sure how to initialize in draw.
The ArrayList part of this is what I’ve been missing. I have been so accustomed to pointing information at the object after it has been initialized I couldn’t think how to do it otherwise. ArrayList is a big help thank you