From ee2b22a572b614e9a0cca1b490cbbe1ef2f295b3 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sun, 4 Dec 2022 18:09:13 +0100 Subject: [PATCH] Logger working --- src/main/java/com/billenius/schack/Board.java | 16 +++++++-- src/main/java/com/billenius/schack/Pawn.java | 10 +++--- src/main/java/com/billenius/schack/Piece.java | 9 +++-- .../java/com/billenius/schack/Schack.java | 36 ++++++++++++++++--- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/billenius/schack/Board.java b/src/main/java/com/billenius/schack/Board.java index 701ffbd..259fc77 100644 --- a/src/main/java/com/billenius/schack/Board.java +++ b/src/main/java/com/billenius/schack/Board.java @@ -9,10 +9,15 @@ 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; import javax.swing.JOptionPane; import javax.swing.JPanel; +import com.billenius.schack.MoveLogging.Move; + public class Board extends JPanel implements MouseListener { public static final int SIZE_OF_TILE = 100; @@ -22,10 +27,13 @@ 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; - public Board() throws IOException { + public Board(DefaultListModel listModel) throws IOException { this.pieces = getPieces(); setPreferredSize(new Dimension(800, 800)); + this.listModel = listModel; } /** @@ -83,7 +91,7 @@ public class Board extends JPanel implements MouseListener { } // Måla alla pjäser - for (Piece[] pieceArr : pieces) + for (Piece[] pieceArr : pieces) for (Piece piece : pieceArr) { if (piece == null) { continue; @@ -116,6 +124,10 @@ public class Board extends JPanel implements MouseListener { validMovesToDraw.clear(); return; } + + Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate); + moveLog.add(m); + listModel.addElement(m); selectedPiece.move(pieces, clickedCoordinate); turnCount++; whitesTurn = !whitesTurn; diff --git a/src/main/java/com/billenius/schack/Pawn.java b/src/main/java/com/billenius/schack/Pawn.java index 75bd5b4..c337c0b 100644 --- a/src/main/java/com/billenius/schack/Pawn.java +++ b/src/main/java/com/billenius/schack/Pawn.java @@ -78,9 +78,9 @@ public class Pawn extends Piece { private List addAttackMovesIfCan(Point pos, Piece[][] pieces) { List movable = new ArrayList<>(); // Se till att vi inte är utanför brädet - if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) { + if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) return movable; - } + final Piece potentialEnemy = pieces[pos.x][pos.y]; // Ifall det är tomt här, gör ingenting if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) { @@ -93,9 +93,8 @@ public class Pawn extends Piece { @Override protected boolean addMovesIfCan(Point pos, List movable, Piece[][] pieces, boolean allowedToRecurse) { - if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { + if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) return false; - } Piece pieceToCheck = pieces[pos.x][pos.y]; if (pieceToCheck == null) { @@ -114,9 +113,8 @@ public class Pawn extends Piece { // Check if the pawn has moved to the end and should be transformed if (this.position.y == 0 && this.isWhite() - || this.position.y == 7 && !this.isWhite()) { + || this.position.y == 7 && !this.isWhite()) transform(pieces); - } } private void transform(Piece[][] pieces) throws HeadlessException { diff --git a/src/main/java/com/billenius/schack/Piece.java b/src/main/java/com/billenius/schack/Piece.java index a5d2fd7..1cfb974 100644 --- a/src/main/java/com/billenius/schack/Piece.java +++ b/src/main/java/com/billenius/schack/Piece.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; +import javax.swing.ImageIcon; public abstract class Piece { @@ -197,9 +198,9 @@ public abstract class Piece { // Kollar ifall kungen står i schack just nu for (Point enemyAttack : enemyAttacks) { Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y]; - if (attackedPiece != null && attackedPiece.supremeRuler) + if (attackedPiece != null && attackedPiece.supremeRuler) return true; - + } return false; } @@ -226,4 +227,8 @@ public abstract class Piece { return moved; } + public BufferedImage getIcon() { + return this.icon; + } + } diff --git a/src/main/java/com/billenius/schack/Schack.java b/src/main/java/com/billenius/schack/Schack.java index d7762f3..9505dc3 100644 --- a/src/main/java/com/billenius/schack/Schack.java +++ b/src/main/java/com/billenius/schack/Schack.java @@ -1,20 +1,35 @@ package com.billenius.schack; +import java.awt.Dimension; 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; 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 @@ -37,11 +52,25 @@ public class Schack { frame = new JFrame(); frame.setTitle("Schack"); frame.setAlwaysOnTop(false); - frame.setResizable(false); + frame.setResizable(true); + + DefaultListModel listModel = new DefaultListModel<>(); // Might throw an IOException if the icon of the Pieces isn't embedded correctly - final Board board = new Board(); - frame.setContentPane(board); + final Board board = new Board(listModel); + + final JList jlist = new JList<>(listModel); + jlist.setVisible(true); + jlist.setCellRenderer(new PieceRenderer()); + + JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + splitPane.setDividerLocation(800); + board.setMinimumSize(new Dimension(800, 800)); + jlist.setMinimumSize(new Dimension(200, 800)); + splitPane.setLeftComponent(board); + splitPane.setRightComponent(new JScrollPane(jlist)); + + frame.setContentPane(splitPane); frame.getContentPane().addMouseListener(board); // Create menu @@ -88,7 +117,6 @@ public class Schack { connectToOpponent.addActionListener((ActionEvent ae) -> { String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?"); System.out.println("opponents ip: " + opponentIP); - }); // Add the menu stuff