Commit a8622cfa authored by HaskellElephant's avatar HaskellElephant
Browse files

Removed use of generics.

parent 36347bb5
package inf101.v17.cell; package inf101.v17.cell;
import java.util.Random;
/** /**
* *
* The State of a cell * The State of a cell
...@@ -7,5 +9,10 @@ package inf101.v17.cell; ...@@ -7,5 +9,10 @@ package inf101.v17.cell;
public enum CellState { public enum CellState {
ALIVE, ALIVE,
DYING, DYING,
DEAD DEAD;
public static CellState random(Random rand){
return CellState.values()[rand.nextInt(3)];
}
} }
...@@ -25,7 +25,7 @@ public class GameOfLife implements ICellAutomaton { ...@@ -25,7 +25,7 @@ public class GameOfLife implements ICellAutomaton {
/** /**
* The grid the game is played in. * The grid the game is played in.
*/ */
IGrid<CellState> currentGeneration; IGrid currentGeneration;
/** /**
* *
...@@ -36,7 +36,7 @@ public class GameOfLife implements ICellAutomaton { ...@@ -36,7 +36,7 @@ public class GameOfLife implements ICellAutomaton {
* @param width * @param width
*/ */
public GameOfLife(int width, int height) { public GameOfLife(int width, int height) {
currentGeneration = new MyGrid<CellState>(width, height, currentGeneration = new MyGrid(width, height,
CellState.DEAD); CellState.DEAD);
} }
...@@ -76,7 +76,7 @@ public class GameOfLife implements ICellAutomaton { ...@@ -76,7 +76,7 @@ public class GameOfLife implements ICellAutomaton {
@Override @Override
public void stepAutomaton() { public void stepAutomaton() {
IGrid<CellState> nextGeneration = new MyGrid<CellState>( IGrid nextGeneration = new MyGrid(
currentGeneration.getWidth(), currentGeneration.getHeight(), currentGeneration.getWidth(), currentGeneration.getHeight(),
CellState.ALIVE); CellState.ALIVE);
......
...@@ -24,7 +24,7 @@ public class SeedsAutomaton implements ICellAutomaton { ...@@ -24,7 +24,7 @@ public class SeedsAutomaton implements ICellAutomaton {
/** /**
* The grid containing the current generation. * The grid containing the current generation.
*/ */
IGrid<CellState> currentGeneration; IGrid currentGeneration;
/** /**
* *
...@@ -35,7 +35,7 @@ public class SeedsAutomaton implements ICellAutomaton { ...@@ -35,7 +35,7 @@ public class SeedsAutomaton implements ICellAutomaton {
* @param width * @param width
*/ */
public SeedsAutomaton(int width, int height) { public SeedsAutomaton(int width, int height) {
currentGeneration = new MyGrid<CellState>(width, height, currentGeneration = new MyGrid(width, height,
CellState.DEAD); CellState.DEAD);
} }
...@@ -75,7 +75,7 @@ public class SeedsAutomaton implements ICellAutomaton { ...@@ -75,7 +75,7 @@ public class SeedsAutomaton implements ICellAutomaton {
@Override @Override
public void stepAutomaton() { public void stepAutomaton() {
IGrid<CellState> nextGeneration = new MyGrid<CellState>( IGrid nextGeneration = new MyGrid(
currentGeneration.getHeight(), currentGeneration.getWidth(), currentGeneration.getHeight(), currentGeneration.getWidth(),
CellState.ALIVE); CellState.ALIVE);
......
...@@ -6,6 +6,8 @@ import java.util.Random; ...@@ -6,6 +6,8 @@ import java.util.Random;
import org.junit.Test; import org.junit.Test;
import inf101.v17.cell.CellState;
public class GridTest { public class GridTest {
Random random = new Random(); Random random = new Random();
...@@ -15,16 +17,16 @@ public class GridTest { ...@@ -15,16 +17,16 @@ public class GridTest {
*/ */
@Test @Test
public void outOfBoundsTest() { public void outOfBoundsTest() {
IGrid<Integer> grid = new MyGrid<Integer>(10, 10, 0); IGrid grid = new MyGrid(10, 10, CellState.DEAD);
try { try {
grid.set(11, 0, 4); grid.set(11, 0, CellState.DEAD);
fail("Should throw exception"); fail("Should throw exception");
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
; ;
} }
try { try {
grid.set(0, 11, 4); grid.set(0, 11, CellState.DEAD);
fail("Should throw exception"); fail("Should throw exception");
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
; ;
...@@ -33,46 +35,50 @@ public class GridTest { ...@@ -33,46 +35,50 @@ public class GridTest {
@Test @Test
public void setGetTest1() { public void setGetTest1() {
IGrid<Integer> grid = new MyGrid<>(100, 100, 0); IGrid grid = new MyGrid(100, 100, CellState.DEAD);
for (int x = 0; x < 100; x++) for (int x = 0; x < 100; x++)
for (int y = 0; y < 100; y++) { for (int y = 0; y < 100; y++) {
Integer i = random.nextInt(); CellState cs = CellState.random(random);
grid.set(x, y, i); grid.set(x, y, cs);
assertEquals(i, grid.get(x, y)); assertEquals(cs, grid.get(x, y));
} }
} }
@Test @Test
public void setGetTest2() { public void setGetTest2() {
IGrid<Integer> grid = new MyGrid<>(100, 100, 0); IGrid grid = new MyGrid(100, 100, CellState.DEAD);
for (int x = 0; x < 100; x++) for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) { for (int y = 0; y < 100; y++) {
grid.set(x, y, x * y); grid.set(x, y, CellState.random(random));
} }
}
for (int x = 0; x < 100; x++) for (int x = 0; x < 100; x++)
for (int y = 0; y < 100; y++) { for (int y = 0; y < 100; y++) {
grid.set(x, y, x * y); CellState cs = CellState.random(random);
assertEquals(x * y, (int) grid.get(x, y)); grid.set(x, y, cs);
assertEquals(cs, grid.get(x, y));
} }
} }
@Test @Test
public void copyTest() { public void copyTest() {
IGrid<Integer> grid = new MyGrid<>(100, 100, 0); IGrid grid = new MyGrid(100, 100, CellState.DEAD);
for (int x = 0; x < 100; x++) for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) { for (int y = 0; y < 100; y++) {
Integer i = random.nextInt(); CellState cs = CellState.random(random);
grid.set(x, y, i); grid.set(x, y, cs);
} }
}
IGrid<Integer> newGrid = grid.copy();
for (int x = 0; x < 100; x++) IGrid newGrid = grid.copy();
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) { for (int y = 0; y < 100; y++) {
assertEquals(grid.get(x, y), newGrid.get(x,y)); assertEquals(grid.get(x, y), newGrid.get(x, y));
} }
}
} }
} }
package inf101.v17.datastructures; package inf101.v17.datastructures;
import inf101.v17.cell.CellState;
public interface IGrid<T> {
public interface IGrid {
/** /**
* @return The height of the grid. * @return The height of the grid.
...@@ -24,7 +26,7 @@ public interface IGrid<T> { ...@@ -24,7 +26,7 @@ public interface IGrid<T> {
* @param y The row of the cell to change the contents of. * @param y The row of the cell to change the contents of.
* @param element The contents the cell is to have. * @param element The contents the cell is to have.
*/ */
void set(int x, int y, T element); void set(int x, int y, CellState element);
/** /**
* *
...@@ -36,13 +38,13 @@ public interface IGrid<T> { ...@@ -36,13 +38,13 @@ public interface IGrid<T> {
* @param x The column of the cell to get the contents of. * @param x The column of the cell to get the contents of.
* @param y The row of the cell to get contents of. * @param y The row of the cell to get contents of.
*/ */
T get(int x, int y); CellState get(int x, int y);
/** /**
* Make a copy * Make a copy
* *
* @return A fresh copy of the grid, with the same elements * @return A fresh copy of the grid, with the same elements
*/ */
IGrid<T> copy(); IGrid copy();
} }
\ No newline at end of file
package inf101.v17.datastructures; package inf101.v17.datastructures;
public interface IList<T> { import inf101.v17.cell.CellState;
public interface IList {
/** /**
* Legg til et element på slutten av listen. * Legg til et element på slutten av listen.
* *
...@@ -8,7 +10,7 @@ public interface IList<T> { ...@@ -8,7 +10,7 @@ public interface IList<T> {
* *
* Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet. * Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet.
*/ */
void add(T s); void add(CellState s);
/** /**
* Fjern et element fra listen * Fjern et element fra listen
...@@ -18,14 +20,14 @@ public interface IList<T> { ...@@ -18,14 +20,14 @@ public interface IList<T> {
* *
* Etterpå vil alle senere indekser i listen flyttes én posisjon frem. * Etterpå vil alle senere indekser i listen flyttes én posisjon frem.
*/ */
T remove(int i); CellState remove(int i);
/** /**
* Returner elementet på posisjon i * Returner elementet på posisjon i
* @param i * @param i
* @return * @return
*/ */
T get(int i); CellState get(int i);
/** /**
* @return True hvis listen er tom * @return True hvis listen er tom
...@@ -45,7 +47,7 @@ public interface IList<T> { ...@@ -45,7 +47,7 @@ public interface IList<T> {
* *
* Etterpå vil get(i) == s * Etterpå vil get(i) == s
*/ */
void set(int i, T s); void set(int i, CellState s);
/** /**
* Fjern alle elementer fra listen. * Fjern alle elementer fra listen.
......
package inf101.v17.datastructures; package inf101.v17.datastructures;
import inf101.v17.cell.CellState;
/** /**
* *
* A Grid contains a set of cells in a square 2D matrix. * A Grid contains a set of cells in a square 2D matrix.
* *
*/ */
public class MyGrid<T> implements IGrid<T> { public class MyGrid implements IGrid {
private IList<T> cells; private IList cells;
private int height; private int height;
private int width; private int width;
...@@ -19,13 +21,13 @@ public class MyGrid<T> implements IGrid<T> { ...@@ -19,13 +21,13 @@ public class MyGrid<T> implements IGrid<T> {
* @param initElement * @param initElement
* What the cells should initially hold (possibly null) * What the cells should initially hold (possibly null)
*/ */
public MyGrid(int width, int height, T initElement) { public MyGrid(int width, int height, CellState initElement) {
if(width <= 0 || height <= 0) if(width <= 0 || height <= 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.height = height; this.height = height;
this.width = width; this.width = width;
cells = new MyList<T>(height * width); cells = new MyList(height * width);
for (int i = 0; i < height * width; ++i) { for (int i = 0; i < height * width; ++i) {
cells.add(initElement); cells.add(initElement);
} }
...@@ -45,7 +47,7 @@ public class MyGrid<T> implements IGrid<T> { ...@@ -45,7 +47,7 @@ public class MyGrid<T> implements IGrid<T> {
@Override @Override
public void set(int x, int y, T elem) { public void set(int x, int y, CellState elem) {
if(x < 0 || x >= width) if(x < 0 || x >= width)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
if(y < 0 || y >= height) if(y < 0 || y >= height)
...@@ -56,7 +58,7 @@ public class MyGrid<T> implements IGrid<T> { ...@@ -56,7 +58,7 @@ public class MyGrid<T> implements IGrid<T> {
@Override @Override
public T get(int x, int y) { public CellState get(int x, int y) {
if(x < 0 || x >= width) if(x < 0 || x >= width)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
if(y < 0 || y >= height) if(y < 0 || y >= height)
...@@ -66,8 +68,8 @@ public class MyGrid<T> implements IGrid<T> { ...@@ -66,8 +68,8 @@ public class MyGrid<T> implements IGrid<T> {
} }
@Override @Override
public IGrid<T> copy() { public IGrid copy() {
MyGrid<T> newGrid = new MyGrid<>(getWidth(), getHeight(), null); MyGrid newGrid = new MyGrid(getWidth(), getHeight(), null);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
for(int y = 0; y < height; y++) for(int y = 0; y < height; y++)
......
package inf101.v17.datastructures; package inf101.v17.datastructures;
import java.util.Arrays; import java.util.Arrays;
import inf101.v17.cell.CellState;
public class MyList<T> implements IList<T> { public class MyList implements IList {
private int length; private int length;
private T[] data; private CellState[] data;
public MyList() { public MyList() {
length = 0; length = 0;
data = (T[]) new Object[10]; data = new CellState[10];
} }
public MyList(int initialSize) { public MyList(int initialSize) {
if(initialSize < 0) if(initialSize < 0)
throw new IllegalArgumentException("initialSize must be positive: " + initialSize); throw new IllegalArgumentException("initialSize must be positive: " + initialSize);
length = 0; length = 0;
data = (T[]) new Object[initialSize]; data = new CellState[initialSize];
} }
@Override @Override
public void add(T s) { public void add(CellState s) {
if (length == data.length) { if (length == data.length) {
data = Arrays.copyOf(data, data.length * 2); data = Arrays.copyOf(data, data.length * 2);
} }
...@@ -29,8 +30,8 @@ public class MyList<T> implements IList<T> { ...@@ -29,8 +30,8 @@ public class MyList<T> implements IList<T> {
} }
@Override @Override
public T remove(int i) { public CellState remove(int i) {
T element = data[i]; CellState element = data[i];
for (int x = i; x < length - 1; x++) { for (int x = i; x < length - 1; x++) {
data[x] = data[x + 1]; data[x] = data[x + 1];
...@@ -41,7 +42,7 @@ public class MyList<T> implements IList<T> { ...@@ -41,7 +42,7 @@ public class MyList<T> implements IList<T> {
} }
@Override @Override
public T get(int i) { public CellState get(int i) {
return data[i]; return data[i];
} }
...@@ -56,7 +57,7 @@ public class MyList<T> implements IList<T> { ...@@ -56,7 +57,7 @@ public class MyList<T> implements IList<T> {
} }
@Override @Override
public void set(int i, T s) { public void set(int i, CellState s) {
data[i] = s; data[i] = s;
} }
......
...@@ -2,14 +2,16 @@ package inf101.v17.datastructures; ...@@ -2,14 +2,16 @@ package inf101.v17.datastructures;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Random;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Random;
import inf101.v17.cell.CellState;
public class MyListTest { public class MyListTest {
Random random = new Random();
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
...@@ -18,31 +20,19 @@ public class MyListTest { ...@@ -18,31 +20,19 @@ public class MyListTest {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
} }
@Test
public void addGetTest() {
IList<Integer> list = new MyList<>();
for(int i = 0; i < 100; i++)
list.add(i);
for(int i = 0; i < 100; i++)
assertEquals(i, (int)list.get(i));
assertEquals(100, list.size());
}
@Test @Test
public void setGetTest() { public void setGetTest() {
IList<Integer> list = new MyList<>(); IList list = new MyList();
Random rand = new Random();
for(int i = 0; i < 1000; i++) for(int i = 0; i < 1000; i++)
list.add(random.nextInt()); list.add(CellState.random(rand));
for(int i = 0; i < 1000; i++) { for(int i = 0; i < 1000; i++) {
int element = random.nextInt(); CellState element = CellState.random(rand);
list.set(i, element); list.set(i, element);