From 0f665c5829ae27e3ba125487e22ac266f13e930f Mon Sep 17 00:00:00 2001 From: lov3b Date: Wed, 11 May 2022 21:12:44 +0200 Subject: [PATCH] =?UTF-8?q?L=C3=A4gg=20till=20remi=20+=20ge=20upp=20+=20sm?= =?UTF-8?q?=C3=A5=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Board.java | 34 ++++++++++++++++++++++++---------- src/schack/Schack.java | 28 ++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 1b68006..b2dc518 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -19,19 +19,30 @@ public class Board extends JPanel implements MouseListener { private Piece[][] pieces = new Piece[8][8]; private ArrayList validMovesToDraw = new ArrayList<>(); private Point selectedPiece = new Point(); - private Color moveableColor = new Color(255, 191, 0); + private final Color moveableColor = new Color(255, 191, 0); public boolean developerMode = false; short turnCount = 0; - boolean whitesTurn = true; + private boolean whitesTurn = true; public Board() throws IOException { - this.pieces = initPieces(); + this.pieces = getPieces(); setPreferredSize(new Dimension(800, 800)); } - private Piece[][] initPieces() throws IOException { + /** + * Nollställ brädet + * + * @throws IOException + */ + public void restartGame() throws IOException { + this.pieces = getPieces(); + this.whitesTurn = true; + getParent().repaint(); + } + + private Piece[][] getPieces() throws IOException { Piece[][] piecesRet = { {new Rook(false, new Point(0, 0)), null, null, null, null, null, null, new Rook(true, new Point(0, 7))}, @@ -102,7 +113,6 @@ public class Board extends JPanel implements MouseListener { public void mousePressed(MouseEvent mouseEvent) { int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE); - Point clicked = new Point(mouseCoordinateX, mouseCoordinateY); // Ifall vi har tryckt på en pjäs och sedan ska gå dit @@ -127,7 +137,7 @@ public class Board extends JPanel implements MouseListener { ArrayList opposingAttacks = checkAttacks(!whitesTurn); - boolean weCanMove = allValidMoves.size() > 0; + boolean weCanMove = !allValidMoves.isEmpty(); boolean inSchack = false; for (Point attack : opposingAttacks) { @@ -140,10 +150,11 @@ public class Board extends JPanel implements MouseListener { if (weCanMove) { JOptionPane.showMessageDialog(this, "Du står i schack"); } else { + validMovesToDraw = new ArrayList<>(); + getParent().repaint(); int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); if (choise == JOptionPane.YES_OPTION) { - this.pieces = initPieces(); - whitesTurn = true; + restartGame(); } } inSchack = true; @@ -205,8 +216,7 @@ public class Board extends JPanel implements MouseListener { validMovesToDraw.clear(); } - getParent() - .repaint(); + getParent().repaint(); } public ArrayList checkAttacks(boolean preferedColor) { @@ -236,4 +246,8 @@ public class Board extends JPanel implements MouseListener { @Override public void mouseExited(MouseEvent e) { } + + public boolean isWhitesTurn() { + return whitesTurn; + } } diff --git a/src/schack/Schack.java b/src/schack/Schack.java index e1e6157..673eda1 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -1,9 +1,12 @@ package schack; +import com.formdev.flatlaf.FlatLightLaf; import java.awt.event.ActionEvent; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; @@ -51,7 +54,6 @@ public class Schack { JMenuItem showLocalIP = new JMenuItem("Show IP"); JMenuItem askForRemi = new JMenuItem("Ask for remi"); JMenuItem surrender = new JMenuItem("Surrender"); - // Actions connectToOpponent.addActionListener((ActionEvent ae) -> { @@ -66,12 +68,30 @@ public class Schack { } }); askForRemi.addActionListener((ActionEvent ae) -> { - System.out.println("I BEG FOR LE MERCY! (TODO)"); + String whosWantingRemi = board.isWhitesTurn() ? "Vit" : "Svart"; + int choice = JOptionPane.showConfirmDialog(board, whosWantingRemi + " erbjuder remi\nAccepterar du?"); + if (choice == JOptionPane.YES_OPTION) { + choice = JOptionPane.showConfirmDialog(board, "Lika\nStarta om?"); + if (choice == JOptionPane.YES_OPTION) { + try { + board.restartGame(); + } catch (IOException ex) { + Logger.getLogger(Schack.class.getName()).log(Level.SEVERE, null, ex); + } + } + } }); surrender.addActionListener((ActionEvent ae) -> { - System.out.println("I'M FRENCH! (TODO)"); + String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart"; + int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?"); + if (choice == JOptionPane.YES_OPTION) { + try { + board.restartGame(); + } catch (IOException ex) { + Logger.getLogger(Schack.class.getName()).log(Level.SEVERE, null, ex); + } + } }); - // Add the menu stuff frame.setJMenuBar(menuBar);