mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-06-25 02:34:43 +02:00
Adds a Grid class and GridTest class
This commit is contained in:
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
|
Reference in New Issue
Block a user