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
+45
View File
@@ -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);
}
}
}
}
}
+27
View File
@@ -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);
}
}
+107
View File
@@ -0,0 +1,107 @@
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){
}
}
+67
View File
@@ -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));
}
}
+36
View File
@@ -0,0 +1,36 @@
package v5.gui;
import v5.draw.Draw;
import v5.game.Control;
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);
}
}
+26
View File
@@ -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();
}
}