mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-04 07:00:21 +01:00 
			
		
		
		
	Båda rör fortfarande samma färg och det repaintas bara varannat drag
This commit is contained in:
		@@ -5,10 +5,15 @@ import java.awt.Dimension;
 | 
				
			|||||||
import java.awt.Font;
 | 
					import java.awt.Font;
 | 
				
			||||||
import java.awt.HeadlessException;
 | 
					import java.awt.HeadlessException;
 | 
				
			||||||
import java.awt.event.ActionEvent;
 | 
					import java.awt.event.ActionEvent;
 | 
				
			||||||
 | 
					import java.awt.image.BufferedImage;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.io.InputStream;
 | 
				
			||||||
import java.net.Inet4Address;
 | 
					import java.net.Inet4Address;
 | 
				
			||||||
import java.net.UnknownHostException;
 | 
					import java.net.UnknownHostException;
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.imageio.ImageIO;
 | 
				
			||||||
import javax.swing.DefaultListModel;
 | 
					import javax.swing.DefaultListModel;
 | 
				
			||||||
import javax.swing.JFrame;
 | 
					import javax.swing.JFrame;
 | 
				
			||||||
import javax.swing.JLabel;
 | 
					import javax.swing.JLabel;
 | 
				
			||||||
@@ -34,8 +39,24 @@ import com.formdev.flatlaf.FlatLightLaf;
 | 
				
			|||||||
public class Schack {
 | 
					public class Schack {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final JFrame frame;
 | 
					    final JFrame frame;
 | 
				
			||||||
 | 
					    // Förlåt mig fader för jag kommer synda
 | 
				
			||||||
 | 
					    public final static Map<String, BufferedImage> 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 {
 | 
					    public Schack() throws IOException {
 | 
				
			||||||
 | 
					        loadAllPieceIcons();
 | 
				
			||||||
        // Set theme
 | 
					        // Set theme
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            if (UIManager.getSystemLookAndFeelClassName()
 | 
					            if (UIManager.getSystemLookAndFeelClassName()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,7 +33,7 @@ public abstract class Board extends JPanel implements MouseListener {
 | 
				
			|||||||
    private final Color moveableColor = new Color(255, 191, 0);
 | 
					    private final Color moveableColor = new Color(255, 191, 0);
 | 
				
			||||||
    short turnCount = 0;
 | 
					    short turnCount = 0;
 | 
				
			||||||
    protected boolean whitesTurn = true;
 | 
					    protected boolean whitesTurn = true;
 | 
				
			||||||
    private DefaultListModel<Move> moveList;
 | 
					    protected DefaultListModel<Move> moveList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Board(DefaultListModel<Move> listModel) throws IOException {
 | 
					    public Board(DefaultListModel<Move> listModel) throws IOException {
 | 
				
			||||||
        this.pieces = getPieces();
 | 
					        this.pieces = getPieces();
 | 
				
			||||||
@@ -133,7 +133,6 @@ public abstract class Board extends JPanel implements MouseListener {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
 | 
					            Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
 | 
				
			||||||
            moveList.addElement(move);
 | 
					 | 
				
			||||||
            makeMove(move);
 | 
					            makeMove(move);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            previouslyClickedPoint = new Point(clickedCoordinate);
 | 
					            previouslyClickedPoint = new Point(clickedCoordinate);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -74,7 +74,7 @@ public final class NetworkBoard extends Board {
 | 
				
			|||||||
                    "What's the IP of your opponent?",
 | 
					                    "What's the IP of your opponent?",
 | 
				
			||||||
                    "Schack: Networking",
 | 
					                    "Schack: Networking",
 | 
				
			||||||
                    JOptionPane.QUESTION_MESSAGE);
 | 
					                    JOptionPane.QUESTION_MESSAGE);
 | 
				
			||||||
            this.socket = new Socket(ip, 1339);
 | 
					            this.socket = new Socket("localhost", 1339);
 | 
				
			||||||
            myColor = false;
 | 
					            myColor = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Get input/output stream
 | 
					            // Get input/output stream
 | 
				
			||||||
@@ -88,7 +88,10 @@ public final class NetworkBoard extends Board {
 | 
				
			|||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    protected void makeMove(Move move) {
 | 
					    protected void makeMove(Move move) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
 | 
					            moveList.addElement(move);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            move.movedPiece.move(pieces, move.to);
 | 
					            move.movedPiece.move(pieces, move.to);
 | 
				
			||||||
 | 
					            getParent().repaint();
 | 
				
			||||||
            outputStream.writeObject(move);
 | 
					            outputStream.writeObject(move);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            SchackState state = getSchackState();
 | 
					            SchackState state = getSchackState();
 | 
				
			||||||
@@ -113,13 +116,31 @@ public final class NetworkBoard extends Board {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            move = (Move) inputStream.readObject();
 | 
					            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) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
            e.printStackTrace();
 | 
					            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
 | 
					    @Override
 | 
				
			||||||
    protected void toDoIfNoPreviousPieceSelected(Piece selectedPiece) {
 | 
					    protected void toDoIfNoPreviousPieceSelected(Piece selectedPiece) {
 | 
				
			||||||
        if (selectedPiece != null && selectedPiece.isWhite() == myColor)
 | 
					        if (selectedPiece != null && selectedPiece.isWhite() == myColor)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,7 @@ public final class SameBoard extends Board {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    protected void makeMove(Move move) {
 | 
					    protected void makeMove(Move move) {
 | 
				
			||||||
 | 
					        moveList.addElement(move);
 | 
				
			||||||
        move.movedPiece.move(pieces, move.to);
 | 
					        move.movedPiece.move(pieces, move.to);
 | 
				
			||||||
        turnCount++;
 | 
					        turnCount++;
 | 
				
			||||||
        whitesTurn = !whitesTurn;
 | 
					        whitesTurn = !whitesTurn;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,7 @@ import java.util.ArrayList;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import javax.imageio.ImageIO;
 | 
					import javax.imageio.ImageIO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.billenius.schack.Schack;
 | 
				
			||||||
import com.billenius.schack.boards.Board;
 | 
					import com.billenius.schack.boards.Board;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public abstract class Piece implements Serializable {
 | 
					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:)
 | 
					     * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public boolean supremeRuler = false;
 | 
					    public boolean supremeRuler = false;
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Bild av pjäsen som ritas ut på bärdet
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    protected BufferedImage icon;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Nödvändigt för rockad
 | 
					     * Nödvändigt för rockad
 | 
				
			||||||
@@ -40,27 +37,12 @@ public abstract class Piece implements Serializable {
 | 
				
			|||||||
    public Piece(boolean white, Point startingPosition) throws IOException {
 | 
					    public Piece(boolean white, Point startingPosition) throws IOException {
 | 
				
			||||||
        this.isWhite = white;
 | 
					        this.isWhite = white;
 | 
				
			||||||
        this.position = startingPosition;
 | 
					        this.position = startingPosition;
 | 
				
			||||||
        setPieceIcon();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Piece(boolean white) {
 | 
					    public Piece(boolean white) {
 | 
				
			||||||
        this.isWhite = 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
 | 
					     * Ger tillbaks alla ställen pjäsen kan gå till
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
@@ -89,8 +71,9 @@ public abstract class Piece implements Serializable {
 | 
				
			|||||||
     * @param g2
 | 
					     * @param g2
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public void draw(Graphics2D g2) {
 | 
					    public void draw(Graphics2D g2) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        g2.drawImage(
 | 
					        g2.drawImage(
 | 
				
			||||||
                icon,
 | 
					                getIcon(),
 | 
				
			||||||
                position.x * Board.SIZE_OF_TILE,
 | 
					                position.x * Board.SIZE_OF_TILE,
 | 
				
			||||||
                position.y * Board.SIZE_OF_TILE,
 | 
					                position.y * Board.SIZE_OF_TILE,
 | 
				
			||||||
                null);
 | 
					                null);
 | 
				
			||||||
@@ -212,6 +195,13 @@ public abstract class Piece implements Serializable {
 | 
				
			|||||||
        return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
 | 
					        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
 | 
					     * @return true ifall pjäsen är vit
 | 
				
			||||||
@@ -230,7 +220,9 @@ public abstract class Piece implements Serializable {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public BufferedImage getIcon() {
 | 
					    public BufferedImage getIcon() {
 | 
				
			||||||
        return this.icon;
 | 
					        String className = this.getClass().getSimpleName();
 | 
				
			||||||
 | 
					        String colorName = this.isWhite() ? "White" : "Black";
 | 
				
			||||||
 | 
					        return Schack.pieceIcons.get(colorName + className);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user