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:
Kristian Knarvik 2020-02-20 14:10:00 +01:00
parent b936414833
commit 29b48c1a87
3 changed files with 67 additions and 42 deletions

View File

@ -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();
}
}
}

View File

@ -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;
}

View File

@ -1,50 +1,63 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Direction;
import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.objects.Grid;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class GridTest {
private Grid<TileType> grid;
private Grid<TileType> grid2;
private Grid<Tile> grid;
private Grid<Tile> grid2;
private Tile defaultTile;
@Before
public void setUp() {
defaultTile = new Tile(TileType.TILE, Direction.NORTH);
grid = new Grid<>(7, 4);
grid2 = new Grid<>(5,8, TileType.TILE);
grid2 = new Grid<>(5,8, defaultTile);
}
@Test
public void getWidthFromGrid() {
assertEquals(4, grid.getWidth());
assertEquals(7, grid.getWidth());
}
@Test
public void getWidthFromGrid2() {
assertEquals(8, grid2.getWidth());
assertEquals(5, grid2.getWidth());
}
@Test
public void getHeightFromGrid() {
assertEquals(7,grid.getHeight());
assertEquals(4, grid.getHeight());
}
@Test
public void getHeightFromGrid2() {
assertEquals(5,grid2.getHeight());
assertEquals(8, grid2.getHeight());
}
@Test
public void getElementFromGrid2() {
assertEquals(TileType.TILE, grid2.getElement(5,3));
assertEquals(defaultTile, grid2.getElement(3,5));
}
@Test
public void setElementInGrid2() {
grid2.setElement(2,1, TileType.HOLE);
assertEquals(TileType.HOLE, grid2.getElement(2,1));
Tile hole = new Tile(TileType.HOLE, Direction.NORTH);
grid2.setElement(2,1, hole);
assertEquals(hole, grid2.getElement(2,1));
}
@Test (expected = IllegalArgumentException.class)
public void invalidPositionThrowsErrorOnGet() {
grid.getElement(7, 4);
}
@Test (expected = IllegalArgumentException.class)
public void invalidPositionThrowsErrorOnSet() {
grid2.setElement(5, 8, null);
}
}