diff --git a/src/game/Control.java b/src/game/Control.java index 48f9d13..0197c25 100644 --- a/src/game/Control.java +++ b/src/game/Control.java @@ -4,6 +4,8 @@ import gui.GUI; import java.util.concurrent.ThreadLocalRandom; +import static java.lang.Math.round; + public class Control { private GUI gui; @@ -11,6 +13,7 @@ public class Control { private int xSize, ySize, cellCount, gen; + private boolean[][] startingCells; private boolean[][] cells; private boolean[][] newcells; @@ -45,6 +48,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 +62,9 @@ public class Control { case Manuel -> { buildManuelGame(); } - } + saveFirstGrid(); calcSize(); @@ -84,6 +88,8 @@ public class Control { } } + syncCells(); + } private void generateTask1() { @@ -94,16 +100,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 +133,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 +156,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 +183,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 threads = new ArrayList<>(); // final int NUM_OF_THREADS = 8; //Or can be passed as an argument @@ -360,6 +416,24 @@ public class Control { public int getGeneration(){ return gen; } + + public void jump2Gen(int wantedtGen){ + if (wantedtGen < gen){ + resetCells(); + syncCells(); + + for (int i = 0; i < wantedtGen; i++) { + nextGenLight(); + + } + + gen = wantedtGen; + gui.updateGameWindowTitle(gen); + + } else { + multipleGenSkip(wantedtGen - gen); + } + } } diff --git a/src/gui/ControlWindow.java b/src/gui/ControlWindow.java index ac51ecf..3b3bc03 100644 --- a/src/gui/ControlWindow.java +++ b/src/gui/ControlWindow.java @@ -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) { diff --git a/src/gui/GUI.java b/src/gui/GUI.java index a26677d..605a67d 100644 --- a/src/gui/GUI.java +++ b/src/gui/GUI.java @@ -155,6 +155,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); diff --git a/src/gui/GameWindow.java b/src/gui/GameWindow.java index 2272163..f7527f2 100644 --- a/src/gui/GameWindow.java +++ b/src/gui/GameWindow.java @@ -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() { diff --git a/src/gui/MenuWindow.java b/src/gui/MenuWindow.java index 3e401e7..a2dc07d 100644 --- a/src/gui/MenuWindow.java +++ b/src/gui/MenuWindow.java @@ -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()); @@ -362,7 +362,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); }