Reputation: 65
I'm trying to make a small game in Java, swing, and I can't figure out a good way to solve my problem. I've got two arrays, first of Crate objects
public class Crate {
private static ImageIcon crate = new ImageIcon(Player.class.getResource("/Images/crate.jpg"));
private int x=0;
private int y=0;
private static int Xdisplacement;
private static int Ydisplacement;
private int id;
//getters and setters n stuff
second one is of a Tile objects.
public class Tile {
private static ImageIcon tile = new ImageIcon(Player.class.getResource("/Images/tile.jpg"));
private int x=0;
private int y=0;
private boolean hasBox=false;
//getters and setters n stuff
I would like to check if all Crates are placed on Tiles. I mean it doesn't matter which box is on which tile, there are few boxes, few tiles, and each box should be placed on a tile, doesn't matter which box on which tile. In the game, player walks and move crates around, so their coords change. Tiles coords do not change (if it may help). This is gonna be my stop condition. The game is over when crates are placed on tiles. I suppose making a loop inside another loop and then checking every single field of every single object isn't a good solution? So are there any other ways to do so?
Upvotes: 0
Views: 111
Reputation: 205785
A complete design is beyond the scope of StackOverflow, but you will probably want to use the model-view-controller pattern, illustrated here. The answer includes a very simple example game and cites a more complex game involving players on a tiled grid.
Addendum: In the particular case of checking grid occupants after a move, the example cited uses nested loops to check the current state against a copy in the method RCModel#moveBots()
.
Upvotes: 1