From be59f829c510f5417c368392a0af9c7c6eb4b82c Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sun, 4 Dec 2022 18:15:26 +0100 Subject: [PATCH] flytta ut move logik till metod --- src/main/java/com/billenius/schack/Board.java | 65 ++++++++++--------- src/main/java/com/billenius/schack/Piece.java | 1 - .../java/com/billenius/schack/Schack.java | 8 --- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/billenius/schack/Board.java b/src/main/java/com/billenius/schack/Board.java index 259fc77..3229d34 100644 --- a/src/main/java/com/billenius/schack/Board.java +++ b/src/main/java/com/billenius/schack/Board.java @@ -9,7 +9,6 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.IOException; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; import javax.swing.DefaultListModel; @@ -27,13 +26,12 @@ public class Board extends JPanel implements MouseListener { private final Color moveableColor = new Color(255, 191, 0); short turnCount = 0; private boolean whitesTurn = true; - private List moveLog = new LinkedList<>(); - private DefaultListModel listModel; + private DefaultListModel moveList; public Board(DefaultListModel listModel) throws IOException { this.pieces = getPieces(); setPreferredSize(new Dimension(800, 800)); - this.listModel = listModel; + this.moveList = listModel; } /** @@ -111,6 +109,34 @@ public class Board extends JPanel implements MouseListener { } } + private void makeMove(Move move) { + move.movedPiece.move(pieces, move.to); + turnCount++; + whitesTurn = !whitesTurn; + + SchackState state = getSchackState(); + switch (state) { + case SCHACK: + JOptionPane.showMessageDialog(this, "Du står i schack"); + break; + case SCHACKMATT: + case PATT: + String stateStr = state.toString(); + String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase(); + int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?"); + + if (choice == JOptionPane.YES_OPTION) + try { + restartGame(); + } catch (IOException ex) { + } + + break; + default: + } + + } + @Override public void mousePressed(MouseEvent mouseEvent) { final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); @@ -125,34 +151,9 @@ public class Board extends JPanel implements MouseListener { return; } - Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate); - moveLog.add(m); - listModel.addElement(m); - selectedPiece.move(pieces, clickedCoordinate); - turnCount++; - whitesTurn = !whitesTurn; - - SchackState state = getSchackState(); - switch (state) { - case SCHACK: - JOptionPane.showMessageDialog(this, "Du står i schack"); - break; - case SCHACKMATT: - case PATT: - String stateStr = state.toString(); - String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase(); - int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?"); - - if (choice == JOptionPane.YES_OPTION) - try { - restartGame(); - } catch (IOException ex) { - } - - break; - default: - } - + Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate); + moveList.addElement(move); + makeMove(move); } else { previouslyClickedPoint = new Point(clickedCoordinate); validMovesToDraw = new ArrayList<>(); // Snabbare än .clear diff --git a/src/main/java/com/billenius/schack/Piece.java b/src/main/java/com/billenius/schack/Piece.java index 1cfb974..8ca48d7 100644 --- a/src/main/java/com/billenius/schack/Piece.java +++ b/src/main/java/com/billenius/schack/Piece.java @@ -8,7 +8,6 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; -import javax.swing.ImageIcon; public abstract class Piece { diff --git a/src/main/java/com/billenius/schack/Schack.java b/src/main/java/com/billenius/schack/Schack.java index 9505dc3..11c45f4 100644 --- a/src/main/java/com/billenius/schack/Schack.java +++ b/src/main/java/com/billenius/schack/Schack.java @@ -5,14 +5,10 @@ import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.io.IOException; import java.net.Inet4Address; -import java.net.NetworkInterface; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; import javax.swing.DefaultListModel; import javax.swing.JFrame; -import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; @@ -20,16 +16,12 @@ import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JSplitPane; -import javax.swing.ListModel; -import javax.swing.SpinnerDateModel; import javax.swing.UIManager; import com.billenius.schack.MoveLogging.Move; import com.billenius.schack.MoveLogging.PieceRenderer; import com.formdev.flatlaf.FlatLightLaf; -import com.billenius.schack.MoveLogging.PieceType; - /** * * @author Love Billenius & Simon Hansson