Battleship Game - Overlaid Ships

I am writing a simple game with a battleship in Java using the ACM library. After the start of the game, ships should be placed on the canvas in random places, but the problem is that the ships can overlap each other, and this is not allowed in the game. How can I avoid placing ships above each other?

My code is:

private void putSmallShips() { for (int i = 0; i < SMALL_SHIP_QUANTITY; i++){ smallShip = new GRect(SMALL_SHIP_WIDTH, SHIP_HEIGHT); int x = rgen.nextInt(10, 510); int y = rgen.nextInt(10, 510); while (true){ gobj = getElementAt(x, y); if (gobj == null) break; x = rgen.nextInt(10, 510); y = rgen.nextInt(10, 510); } smallShip.setLocation(x, y); add(smallShip); } } private void putMiddleShips() { for (int i = 0; i < MIDDLE_SHIP_QUANTITY; i++){ middleShip = new GRect(MIDDLE_SHIP_WIDTH, SHIP_HEIGHT); int x = rgen.nextInt(10, 530); int y = rgen.nextInt(10, 530); while (true){ gobj = getElementAt(x, y); if (gobj == null) break; System.out.println("opa!"); x = rgen.nextInt(10, 530); y = rgen.nextInt(10, 530); } x = x + i * 10; y = y + i * 10; middleShip.setLocation(x, y); add(middleShip); } } private void putBigShips() { for (int i = 0; i < BIG_SHIP_QUANTITY; i++){ bigShip = new GRect(BIG_SHIP_WIDTH, SHIP_HEIGHT); int x = rgen.nextInt(10, 550); int y = rgen.nextInt(10, 550); while (true){ gobj = getElementAt(x, y); if (gobj == null) break; x = rgen.nextInt(10, 550); y = rgen.nextInt(10, 550); } bigShip.setLocation(x, y); add(bigShip); } } 

As you can see, I put the while loop inside the for loop, but that doesn't help.

+7
java java-canvas acm-java-libraries
source share
3 answers

First of all, I suggest you separate the model layer and presentation layer.

In other words, you can define the BattleShip class, this class will contain the shipโ€™s position, size and other properties, and may also contain a method for checking whether it intersects with any other ship.

Then you can create instances and add them to the collection only if the instance does not intersect with any instance present in the collection.

Then you can display them on the screen at a time.

+3
source share

I would create an array and save the values โ€‹โ€‹of each location of the vessel when you enter them on the canvas. Then for the next ship, before you place it, check that this place has already been taken on canvas. Obviously, you will have to remember that ships are of different lengths, and some are horizontal and vertical.

+1
source share

I recently implemented this game as part of an interview. My solution was to simply randomly split / alternate the grid in N> 3 areas, and then place the 3 ships that were needed (2 battleships and 1 destroyer), placing one ship in one zone. Regions N do not overlap in pairs and taken together cover the entire grid (I think this is called grid tile). Since areas N do not overlap, ships do not overlap. The interviewer liked this decision. In addition, the warranty was guaranteed.

An alternative solution was to keep the ships in random positions and continue to check if the ship K was blocking any of the previous ships (1,2,3, ..., K-1), but this had some obvious drawbacks that I did not like: 1 ) the check of the ship's overlap itself, which must be done, is not very elegant and clean; 2) the fact that this is not a deterministic procedure, and you do not know in advance if your placement algorithm will complete, and if it will complete, how many steps it will complete.

So, I just made this random partitioning / partitioning solution mentioned above.

+1
source share

All Articles