Initial Commit
This commit is contained in:
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/Conways-Game-of-Life.iml" filepath="$PROJECT_DIR$/Conways-Game-of-Life.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RunConfigurationProducerService">
|
||||||
|
<option name="ignoredProducers">
|
||||||
|
<set>
|
||||||
|
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
|
||||||
|
</set>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+124
@@ -0,0 +1,124 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -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
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package v3;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Control c = new Control();
|
||||||
|
c.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package v5.draw;
|
||||||
|
|
||||||
|
import v4.gui.GUI;
|
||||||
|
import v5.game.Control;
|
||||||
|
|
||||||
|
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, Control.CELLCOUNT_X + 1, Control.CELLCOUNT_Y + 1);
|
||||||
|
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
|
||||||
|
// for (int x = 0; x < Control.CELLCOUNT_X; x++) {
|
||||||
|
// for (int y = 0; y < Control.CELLCOUNT_Y; y++) {
|
||||||
|
// if (Control.cells[x][y]) {
|
||||||
|
// g.setColor(Color.BLACK);
|
||||||
|
// g.fillRect(x + GUI.XOFF, y + GUI.YOFF, 1, 1);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
for (int x = 0; x < Control.CELLCOUNT_X; x++) {
|
||||||
|
for (int y = 0; y < Control.CELLCOUNT_X; y++) {
|
||||||
|
if (Control.cells[x][y]) {
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.fillRect(x * 85 + GUI.XOFF, y * 85 + GUI.YOFF, 85, 85);
|
||||||
|
} else {
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.fillRect(x * 85 + GUI.XOFF, y * 85 + GUI.YOFF, 85, 85);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package v5.game;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class Clock extends Thread{
|
||||||
|
|
||||||
|
public static boolean running = true;
|
||||||
|
|
||||||
|
public void start(){
|
||||||
|
|
||||||
|
Timer t = new Timer();
|
||||||
|
TimerTask tt = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (running){
|
||||||
|
Control.nextGen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
t.scheduleAtFixedRate(tt, 1, 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package v5.game;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class Control {
|
||||||
|
|
||||||
|
public static final int CELLCOUNT_X = 6;
|
||||||
|
public static final int CELLCOUNT_Y = 6;
|
||||||
|
public static boolean[][] cells = new boolean[CELLCOUNT_X][CELLCOUNT_Y];
|
||||||
|
public static boolean[][] newcells = new boolean[CELLCOUNT_X][CELLCOUNT_Y];
|
||||||
|
|
||||||
|
int startCells = 6;
|
||||||
|
static int gen = 0;
|
||||||
|
|
||||||
|
|
||||||
|
public void create(){
|
||||||
|
// for(int i = 0; i < startCells; i++){
|
||||||
|
// int x = rand(0, CELLCOUNT_X);
|
||||||
|
// int y = rand(0, CELLCOUNT_Y);
|
||||||
|
//
|
||||||
|
// cells[x][y] = true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
for (int x = 0; x < 6; x++) {
|
||||||
|
for (int y = 0; y < 6; y++) {
|
||||||
|
cells[x][y] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cells[1][2] = true;
|
||||||
|
cells[2][2] = true;
|
||||||
|
cells[2][4] = true;
|
||||||
|
cells[3][1] = true;
|
||||||
|
cells[3][3] = true;
|
||||||
|
cells[4][3] = true;
|
||||||
|
|
||||||
|
|
||||||
|
for (int x = 0; x < CELLCOUNT_X; x++) {
|
||||||
|
for (int y = 0; y < CELLCOUNT_Y; y++) {
|
||||||
|
newcells[x][y] = cells[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void nextGen(){
|
||||||
|
gen ++;
|
||||||
|
System.out.println("Generation:" + gen);
|
||||||
|
|
||||||
|
for (int x = 0; x < CELLCOUNT_X; x++){
|
||||||
|
for (int y = 0; y < CELLCOUNT_Y; y++) {
|
||||||
|
int n = aliveNeigbours(x, y);
|
||||||
|
|
||||||
|
if (n == 3 && !cells[x][y]){
|
||||||
|
newcells[x][y] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n < 2){
|
||||||
|
newcells[x][y] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 2 || n == 3){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n > 3){
|
||||||
|
newcells[x][y] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int x = 0; x < CELLCOUNT_X; x++) {
|
||||||
|
for (int y = 0; y < CELLCOUNT_Y; y++) {
|
||||||
|
cells[x][y] = newcells[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calcDimension(int cellsX, int cellsY){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package v5.gui;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.event.ChangeEvent;
|
||||||
|
import javax.swing.event.ChangeListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class ControlGUI {
|
||||||
|
|
||||||
|
public static final int WIDTH = 512, HEIGHT = 512, XOFF = 10, YOFF = 10;
|
||||||
|
private JFrame jf;
|
||||||
|
|
||||||
|
private JSlider gridSize;
|
||||||
|
private JLabel gridSizeLabel;
|
||||||
|
private JSlider startCells;
|
||||||
|
|
||||||
|
private JButton start;
|
||||||
|
|
||||||
|
public void create() {
|
||||||
|
jf = new JFrame("Game of Live");
|
||||||
|
jf.setSize(305, 500);
|
||||||
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
jf.setLayout(null);
|
||||||
|
jf.setLocationRelativeTo(null);
|
||||||
|
jf.setResizable(false);
|
||||||
|
|
||||||
|
gridSize = new JSlider(6, 512);
|
||||||
|
gridSize.setBounds(50, 50, 150, 30);
|
||||||
|
gridSize.setValue(6);
|
||||||
|
gridSize.addChangeListener(new ChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void stateChanged(ChangeEvent e) {
|
||||||
|
int value = gridSize.getValue();
|
||||||
|
update(value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
gridSizeLabel = new JLabel("6");
|
||||||
|
gridSizeLabel.setBounds(50, 30, 30, 30);
|
||||||
|
|
||||||
|
startCells = new JSlider();
|
||||||
|
startCells.setBounds(50, 100, 150, 30);
|
||||||
|
|
||||||
|
start = new JButton("start");
|
||||||
|
start.setBounds(0, 0, 50, 10);
|
||||||
|
start.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
jf.add(start);
|
||||||
|
jf.add(gridSize);
|
||||||
|
jf.add(startCells);
|
||||||
|
jf.add(gridSizeLabel);
|
||||||
|
jf.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void update(int val){
|
||||||
|
gridSizeLabel.setText(String.valueOf(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package v5.gui;
|
||||||
|
|
||||||
|
import v5.draw.Draw;
|
||||||
|
import v5.game.Control;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
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.getContentPane().setPreferredSize(new Dimension(Control.CELLCOUNT_X + 19, Control.CELLCOUNT_Y + 19));
|
||||||
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
jf.setResizable(true);
|
||||||
|
|
||||||
|
d = new Draw();
|
||||||
|
d.setVisible(true);
|
||||||
|
|
||||||
|
jf.add(d);
|
||||||
|
|
||||||
|
jf.pack();
|
||||||
|
jf.setLocationRelativeTo(null);
|
||||||
|
jf.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package v5.main;
|
||||||
|
|
||||||
|
import v5.game.Clock;
|
||||||
|
import v5.game.Control;
|
||||||
|
import v5.gui.ControlGUI;
|
||||||
|
import v5.gui.GUI;
|
||||||
|
import v5.draw.Draw;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// ControlGUI controlGUI = new ControlGUI();
|
||||||
|
// controlGUI.create();
|
||||||
|
|
||||||
|
Control control = new Control();
|
||||||
|
GUI gui = new GUI();
|
||||||
|
Clock clock = new Clock();
|
||||||
|
|
||||||
|
control.create();
|
||||||
|
gui.create();
|
||||||
|
clock.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user