mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Adds a Grid class and GridTest class
This commit is contained in:
parent
fd6ae823c5
commit
61eb05d66a
71
src/main/java/inf112/fiasko/roborally/objects/Grid.java
Normal file
71
src/main/java/inf112/fiasko/roborally/objects/Grid.java
Normal file
@ -0,0 +1,71 @@
|
||||
package inf112.fiasko.roborally.objects;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Grid<K> implements IGrid<K> {
|
||||
|
||||
private int height;
|
||||
private int width;
|
||||
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
|
||||
*/
|
||||
public Grid(int height, int width) {
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
for(int y = 0; y < height; y++) {
|
||||
ArrayList<K> row = new ArrayList<>();
|
||||
for(int x = 0; x < width; x++) {
|
||||
row.add(null);
|
||||
}
|
||||
this.grid.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a grid filled with standard tiles.
|
||||
* @param height sets the height of the grid
|
||||
* @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;
|
||||
this.width = width;
|
||||
for(int y = 0; y < height; y++) {
|
||||
ArrayList<K> row = new ArrayList<>();
|
||||
for(int x = 0; x < width; x++) {
|
||||
row.add(tile);
|
||||
}
|
||||
this.grid.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package inf112.fiasko.roborally.element_properties;
|
||||
package inf112.fiasko.roborally.objects;
|
||||
|
||||
/**
|
||||
* This Interface describes a grid
|
50
src/test/java/inf112/fiasko/roborally/GridTest.java
Normal file
50
src/test/java/inf112/fiasko/roborally/GridTest.java
Normal file
@ -0,0 +1,50 @@
|
||||
package inf112.fiasko.roborally;
|
||||
|
||||
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;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
grid = new Grid<>(7, 4);
|
||||
grid2 = new Grid<>(5,8, TileType.TILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWidthFromGrid() {
|
||||
assertEquals(4, grid.getWidth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWidthFromGrid2() {
|
||||
assertEquals(8, grid2.getWidth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHeightFromGrid() {
|
||||
assertEquals(7,grid.getHeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHeightFromGrid2() {
|
||||
assertEquals(5,grid2.getHeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementFromGrid2() {
|
||||
assertEquals(TileType.TILE, grid2.getElement(5,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setElementInGrid2() {
|
||||
grid2.setElement(2,1, TileType.HOLE);
|
||||
assertEquals(TileType.HOLE, grid2.getElement(2,1));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user