Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Life
- * <Description>
- * Conway's "Game of Life" using the StdLib Draw class.
- * <Usage>
- * % javac Life.java
- *
- * x | y | rolldice%
- * % java 50 50 .5
- *
- * <References>
- * CIS 201-L UAB Dr. Sloan
- *
- * @author
- * Dan Latham <[email protected]>
- *
- * @version 0.0.1
- *
- */
- public class Life
- {
- //other dir 2
- public static boolean[][] checklifeB2(boolean[][] map)
- {
- for (int x = map[0].length-1; x >= 0; x -= 1)
- {
- for (int y = map.length-1; y >= 0; y -= 1)
- {
- int livecells = 0;
- //try to get values, unless indexOOB problems
- //top 3
- try
- {
- //top left corner
- livecells += (map[y-1][x-1]) ? 1 : 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //top middle
- livecells += (map[y-1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a2)
- {
- livecells += 0;
- }
- try
- {
- //top right corner
- livecells += (map[y-1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a3)
- {
- livecells += 0;
- }
- //middle 2 sides
- try
- {
- //left side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b1)
- {
- livecells += 0;
- }
- try
- {
- //right side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b3)
- {
- livecells += 0;
- }
- //bottom 3
- try
- {
- //bottom left corner
- livecells += (map[y+1][x-1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom middle
- livecells += (map[y+1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom right corner
- livecells += (map[y+1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- //check dead cells first
- if (map[y][x] == false && livecells == 3)
- {
- map[y][x] = true; //dead to alive
- }
- //check alives
- else if (map[y][x] == true && livecells <= 1)
- {
- map[y][x] = false; //alive to dead
- }
- else if (map[y][x] == true && livecells > 3)
- {
- map[y][x] = false; //alive to dead
- }
- else
- {
- map[y][x] = map[y][x];
- }
- }
- }
- return map;
- }
- //other dir 1
- public static boolean[][] checklifeB1(boolean[][] map)
- {
- for (int x = 0; x < map[0].length; x += 1)
- {
- for (int y = 0; y < map.length; y += 1)
- {
- int livecells = 0;
- //try to get values, unless indexOOB problems
- //top 3
- try
- {
- //top left corner
- livecells += (map[y-1][x-1]) ? 1 : 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //top middle
- livecells += (map[y-1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a2)
- {
- livecells += 0;
- }
- try
- {
- //top right corner
- livecells += (map[y-1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a3)
- {
- livecells += 0;
- }
- //middle 2 sides
- try
- {
- //left side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b1)
- {
- livecells += 0;
- }
- try
- {
- //right side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b3)
- {
- livecells += 0;
- }
- //bottom 3
- try
- {
- //bottom left corner
- livecells += (map[y+1][x-1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom middle
- livecells += (map[y+1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom right corner
- livecells += (map[y+1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- //check dead cells first
- if (map[y][x] == false && livecells == 3)
- {
- map[y][x] = true; //dead to alive
- }
- //check alives
- else if (map[y][x] == true && livecells <= 1)
- {
- map[y][x] = false; //alive to dead
- }
- else if (map[y][x] == true && livecells > 3)
- {
- map[y][x] = false; //alive to dead
- }
- else
- {
- map[y][x] = map[y][x];
- }
- }
- }
- return map;
- }
- //backwards loop
- public static boolean[][] checklifeB(boolean[][] map)
- {
- for (int y = map.length-1; y >= 0; y -= 1)
- {
- for (int x = map[0].length-1; x >= 0; x -= 1)
- {
- int livecells = 0;
- //try to get values, unless indexOOB problems
- //top 3
- try
- {
- //top left corner
- livecells += (map[y-1][x-1]) ? 1 : 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //top middle
- livecells += (map[y-1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a2)
- {
- livecells += 0;
- }
- try
- {
- //top right corner
- livecells += (map[y-1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a3)
- {
- livecells += 0;
- }
- //middle 2 sides
- try
- {
- //left side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b1)
- {
- livecells += 0;
- }
- try
- {
- //right side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b3)
- {
- livecells += 0;
- }
- //bottom 3
- try
- {
- //bottom left corner
- livecells += (map[y+1][x-1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom middle
- livecells += (map[y+1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom right corner
- livecells += (map[y+1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- //check dead cells first
- if (map[y][x] == false && livecells == 3)
- {
- map[y][x] = true; //dead to alive
- }
- //check alives
- else if (map[y][x] == true && livecells <= 1)
- {
- map[y][x] = false; //alive to dead
- }
- else if (map[y][x] == true && livecells > 3)
- {
- map[y][x] = false; //alive to dead
- }
- else
- {
- map[y][x] = map[y][x];
- }
- }
- }
- return map;
- }
- public static boolean[][] checklife(boolean[][] map)
- {
- for (int y = 0; y < map.length; y += 1)
- {
- for (int x = 0; x < map[0].length; x += 1)
- {
- int livecells = 0;
- //try to get values, unless indexOOB problems
- //top 3
- try
- {
- //top left corner
- livecells += (map[y-1][x-1]) ? 1 : 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //top middle
- livecells += (map[y-1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a2)
- {
- livecells += 0;
- }
- try
- {
- //top right corner
- livecells += (map[y-1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a3)
- {
- livecells += 0;
- }
- //middle 2 sides
- try
- {
- //left side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b1)
- {
- livecells += 0;
- }
- try
- {
- //right side
- livecells += (map[y][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException b3)
- {
- livecells += 0;
- }
- //bottom 3
- try
- {
- //bottom left corner
- livecells += (map[y+1][x-1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom middle
- livecells += (map[y+1][x]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- try
- {
- //bottom right corner
- livecells += (map[y+1][x+1]) ? 1: 0;
- }
- catch (IndexOutOfBoundsException a1)
- {
- livecells += 0;
- }
- //check dead cells first
- if (map[y][x] == false && livecells == 3)
- {
- map[y][x] = true; //dead to alive
- }
- //check alives
- else if (map[y][x] == true && livecells <= 1)
- {
- map[y][x] = false; //alive to dead
- }
- else if (map[y][x] == true && livecells > 3)
- {
- map[y][x] = false; //alive to dead
- }
- else
- {
- map[y][x] = map[y][x];
- }
- }
- }
- return map;
- }
- public static void life(boolean[][] map, Draw Map)
- {
- Map.clear();
- for (int y = 0; y < map.length; y += 1)
- {
- for (int x = 0; x < map[0].length; x += 1)
- {
- if (map[y][x] == true)
- {
- Map.filledSquare(x/10.0,y/10.0,.05);
- }
- }
- }
- Map.show(750);
- }
- public static boolean[][] random(boolean[][] map, double chance)
- {
- //iterate through all x's of all y's
- for (int y = 0; y < map.length; y+=1)
- {
- for (int x = 0; x < map[0].length; x+=1)
- {
- double a = Math.random();
- if (a >= (1-chance))
- {
- map[y][x] = true;
- }
- }
- }
- return map;
- }
- public static void main(String[] args)
- {
- //x and y dimensions
- int x = Integer.parseInt(args[0]);
- int y = Integer.parseInt(args[1]);
- //initial chance of map[y][x] being alive
- double chance = Double.parseDouble(args[2]);
- //new clean map of array[y][x] for game
- boolean[][] map = new boolean[y][x];
- //calls random to set up map
- map = random(map, chance);
- //StdArrayIO.print(map);
- //Create Draw object
- Draw Map = new Draw();
- Map.setXscale(0, map[0].length/10.0);
- Map.setYscale(0, map.length/10.0);
- while (true)
- {
- life(map, Map);
- //update map, loop back
- map = checklife(map);
- life(map, Map);
- //now backwards
- map = checklifeB(map);
- life(map, Map);
- //now other dir 1
- map = checklifeB1(map);
- life(map, Map);
- //now orther dir 2
- map = checklifeB2(map);
- }
- }
- }
Add Comment
Please, Sign In to add comment