diff --git a/src/backend/Grid.java b/src/backend/Grid.java new file mode 100644 index 0000000..1393270 --- /dev/null +++ b/src/backend/Grid.java @@ -0,0 +1,29 @@ +package backend; + +public class Grid { + private int width; + private int height; + private int[][] grid; + + public Grid(int width, int height) { + this.width = width; + this.height = height; + this.grid = new int[height][width]; + } + + public void setValue(int x, int y, int value) { + if (x >= 0 && x < width && y >= 0 && y < height) { + grid[y][x] = value; + } else { + throw new IndexOutOfBoundsException("Grid position out of range"); + } + } + + public int getValue(int x, int y) { + if (x >= 0 && x < width && y >= 0 && y < height) { + return grid[y][x]; + } else { + throw new IndexOutOfBoundsException("Grid position out of range"); + } + } +} \ No newline at end of file diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index dc1efa3..73c3c66 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -22,8 +22,9 @@ public class Simulator extends Thread { private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; - private boolean clickActionFlag; + private boolean clickActionFlag=false; private int loopDelay = 150; + Grid maingrid = new Grid(COL_NUM, LINE_NUM); //TODO : add missing attribute(s) @@ -57,7 +58,7 @@ public class Simulator extends Thread { return LINE_NUM; } - //Should probably stay as is + //Should probably stay as it is public void run() { int stepCount=0; while(!stopFlag) { @@ -142,6 +143,13 @@ public class Simulator extends Thread { */ public void clickCell(int x, int y) { //TODO : complete method + if(clickActionFlag) { + if (getCell(x, y)==0) { + setCell(x, y,1); + }else { + setCell(x, y,0); + } + } } /** @@ -151,8 +159,8 @@ public class Simulator extends Thread { * @return value of cell */ public int getCell(int x, int y) { - //TODO : complete method with proper return - return 0; + //TODO : complete method with proper retur + return maingrid.getValue(x, y); } /** * @@ -187,7 +195,8 @@ public class Simulator extends Thread { */ public void setCell(int x, int y, int val) { //TODO : complete method - } + maingrid.setValue(x, y, val); + } /** * @@ -265,6 +274,11 @@ public class Simulator extends Thread { public void toggleClickAction() { //TODO : complete method + if(clickActionFlag) { + clickActionFlag=false; + }else { + clickActionFlag=true; + } } /** @@ -327,4 +341,4 @@ public class Simulator extends Thread { return ""; } -} \ No newline at end of file +} diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index cf2b1ef..69dabf4 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -92,13 +92,13 @@ public class MyInterface extends JFrame { }); panelTop.add(speedSlider); -// JButton btnSpeed = new JButton("Set Speed"); -// btnSpeed.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// clicButtonSpeed(); -// } -// }); -// panelTop.add(btnSpeed); + /*JButton btnSpeed = new JButton("Set Speed"); + btnSpeed.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + clicButtonSpeed(); + } + }); + panelTop.add(btnSpeed);*/ JButton btnLoad = new JButton("Load World"); btnLoad.addActionListener(new ActionListener() {