diff --git a/src/main/java/com/billenius/schack/Schack.java b/src/main/java/com/billenius/schack/Schack.java index 0ae632b..41d1823 100644 --- a/src/main/java/com/billenius/schack/Schack.java +++ b/src/main/java/com/billenius/schack/Schack.java @@ -5,10 +5,15 @@ import java.awt.Dimension; import java.awt.Font; import java.awt.HeadlessException; import java.awt.event.ActionEvent; +import java.awt.image.BufferedImage; import java.io.IOException; +import java.io.InputStream; import java.net.Inet4Address; import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.Map; +import javax.imageio.ImageIO; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JLabel; @@ -34,8 +39,24 @@ import com.formdev.flatlaf.FlatLightLaf; public class Schack { final JFrame frame; + // Förlåt mig fader för jag kommer synda + public final static Map pieceIcons = new HashMap<>(); + + /** + * Ladda in samtliga pjäsbilder från paketet img + * + * @throws IOException ifall det inte finns någon bild på pjäsen + */ + public void loadAllPieceIcons() throws IOException { + for (String color : new String[] { "Black", "White" }) + for (String piece : new String[] { "Bishop", "Horse", "King", "Pawn", "Queen", "Rook" }) { + InputStream is = getClass().getResourceAsStream("/com/billenius/img/" + color + piece + ".png"); + pieceIcons.put(color + piece, ImageIO.read(is)); + } + } public Schack() throws IOException { + loadAllPieceIcons(); // Set theme try { if (UIManager.getSystemLookAndFeelClassName() diff --git a/src/main/java/com/billenius/schack/boards/Board.java b/src/main/java/com/billenius/schack/boards/Board.java index 7a5e827..6ee0971 100644 --- a/src/main/java/com/billenius/schack/boards/Board.java +++ b/src/main/java/com/billenius/schack/boards/Board.java @@ -33,7 +33,7 @@ public abstract class Board extends JPanel implements MouseListener { private final Color moveableColor = new Color(255, 191, 0); short turnCount = 0; protected boolean whitesTurn = true; - private DefaultListModel moveList; + protected DefaultListModel moveList; public Board(DefaultListModel listModel) throws IOException { this.pieces = getPieces(); @@ -133,7 +133,6 @@ public abstract class Board extends JPanel implements MouseListener { } Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate); - moveList.addElement(move); makeMove(move); } else { previouslyClickedPoint = new Point(clickedCoordinate); diff --git a/src/main/java/com/billenius/schack/boards/NetworkBoard.java b/src/main/java/com/billenius/schack/boards/NetworkBoard.java index 3cb7b28..154eba0 100644 --- a/src/main/java/com/billenius/schack/boards/NetworkBoard.java +++ b/src/main/java/com/billenius/schack/boards/NetworkBoard.java @@ -74,7 +74,7 @@ public final class NetworkBoard extends Board { "What's the IP of your opponent?", "Schack: Networking", JOptionPane.QUESTION_MESSAGE); - this.socket = new Socket(ip, 1339); + this.socket = new Socket("localhost", 1339); myColor = false; // Get input/output stream @@ -88,7 +88,10 @@ public final class NetworkBoard extends Board { @Override protected void makeMove(Move move) { try { + moveList.addElement(move); + move.movedPiece.move(pieces, move.to); + getParent().repaint(); outputStream.writeObject(move); SchackState state = getSchackState(); @@ -113,13 +116,31 @@ public final class NetworkBoard extends Board { } move = (Move) inputStream.readObject(); - move.movedPiece.move(pieces, move.to); + moveList.addElement(move); + + System.out.println(move); + pieces[move.from.x][move.from.y].move(pieces, move.to); + getParent().repaint(); } catch (Exception e) { e.printStackTrace(); } } + // Hitta våran lokala pjäs på brädet + protected Piece getLocalFromRemote(Piece remotePiece) { + for (Piece[] row : pieces) + for (Piece localPiece : row) { + if (localPiece == null) + continue; + + if (localPiece.equals(remotePiece)) + return remotePiece; + + } + return null; + } + @Override protected void toDoIfNoPreviousPieceSelected(Piece selectedPiece) { if (selectedPiece != null && selectedPiece.isWhite() == myColor) diff --git a/src/main/java/com/billenius/schack/boards/SameBoard.java b/src/main/java/com/billenius/schack/boards/SameBoard.java index 059dac8..5df047f 100644 --- a/src/main/java/com/billenius/schack/boards/SameBoard.java +++ b/src/main/java/com/billenius/schack/boards/SameBoard.java @@ -17,7 +17,8 @@ public final class SameBoard extends Board { } @Override - protected void makeMove(Move move) { + protected void makeMove(Move move) { + moveList.addElement(move); move.movedPiece.move(pieces, move.to); turnCount++; whitesTurn = !whitesTurn; diff --git a/src/main/java/com/billenius/schack/pieces/Piece.java b/src/main/java/com/billenius/schack/pieces/Piece.java index a9e6620..cf6db96 100644 --- a/src/main/java/com/billenius/schack/pieces/Piece.java +++ b/src/main/java/com/billenius/schack/pieces/Piece.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; +import com.billenius.schack.Schack; import com.billenius.schack.boards.Board; public abstract class Piece implements Serializable { @@ -27,10 +28,6 @@ public abstract class Piece implements Serializable { * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:) */ public boolean supremeRuler = false; - /** - * Bild av pjäsen som ritas ut på bärdet - */ - protected BufferedImage icon; /** * Nödvändigt för rockad @@ -40,27 +37,12 @@ public abstract class Piece implements Serializable { public Piece(boolean white, Point startingPosition) throws IOException { this.isWhite = white; this.position = startingPosition; - setPieceIcon(); } public Piece(boolean white) { this.isWhite = white; } - /** - * Ladda in pjäsbild från paketet img - * - * @param className - * @throws IOException ifall det inte finns någon bild på pjäsen - */ - private void setPieceIcon() throws IOException { - String className = this.getClass().getSimpleName(); - String colorName = this.isWhite() ? "White" : "Black"; - String fileName = colorName + className + ".png"; - InputStream is = getClass().getResourceAsStream("/com/billenius/img/" + fileName); - icon = ImageIO.read(is); - } - /** * Ger tillbaks alla ställen pjäsen kan gå till * @@ -89,8 +71,9 @@ public abstract class Piece implements Serializable { * @param g2 */ public void draw(Graphics2D g2) { + g2.drawImage( - icon, + getIcon(), position.x * Board.SIZE_OF_TILE, position.y * Board.SIZE_OF_TILE, null); @@ -212,6 +195,13 @@ public abstract class Piece implements Serializable { return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}'; } + public boolean equals(Piece otherPiece) { + return getClass().getName().equals(otherPiece.getClass().getName()) + && isWhite() == otherPiece.isWhite() + && supremeRuler == otherPiece.supremeRuler && moved == otherPiece.moved + && position.equals(otherPiece.position); + } + /** * * @return true ifall pjäsen är vit @@ -230,7 +220,9 @@ public abstract class Piece implements Serializable { } public BufferedImage getIcon() { - return this.icon; + String className = this.getClass().getSimpleName(); + String colorName = this.isWhite() ? "White" : "Black"; + return Schack.pieceIcons.get(colorName + className); } }