From 29b48c1a87731c332c7248387e121aa8b6cf71ae Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 20 Feb 2020 14:10:00 +0100 Subject: [PATCH] =?UTF-8?q?Gj=C3=B8r=20n=C3=B8dvendige=20forandringer=20i?= =?UTF-8?q?=20klasser=20og=20interfaces=20relatert=20til=20Grid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../inf112/fiasko/roborally/objects/Grid.java | 44 ++++++++++++------- .../fiasko/roborally/objects/IGrid.java | 30 ++++++------- .../fiasko/roborally/objects/GridTest.java | 35 ++++++++++----- 3 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Grid.java b/src/main/java/inf112/fiasko/roborally/objects/Grid.java index c72a8e0..ed2b71a 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Grid.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Grid.java @@ -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 The type of element the grid should store + */ public class Grid implements IGrid { private int height; @@ -10,14 +14,14 @@ public class Grid implements IGrid { private List> 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 row = new ArrayList<>(); for(int x = 0; x < width; x++) { row.add(null); @@ -32,10 +36,10 @@ public class Grid implements IGrid { * @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 row = new ArrayList<>(); for(int x = 0; x < width; x++) { row.add(tile); @@ -56,16 +60,24 @@ public class Grid implements IGrid { @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(); } } } diff --git a/src/main/java/inf112/fiasko/roborally/objects/IGrid.java b/src/main/java/inf112/fiasko/roborally/objects/IGrid.java index d509c66..e8c4fd8 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/IGrid.java +++ b/src/main/java/inf112/fiasko/roborally/objects/IGrid.java @@ -2,36 +2,36 @@ package inf112.fiasko.roborally.objects; /** * This Interface describes a grid - * @param type of element + * @param The type of element the grid is to store */ public interface IGrid { /** - * 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; } diff --git a/src/test/java/inf112/fiasko/roborally/objects/GridTest.java b/src/test/java/inf112/fiasko/roborally/objects/GridTest.java index 5d69afd..cfa0a2e 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/GridTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/GridTest.java @@ -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 grid; - private Grid grid2; + private Grid grid; + private Grid 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); } } \ No newline at end of file