mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
can now delete your save and start a new game
This commit is contained in:
@@ -40,6 +40,9 @@ public class ConnectionManager {
|
||||
private String startConnectionRequest(String ip, String port) {
|
||||
String connectionId = UUID.randomUUID().toString();
|
||||
try {
|
||||
if (!port.matches("[0-9]+")) {
|
||||
port = "0000";
|
||||
}
|
||||
ServerConnection connection = new ServerConnection(connectionId, ip, port);
|
||||
this.serverConnections.put(connectionId, connection);
|
||||
new Thread(connection, "Connection-" + connectionId).start();
|
||||
|
||||
@@ -21,7 +21,7 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
volatile boolean running = false;
|
||||
|
||||
public ServerConnection(String uuid, String ip, String port) throws IOException {
|
||||
super(ip, Integer.parseInt(port)); // TODO: Verify if port is integer first, to avoid crash.
|
||||
super(ip, Integer.parseInt(port));
|
||||
this.uuid = uuid;
|
||||
this.initEvents();
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ public class LocalGameSelector extends JFrame {
|
||||
private JComboBox gameSelectionComboBox;
|
||||
private JButton startGame;
|
||||
private JComboBox playerTypeSelectionBox;
|
||||
private JButton deleteSave;
|
||||
|
||||
private JPanel cards; // CardLayout panel
|
||||
private CardLayout cardLayout;
|
||||
@@ -48,6 +49,11 @@ public class LocalGameSelector extends JFrame {
|
||||
startGame = new JButton("Start Game");
|
||||
panel1.add(startGame);
|
||||
|
||||
deleteSave = new JButton("Delete Save");
|
||||
panel1.add(deleteSave);
|
||||
deleteSave.setEnabled(false);
|
||||
deleteSave.addActionListener(e -> {tttBoard = null; deleteSave.setEnabled(false);});
|
||||
|
||||
cards.add(panel1, "MainMenu");
|
||||
|
||||
// Start button action
|
||||
@@ -86,5 +92,10 @@ public class LocalGameSelector extends JFrame {
|
||||
|
||||
public void showMainMenu() {
|
||||
cardLayout.show(cards, "MainMenu");
|
||||
gameSelectionComboBox.setSelectedIndex(0);
|
||||
playerTypeSelectionBox.setSelectedIndex(0);
|
||||
if (tttBoard != null) {
|
||||
deleteSave.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
import org.toop.frontend.games.LocalTicTacToe;
|
||||
|
||||
public class RemoteGameSelector {
|
||||
private static final Logger logger = LogManager.getLogger(RemoteGameSelector.class);
|
||||
@@ -25,6 +26,8 @@ public class RemoteGameSelector {
|
||||
private JFrame frame;
|
||||
private JLabel fillAllFields;
|
||||
|
||||
private LocalTicTacToe localTicTacToe;
|
||||
|
||||
public RemoteGameSelector() {
|
||||
gameSelectorBox.addItem("Tic Tac Toe");
|
||||
gameSelectorBox.addItem("Reversi");
|
||||
@@ -122,9 +125,9 @@ public class RemoteGameSelector {
|
||||
// } // TODO: Better error handling to not crash the system.
|
||||
|
||||
frame.remove(mainMenu);
|
||||
// UIGameBoard ttt = new UIGameBoard("tic tac toe", "test",
|
||||
// "test",this); // TODO: Fix later
|
||||
// frame.add(ttt.getTTTPanel()); // TODO: Fix later
|
||||
localTicTacToe = LocalTicTacToe.createRemote(ipTextField.getText(), portTextField.getText());
|
||||
UIGameBoard ttt = new UIGameBoard(localTicTacToe, this); // TODO: Fix later
|
||||
frame.add(ttt.getTTTPanel()); // TODO: Fix later
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
} else {
|
||||
@@ -133,7 +136,7 @@ public class RemoteGameSelector {
|
||||
});
|
||||
}
|
||||
|
||||
public void returnToMainMenu() {
|
||||
public void showMainMenu() {
|
||||
frame.removeAll();
|
||||
frame.add(mainMenu);
|
||||
frame.revalidate();
|
||||
|
||||
@@ -20,10 +20,20 @@ public class UIGameBoard {
|
||||
private String currentPlayer = "X";
|
||||
private int currentPlayerIndex = 0;
|
||||
|
||||
private LocalGameSelector parentSelector;
|
||||
private Object parentSelector;
|
||||
private boolean parentLocal;
|
||||
private LocalTicTacToe localTicTacToe;
|
||||
|
||||
public UIGameBoard(LocalTicTacToe lttt, LocalGameSelector parent) {
|
||||
private boolean gameOver = false;
|
||||
|
||||
public UIGameBoard(LocalTicTacToe lttt, Object parent) {
|
||||
if (!(parent == null)) {
|
||||
if (parent instanceof LocalGameSelector) {
|
||||
parentLocal = true;
|
||||
} else if (parent instanceof RemoteGameSelector) {
|
||||
parentLocal = false;
|
||||
}
|
||||
}
|
||||
this.parentSelector = parent;
|
||||
this.localTicTacToe = lttt;
|
||||
lttt.setUIReference(this);
|
||||
@@ -35,9 +45,16 @@ public class UIGameBoard {
|
||||
backToMainMenuButton = new JButton("Back to Main Menu");
|
||||
tttPanel.add(backToMainMenuButton, BorderLayout.SOUTH);
|
||||
backToMainMenuButton.addActionListener(
|
||||
_ ->
|
||||
_ ->{
|
||||
// TODO reset game and connections
|
||||
parent.showMainMenu());
|
||||
// Game now gets reset in local
|
||||
if(parentLocal) {
|
||||
((LocalGameSelector)parent).showMainMenu();
|
||||
}
|
||||
else{
|
||||
((RemoteGameSelector)parent).showMainMenu();
|
||||
}
|
||||
});
|
||||
|
||||
// Game grid
|
||||
JPanel gameGrid = createGridPanel(TICTACTOE_SIZE, TICTACTOE_SIZE);
|
||||
@@ -63,20 +80,24 @@ public class UIGameBoard {
|
||||
final int index = i;
|
||||
cells[i].addActionListener(
|
||||
(ActionEvent _) -> {
|
||||
if (cells[index].getText().equals(" ")) {
|
||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||
if (cp == 0) {
|
||||
this.currentPlayer = "X";
|
||||
currentPlayerIndex = 0;
|
||||
} else if (cp == 1) {
|
||||
this.currentPlayer = "O";
|
||||
currentPlayerIndex = 1;
|
||||
if(!gameOver) {
|
||||
if (cells[index].getText().equals(" ")) {
|
||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||
if (cp == 0) {
|
||||
this.currentPlayer = "X";
|
||||
currentPlayerIndex = 0;
|
||||
} else if (cp == 1) {
|
||||
this.currentPlayer = "O";
|
||||
currentPlayerIndex = 1;
|
||||
}
|
||||
this.localTicTacToe.move(index);
|
||||
cells[index].setText(currentPlayer);
|
||||
}
|
||||
this.localTicTacToe.move(index);
|
||||
cells[index].setText(currentPlayer);
|
||||
}
|
||||
else{
|
||||
logger.info("Player " + currentPlayerIndex + " attempted invalid move at: " + cells[index].getText());
|
||||
else{
|
||||
logger.info("Player " + currentPlayerIndex + " attempted invalid move at: " + cells[index].getText());
|
||||
}
|
||||
}else {
|
||||
logger.info("Player " + currentPlayerIndex + " attempted to move after the game has ended.");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -96,12 +117,18 @@ public class UIGameBoard {
|
||||
else if (state == GameBase.State.WIN) {
|
||||
color = new Color(220,160,160);
|
||||
}
|
||||
else{
|
||||
else if (state == GameBase.State.DRAW){
|
||||
color = new Color(220,220,160);
|
||||
}
|
||||
else {
|
||||
color = new Color(220,220,220);
|
||||
}
|
||||
for (JButton cell : cells) {
|
||||
cell.setBackground(color);
|
||||
}
|
||||
if (state == GameBase.State.DRAW || state == GameBase.State.WIN) {
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
|
||||
public JPanel getTTTPanel() {
|
||||
|
||||
@@ -27,8 +27,14 @@ public class MinMaxTicTacToe {
|
||||
}
|
||||
}
|
||||
|
||||
if (empty && game.validateMove(4)) {
|
||||
return 0;
|
||||
if (empty) { //start in a random corner
|
||||
return switch ((int) (Math.random() * 4)) {
|
||||
case 0 -> 0;
|
||||
case 1 -> 2;
|
||||
case 2 -> 6;
|
||||
case 3 -> 8;
|
||||
default -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
// simulate all possible moves on the field
|
||||
|
||||
Reference in New Issue
Block a user