Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Noah
2021-12-01 09:47:13 +01:00
11 changed files with 1529 additions and 23 deletions
+1
View File
@@ -7,5 +7,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="h2-1.4.200" level="project" />
</component>
</module>
Binary file not shown.
File diff suppressed because it is too large Load Diff
Binary file not shown.
+124
View File
@@ -0,0 +1,124 @@
package data.database;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DataBase {
private Connection connection;
private Statement statement;
public void connect(){
try {
connection = DriverManager.getConnection("jdbc:h2:./lib/Conway-Game-Of-Live", "sa", "");
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void disconnect(){
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void simpleSQL(String sqlStatement){
try {
statement.executeUpdate(sqlStatement);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet resultSetSQL(String sqlStatement){
try {
return statement.executeQuery(sqlStatement);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void createTables(){
connect();
simpleSQL("create table gameInfo(id int not null auto_increment, name varchar, width int, height int, generation int)");
simpleSQL("create table gameData(id int, x int, y int)");
disconnect();
}
public void deleteTables(){
connect();
simpleSQL("drop table gameInfo");
simpleSQL("drop table gameData");
disconnect();
}
public List<String> getAllGrids(){
List<String> allGrids = new ArrayList<>();
connect();
ResultSet resultSet = resultSetSQL("select name from gameInfo");
while (true){
try {
if (!resultSet.next()) break;
} catch (SQLException e) {
e.printStackTrace();
}
try {
allGrids.add(resultSet.getString(1));
} catch (SQLException e) {
e.printStackTrace();
}
}
disconnect();
return allGrids;
}
public void saveGrid(boolean[][] grid, String gridName, int generation){
connect();
simpleSQL("insert into GAMEINFO(name, width, height, GENERATION) values ('"+gridName+"', "+grid.length+", "+grid[0].length+", "+generation+")");
ResultSet resultSet = resultSetSQL("select * from gameInfo order by id desc limit 1");
try {
resultSet.next();
int id = resultSet.getInt("id");
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[0].length; y++) {
if (grid[x][y]){
simpleSQL("insert into gameData (id, x, y) values ("+id+", "+x+", "+y+")");
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
disconnect();
}
}
+98 -11
View File
@@ -1,16 +1,23 @@
package game;
import data.database.DataBase;
import gui.GUI;
import java.sql.ResultSet;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import static java.lang.Math.round;
public class Control {
private GUI gui;
private Clock clock;
private DataBase dataBase;
private int xSize, ySize, cellCount, gen;
private boolean[][] startingCells;
private boolean[][] cells;
private boolean[][] newcells;
@@ -25,6 +32,7 @@ public class Control {
clock = new Clock(this, gui);
gui = new GUI(this);
dataBase = new DataBase();
gui.createMenuWindow();
}
@@ -45,6 +53,7 @@ public class Control {
cells = new boolean[this.xSize][this.ySize];
newcells = new boolean[this.xSize][this.ySize];
startingCells = new boolean[this.xSize][this.ySize];
switch (startMode) {
@@ -58,9 +67,9 @@ public class Control {
case Manuel -> {
buildManuelGame();
}
}
saveFirstGrid();
calcSize();
@@ -84,6 +93,8 @@ public class Control {
}
}
syncCells();
}
private void generateTask1() {
@@ -94,16 +105,12 @@ public class Control {
}
// cells[1][2] = true;
// cells[2][2] = true;
// cells[2][4] = true;
// cells[3][1] = true;
// cells[3][3] = true;
// cells[4][3] = true;
cells[1][0] = true;
cells[1][1] = true;
cells[1][2] = true;
cells[2][2] = true;
cells[2][4] = true;
cells[3][1] = true;
cells[3][3] = true;
cells[4][3] = true;
syncCells();
@@ -131,6 +138,22 @@ public class Control {
}
}
public void saveFirstGrid(){
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
startingCells[x][y] = cells[x][y];
}
}
}
private void resetCells(){
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
cells[x][y] = startingCells[x][y];
}
}
}
public boolean[][] getCells() {
return cells;
}
@@ -138,7 +161,6 @@ public class Control {
public void nextGen() {
gen++;
System.out.println("Generation:" + gen);
for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) {
@@ -166,6 +188,45 @@ public class Control {
gui.updateGameWindowTitle(gen);
}
public void nextGenLight() {
for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) {
int n = aliveNeigbours(x, y);
switch (n) {
case 3:
newcells[x][y] = true;
break;
case 2:
break;
default:
newcells[x][y] = false;
break;
}
}
}
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
cells[x][y] = newcells[x][y];
}
}
}
public void previousGen(){
if(gen > 0) {
resetCells();
syncCells();
for (int i = 0; i < gen - 1; i++) {
nextGenLight();
}
gen--;
gui.updateGameWindowTitle(gen);
}
}
// public void checkN(Boolean[][] arr, Boolean[][] futureGen, int columns, int rows) {
// ArrayList<Thread> threads = new ArrayList<>();
// final int NUM_OF_THREADS = 8; //Or can be passed as an argument
@@ -360,6 +421,32 @@ public class Control {
public int getGeneration(){
return gen;
}
public void jump2Gen(int wantedtGen){
if (wantedtGen < gen){
resetCells();
syncCells();
int counter = ((gen - wantedtGen) / wantedtGen ) + 1;
for (int i = 0; i < wantedtGen; i++) {
nextGenLight();
gen = gen - counter;
gui.updateGameWindowTitle(gen);
}
gen = wantedtGen;
gui.updateGameWindowTitle(gen);
} else {
multipleGenSkip(wantedtGen - gen);
}
}
public List<String> getAllGrids(){
return dataBase.getAllGrids();
}
}
+80 -7
View File
@@ -20,10 +20,13 @@ public class ControlWindow {
private JLabel heading;
private JButton toggleGame;
private JButton nextGenButton;
private JButton previousGenButton;
private JButton lockMouse;
private JButton clearButton;
private JButton multipleGenSkipButton;
private JButton jump2GenButton;
private JTextField multipleGenSkipLable;
private JTextField jump2GenLable;
private int velocity, minVelocity = 1 , maxVelocity = 5000;
@@ -52,7 +55,7 @@ public class ControlWindow {
controlWindow.setLocation(gameWindow.getPos().x - (width + offset), gameWindow.getPos().y);
controlWindow.setLayout(null);
ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png");
ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
controlWindow.setIconImage(imageIcon.getImage());
controlWindow.addWindowListener(new WindowAdapter() {
@@ -79,9 +82,11 @@ public class ControlWindow {
initVelocity();
initToggleButton();
initNextButton();
initPreviousGenButton();
initDrawLockButton();
initClearButton();
initMultibleGenSkip();
initJump2Gen();
}
private void initHeading(){
@@ -176,6 +181,9 @@ public class ControlWindow {
toggleGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (gui.getGeneration() == 0){
gui.saveFirstGrid();
}
gui.setRunning(!gui.isRunning());
updateToggleButton();
gui.setVelocity(velocity);
@@ -187,25 +195,49 @@ public class ControlWindow {
private void initNextButton(){
nextGenButton = new JButton();
nextGenButton.setText("Next Generation");
nextGenButton.setText("Next Gen.");
nextGenButton.setFont(text);
nextGenButton.setForeground(Color.decode("#121212"));
nextGenButton.setBorderPainted(true);
nextGenButton.setFocusPainted(false);
nextGenButton.setContentAreaFilled(false);
nextGenButton.setBounds(50, 675, 300, 50);
nextGenButton.setBounds(225, 675, 125, 50);
nextGenButton.setVisible(true);
nextGenButton.addMouseListener(hover);
nextGenButton.setBorder(new RoundedBorder(25));
nextGenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (gui.getGeneration() == 0){
gui.saveFirstGrid();
}
gui.nextGen();
gui.showGrid(gui.getCells());
}
});
}
private void initPreviousGenButton(){
previousGenButton = new JButton();
previousGenButton.setText("Prior Gen.");
previousGenButton.setFont(text);
previousGenButton.setForeground(Color.decode("#121212"));
previousGenButton.setBorderPainted(true);
previousGenButton.setFocusPainted(false);
previousGenButton.setContentAreaFilled(false);
previousGenButton.setBounds(50, 675, 125, 50);
previousGenButton.setVisible(true);
previousGenButton.addMouseListener(hover);
previousGenButton.setBorder(new RoundedBorder(25));
previousGenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gui.previousGen();
gui.showGrid(gui.getCells());
}
});
}
private void initDrawLockButton(){
lockMouse = new JButton();
lockMouse.setFont(text);
@@ -213,7 +245,7 @@ public class ControlWindow {
lockMouse.setBorderPainted(true);
lockMouse.setFocusPainted(false);
lockMouse.setContentAreaFilled(false);
lockMouse.setBounds(50, 600, 300, 50);
lockMouse.setBounds(50, 450, 300, 50);
lockMouse.setVisible(true);
lockMouse.addMouseListener(hover);
lockMouse.setBorder(new RoundedBorder(25));
@@ -235,7 +267,7 @@ public class ControlWindow {
clearButton.setBorderPainted(true);
clearButton.setFocusPainted(false);
clearButton.setContentAreaFilled(false);
clearButton.setBounds(50, 525, 300, 50);
clearButton.setBounds(50, 375, 300, 50);
clearButton.setVisible(true);
clearButton.addMouseListener(hover);
clearButton.setBorder(new RoundedBorder(25));
@@ -257,7 +289,7 @@ public class ControlWindow {
multipleGenSkipButton.setBorderPainted(true);
multipleGenSkipButton.setFocusPainted(false);
multipleGenSkipButton.setContentAreaFilled(false);
multipleGenSkipButton.setBounds(50, 450, 300, 50);
multipleGenSkipButton.setBounds(50, 600, 300, 50);
multipleGenSkipButton.setVisible(true);
multipleGenSkipButton.addMouseListener(hover);
multipleGenSkipButton.setBorder(new RoundedBorder(25));
@@ -273,7 +305,7 @@ public class ControlWindow {
multipleGenSkipLable.setOpaque(false);
multipleGenSkipLable.setBackground(null);
multipleGenSkipLable.setBorder(null);
multipleGenSkipLable.setBounds(160, 450, 50, 50);
multipleGenSkipLable.setBounds(160, 600, 50, 50);
multipleGenSkipLable.setFont(text);
multipleGenSkipLable.setForeground(Color.decode("#121212"));
multipleGenSkipLable.setVisible(true);
@@ -286,6 +318,44 @@ public class ControlWindow {
});
}
private void initJump2Gen(){
jump2GenButton = new JButton();
jump2GenButton.setText("Jump to generation");
jump2GenButton.setFont(text);
jump2GenButton.setForeground(Color.decode("#121212"));
jump2GenButton.setBorderPainted(true);
jump2GenButton.setFocusPainted(false);
jump2GenButton.setContentAreaFilled(false);
jump2GenButton.setBounds(50, 525, 300, 50);
jump2GenButton.setVisible(true);
jump2GenButton.addMouseListener(hover);
jump2GenButton.setBorder(new RoundedBorder(25));
jump2GenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gui.jump2Gen(Integer.parseInt(jump2GenLable.getText()));
gui.showGrid(gui.getCells());
}
});
jump2GenLable = new JTextField("6");
jump2GenLable.setOpaque(false);
jump2GenLable.setBackground(null);
jump2GenLable.setBorder(null);
jump2GenLable.setBounds(275, 525, 50, 50);
jump2GenLable.setFont(text);
jump2GenLable.setForeground(Color.decode("#121212"));
jump2GenLable.setVisible(true);
jump2GenLable.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
if (!Character.isDigit(evt.getKeyChar())) {
evt.consume();
}
}
});
}
private void initSaveButton(){}
private void initSaveNameField(){}
@@ -297,10 +367,13 @@ public class ControlWindow {
controlWindow.add(velocitySliderLabel);
controlWindow.add(toggleGame);
controlWindow.add(nextGenButton);
controlWindow.add(previousGenButton);
controlWindow.add(lockMouse);
controlWindow.add(clearButton);
controlWindow.add(multipleGenSkipLable);
controlWindow.add(jump2GenLable);
controlWindow.add(multipleGenSkipButton);
controlWindow.add(jump2GenButton);
}
public void setVisibility(boolean value) {
+18
View File
@@ -7,6 +7,8 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.util.List;
public class GUI {
@@ -155,6 +157,18 @@ public class GUI {
return control.getVelocity();
}
public void saveFirstGrid(){
control.saveFirstGrid();
}
public void previousGen(){
control.previousGen();
}
public void jump2Gen(int wantedtGen){
control.jump2Gen(wantedtGen);
}
//TODO: Delet this Methods. Those are shit.
public void setVisibility(boolean value){
controlWindow.setVisibility(value);
@@ -164,5 +178,9 @@ public class GUI {
return controlWindow;
}
public List<String> getAllGrids(){
return control.getAllGrids();
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ public class GameWindow {
gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset)));
gameWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png");
ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
gameWindow.setIconImage(imageIcon.getImage());
gameWindow.addWindowListener(new WindowAdapter() {
+6 -2
View File
@@ -57,7 +57,7 @@ public class MenuWindow {
menuWindow.setTitle(title);
ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png");
ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
menuWindow.setIconImage(imageIcon.getImage());
@@ -306,6 +306,10 @@ public class MenuWindow {
addComboBoxEntry(modeBox, "Blinker");
addComboBoxEntry(modeBox, "Toad");
for (int i = 0; i < gui.getAllGrids().size(); i++){
addComboBoxEntry(modeBox, gui.getAllGrids().get(i));
}
modeName = new JLabel("Select Figure: ");
modeName.setBounds(50, 250, 200, 30);
modeName.setFont(text);
@@ -362,7 +366,7 @@ public class MenuWindow {
}
private void initIMG(){
preview = new ImageIcon("src/files/PNG/Preview.png");
preview = new ImageIcon("src/data/pictures/Preview.png");
previewLabel = new JLabel(preview);
previewLabel.setBounds(450, 45, 203, 302);
}
+24 -2
View File
@@ -1,6 +1,7 @@
package main;
import data.database.DataBase;
import game.Control;
public class Main {
@@ -12,8 +13,29 @@ public class Main {
public static void main(String[] args) {
Control control = new Control();
control.start();
// Control control = new Control();
// control.start();
DataBase dataBase = new DataBase();
dataBase.deleteTables();
dataBase.createTables();
// boolean[][] cells = new boolean[44][66];
//
// for (int x = 0; x < 6; x++) {
// for (int y = 0; y < 6; y++) {
// cells[x][y] = false;
// }
// }
//
//
// cells[3][3] = true;
//
// dataBase.saveGrid(cells, "whoa", 88);
//
// System.out.println(dataBase.getAllGrids());
}