Backup added

This commit is contained in:
Noah
2021-09-28 22:10:02 +02:00
parent 9d2c7c9891
commit c3e19db3d3
15 changed files with 726 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package v4.draw;
import v4.game.Control;
import v4.gui.GUI;
import javax.swing.*;
import java.awt.*;
public class Draw extends JLabel {
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setColor(Color.RED);
g.drawRect(9 ,9, GUI.WIDTH + 2, GUI.HEIGHT + 2);
repaint();
for (int x = 0; x < Control.CELLCOUNT; x++){
for (int y = 0; y < Control.CELLCOUNT; y++){
if (Control.cells[x][y]){
g.setColor(Color.BLACK);
g.drawRect(x +GUI.XOFF, y + GUI.YOFF, 1, 1);
} else {
g.setColor(Color.WHITE);
g.drawRect(x +GUI.XOFF, y + GUI.YOFF, 1, 1);
}
}
}
}
}
+21
View File
@@ -0,0 +1,21 @@
package v4.game;
public class Clock extends Thread {
public static boolean running = true;
public void run(){
while (running){
try {
sleep(50);
v4.game.Control.nextGen();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
+73
View File
@@ -0,0 +1,73 @@
package v4.game;
import java.util.concurrent.ThreadLocalRandom;
public class Control {
public static final int CELLCOUNT = 512;
public static boolean[][] cells = new boolean[CELLCOUNT][CELLCOUNT];
int startCells = 10000;
static int gen = 0;
public void create(){
for(int i = 0; i < startCells; i++){
int x = rand(0, CELLCOUNT);
int y = rand(0, CELLCOUNT);
cells[x][y] = true;
}
}
public static void nextGen(){
gen ++;
System.out.println("Generation:" + gen);
for (int x = 0; x < CELLCOUNT; x++){
for (int y = 0; y < CELLCOUNT; y++) {
int n = aliveNeigbours(x, y);
if (n == 3 && !cells[x][y]){
cells[x][y] = true;
}
if (n < 2){
cells[x][y] = false;
}
if (n == 2 || n == 3){
}
if (n > 3){
cells[x][y] = false;
}
}
}
}
public static int aliveNeigbours(int x, int y){
int count = 0;
int[] xoff = {1, 1, 0, -1, -1, -1, 0, 1};
int[] yoff = {0, 1, 1, 1, 0, -1, -1, -1};
for (int i = 0; i < 8; i++){
try {
if (cells[x + xoff[i]][y + yoff[i]]){
count ++;
}
} catch (Exception e) {
}
}
return count;
}
public static int rand(int min, int max){
return ThreadLocalRandom.current().nextInt(min, max);
}
}
+31
View File
@@ -0,0 +1,31 @@
package v4.gui;
import v4.draw.Draw;
import javax.swing.*;
public class GUI {
JFrame jf;
public static Draw d;
public static final int WIDTH = 512, HEIGHT = 512, XOFF = 10, YOFF = 10;
public void create(){
jf = new JFrame("Game of Live");
jf.setSize(550, 570);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setResizable(false);
d = new Draw();
d.setBounds(0, 0, 550, 570);
d.setVisible(true);
jf.add(d);
jf.setVisible(true);
}
}
+20
View File
@@ -0,0 +1,20 @@
package v4.main;
import v4.game.Clock;
import v4.game.Control;
import v4.gui.GUI;
public class Main {
public static void main(String[] args) {
GUI g = new GUI();
Control c = new Control();
Clock clk = new Clock();
c.create();
g.create();
clk.start();
}
}