isefire

ConwayGameOfLife

Jul 16th, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.50 KB | None | 0 0
  1. /* Life
  2.  * <Description>
  3.  * Conway's "Game of Life" using the StdLib Draw class.
  4.  * <Usage>
  5.  * % javac Life.java
  6.  *
  7.  *        x | y | rolldice%
  8.  * % java 50 50 .5
  9.  *
  10.  * <References>
  11.  * CIS 201-L UAB Dr. Sloan
  12.  *
  13.  * @author
  14.  * Dan Latham <[email protected]>
  15.  *
  16.  * @version 0.0.1
  17.  *
  18.  */
  19. public class Life
  20. {
  21.     //other dir 2
  22.     public static boolean[][] checklifeB2(boolean[][] map)
  23.     {
  24.         for (int x = map[0].length-1; x >= 0; x -= 1)
  25.         {
  26.             for (int y = map.length-1; y >= 0; y -= 1)
  27.             {
  28.                 int livecells = 0;
  29.                 //try to get values, unless indexOOB problems
  30.                 //top 3
  31.                 try
  32.                 {
  33.                     //top left corner
  34.                     livecells += (map[y-1][x-1]) ? 1 : 0;
  35.                 }
  36.                 catch (IndexOutOfBoundsException a1)
  37.                 {
  38.                     livecells += 0;
  39.                 }
  40.  
  41.                 try
  42.                 {
  43.                     //top middle
  44.                     livecells += (map[y-1][x]) ? 1: 0;
  45.                 }
  46.                 catch (IndexOutOfBoundsException a2)
  47.                 {
  48.                     livecells += 0;
  49.                 }
  50.                
  51.                 try
  52.                 {
  53.                     //top right corner
  54.                     livecells += (map[y-1][x+1]) ? 1: 0;
  55.                 }
  56.                 catch (IndexOutOfBoundsException a3)
  57.                 {
  58.                     livecells += 0;
  59.                 }
  60.                
  61.                 //middle 2 sides
  62.                 try
  63.                 {
  64.                     //left side
  65.                     livecells += (map[y][x+1]) ? 1: 0;
  66.                 }
  67.                 catch (IndexOutOfBoundsException b1)
  68.                 {
  69.                     livecells += 0;
  70.                 }
  71.                 try
  72.                 {
  73.                     //right side
  74.                     livecells += (map[y][x+1]) ? 1: 0;
  75.                 }
  76.                 catch (IndexOutOfBoundsException b3)
  77.                 {
  78.                     livecells += 0;
  79.                 }
  80.                
  81.                 //bottom 3
  82.                
  83.                 try
  84.                 {
  85.                     //bottom left corner
  86.                     livecells += (map[y+1][x-1]) ? 1: 0;
  87.                 }
  88.                 catch (IndexOutOfBoundsException a1)
  89.                 {
  90.                     livecells += 0;
  91.                 }
  92.  
  93.                 try
  94.                 {
  95.                     //bottom middle
  96.                     livecells += (map[y+1][x]) ? 1: 0;
  97.                 }
  98.                 catch (IndexOutOfBoundsException a1)
  99.                 {
  100.                     livecells += 0;
  101.                 }
  102.  
  103.                 try
  104.                 {
  105.                     //bottom right corner
  106.                     livecells += (map[y+1][x+1]) ? 1: 0;
  107.                 }
  108.                 catch (IndexOutOfBoundsException a1)
  109.                 {
  110.                     livecells += 0;
  111.                 }
  112.  
  113.                
  114.                 //check dead cells first
  115.                 if (map[y][x] == false && livecells == 3)
  116.                 {
  117.                     map[y][x] = true; //dead to alive
  118.                 }
  119.                 //check alives
  120.                 else if (map[y][x] == true && livecells <= 1)
  121.                 {
  122.                     map[y][x] = false; //alive to dead
  123.                 }
  124.                 else if (map[y][x] == true && livecells > 3)
  125.                 {
  126.                     map[y][x] = false; //alive to dead
  127.                 }
  128.                 else
  129.                 {
  130.                     map[y][x] = map[y][x];
  131.                 }
  132.             }
  133.         }
  134.         return map;
  135.     }
  136.    
  137.     //other dir 1
  138.     public static boolean[][] checklifeB1(boolean[][] map)
  139.     {
  140.         for (int x = 0; x < map[0].length; x += 1)
  141.         {
  142.             for (int y = 0; y < map.length; y += 1)
  143.             {
  144.                 int livecells = 0;
  145.                 //try to get values, unless indexOOB problems
  146.                 //top 3
  147.                 try
  148.                 {
  149.                     //top left corner
  150.                     livecells += (map[y-1][x-1]) ? 1 : 0;
  151.                 }
  152.                 catch (IndexOutOfBoundsException a1)
  153.                 {
  154.                     livecells += 0;
  155.                 }
  156.  
  157.                 try
  158.                 {
  159.                     //top middle
  160.                     livecells += (map[y-1][x]) ? 1: 0;
  161.                 }
  162.                 catch (IndexOutOfBoundsException a2)
  163.                 {
  164.                     livecells += 0;
  165.                 }
  166.                
  167.                 try
  168.                 {
  169.                     //top right corner
  170.                     livecells += (map[y-1][x+1]) ? 1: 0;
  171.                 }
  172.                 catch (IndexOutOfBoundsException a3)
  173.                 {
  174.                     livecells += 0;
  175.                 }
  176.                
  177.                 //middle 2 sides
  178.                 try
  179.                 {
  180.                     //left side
  181.                     livecells += (map[y][x+1]) ? 1: 0;
  182.                 }
  183.                 catch (IndexOutOfBoundsException b1)
  184.                 {
  185.                     livecells += 0;
  186.                 }
  187.                 try
  188.                 {
  189.                     //right side
  190.                     livecells += (map[y][x+1]) ? 1: 0;
  191.                 }
  192.                 catch (IndexOutOfBoundsException b3)
  193.                 {
  194.                     livecells += 0;
  195.                 }
  196.                
  197.                 //bottom 3
  198.                
  199.                 try
  200.                 {
  201.                     //bottom left corner
  202.                     livecells += (map[y+1][x-1]) ? 1: 0;
  203.                 }
  204.                 catch (IndexOutOfBoundsException a1)
  205.                 {
  206.                     livecells += 0;
  207.                 }
  208.  
  209.                 try
  210.                 {
  211.                     //bottom middle
  212.                     livecells += (map[y+1][x]) ? 1: 0;
  213.                 }
  214.                 catch (IndexOutOfBoundsException a1)
  215.                 {
  216.                     livecells += 0;
  217.                 }
  218.  
  219.                 try
  220.                 {
  221.                     //bottom right corner
  222.                     livecells += (map[y+1][x+1]) ? 1: 0;
  223.                 }
  224.                 catch (IndexOutOfBoundsException a1)
  225.                 {
  226.                     livecells += 0;
  227.                 }
  228.  
  229.                
  230.                 //check dead cells first
  231.                 if (map[y][x] == false && livecells == 3)
  232.                 {
  233.                     map[y][x] = true; //dead to alive
  234.                 }
  235.                 //check alives
  236.                 else if (map[y][x] == true && livecells <= 1)
  237.                 {
  238.                     map[y][x] = false; //alive to dead
  239.                 }
  240.                 else if (map[y][x] == true && livecells > 3)
  241.                 {
  242.                     map[y][x] = false; //alive to dead
  243.                 }
  244.                 else
  245.                 {
  246.                     map[y][x] = map[y][x];
  247.                 }
  248.             }
  249.         }
  250.         return map;
  251.     }
  252.  
  253.  
  254.     //backwards loop
  255.     public static boolean[][] checklifeB(boolean[][] map)
  256.     {
  257.         for (int y = map.length-1; y >= 0; y -= 1)
  258.         {
  259.             for (int x = map[0].length-1; x >= 0; x -= 1)
  260.             {
  261.                 int livecells = 0;
  262.                 //try to get values, unless indexOOB problems
  263.                 //top 3
  264.                 try
  265.                 {
  266.                     //top left corner
  267.                     livecells += (map[y-1][x-1]) ? 1 : 0;
  268.                 }
  269.                 catch (IndexOutOfBoundsException a1)
  270.                 {
  271.                     livecells += 0;
  272.                 }
  273.  
  274.                 try
  275.                 {
  276.                     //top middle
  277.                     livecells += (map[y-1][x]) ? 1: 0;
  278.                 }
  279.                 catch (IndexOutOfBoundsException a2)
  280.                 {
  281.                     livecells += 0;
  282.                 }
  283.                
  284.                 try
  285.                 {
  286.                     //top right corner
  287.                     livecells += (map[y-1][x+1]) ? 1: 0;
  288.                 }
  289.                 catch (IndexOutOfBoundsException a3)
  290.                 {
  291.                     livecells += 0;
  292.                 }
  293.                
  294.                 //middle 2 sides
  295.                 try
  296.                 {
  297.                     //left side
  298.                     livecells += (map[y][x+1]) ? 1: 0;
  299.                 }
  300.                 catch (IndexOutOfBoundsException b1)
  301.                 {
  302.                     livecells += 0;
  303.                 }
  304.                 try
  305.                 {
  306.                     //right side
  307.                     livecells += (map[y][x+1]) ? 1: 0;
  308.                 }
  309.                 catch (IndexOutOfBoundsException b3)
  310.                 {
  311.                     livecells += 0;
  312.                 }
  313.                
  314.                 //bottom 3
  315.                
  316.                 try
  317.                 {
  318.                     //bottom left corner
  319.                     livecells += (map[y+1][x-1]) ? 1: 0;
  320.                 }
  321.                 catch (IndexOutOfBoundsException a1)
  322.                 {
  323.                     livecells += 0;
  324.                 }
  325.  
  326.                 try
  327.                 {
  328.                     //bottom middle
  329.                     livecells += (map[y+1][x]) ? 1: 0;
  330.                 }
  331.                 catch (IndexOutOfBoundsException a1)
  332.                 {
  333.                     livecells += 0;
  334.                 }
  335.  
  336.                 try
  337.                 {
  338.                     //bottom right corner
  339.                     livecells += (map[y+1][x+1]) ? 1: 0;
  340.                 }
  341.                 catch (IndexOutOfBoundsException a1)
  342.                 {
  343.                     livecells += 0;
  344.                 }
  345.  
  346.                
  347.                 //check dead cells first
  348.                 if (map[y][x] == false && livecells == 3)
  349.                 {
  350.                     map[y][x] = true; //dead to alive
  351.                 }
  352.                 //check alives
  353.                 else if (map[y][x] == true && livecells <= 1)
  354.                 {
  355.                     map[y][x] = false; //alive to dead
  356.                 }
  357.                 else if (map[y][x] == true && livecells > 3)
  358.                 {
  359.                     map[y][x] = false; //alive to dead
  360.                 }
  361.                 else
  362.                 {
  363.                     map[y][x] = map[y][x];
  364.                 }
  365.             }
  366.         }
  367.         return map;
  368.     }
  369.     public static boolean[][] checklife(boolean[][] map)
  370.     {
  371.         for (int y = 0; y < map.length; y += 1)
  372.         {
  373.             for (int x = 0; x < map[0].length; x += 1)
  374.             {
  375.                 int livecells = 0;
  376.                 //try to get values, unless indexOOB problems
  377.                 //top 3
  378.                 try
  379.                 {
  380.                     //top left corner
  381.                     livecells += (map[y-1][x-1]) ? 1 : 0;
  382.                 }
  383.                 catch (IndexOutOfBoundsException a1)
  384.                 {
  385.                     livecells += 0;
  386.                 }
  387.  
  388.                 try
  389.                 {
  390.                     //top middle
  391.                     livecells += (map[y-1][x]) ? 1: 0;
  392.                 }
  393.                 catch (IndexOutOfBoundsException a2)
  394.                 {
  395.                     livecells += 0;
  396.                 }
  397.                
  398.                 try
  399.                 {
  400.                     //top right corner
  401.                     livecells += (map[y-1][x+1]) ? 1: 0;
  402.                 }
  403.                 catch (IndexOutOfBoundsException a3)
  404.                 {
  405.                     livecells += 0;
  406.                 }
  407.                
  408.                 //middle 2 sides
  409.                 try
  410.                 {
  411.                     //left side
  412.                     livecells += (map[y][x+1]) ? 1: 0;
  413.                 }
  414.                 catch (IndexOutOfBoundsException b1)
  415.                 {
  416.                     livecells += 0;
  417.                 }
  418.                 try
  419.                 {
  420.                     //right side
  421.                     livecells += (map[y][x+1]) ? 1: 0;
  422.                 }
  423.                 catch (IndexOutOfBoundsException b3)
  424.                 {
  425.                     livecells += 0;
  426.                 }
  427.                
  428.                 //bottom 3
  429.                
  430.                 try
  431.                 {
  432.                     //bottom left corner
  433.                     livecells += (map[y+1][x-1]) ? 1: 0;
  434.                 }
  435.                 catch (IndexOutOfBoundsException a1)
  436.                 {
  437.                     livecells += 0;
  438.                 }
  439.  
  440.                 try
  441.                 {
  442.                     //bottom middle
  443.                     livecells += (map[y+1][x]) ? 1: 0;
  444.                 }
  445.                 catch (IndexOutOfBoundsException a1)
  446.                 {
  447.                     livecells += 0;
  448.                 }
  449.  
  450.                 try
  451.                 {
  452.                     //bottom right corner
  453.                     livecells += (map[y+1][x+1]) ? 1: 0;
  454.                 }
  455.                 catch (IndexOutOfBoundsException a1)
  456.                 {
  457.                     livecells += 0;
  458.                 }
  459.  
  460.                
  461.                 //check dead cells first
  462.                 if (map[y][x] == false && livecells == 3)
  463.                 {
  464.                     map[y][x] = true; //dead to alive
  465.                 }
  466.                 //check alives
  467.                 else if (map[y][x] == true && livecells <= 1)
  468.                 {
  469.                     map[y][x] = false; //alive to dead
  470.                 }
  471.                 else if (map[y][x] == true && livecells > 3)
  472.                 {
  473.                     map[y][x] = false; //alive to dead
  474.                 }
  475.                 else
  476.                 {
  477.                     map[y][x] = map[y][x];
  478.                 }
  479.             }
  480.         }
  481.         return map;
  482.     }
  483.    
  484.     public static void life(boolean[][] map, Draw Map)
  485.     {
  486.         Map.clear();
  487.         for (int y = 0; y < map.length; y += 1)
  488.         {
  489.             for (int x = 0; x < map[0].length; x += 1)
  490.             {
  491.                 if (map[y][x] == true)
  492.                 {
  493.                     Map.filledSquare(x/10.0,y/10.0,.05);
  494.                 }
  495.             }
  496.         }
  497.         Map.show(750);
  498.     }
  499.     public static boolean[][] random(boolean[][] map, double chance)
  500.     {
  501.         //iterate through all x's of all y's
  502.         for (int y = 0; y < map.length; y+=1)
  503.         {
  504.             for (int x = 0; x < map[0].length; x+=1)
  505.             {
  506.                 double a = Math.random();
  507.                 if (a >= (1-chance))
  508.                 {
  509.                     map[y][x] = true;
  510.                 }
  511.             }
  512.         }
  513.         return map;
  514.     }
  515.     public static void main(String[] args)
  516.     {
  517.         //x and y dimensions
  518.         int x = Integer.parseInt(args[0]);
  519.         int y = Integer.parseInt(args[1]);
  520.         //initial chance of map[y][x] being alive
  521.         double chance = Double.parseDouble(args[2]);
  522.         //new clean map of array[y][x] for game
  523.         boolean[][] map = new boolean[y][x];
  524.         //calls random to set up map
  525.         map = random(map, chance);
  526.         //StdArrayIO.print(map);
  527.         //Create Draw object
  528.         Draw Map = new Draw();
  529.         Map.setXscale(0, map[0].length/10.0);
  530.         Map.setYscale(0, map.length/10.0);
  531.         while (true)
  532.         {
  533.             life(map, Map);
  534.             //update map, loop back
  535.             map = checklife(map);
  536.  
  537.             life(map, Map);
  538.             //now backwards
  539.             map = checklifeB(map);
  540.  
  541.             life(map, Map);
  542.             //now other dir 1
  543.             map = checklifeB1(map);
  544.  
  545.             life(map, Map);
  546.             //now orther dir 2
  547.             map = checklifeB2(map);
  548.            
  549.         }
  550.     }
  551. }
Add Comment
Please, Sign In to add comment