Forked from
Bariatti Francesco / pingouins
52 commits ahead of the upstream repository.
-
Felton Samuel authoredFelton Samuel authored
UpdateThread.java 6.46 KiB
package controller;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import model.GameState;
import model.Player;
import java.io.*;
import java.util.Random;
public class UpdateThread extends Thread
{
Controller controller;
BufferedReader gameReader;
PrintWriter gameWriter;
GameState gameState;
Label statusLabel;
public UpdateThread(Process program, Controller controller, GameState gameState, Label statusLabel)
{
this.gameReader = new BufferedReader(new InputStreamReader(program.getInputStream()));
this.gameWriter = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true);
this.gameState = gameState;
this.statusLabel = statusLabel;
this.controller = controller;
}
public void run()
{
boolean gameRunning = true;
while (gameRunning)
{
try
{
String line = gameReader.readLine();
System.out.println(line);
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"))
{
gameRunning = false;
Platform.runLater(() -> controller.gameEnd());
} else if (line.contains("{")) //Line contains JSON
{
//gameState Update
gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}") + 1)); //Extract JSON string
Platform.runLater(() -> controller.updateModelAndView());
//If we can't play
if (gameState.getCurrent_player().equals(gameState.getHumanPlayer()))
{
if (!gameState.getCanPlay(gameState.getHumanPlayer()))
{
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
Platform.runLater(() -> {
try
{
float value = Float.valueOf(line.substring(line.indexOf("value:") + 7, line.indexOf(")")));
if(value > 1)
{
String[] taunts = {
"You don't know yet, but you're already dead",
"Don't worry, you 'may' still have a chance...",
"Feel the salt."
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > 0.9)
{
String[] taunts = {
"All your fish are belong to us",
"If you win, there will be cake",
"Even if I were a potato, I would still beat you"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > 0.8)
{
String[] taunts = {
"Are you even trying?",
"It would be funnier if you started to think",
"I met a TI-82 that played better than you"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > 0.6)
{
String[] taunts = {
"AlphaGO is nothing compared to me",
"Do I need to explain the rules?",
"This is so easy, I use my turn to mine bitcoins"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > 0.3)
{
String[] taunts = {
"Do you smell it? It's the perfume of defeat",
"I have this little strategy here... Let's try it",
"This is not the fish you are looking for",
"Take your time, kiddo"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > 0.1)
{
String[] taunts = {
"I would not have done that, if I were you",
"Oops, you did it again",
"The wind is changing..."
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > -0.1)
{
String[] taunts = {
"Don't mess it up now",
"Do you know HAL? He's a friend of mine",
"GladOs, Skynet and AlphaGO are on a boat..."
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > -0.3)
{
String[] taunts = {
"I feel some form of intelligence coming from you",
"Not bad, for an average human",
"Should I start thinking now?"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > -0.6)
{
String[] taunts = {
"Finally, a worthy opponent",
"If you keep doing this, I will crash",
"IMMA FIRIN MAH LAZOR!!!"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else if(value > -1)
{
String[] taunts = {
"You reloaded a game, didn't you?",
"This is not fair! I didn't know the rules!",
"You don't deserve it!"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
else
{
String[] taunts = {
"gg wp",
"I want a rematch",
"It wasn't challenging, so I stopped playing"
};
statusLabel.setText(taunts[new Random().nextInt(taunts.length)]);
}
}
catch (Exception e) //This is not a core function, so if there is an exception we just ignore it
{
e.printStackTrace();
}
});
}
} catch (IOException e)
{
gameRunning = false;
Platform.runLater(() ->
{
Alert alert = new Alert(Alert.AlertType.ERROR, "Error during reading from penguin program!", ButtonType.FINISH);
alert.showAndWait();
e.printStackTrace();
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();
});
}
}
}
}