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.ArrayList;
import java.util.List; 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> { public class Grid<K> implements IGrid<K> {
private int height; private int height;
@ -10,14 +14,14 @@ public class Grid<K> implements IGrid<K> {
private List<ArrayList<K>> grid = new ArrayList<>(); private List<ArrayList<K>> grid = new ArrayList<>();
/** /**
* Initializes a empty grid * Initializes an empty grid
* @param height sets the height of the grid * @param width The width of the grid
* @param width sets the width of the grid * @param height The height of the grid
*/ */
public Grid(int height, int width) { public Grid(int width, int height) {
this.height = height;
this.width = width; 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<>(); ArrayList<K> row = new ArrayList<>();
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
row.add(null); row.add(null);
@ -32,10 +36,10 @@ public class Grid<K> implements IGrid<K> {
* @param width sets the width of the grid * @param width sets the width of the grid
* @param tile gives the TileType the grid is to be filled with * @param tile gives the TileType the grid is to be filled with
*/ */
public Grid(int height, int width, K tile) { public Grid(int width, int height, K tile) {
this.height = height;
this.width = width; 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<>(); ArrayList<K> row = new ArrayList<>();
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
row.add(tile); row.add(tile);
@ -56,16 +60,24 @@ public class Grid<K> implements IGrid<K> {
@Override @Override
public K getElement(int x, int y) throws IllegalArgumentException { public K getElement(int x, int y) throws IllegalArgumentException {
if(x >= 0 && x <= width && y >= 0 && y <= height) { makeSureCoordinatesAreWithinBounds(x, y);
return grid.get(y).get(x); return grid.get(y).get(x);
}
throw new IllegalArgumentException();
} }
@Override @Override
public void setElement(int x, int y, K element) { public void setElement(int x, int y, K element) throws IllegalArgumentException {
if(x >= 0 && x <= width && y >= 0 && y <= height) { makeSureCoordinatesAreWithinBounds(x, y);
grid.get(y).set(x, element); 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 * 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> { public interface IGrid<K> {
/** /**
* returns the width of the grid * Gets the width of the grid
* @return the width * @return The width of the grid
*/ */
int getWidth(); int getWidth();
/** /**
* returns the height of the grid * Gets height of the grid
* @return the height * @return The height of the grid
*/ */
int getHeight(); int getHeight();
/** /**
* returns the element in a given x and y coordinate * Gets the element in a given x and y coordinate
* @param x coordinate in the grid * @param x Coordinate in the grid
* @param y coordinate in the grid * @param y Coordinate in the grid
* @return element in the x and y coordinate * @return Element in the x and y coordinate
* @throws IllegalArgumentException throws exception if coordinates are not in the grid * @throws IllegalArgumentException Throws an exception if the coordinates are outside of the grid
*/ */
K getElement(int x,int y) throws IllegalArgumentException; K getElement(int x,int y) throws IllegalArgumentException;
/** /**
* places the element in the given x and y coordinate * Places the element on the given x and y coordinate
* @param x coordinate * @param x Coordinate in the grid
* @param y coordinate * @param y Coordinate in the grid
* @param element that is being placed 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; package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Direction;
import inf112.fiasko.roborally.element_properties.TileType; import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.objects.Grid;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class GridTest { public class GridTest {
private Grid<TileType> grid; private Grid<Tile> grid;
private Grid<TileType> grid2; private Grid<Tile> grid2;
private Tile defaultTile;
@Before @Before
public void setUp() { public void setUp() {
defaultTile = new Tile(TileType.TILE, Direction.NORTH);
grid = new Grid<>(7, 4); grid = new Grid<>(7, 4);
grid2 = new Grid<>(5,8, TileType.TILE); grid2 = new Grid<>(5,8, defaultTile);
} }
@Test @Test
public void getWidthFromGrid() { public void getWidthFromGrid() {
assertEquals(4, grid.getWidth()); assertEquals(7, grid.getWidth());
} }
@Test @Test
public void getWidthFromGrid2() { public void getWidthFromGrid2() {
assertEquals(8, grid2.getWidth()); assertEquals(5, grid2.getWidth());
} }
@Test @Test
public void getHeightFromGrid() { public void getHeightFromGrid() {
assertEquals(7,grid.getHeight()); assertEquals(4, grid.getHeight());
} }
@Test @Test
public void getHeightFromGrid2() { public void getHeightFromGrid2() {
assertEquals(5,grid2.getHeight()); assertEquals(8, grid2.getHeight());
} }
@Test @Test
public void getElementFromGrid2() { public void getElementFromGrid2() {
assertEquals(TileType.TILE, grid2.getElement(5,3)); assertEquals(defaultTile, grid2.getElement(3,5));
} }
@Test @Test
public void setElementInGrid2() { public void setElementInGrid2() {
grid2.setElement(2,1, TileType.HOLE); Tile hole = new Tile(TileType.HOLE, Direction.NORTH);
assertEquals(TileType.HOLE, grid2.getElement(2,1)); 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);
} }
} }