Tiles

Report a typo

In this program, the GameField class represents a 2D tile-based gamefield. Tiles have a common interface named Tile and there are a couple of concrete classes implementing the Tile interface. It also has Tile[][], a two-dimensional array storing tiles of different types enumerated in TileType and fill methods that accept coordinates and the tile type and fill the array.

Your task is to complete the fill method. Use the Flyweight pattern for that purpose and make sure that only a single instance of each concrete Tile implementation is created.

Write a program in Java 17
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

class GameField {
static final int SIZE = 3;
private final Tile[][] field = new Tile[SIZE][SIZE];

public void fill(int x, int y, TileType type) {
field[y][x] = // write your code here
}

public Tile[][] getField() {
return field;
}
}

enum TileType { GRASS, WATER }

interface Tile {
String getDescription();
}

class GrassTile implements Tile {

@Override
public String getDescription() {
return "Grass";
}
}

class WaterTile implements Tile {

@Override
public String getDescription() {
___

Create a free account to access the full topic