From 7c1972d74007796e1e98b1eb4ee27830ea1524d1 Mon Sep 17 00:00:00 2001
From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr>
Date: Thu, 12 May 2016 19:31:14 +0200
Subject: [PATCH] GUI refactoring and cleaning

---
 gui/src/controller/Controller.java   | 41 +++++++++++++-
 gui/src/controller/UpdateThread.java | 85 +++++++++-------------------
 2 files changed, 68 insertions(+), 58 deletions(-)

diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java
index 1d64874..010484f 100644
--- a/gui/src/controller/Controller.java
+++ b/gui/src/controller/Controller.java
@@ -77,7 +77,7 @@ public class Controller implements Initializable
 					gameProcess.destroy();
 				}
 			});
-			UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, scoreRedLabel, scoreBlueLabel, turnLabel, statusLabel);
+			UpdateThread upT = new UpdateThread(gameProcess, this, this.gameState, statusLabel);
 			upT.setDaemon(true);
 			upT.start();
 		} catch (IOException e)
@@ -162,7 +162,46 @@ public class Controller implements Initializable
 			t.getFishLabel().setOnMouseClicked(gch);
 
 		}
+	}
+
+	/**
+	 * Updates model and view from the state
+	 */
+	public void updateModelAndView()
+	{
+		for (int i = 0; i < board.length; i++)
+		{
+			board[i].setNbFish(gameState.getNbFish(i));
+			board[i].setPenguinPresence(Tile.PenguinPresence.NO_PENGUIN);
+		}
+		for (int i = 0; i < 4; i++)
+		{
+			board[gameState.getPenguinPos(Player.Red, i)].setPenguinPresence(Tile.PenguinPresence.RED_PENGUIN);
+			board[gameState.getPenguinPos(Player.Blue, i)].setPenguinPresence(Tile.PenguinPresence.BLUE_PENGUIN);
+		}
+		for(TileView t : boardView)	//No need to deselect or de-highlight tiles because it is done in the click listener
+			t.update();
+		scoreRedLabel.setText(Integer.toString(gameState.getScore(Player.Red)));
+		scoreBlueLabel.setText(Integer.toString(gameState.getScore(Player.Blue)));
+		turnLabel.setText(gameState.getCurrent_player()+ "'s turn");
+	}
+	//
+	//
 
+	public void gameEnd()
+	{
+		Player human = gameState.getHumanPlayer();
+		Player computer = human.equals(Player.Red) ? Player.Blue : Player.Red;
+		String message = "";
+		if (gameState.getScore(human) > gameState.getScore(computer))
+			message = "You won!!! I can't believe it!";
+		else if (gameState.getScore(human) == gameState.getScore(computer))
+			message = "That's a draw. Not bad!";
+		else
+			message = "You just lost the penguin game.";
+		statusLabel.setText("End of game");
+		Alert alert = new Alert(Alert.AlertType.INFORMATION, message, ButtonType.FINISH);
+		alert.showAndWait();
 	}
 
 	private class PlacePenguinClickHandler implements EventHandler<MouseEvent>
diff --git a/gui/src/controller/UpdateThread.java b/gui/src/controller/UpdateThread.java
index 5aefad0..f41eab7 100644
--- a/gui/src/controller/UpdateThread.java
+++ b/gui/src/controller/UpdateThread.java
@@ -6,31 +6,24 @@ import javafx.scene.control.ButtonType;
 import javafx.scene.control.Label;
 import model.GameState;
 import model.Player;
-import model.Tile;
-import view.TileView;
 
 import java.io.*;
 
 public class UpdateThread extends Thread
 {
-	BufferedReader reader;
-	PrintWriter writer;
+	Controller controller;
+	BufferedReader gameReader;
+	PrintWriter gameWriter;
 	GameState gameState;
-	Tile[] board;
-	TileView[] boardView;
-	Label scoreRed, scoreBlue, turnLabel, statusLabel;
+	Label statusLabel;
 
-	public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Label scoreRed, Label scoreBlue, Label turnLabel, Label statusLabel)
+	public UpdateThread(Process program, Controller controller, GameState gameState, Label statusLabel)
 	{
-		this.reader = new BufferedReader(new InputStreamReader(program.getInputStream()));
-		this.writer = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true);
+		this.gameReader = new BufferedReader(new InputStreamReader(program.getInputStream()));
+		this.gameWriter = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true);
 		this.gameState = gameState;
-		this.board = board;
-		this.boardView = boardView;
-		this.scoreRed = scoreRed;
-		this.scoreBlue = scoreBlue;
-		this.turnLabel = turnLabel;
 		this.statusLabel = statusLabel;
+		this.controller = controller;
 	}
 
 	public void run()
@@ -40,63 +33,41 @@ public class UpdateThread extends Thread
 		{
 			try
 			{
-				String line = reader.readLine();
+				String line = gameReader.readLine();
 				System.out.println(line);
-				if (line == null)
+				if (line == null) //Normally this shouldn't happen (The game always end). So it is an error
 				{
 					gameRunning = false;
+					Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, "That's it! I rage quit!", ButtonType.FINISH).showAndWait());
 				} else if (line.startsWith(Player.Red + " won") || line.startsWith(Player.Blue + " won") || line.startsWith("draw"))
 				{
-					//System.out.println("======= END OF GAME=======");
-					Platform.runLater(() -> {
-						Player computer = gameState.getHumanPlayer().equals(Player.Red) ? Player.Blue : Player.Red;
-						String message = "";
-						if (line.startsWith(gameState.getHumanPlayer().toString())) //Human won
-							message = "You won!!! I can't believe it!";
-						else if (line.startsWith(computer.toString()))
-							message = "You just lost the penguin game.";
-						else
-							message = "That's a draw. Not bad!";
-						Alert alert = new Alert(Alert.AlertType.INFORMATION, message, ButtonType.OK);
-						alert.showAndWait();
-					});
 					gameRunning = false;
+					Platform.runLater(() -> controller.gameEnd());
 				} else if (line.contains("{")) //Line contains JSON
 				{
-					//UPDATE MODEL
+					//gameState Update
 					gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}") + 1)); //Extract JSON string
-					for (int i = 0; i < board.length; i++)
-					{
-						board[i].setNbFish(gameState.getNbFish(i));
-						board[i].setPenguinPresence(Tile.PenguinPresence.NO_PENGUIN);
-					}
-					for (int i = 0; i < 4; i++)
-					{
-						board[gameState.getPenguinPos(Player.Red, i)].setPenguinPresence(Tile.PenguinPresence.RED_PENGUIN);
-						board[gameState.getPenguinPos(Player.Blue, i)].setPenguinPresence(Tile.PenguinPresence.BLUE_PENGUIN);
-					}
-
-					//UPDATE VIEW
-					Platform.runLater(() -> {
-						for (int i = 0; i < boardView.length; i++)
-							boardView[i].update();
-						scoreRed.setText(Integer.toString(gameState.getScore(Player.Red)));
-						scoreBlue.setText(Integer.toString(gameState.getScore(Player.Blue)));
-						turnLabel.setText(gameState.getCurrent_player()+ "'s turn");
-					});
-
-					if (gameState.getCurrent_player() == gameState.getHumanPlayer())
+					Platform.runLater(() -> controller.updateModelAndView());
+					//If we can't play
+					if (gameState.getCurrent_player().equals(gameState.getHumanPlayer()))
 					{
 						if (!gameState.getCanPlay(gameState.getHumanPlayer()))
 						{
-							Platform.runLater(() ->
-							{
-								Alert alert = new Alert(Alert.AlertType.INFORMATION, "You can't play any move!", ButtonType.OK);
-								alert.showAndWait();
-								writer.println("0"); //This pass the turn
+							Platform.runLater(() -> {
+								statusLabel.setText("You can't play any move!");
+								try
+								{
+									sleep(2000);
+								}
+								catch (InterruptedException e)
+								{
+									e.printStackTrace();
+								}
+								gameWriter.println("0"); //This pass the turn
 							});
 						}
 					}
+
 				}
 				else if (line.startsWith("(") && line.contains("value:")) //lines with values (estimation) of the computer winning chances
 				{ //We want to show a little message to the user depending on if we win or not
-- 
GitLab