Map Array Task - UQcsse3200/2023-studio-3 GitHub Wiki
Map Array Task
This page explains the code related to a grid-based map using Java. The code consists of classes for managing tiles and a grid of tiles.
Tile Class
The Tile class represents a single tile on the map and contains the following properties and methods:
public class Tile {
private int row;
private int col;
private Object object;
public Tile(int row, int col) {
// Constructor to initialize a tile with its row and column
this.row = row;
this.col = col;
this.object = null; // Initially, no object is placed on the tile
}
public void setObject(Object object) {
// Method to set an object on the tile
this.object = object;
}
public Object getObject() {
// Method to get the object placed on the tile
return object;
}
public String getLogCoordinates() {
// Method to get the coordinates of the tile as a string
return "(" + row + ", " + col + ")";
}
}
Grid Class
The Grid class represents a grid of tiles and provides methods for managing objects on the tiles:
public class Grid {
private Tile[][] tiles;
public Grid(int numRows, int numCols) {
// Constructor to initialize the grid with the specified number of rows and columns
tiles = new Tile[numRows][numCols];
// Initialize each tile in the grid
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
tiles[row][col] = new Tile(row, col);
}
}
}
public void placeObject(int row, int col, Object object) {
// Method to place an object on a specific tile
if (isValidCoordinate(row, col)) {
tiles[row][col].setObject(object);
} else {
System.out.println("Invalid coordinates.");
}
}
public Object getObject(int row, int col) {
// Method to get the object from a specific tile
if (isValidCoordinate(row, col)) {
return tiles[row][col].getObject();
} else {
System.out.println("Invalid coordinates.");
return null;
}
}
public String getLogCoordinates(int row, int col) {
// Method to get the coordinates of a tile as a string
if (isValidCoordinate(row, col)) {
return tiles[row][col].getLogCoordinates();
} else {
return "Invalid coordinates.";
}
}
private boolean isValidCoordinate(int row, int col) {
// Helper method to check if the coordinates are valid within the grid
return row >= 0 && row < tiles.length && col >= 0 && col < tiles[0].length;
}
public void placeEntity(int row, int col, Object existingEntity) {
// Method to place an existing entity on a specific tile (placeholder for custom implementation)
}
public Object getEntity(int row, int col) {
// Method to get an entity from a specific tile (placeholder for custom implementation)
return null;
}
}
Array Class
Here's an example of how to use the Grid class to place and retrieve entities on the map:
public class Array {
public static void main(String[] args) {
int numRows = 8;
int numCols = 20;
Grid grid = new Grid(numRows, numCols);
// Place an existing entity in a specific tile
int row = 3;
int col = 5;
// Replace 'Object' with the type of existing entity you want to place
Object existingEntity = new YourExistingEntity();
grid.placeEntity(row, col, existingEntity);
// Get the entity from a tile
Object entity = grid.getEntity(row, col);
System.out.println("Entity at " + grid.getLogCoordinates(row, col) + ": " + entity);
}
}
This code allows to create a grid-based map and place and retrieve objects/entities on specific tiles.