From f41150106c23d0e4de8f57a319d5aa23ff3b4201 Mon Sep 17 00:00:00 2001
From: steyer <steyer10@gmail.com>
Date: Wed, 27 Nov 2019 12:18:52 +0100
Subject: [PATCH] =?UTF-8?q?=C3=9Aj=20GameRunner=20(m=C5=B1k=C3=B6dik)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 manualtest/game/JTableTest.java         |  3 ++
 manualtest/game/StopButtonListener.java | 17 ++++++++
 src/game/GameModel.java                 | 15 ++++++-
 src/game/GameRunner2.java               | 58 +++++++++++++++++++++++++
 4 files changed, 91 insertions(+), 2 deletions(-)
 create mode 100644 manualtest/game/StopButtonListener.java
 create mode 100644 src/game/GameRunner2.java

diff --git a/manualtest/game/JTableTest.java b/manualtest/game/JTableTest.java
index f6da19c..5217025 100644
--- a/manualtest/game/JTableTest.java
+++ b/manualtest/game/JTableTest.java
@@ -16,11 +16,14 @@ public class JTableTest {
 		testFrame.setSize(300, 300);
 		JButton stepButton = new JButton("Step");
 		JButton startButton = new JButton("Start");
+		JButton stopButton = new JButton("Stop");
 		startButton.addActionListener(new StartButtonListener(testModel));
 		stepButton.addActionListener(new StepButtonListener(testModel));
+		stopButton.addActionListener(new StopButtonListener(testModel));
 		JPanel bottomPanel = new JPanel();
 		bottomPanel.add(stepButton);
 		bottomPanel.add(startButton);
+		bottomPanel.add(stopButton);
 		testFrame.add(bottomPanel, BorderLayout.PAGE_END);
 		testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		testFrame.setVisible(true);
diff --git a/manualtest/game/StopButtonListener.java b/manualtest/game/StopButtonListener.java
new file mode 100644
index 0000000..a90ccbc
--- /dev/null
+++ b/manualtest/game/StopButtonListener.java
@@ -0,0 +1,17 @@
+package game;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+public class StopButtonListener implements ActionListener {
+
+    private GameModel model;
+
+    public StopButtonListener(GameModel model) {
+        this.model = model;
+    }
+
+    public void actionPerformed(ActionEvent e) {
+        model.stop();
+    }
+}
diff --git a/src/game/GameModel.java b/src/game/GameModel.java
index c1ca890..d64720b 100644
--- a/src/game/GameModel.java
+++ b/src/game/GameModel.java
@@ -6,11 +6,14 @@ public class GameModel extends AbstractTableModel {
 
     private GameBoard cells;
     private GameRunner runner;
+    private GameRunner2 runner2; //TODO runners
     private boolean editable;
 
     public GameModel(int height, int width) {
         cells = new GameBoard(height, width);
         runner = new GameRunner(this, cells);
+        runner2 = new GameRunner2(this, cells);
+        runner2.start();
         editable = true;
     }
 
@@ -32,10 +35,13 @@ public class GameModel extends AbstractTableModel {
 
     public void start() {
         editable=false;
-        runner.start();
+        //runner.start();
+        //TODO runners
+        runner2.resumeRunner();
     }
 
     public void stop(){
+        /*
         runner.stopRunner();
         while (runner.isAlive()){
             try {
@@ -44,7 +50,12 @@ public class GameModel extends AbstractTableModel {
                 e.printStackTrace();
             }
         }
-        editable = true;
+         */
+        //TODO runners
+        runner2.pauseRunner();
+        synchronized (cells) {
+            editable = true;
+        }
     }
 
     public boolean isCellEditable(int rowIndex, int columnIndex) {
diff --git a/src/game/GameRunner2.java b/src/game/GameRunner2.java
new file mode 100644
index 0000000..a646ac0
--- /dev/null
+++ b/src/game/GameRunner2.java
@@ -0,0 +1,58 @@
+package game;
+
+public class GameRunner2 extends Thread {
+
+    private final GameModel model;
+    private final GameBoard board;
+    private volatile boolean paused;
+
+    public GameRunner2(GameBoard board) {
+        this.board = board;
+        model = null;
+        paused = true;
+    }
+
+    public GameRunner2(GameModel model, GameBoard board) {
+        this.model = model;
+        this.board = board;
+        paused = true;
+    }
+
+    public void run() {
+        synchronized (board) {
+            while (true) {
+                while (paused) {
+                    try {
+                        board.wait();
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+
+                while (!paused) {
+                    board.step();
+                    if (model != null)
+                        model.fireTableDataChanged();
+                    try {
+                        sleep(500);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+    }
+
+    public void pauseRunner() {
+        paused = true;
+    }
+
+    public void resumeRunner() {
+        if (paused) {
+            synchronized (board) {
+                paused = false;
+                board.notify();
+            }
+        }
+    }
+}
-- 
GitLab