mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-06-27 11:44:42 +02:00
Gjør nødvendige forandringer i klasser og interfaces relatert til Grid
Bytter rekkefølge mellom height og width Forbedrer kommentarer Gjør slik at testene bruker Tile i stedet for TileType Legger til manglende exception i setElement til IGrid og Grid
This commit is contained in:
@ -3,6 +3,10 @@ package inf112.fiasko.roborally.objects;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class represents a grid which can store anything
|
||||
* @param <K> The type of element the grid should store
|
||||
*/
|
||||
public class Grid<K> implements IGrid<K> {
|
||||
|
||||
private int height;
|
||||
@ -10,14 +14,14 @@ public class Grid<K> implements IGrid<K> {
|
||||
private List<ArrayList<K>> grid = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Initializes a empty grid
|
||||
* @param height sets the height of the grid
|
||||
* @param width sets the width of the grid
|
||||
* Initializes an empty grid
|
||||
* @param width The width of the grid
|
||||
* @param height The height of the grid
|
||||
*/
|
||||
public Grid(int height, int width) {
|
||||
this.height = height;
|
||||
public Grid(int width, int height) {
|
||||
this.width = width;
|
||||
for(int y = 0; y < height; y++) {
|
||||
this.height = height;
|
||||
for (int y = 0; y < height; y++) {
|
||||
ArrayList<K> row = new ArrayList<>();
|
||||
for(int x = 0; x < width; x++) {
|
||||
row.add(null);
|
||||
@ -32,10 +36,10 @@ public class Grid<K> implements IGrid<K> {
|
||||
* @param width sets the width of the grid
|
||||
* @param tile gives the TileType the grid is to be filled with
|
||||
*/
|
||||
public Grid(int height, int width, K tile) {
|
||||
this.height = height;
|
||||
public Grid(int width, int height, K tile) {
|
||||
this.width = width;
|
||||
for(int y = 0; y < height; y++) {
|
||||
this.height = height;
|
||||
for (int y = 0; y < height; y++) {
|
||||
ArrayList<K> row = new ArrayList<>();
|
||||
for(int x = 0; x < width; x++) {
|
||||
row.add(tile);
|
||||
@ -56,16 +60,24 @@ public class Grid<K> implements IGrid<K> {
|
||||
|
||||
@Override
|
||||
public K getElement(int x, int y) throws IllegalArgumentException {
|
||||
if(x >= 0 && x <= width && y >= 0 && y <= height) {
|
||||
return grid.get(y).get(x);
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
makeSureCoordinatesAreWithinBounds(x, y);
|
||||
return grid.get(y).get(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setElement(int x, int y, K element) {
|
||||
if(x >= 0 && x <= width && y >= 0 && y <= height) {
|
||||
grid.get(y).set(x, element);
|
||||
public void setElement(int x, int y, K element) throws IllegalArgumentException {
|
||||
makeSureCoordinatesAreWithinBounds(x, y);
|
||||
grid.get(y).set(x, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if input coordinates are out of bounds
|
||||
* @param x The x coordinate to check
|
||||
* @param y The y coordinate to check
|
||||
*/
|
||||
private void makeSureCoordinatesAreWithinBounds(int x, int y) {
|
||||
if (x < 0 || x >= width || y < 0 || y >= height) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,36 +2,36 @@ package inf112.fiasko.roborally.objects;
|
||||
|
||||
/**
|
||||
* This Interface describes a grid
|
||||
* @param <K> type of element
|
||||
* @param <K> The type of element the grid is to store
|
||||
*/
|
||||
public interface IGrid<K> {
|
||||
|
||||
/**
|
||||
* returns the width of the grid
|
||||
* @return the width
|
||||
* Gets the width of the grid
|
||||
* @return The width of the grid
|
||||
*/
|
||||
int getWidth();
|
||||
|
||||
/**
|
||||
* returns the height of the grid
|
||||
* @return the height
|
||||
* Gets height of the grid
|
||||
* @return The height of the grid
|
||||
*/
|
||||
int getHeight();
|
||||
|
||||
/**
|
||||
* returns the element in a given x and y coordinate
|
||||
* @param x coordinate in the grid
|
||||
* @param y coordinate in the grid
|
||||
* @return element in the x and y coordinate
|
||||
* @throws IllegalArgumentException throws exception if coordinates are not in the grid
|
||||
* Gets the element in a given x and y coordinate
|
||||
* @param x Coordinate in the grid
|
||||
* @param y Coordinate in the grid
|
||||
* @return Element in the x and y coordinate
|
||||
* @throws IllegalArgumentException Throws an exception if the coordinates are outside of the grid
|
||||
*/
|
||||
K getElement(int x,int y) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* places the element in the given x and y coordinate
|
||||
* @param x coordinate
|
||||
* @param y coordinate
|
||||
* @param element that is being placed in the grid
|
||||
* Places the element on the given x and y coordinate
|
||||
* @param x Coordinate in the grid
|
||||
* @param y Coordinate in the grid
|
||||
* @param element The element to place in the grid
|
||||
*/
|
||||
void setElement(int x, int y, K element);
|
||||
void setElement(int x, int y, K element) throws IllegalArgumentException;
|
||||
}
|
||||
|
Reference in New Issue
Block a user