Advertisement
makispaiktis

Conway's Game Of Life - V3 - No while (10 ms)

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