This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
For practice and for fun I am trying to create a small game.
Currently, I loop through an array of objects and call a method to make each one of them do what they should do. For this question, say there are two objects: Object A and Object B. Object A moves to the right, Object B looks above itself to see if Object A is there, and if not it will delete itself. Object A is in position 0 of the array, object B in position 1.
The game starts
Object A moves right
Object B checks above it to see if Obj A is above it. It isn't, since it moved to the right, so Obj B removes itself.
The way it currently works, nothing can happen "at the same time." Even though Object A was there, since it moves just 1 turn before Object B, Object B doesn't notice it. This might be a stupid question, but is it possible to make two things do something at the same time? I've had a few ideas, but most of them would only work for specific cases and wouldn't be very convenient anyways. I know there are threads, which are kind of similar to what I'm asking for, but I'm pretty sure that wouldn't work for this.
Threads may not run at the same time. However, they may eventually run.
So, for your case, it could be xab, axb or abx depends on thread scheduling.
You could create N separate threads for each object if you want N things eventually get executed.
But how they get executed may depends on number of processors the platform available.
Too many threads may eat up memory as well, so create threads only when needed.
You may also want to consider having two sets of data - one that keeps track of where everything IS, and one that keeps track of where everything is GOING TO BE. So you iterate through the IS list. When A moves to the right, you update the GOING TO BE data. When you get to B doing its check, you look as where A IS, then update B's GOING TO BE.
Once you have gone through the entire list of objects, you then move stuff, and copy the GOING TO BE into the IS.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors