Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Implementation of Conway's Game of Life
- //https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Arrays;
- import java.lang.Integer;
- import java.lang.Boolean;
- class GameOfLife{
- public static void main(String[] args){
- GameOfLife gameOfLife = new GameOfLife();
- gameOfLife.test();
- }
- private boolean willBeAlive(boolean alive, int liveNeighborCt){
- return (alive && liveNeighborCt == 2) || liveNeighborCt == 3;
- }
- private int[][] generateSignals(int[] cell){
- int x = cell[0];
- int y = cell[1];
- int[][] signals = {
- {x-1, y-1}, {x-1, y}, {x-1, y+1},
- {x, y-1}, {x, y+1},
- {x+1, y-1}, {x+1, y}, {x+1, y+1},
- };
- return signals;
- }
- private HashMap<String, Integer> generateLiveNeighborCtMap(int[][] liveCells){
- HashMap<String, Integer> liveNeighborCtMap = new HashMap<String, Integer>();
- for(int i = 0; i < liveCells.length; i++){
- String cellKey = intDuple2Str(liveCells[i]);
- if( !liveNeighborCtMap.containsKey( cellKey ) ){
- liveNeighborCtMap.put(cellKey, 0);
- }
- int[][] signals = generateSignals(liveCells[i]);
- for(int j = 0; j < signals.length; j++){
- String signalKey = intDuple2Str(signals[j]);
- if( liveNeighborCtMap.containsKey(signalKey) ){
- liveNeighborCtMap.put(signalKey, liveNeighborCtMap.get(signalKey)+1);
- }
- else{
- liveNeighborCtMap.put(signalKey, 1);
- }
- }
- }
- return liveNeighborCtMap;
- }
- /*private int[][] calcNextState(int[][] liveCells){
- ArrayList<Integer[]> nextState = new ArrayList<Integer[]>();
- HashMap<String, Integer> liveNeighborCtMap = new HashMap<String, Integer>();
- for(String cell : liveNeighborCtMap.keySet()){
- if(willBeAlive(cell))
- }
- return nextState.toArray();
- }*/
- //Helper Functions
- private String intDuple2Str(int[] duple){
- return "(" + duple[0] + "," + duple[1] + ")";
- }
- private boolean contains(int[][] container, int[] target){
- for(int i = 0; i < container.length; i++){
- if(Arrays.equals(target, container[i])){
- return true;
- }
- }
- return false;
- }
- //TESTING:
- private HashMap<String, Boolean> getTests(){
- HashMap<String, Boolean> TESTS = new HashMap<String, Boolean>();
- //Tests willBeAlive:
- TESTS.put("alive, 1 live neighbor", willBeAlive(true, 1) == false);
- TESTS.put("alive, 3 live neighbors", willBeAlive(true, 3) == true);
- TESTS.put("alive, 4 live neighbors", willBeAlive(true, 4) == false);
- TESTS.put("dead, 2 live neighbors", willBeAlive(false, 2) == false);
- TESTS.put("dead, 3 live neighbors", willBeAlive(false, 3) == true);
- TESTS.put("dead, 4 live neighbors", willBeAlive(false, 4) == false);
- //Tests compare2dIntArrays:
- TESTS.put("Array Compare Equal",
- compare2dIntArrays(new int[][]{ {0, 0}, {2, 4} }, new int[][]{ {0, 0}, {2, 4} }) == true);
- TESTS.put("Array Compare Not Equal",
- compare2dIntArrays(new int[][]{ {1, 1} }, new int[][]{ {2, 2} }) == false);
- //Tests generateSignals:
- int[][] signalsExpected;
- signalsExpected = new int[][]{
- {-1, -1}, {-1, 0}, {-1, 1},
- {0, -1}, {0, 1},
- {1, -1}, {1, 0}, {1, 1},
- };
- TESTS.put("Generate Signals for (0, 0)", compare2dIntArrays( generateSignals(new int[]{ 0, 0 }), signalsExpected ));
- signalsExpected = new int[][]{
- {4, 0}, {4, 1}, {4, 2},
- {5, 0}, {5, 2},
- {6, 0}, {6, 1}, {6, 2},
- };
- TESTS.put("Generate Signals for (5, 1)", compare2dIntArrays( generateSignals(new int[]{ 5, 1 }), signalsExpected ));
- //Tests intDuple2Str:
- TESTS.put("duple2string (4,5)", intDuple2Str(new int[]{4, 5}).equals("(4,5)"));
- TESTS.put("duple2string (0,0)", intDuple2Str(new int[]{0, 0}).equals("(0,0)"));
- //Tests generateLiveNeighborCtMap:
- HashMap<String, Integer> mapExpected = new HashMap<String, Integer>();
- TESTS.put("Live Neighbor Count Map for Empty Set", generateLiveNeighborCtMap(new int[][]{}).equals(mapExpected));
- mapExpected.put("(1,1)", 1); mapExpected.put("(0,1)", 1);
- mapExpected.put("(1,0)", 1); mapExpected.put("(0,0)", 0);
- mapExpected.put("(-1,1)", 1); mapExpected.put("(-1,0)", 1);
- mapExpected.put("(0,-1)", 1); mapExpected.put("(1,-1)", 1);
- mapExpected.put("(-1,-1)", 1);
- TESTS.put("Live Neighbor Count Map for One Cell", generateLiveNeighborCtMap(new int[][]{{0,0}}).equals(mapExpected));
- mapExpected.put("(-1,1)", 1); mapExpected.put("(-1,0)", 1);
- mapExpected.put("(1,-1)", 1); mapExpected.put("(-1,-1)", 1);
- mapExpected.put("(1,1)", 1); mapExpected.put("(0,1)", 1);
- mapExpected.put("(1,0)", 1); mapExpected.put("(0,0)", 0);
- mapExpected.put("(4,6)", 1); mapExpected.put("(5,4)", 1);
- mapExpected.put("(0,-1)", 1); mapExpected.put("(6,6)", 1);
- mapExpected.put("(4,4)", 1); mapExpected.put("(5,6)", 1);
- mapExpected.put("(6,5)", 1); mapExpected.put("(5,5)", 0);
- mapExpected.put("(4,5)", 1); mapExpected.put("(6,4)", 1);
- TESTS.put("Live Neighbor Count Map for Two Far Cells", generateLiveNeighborCtMap(new int[][]{{0,0},{5,5}}).equals(mapExpected));
- mapExpected.clear();
- mapExpected.put("(2,0)", 1); mapExpected.put("(1,1)", 2);
- mapExpected.put("(2,1)", 1); mapExpected.put("(2,-1)", 1);
- mapExpected.put("(0,1)", 2); mapExpected.put("(1,0)", 1);
- mapExpected.put("(0,0)", 1); mapExpected.put("(-1,1)", 1);
- mapExpected.put("(-1,0)", 1); mapExpected.put("(0,-1)", 2);
- mapExpected.put("(1,-1)", 2); mapExpected.put("(-1,-1)", 1);
- TESTS.put("Live Neighbor Count Map for Two Adjacent Cells", generateLiveNeighborCtMap(new int[][]{{0,0},{1,0}}).equals(mapExpected));
- /*int[][] neighborCtMap = {{0,0}};
- printLiveNeighborCtMapCode(
- generateLiveNeighborCtMap( new int[][]{{0,0}} )
- );*/
- //Tests contains:
- TESTS.put("[[2, 1], [3]] contains [2, 1]", contains(new int[][]{ {2, 1}, {3} }, new int[]{2, 1}) == true);
- TESTS.put("[[4], [3, 2]] contains [4]", contains(new int[][]{ {4}, {3, 2} }, new int[]{4}) == true);
- //Tests calcNextState:
- return TESTS;
- }
- //Helper Test Functions
- private void printTests(HashMap<String, Boolean> TESTS){
- System.out.println("STAT - TEST:");
- System.out.println("------------");
- for(String test : TESTS.keySet()){
- System.out.println((TESTS.get(test) ? "PASS" : "FAIL") + " - " + test);
- }
- }
- private void test(){
- printTests(getTests());
- }
- private boolean compare2dIntArrays(int[][] a1, int[][] a2){
- if(a1 == null) return a2 == null;
- if(a2 == null) return false;
- if(a1.length != a2.length) return false;
- for(int i = 0; i < a1.length; i++){
- if(!Arrays.equals(a1[i], a2[i])){
- return false;
- }
- }
- return true;
- }
- public void printLiveNeighborCtMapCode(HashMap<String, Integer> liveNeighborCtMap){
- for(String cell : liveNeighborCtMap.keySet()){
- System.out.println( "mapExpected.put(\"" + cell + "\", " + liveNeighborCtMap.get(cell) + ");");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement