Advertisement
makispaiktis

Conway 's Game Of Life - V1 - While (20 ms) and steps by 5

Jun 8th, 2022 (edited)
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.35 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class Gui extends JFrame{
  7.  
  8.     // *******************************************************************************
  9.     // *******************************************************************************
  10.     // Variables
  11.     private static int N = 8;
  12.     private static int GENS_LIMIT = 1;
  13.     private static int millis = 20;
  14.     public static JButton[][] tiles;
  15.     private static JButton[] down;
  16.     private static JButton start;
  17.     private static JButton[][] tiles2;
  18.     public static int[][] values;
  19.     public static int[][] values2;
  20.     private static boolean[][] initialization;
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     // *******************************************************************************
  27.     // *******************************************************************************
  28.     // Constructor
  29.     public Gui(){
  30.         super("Conway's Game Of Life");
  31.         setLayout(new GridLayout(N+1, N));
  32.         tiles = new JButton[N][N];
  33.         tiles2 = new JButton[N][N];
  34.         down = new JButton[N];
  35.         values = new int[N][N];
  36.         values2 = new int[N][N];
  37.         initialization = new boolean[N][N];
  38.  
  39.         // 3 Main Variables
  40.         for(int i=0; i<N; i++){
  41.             for(int j=0; j<N; j++){
  42.                 values[i][j] = 0;
  43.                 JButton b = new JButton("(" + i + ", " + j + ")");
  44.                 tiles[i][j] = b;
  45.                 initialization[i][j] = true;
  46.                 add(b);
  47.                 b.setBackground(Color.BLACK);
  48.             }
  49.         }
  50.  
  51.         // 1 Down Variable
  52.         for(int i=0; i<N; i++){
  53.             JButton b = new JButton("Proceed");
  54.             down[i] = b;
  55.             add(b);
  56.             if(i != 0){
  57.                 b.setVisible(false);
  58.                 b.setEnabled(false);
  59.             }
  60.             else{
  61.                 b.setVisible(true);
  62.                 b.setEnabled(true);
  63.                 start = b;
  64.             }
  65.  
  66.         }
  67.     }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.     // *******************************************************************************
  75.     // *******************************************************************************
  76.     // Initialize the states by clicking the buttons
  77.     public static void initialize(){
  78.         for(int i=0; i<N; i++){
  79.             for(int j=0; j<N; j++){
  80.                 JButton b = tiles[i][j];
  81.                 boolean init = initialization[i][j];
  82.                 b.addActionListener(
  83.                         new ActionListener() {
  84.                             @Override
  85.                             public void actionPerformed(ActionEvent event) {
  86.                                 if(b.isEnabled()){
  87.                                     b.setBackground(Color.RED);
  88.                                     // b.setEnabled(false);                    // Maybe wrong
  89.                                     // values[i][j] = 1;
  90.                                     // b.removeActionListener();
  91.                                 }
  92.                             }
  93.                         }
  94.                 );
  95.             }
  96.         }
  97.  
  98.         start.addActionListener(
  99.                 new ActionListener() {
  100.                     @Override
  101.                     public void actionPerformed(ActionEvent event) {
  102.                         if(start.isEnabled()){
  103.                             start.setBackground(Color.GREEN);
  104.                             // start.setEnabled(false);
  105.                             // System.out.println("Starting....");
  106.                             updateValues();
  107.                             int counter = 1;
  108.                             while(counter <= GENS_LIMIT){
  109.                                 defineNextGenValues(counter);
  110.                                 counter ++;
  111.  
  112.                             }
  113.                         }
  114.                     }
  115.                 }
  116.         );
  117.  
  118.     }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.     // *******************************************************************************
  125.     // *******************************************************************************
  126.     public static void updateValues(){
  127.         System.out.println("***********************************");
  128.         System.out.println("Generation 0");
  129.         for(int i=0; i<N; i++){
  130.             for(int j=0; j<N; j++){
  131.                 if(tiles[i][j].getBackground() == Color.RED){
  132.                     values[i][j] = 1;
  133.                 }
  134.                 else{
  135.                     values[i][j] = 0;
  136.                 }
  137.             }
  138.         }
  139.         showTable();
  140.         delay();
  141.         // System.out.println("Leaving initialize function");
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.     public static void updateTiles(){
  148.         for(int i=0; i<N; i++){
  149.             for(int j=0; j<N; j++){
  150.                 if(values[i][j] == 1){
  151.                     tiles[i][j].setBackground(Color.RED);
  152.                 }
  153.                 else{
  154.                     tiles[i][j].setBackground(Color.BLACK);
  155.                 }
  156.             }
  157.         }
  158.         showTable();
  159.         delay();
  160.     }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.     // *******************************************************************************
  169.     // *******************************************************************************
  170.     public static void showTable(){
  171.         for(int i=0; i<N; i++) {
  172.             for (int j = 0; j < N; j++) {
  173.                 System.out.printf("%d ", values[i][j]);
  174.             }
  175.             System.out.println();
  176.         }
  177.     }
  178.  
  179.  
  180.     public static int sum(ArrayList<Integer> list){
  181.         int SUM = 0;
  182.         for(int i=0; i<list.size(); i++){
  183.             SUM += list.get(i);
  184.         }
  185.         return SUM;
  186.     }
  187.  
  188.  
  189.     public static void delay(){
  190.         for(int i=0; i<N; i++){
  191.             for(int j=0; j<N; j++){
  192.                 JButton b = tiles[i][j];
  193.                 if(values[i][j] == 1){
  194.                     b.setBackground(Color.RED);
  195.                 }
  196.                 else{
  197.                     b.setBackground(Color.BLACK);
  198.                 }
  199.             }
  200.         }
  201.         // int millis = 500;
  202.         try {
  203.             Thread.sleep(millis);
  204.         } catch (InterruptedException ie) {
  205.             Thread.currentThread().interrupt();
  206.         }
  207.     }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.     // *******************************************************************************
  219.     // *******************************************************************************
  220.     public static int decide(int state, ArrayList<Integer> statesNear){
  221.         int SUM = sum(statesNear);
  222.         int nextState = -1000;
  223.         if(state == 0){
  224.             // It's a dead cell and the only chance to live is to have 3 alive neighbors
  225.             if(SUM == 3){
  226.                 nextState = 1;
  227.             }
  228.             else{
  229.                 nextState = 0;
  230.             }
  231.         }
  232.  
  233.         else{
  234.             // It's an alive cell. The only chances to continue living is to have 2 or 3 neighbors
  235.             if(SUM == 2 || SUM == 3){
  236.                 nextState = 1;
  237.             }
  238.             else{
  239.                 nextState = 0;
  240.             }
  241.         }
  242.         return nextState;
  243.  
  244.     }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.     // *******************************************************************************
  256.     // *******************************************************************************
  257.     public static void defineNextGenValues(int counter){
  258.         // System.out.println("Entering next gen");
  259.         System.out.println("***********************************");
  260.         System.out.println("Generation " + counter);
  261.         for(int i=0; i<N; i++){
  262.             for(int j=0; j<N; j++){
  263.  
  264.                 int state = values[i][j];
  265.                 int nextState = -1000;
  266.  
  267.                 // Case 1.a - UP and LEFT
  268.                 if(i == 0 && j == 0){
  269.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  270.                     statesNear.add(values[0][1]);
  271.                     statesNear.add(values[1][0]);
  272.                     statesNear.add(values[1][1]);
  273.                     nextState = decide(state, statesNear);
  274.                     values2[i][j] = nextState;
  275.                 }
  276.  
  277.                 // Case 1.b - UP and RIGHT
  278.                 else if (i == 0 && j == N-1){
  279.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  280.                     statesNear.add(values[0][N-2]);
  281.                     statesNear.add(values[1][N-1]);
  282.                     statesNear.add(values[1][N-2]);
  283.                     nextState = decide(state, statesNear);
  284.                     values2[i][j] = nextState;
  285.                 }
  286.  
  287.                 // Case 1.c - UP and MIDDLE
  288.                 else if (i == 0 && (j != 0 || j != N-1)){
  289.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  290.                     statesNear.add(values[0][j-1]);
  291.                     statesNear.add(values[0][j+1]);
  292.                     statesNear.add(values[1][j-1]);
  293.                     statesNear.add(values[1][j]);
  294.                     statesNear.add(values[1][j+1]);
  295.                     nextState = decide(state, statesNear);
  296.                     values2[i][j] = nextState;
  297.                 }
  298.  
  299.                 // Case 2.a - DOWN and LEFT
  300.                 else if (i == N-1 && j == 0){
  301.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  302.                     statesNear.add(values[N-1][1]);
  303.                     statesNear.add(values[N-2][0]);
  304.                     statesNear.add(values[N-2][1]);
  305.                     nextState = decide(state, statesNear);
  306.                     values2[i][j] = nextState;
  307.                 }
  308.  
  309.                 // Case 2.b - DOWN and RIGHT
  310.                 else if (i == N-1 && j == N-1){
  311.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  312.                     statesNear.add(values[N-1][N-2]);
  313.                     statesNear.add(values[N-2][N-1]);
  314.                     statesNear.add(values[N-2][N-2]);
  315.                     nextState = decide(state, statesNear);
  316.                     values2[i][j] = nextState;
  317.                 }
  318.  
  319.                 // Case 2.c - DOWN and MIDDLE
  320.                 else if (i == N-1 && (j != 0 || j != N-1)){
  321.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  322.                     statesNear.add(values[N-1][j-1]);
  323.                     statesNear.add(values[N-1][j+1]);
  324.                     statesNear.add(values[N-2][j-1]);
  325.                     statesNear.add(values[N-2][j]);
  326.                     statesNear.add(values[N-2][j+1]);
  327.                     nextState = decide(state, statesNear);
  328.                     values2[i][j] = nextState;
  329.                 }
  330.  
  331.                 // Case 3 - LEFT
  332.                 else if (j == 0 && (i != 0 || i != N-1)){
  333.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  334.                     statesNear.add(values[i-1][0]);
  335.                     statesNear.add(values[i+1][0]);
  336.                     statesNear.add(values[i-1][1]);
  337.                     statesNear.add(values[i][1]);
  338.                     statesNear.add(values[i+1][1]);
  339.                     nextState = decide(state, statesNear);
  340.                     values2[i][j] = nextState;
  341.                 }
  342.  
  343.                 // Case 4 - RIGHT
  344.                 else if (j == N-1 && (i != 0 || i != N-1)){
  345.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  346.                     statesNear.add(values[i-1][N-1]);
  347.                     statesNear.add(values[i+1][N-1]);
  348.                     statesNear.add(values[i-1][N-2]);
  349.                     statesNear.add(values[i][N-2]);
  350.                     statesNear.add(values[i+1][N-2]);
  351.                     nextState = decide(state, statesNear);
  352.                     values2[i][j] = nextState;
  353.                 }
  354.  
  355.                 // Case 5 MIDDLE
  356.                 else{
  357.                     ArrayList<Integer> statesNear = new ArrayList<Integer>();
  358.                     statesNear.add(values[i-1][j-1]);
  359.                     statesNear.add(values[i-1][j]);
  360.                     statesNear.add(values[i-1][j+1]);
  361.                     statesNear.add(values[i][j-1]);
  362.                     statesNear.add(values[i][j+1]);
  363.                     statesNear.add(values[i+1][j-1]);
  364.                     statesNear.add(values[i+1][j]);
  365.                     statesNear.add(values[i+1][j+1]);
  366.                     nextState = decide(state, statesNear);
  367.                     values2[i][j] = nextState;
  368.                 }
  369.  
  370.             } // END OF FOR2
  371.  
  372.         }   // END OF FOR1
  373.         // Now, I have defined my next generation.
  374.         // The data are in 'values2' and not in 'values', so I have to copy them
  375.  
  376.         values = Arrays.stream(values2).map(int[]::clone).toArray(int[][]::new);
  377.         updateTiles();
  378.     }
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.     // *******************************************************************************
  388.     // *******************************************************************************
  389.     // Main Function
  390.     public static void main(String[] args){
  391.         Gui gui = new Gui();
  392.         gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
  393.         gui.setSize(800, 800);
  394.         gui.setVisible(true);
  395.         initialize();
  396.         // Until the previous line, we have predetermine the initial stage (generation 0)
  397.         // Both tiles and values have now the desired info
  398.         // From now and then, I will work with values array to determine next generations
  399.         showTable();
  400.         delay();
  401.  
  402.     }
  403. }
  404.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement