implement ready for players and board refresh method
This commit is contained in:
parent
970550308b
commit
f565708461
|
@ -88,7 +88,6 @@ public class BoardDisplay extends JPanel {
|
||||||
Point o = new Point(x, y);
|
Point o = new Point(x, y);
|
||||||
currentShip.setHorizontal(vertical);
|
currentShip.setHorizontal(vertical);
|
||||||
handleFieldClick(o); // Linksklick -> Schiff platzieren
|
handleFieldClick(o); // Linksklick -> Schiff platzieren
|
||||||
vertical = false; // TODO nur nach auswahl von neuem Modul wieder auf false setzten
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -183,4 +182,8 @@ public class BoardDisplay extends JPanel {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void refresh() {
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -13,6 +13,14 @@ import java.util.List;
|
||||||
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
|
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
|
||||||
*/
|
*/
|
||||||
public class GameBoard extends JPanel {
|
public class GameBoard extends JPanel {
|
||||||
|
|
||||||
|
private BoardDisplay ownBoardPanel;
|
||||||
|
private BoardDisplay opponentBoardPanel;
|
||||||
|
|
||||||
|
private Player p1;
|
||||||
|
private Player p2;
|
||||||
|
|
||||||
|
|
||||||
// Funktionshilfen
|
// Funktionshilfen
|
||||||
//int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel
|
//int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel
|
||||||
|
|
||||||
|
@ -46,7 +54,9 @@ public class GameBoard extends JPanel {
|
||||||
* @param p2 Zweites Spielerobjekt
|
* @param p2 Zweites Spielerobjekt
|
||||||
*/
|
*/
|
||||||
GameBoard(MainFrame frame, int semesterCounter,Player p1, Player p2) {
|
GameBoard(MainFrame frame, int semesterCounter,Player p1, Player p2) {
|
||||||
buildPanel(frame, semesterCounter,p1,p2);
|
this.p1 = p1;
|
||||||
|
this.p2 = p2;
|
||||||
|
buildPanel(frame, semesterCounter);
|
||||||
List<Ship> shipsP1 =p1.getBoard().getShips();
|
List<Ship> shipsP1 =p1.getBoard().getShips();
|
||||||
List<Ship> shipsP2 =p2.getBoard().getShips();
|
List<Ship> shipsP2 =p2.getBoard().getShips();
|
||||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
||||||
|
@ -93,18 +103,16 @@ public class GameBoard extends JPanel {
|
||||||
* TODO Funktion beschreiben etc.
|
* TODO Funktion beschreiben etc.
|
||||||
* @param frame
|
* @param frame
|
||||||
* @param semesterCounter
|
* @param semesterCounter
|
||||||
* @param p1
|
|
||||||
* @param p2
|
|
||||||
*/
|
*/
|
||||||
public void buildPanel(MainFrame frame, int semesterCounter,Player p1,Player p2) {
|
public void buildPanel(MainFrame frame, int semesterCounter) {
|
||||||
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
// Spielfelder erstellen (eigenes und gegnerisches)
|
// Spielfelder erstellen (eigenes und gegnerisches)
|
||||||
// Spielfelder werden in BoardDisplay erstellt
|
// Spielfelder werden in BoardDisplay erstellt
|
||||||
int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds
|
int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds
|
||||||
BoardDisplay ownBoardPanel = new BoardDisplay(gridSize,p1);
|
this.ownBoardPanel = new BoardDisplay(gridSize,p1);
|
||||||
BoardDisplay opponentBoardPanel = new BoardDisplay(gridSize, p2);
|
this.opponentBoardPanel = new BoardDisplay(gridSize, p2);
|
||||||
|
|
||||||
// Panel für das Kontext-Text-Feld
|
// Panel für das Kontext-Text-Feld
|
||||||
JPanel headerPanel = new JPanel();
|
JPanel headerPanel = new JPanel();
|
||||||
|
@ -137,6 +145,7 @@ public class GameBoard extends JPanel {
|
||||||
rightButtonsPanel.add(shipButton);
|
rightButtonsPanel.add(shipButton);
|
||||||
rightButtonGroup.add(shipButton);
|
rightButtonGroup.add(shipButton);
|
||||||
opponentBoardPanel.addShipButton(shipButton);
|
opponentBoardPanel.addShipButton(shipButton);
|
||||||
|
shipButton.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
JToggleButton readyButton = new JToggleButton("Bereit");
|
JToggleButton readyButton = new JToggleButton("Bereit");
|
||||||
|
@ -157,8 +166,8 @@ public class GameBoard extends JPanel {
|
||||||
readyButton.addActionListener(new ActionListener() {
|
readyButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
kontextText.setText(kT2);
|
kontextText.setText(kT2);
|
||||||
|
p1.ready();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Panel für die Buttons des rechten Spielers (ganz rechts)
|
// Panel für die Buttons des rechten Spielers (ganz rechts)
|
||||||
|
@ -200,4 +209,14 @@ public class GameBoard extends JPanel {
|
||||||
add(namesAndBoardsPanel, BorderLayout.CENTER);
|
add(namesAndBoardsPanel, BorderLayout.CENTER);
|
||||||
timer.start();
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void refresh() {
|
||||||
|
if (this.p1.myTurn) {
|
||||||
|
this.kontextText.setText(kT5);
|
||||||
|
} else {
|
||||||
|
this.kontextText.setText(kT6);
|
||||||
|
}
|
||||||
|
this.ownBoardPanel.refresh();
|
||||||
|
this.opponentBoardPanel.refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,21 +38,24 @@ public class LocalPlayer extends Player {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
public synchronized void receiveCoin(boolean coin) {
|
||||||
if (!this.haseReceivedCoin) {
|
if (!this.hasReceivedCoin) {
|
||||||
boolean result = coin ^ this.myCoin; // XOR
|
this.hasReceivedCoin = true;
|
||||||
this.myTurn = result == this.isServer;
|
this.determineCoinToss();
|
||||||
this.haseReceivedCoin = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendCoin() {
|
|
||||||
enemy.receiveCoin(this.myCoin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shoot(Point point){
|
public void shoot(Point point){
|
||||||
this.myTurn = false;
|
this.myTurn = false;
|
||||||
enemy.receiveShoot(point);
|
enemy.receiveShoot(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void ready() {
|
||||||
|
for (Ship ship : this.board.getShips()) {
|
||||||
|
if (!ship.isPlaced()) return;
|
||||||
|
}
|
||||||
|
super.ready();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -21,6 +21,7 @@ public class MainFrame extends JFrame {
|
||||||
// Von startLocalGameLoadingScreen an GameBoard
|
// Von startLocalGameLoadingScreen an GameBoard
|
||||||
int semesterCounter;
|
int semesterCounter;
|
||||||
// ---------- //
|
// ---------- //
|
||||||
|
private GameBoard gameBoard;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Konstruktor von MainFrame.
|
* Konstruktor von MainFrame.
|
||||||
|
@ -95,7 +96,7 @@ public class MainFrame extends JFrame {
|
||||||
public void showPanelSLG(String panelName,int semesterCounter, Player p1, Player p2) {
|
public void showPanelSLG(String panelName,int semesterCounter, Player p1, Player p2) {
|
||||||
this.semesterCounter = semesterCounter;
|
this.semesterCounter = semesterCounter;
|
||||||
|
|
||||||
GameBoard gameBoard = new GameBoard(this, semesterCounter, p1, p2);
|
this.gameBoard = new GameBoard(this, semesterCounter, p1, p2);
|
||||||
mainPanel.add(gameBoard, panelName);
|
mainPanel.add(gameBoard, panelName);
|
||||||
mainPanel.revalidate();
|
mainPanel.revalidate();
|
||||||
mainPanel.repaint();
|
mainPanel.repaint();
|
||||||
|
@ -116,4 +117,8 @@ public class MainFrame extends JFrame {
|
||||||
mainPanel.repaint();
|
mainPanel.repaint();
|
||||||
cardLayout.show(mainPanel, panelName); // Show the panel
|
cardLayout.show(mainPanel, panelName); // Show the panel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void refreshGameBoard() {
|
||||||
|
this.gameBoard.refresh();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -7,6 +7,7 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
|
||||||
public OnlinePlayer(Integer size, AsyncSocket socket) {
|
public OnlinePlayer(Integer size, AsyncSocket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.wantedBoardSize = size;
|
this.wantedBoardSize = size;
|
||||||
|
this.myCoin = null;
|
||||||
socket.setHandler(this);
|
socket.setHandler(this);
|
||||||
//TODO Auto-generated constructor stub
|
//TODO Auto-generated constructor stub
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,8 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
|
|
||||||
case "COIN":
|
case "COIN":
|
||||||
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
|
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
|
||||||
this.myCoin = p.getData().equals("1");
|
this.myCoin = p.getData().equals("1");
|
||||||
enemy.receiveCoin(this.myCoin);
|
this.ready();
|
||||||
this.hasReceivedCoinPackage = true;
|
this.hasReceivedCoinPackage = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -85,9 +85,10 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
public synchronized void receiveCoin(boolean coin) {
|
||||||
if (!this.haseReceivedCoin) {
|
if (!this.hasReceivedCoin) {
|
||||||
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
|
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
|
||||||
this.haseReceivedCoin = true;
|
this.hasReceivedCoin = true;
|
||||||
|
this.determineCoinToss();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,19 +2,20 @@ public abstract class Player {
|
||||||
protected boolean myTurn;
|
protected boolean myTurn;
|
||||||
protected boolean isServer;
|
protected boolean isServer;
|
||||||
protected boolean waitingForResponse;
|
protected boolean waitingForResponse;
|
||||||
protected Player enemy;
|
protected Player enemy;
|
||||||
protected String name;
|
protected String name;
|
||||||
protected Board board;
|
protected Board board;
|
||||||
protected boolean myCoin;
|
protected Boolean myCoin;
|
||||||
|
|
||||||
protected boolean sendCoin;
|
protected boolean sentCoin;
|
||||||
|
|
||||||
protected boolean haseReceivedCoin;
|
protected boolean hasReceivedCoin;
|
||||||
|
|
||||||
public Player() {
|
public Player() {
|
||||||
this.setName("Player");
|
this.setName("Player");
|
||||||
this.haseReceivedCoin = false;
|
this.hasReceivedCoin = false;
|
||||||
this.sendCoin = false;
|
this.sentCoin = false;
|
||||||
|
this.myTurn = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createBoard(int size) {
|
public void createBoard(int size) {
|
||||||
|
@ -27,8 +28,8 @@ public abstract class Player {
|
||||||
|
|
||||||
public abstract void shoot(Point point);
|
public abstract void shoot(Point point);
|
||||||
|
|
||||||
public void beginTrun() {
|
public void beginTurn() {
|
||||||
|
System.out.println("issa my turn-a");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnemy(Player enemy) {
|
public void setEnemy(Player enemy) {
|
||||||
|
@ -46,5 +47,23 @@ public abstract class Player {
|
||||||
return this.board;
|
return this.board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ready() {
|
||||||
|
this.enemy.receiveCoin(this.myCoin);
|
||||||
|
this.sentCoin = true;
|
||||||
|
if (hasReceivedCoin) {
|
||||||
|
this.determineCoinToss();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
protected void determineCoinToss() {
|
||||||
|
if (!this.sentCoin || this.myCoin == null || !this.hasReceivedCoin || this.enemy.myCoin == null) return;
|
||||||
|
boolean result = this.enemy.myCoin ^ this.myCoin; // XOR
|
||||||
|
this.myTurn = result == this.isServer;
|
||||||
|
if (this.myTurn) {
|
||||||
|
this.beginTurn();
|
||||||
|
}
|
||||||
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void receiveCoin(boolean coin);
|
public abstract void receiveCoin(boolean coin);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue