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
+113
View File
@@ -0,0 +1,113 @@
package v3;
public class Control {
private boolean[][] generation = new boolean[6][6];
private GUI g = new GUI(this);
public Control() {
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 6; y++) {
generation[x][y] = false;
}
}
generation[1][2] = true;
generation[2][2] = true;
generation[2][4] = true;
generation[3][1] = true;
generation[3][3] = true;
generation[4][3] = true;
}
public void start() {
g.showGeneration(bool2int(generation));
}
public void update(){
generation = calcNextGeneration();
g.showGeneration(bool2int(generation));
}
private int[][] bool2int(boolean[][] booleans) {
int[][] ret = new int[6][6];
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 6; y++) {
if (booleans[x][y]){
ret[x][y] = 1;
} else {
ret[x][y] = 0;
}
}
}
return ret;
}
private int countNeighboursAlive(int x, int y) {
int count = 0;
//Anfang mitte rechts, Uhrzeigersinn
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 (generation[x + xoff[i]][y + yoff[i]]) count++;
} catch (Exception e) {
}
}
System.out.println(count);
return count;
}
public boolean[][] calcNextGeneration() {
boolean[][] gen = new boolean[6][6];
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 6; y++) {
int n = countNeighboursAlive(x, y);
//Regel 1: Wiederbeleben
if (n == 3 && !gen[x][y]) {
gen[x][y] = true;
}
//Regel 2: Unterbevölkerung
if (n < 2) {
gen[x][y] = false;
}
//Regel 3: Am Leben bleiben
if (n == 2 || n == 3) {
//Zelle bleibt unverändert
}
//Regel 4: Überbevölkerung
if (n > 3) {
gen[x][y] = false;
}
}
}
return gen;
}
public void newData(boolean[][] data) {
generation = data;
}
}
+109
View File
@@ -0,0 +1,109 @@
package v3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
import v3.Control;
public class GUI extends JFrame {
private JButton[][] buttons = new JButton[6][6];
private JButton nextGen = new JButton();
public Control c;
public GUI(Control c){
this.c = c;
setTitle("Test");
setUndecorated(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setAutoRequestFocus(false);
setLayout(null);
setSize(500, 500);
setResizable(true);
initComponents();
addComponents();
setLocationRelativeTo(null);
setVisible(true);
}
private void initComponents() {
nextGen.setBounds(0, 0, 100, 50);
nextGen.setText("Next");
nextGen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.update();
}
});
for (int i = 0; i < 6; i++) {
for (int y = 0; y < 6; y++) {
buttons[i][y] = new JButton();
buttons[i][y].setBounds(i * 50 + 50, y * 50 + 50, 50, 50);
int finalI = i;
int finalY = y;
buttons[i][y].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (Objects.equals(buttons[finalI][finalY].getText(), "0")){
buttons[finalI][finalY].setText("1");
buttons[finalI][finalY].setBackground(Color.green);
} else {
buttons[finalI][finalY].setText("0");
buttons[finalI][finalY].setBackground(Color.yellow);
}
boolean[][] data = new boolean[6][6];
for (int i = 0; i < 6; i++) {
for (int y = 0; y < 6; y++) {
if (Objects.equals(buttons[i][y].getText(), "1")){
data[i][y] = true;
} else {
data[i][y] = false;
}
}
}
c.newData(data);
}
});
}
}
}
private void addComponents() {
add(nextGen);
for (int i = 0; i < 6; i++) {
for (int y = 0; y < 6; y++) {
add(buttons[i][y]);
}
}
}
public void showGeneration(int[][] pGeneration) {
for (int i = 0; i < 6; i++) {
for (int y = 0; y < 6; y++) {
buttons[i][y].setText(String.valueOf(pGeneration[y][i]));
if (Objects.equals(buttons[i][y].getText(), "0")){
buttons[i][y].setBackground(Color.green);
} else {
buttons[i][y].setBackground(Color.yellow);
}
}
}
}
}
+10
View File
@@ -0,0 +1,10 @@
package v3;
public class Main {
public static void main(String[] args) {
Control c = new Control();
c.start();
}
}