Commit a8622cfa authored by HaskellElephant's avatar HaskellElephant

Removed use of generics.

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