Untitled
unknown
java
3 years ago
1.6 kB
11
Indexable
package com.comp301.a02adventure;
public class MapImpl implements Map {
private final int width;
private final int height;
private final int numItems;
private final Cell[][] cells;
public MapImpl(int width, int height, int numItems) throws IllegalArgumentException {
this.width = width;
if (this.width <= 0) {
throw new IllegalArgumentException("Width less than 0");
}
this.height = height;
if (this.height <= 0) {
throw new IllegalArgumentException("Height less than 0");
}
this.numItems = numItems;
cells = new Cell[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
cells[i][j] = null;
}
}
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public Cell getCell(int x, int y) throws IndexOutOfBoundsException {
if (x > width) {
throw new IndexOutOfBoundsException();
}
if (y > height) {
throw new IndexOutOfBoundsException();
}
return cells[x][y];
}
@Override
public Cell getCell(Position position) {
if (position.getX() > width) {
throw new IndexOutOfBoundsException();
}
if (position.getY() > height) {
throw new IndexOutOfBoundsException();
}
return cells[position.getX()][position.getY()];
}
@Override
public void initCell(int x, int y) {
cells[x][y] = new CellImpl(x, y);
}
@Override
public int getNumItems() {
return numItems;
}
}
Editor is loading...