From 7557f76342a09049d28557f602796e0568683d60 Mon Sep 17 00:00:00 2001
From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr>
Date: Sat, 30 Apr 2016 16:20:11 +0200
Subject: [PATCH] MCTS is now properly closed when the GUI exits

---
 gui/src/controller/Controller.java   |  7 +++++++
 gui/src/controller/UpdateThread.java | 15 ++++++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java
index 42bba4d..501b215 100644
--- a/gui/src/controller/Controller.java
+++ b/gui/src/controller/Controller.java
@@ -50,6 +50,13 @@ public class Controller implements Initializable
 		{
 			this.gameProcess = new ProcessBuilder("../bin/theturk").start();
 			gameInput = new PrintWriter(new OutputStreamWriter(gameProcess.getOutputStream()), true);
+			Runtime.getRuntime().addShutdownHook(new Thread()
+			{
+				public void run()
+				{
+					gameProcess.destroy();
+				}
+			});
 		}
 		catch (IOException e)
 		{
diff --git a/gui/src/controller/UpdateThread.java b/gui/src/controller/UpdateThread.java
index 0d70aa3..fb2d517 100644
--- a/gui/src/controller/UpdateThread.java
+++ b/gui/src/controller/UpdateThread.java
@@ -26,7 +26,7 @@ public class UpdateThread extends Thread
 		this.board = board;
 		this.boardView = boardView;
 	}
-
+	
 
 	public void run()
 	{
@@ -69,10 +69,19 @@ public class UpdateThread extends Thread
 					Alert alert = new Alert(Alert.AlertType.ERROR, "Error during reading from penguin program!", ButtonType.FINISH);
 					alert.showAndWait();
 					e.printStackTrace();
-					System.exit(1);
+					Platform.exit();
+				});
+			}
+			catch (Throwable e)
+			{
+				gameRunning = false;
+				Platform.runLater(() -> {
+					Alert alert = new Alert(Alert.AlertType.ERROR, "Unhandled exception in update thread: " + e.getMessage(), ButtonType.FINISH);
+					alert.showAndWait();
+					e.printStackTrace();
+					Platform.exit();
 				});
 			}
-
 		}
 	}
 }
-- 
GitLab