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