mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-03 22:50:24 +01:00 
			
		
		
		
	Merge branch 'finals'
This commit is contained in:
		@@ -103,22 +103,22 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    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);
 | 
			
		||||
        final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
 | 
			
		||||
        final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
 | 
			
		||||
        final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY);
 | 
			
		||||
 | 
			
		||||
        // Ifall vi har tryckt på en pjäs och sedan ska gå dit
 | 
			
		||||
        if (validMovesToDraw.contains(clicked)) {
 | 
			
		||||
        if (validMovesToDraw.contains(clickedCoordinate)) {
 | 
			
		||||
            final Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
 | 
			
		||||
            if (selectedPiece == null) {
 | 
			
		||||
                validMovesToDraw.clear();
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            selectedPiece.move(pieces, clicked);
 | 
			
		||||
            selectedPiece.move(pieces, clickedCoordinate);
 | 
			
		||||
            turnCount++;
 | 
			
		||||
            whitesTurn = !whitesTurn;
 | 
			
		||||
 | 
			
		||||
            ArrayList<Point> allValidMoves = new ArrayList<>();
 | 
			
		||||
            final ArrayList<Point> allValidMoves = new ArrayList<>();
 | 
			
		||||
            for (Piece[] pieceArr : pieces) {
 | 
			
		||||
                for (Piece piece : pieceArr) {
 | 
			
		||||
                    if (piece == null || whitesTurn != piece.isWhite()) {
 | 
			
		||||
@@ -132,7 +132,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
 | 
			
		||||
            ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
 | 
			
		||||
 | 
			
		||||
            boolean weCanMove = !allValidMoves.isEmpty();
 | 
			
		||||
            final boolean weCanMove = !allValidMoves.isEmpty();
 | 
			
		||||
            boolean inSchack = false;
 | 
			
		||||
 | 
			
		||||
            for (Point attack : opposingAttacks) {
 | 
			
		||||
@@ -147,7 +147,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
                    if (weCanMove) {
 | 
			
		||||
                        JOptionPane.showMessageDialog(this, "Du står i schack");
 | 
			
		||||
                    } else {
 | 
			
		||||
                        int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
 | 
			
		||||
                        final int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
 | 
			
		||||
                        if (choise == JOptionPane.YES_OPTION) {
 | 
			
		||||
                            try {
 | 
			
		||||
                                restartGame();
 | 
			
		||||
@@ -164,12 +164,12 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
            previouslyClickedPoint = new Point(clicked);
 | 
			
		||||
            previouslyClickedPoint = new Point(clickedCoordinate);
 | 
			
		||||
            validMovesToDraw.clear();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Om vi inte redan har valt en pjäs klickar vi på en pjäs
 | 
			
		||||
        if (!validMovesToDraw.contains(clicked)) {
 | 
			
		||||
        if (!validMovesToDraw.contains(clickedCoordinate)) {
 | 
			
		||||
 | 
			
		||||
            final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
 | 
			
		||||
 | 
			
		||||
@@ -186,7 +186,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ArrayList<Point> checkAttacks(boolean preferedColor) {
 | 
			
		||||
        ArrayList attacks = new ArrayList();
 | 
			
		||||
        final ArrayList attacks = new ArrayList();
 | 
			
		||||
        for (Piece[] pieceArr : pieces) {
 | 
			
		||||
            for (Piece piece : pieceArr) {
 | 
			
		||||
                // Ifall det är tomrum skippa
 | 
			
		||||
 
 | 
			
		||||
@@ -12,13 +12,13 @@ public class Horse extends Piece {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
        final ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (int dx : new int[]{-2, -1, 1, 2}) {
 | 
			
		||||
            for (int direction : new int[]{-1, 1}) {
 | 
			
		||||
                int stepLength = (3 - Math.abs(dx));
 | 
			
		||||
                int dy = direction * stepLength;
 | 
			
		||||
                Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
 | 
			
		||||
                final int stepLength = (3 - Math.abs(dx)),
 | 
			
		||||
                        dy = direction * stepLength;
 | 
			
		||||
                final Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
 | 
			
		||||
                addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,6 @@ package schack;
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.stream.Stream;
 | 
			
		||||
 | 
			
		||||
public final class King extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
@@ -20,7 +18,7 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) {
 | 
			
		||||
        ArrayList<Point> possibleCastling = new ArrayList<>();
 | 
			
		||||
        final ArrayList<Point> possibleCastling = new ArrayList<>();
 | 
			
		||||
        if (this.isMoved()) {
 | 
			
		||||
            return possibleCastling;
 | 
			
		||||
        }
 | 
			
		||||
@@ -85,8 +83,8 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
     * @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll
 | 
			
		||||
     */
 | 
			
		||||
    private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) {
 | 
			
		||||
        Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
 | 
			
		||||
        Piece king = this;
 | 
			
		||||
        final Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
 | 
			
		||||
        final Piece king = this;
 | 
			
		||||
 | 
			
		||||
        // Null där de stod
 | 
			
		||||
        pieces[king.position.x][king.position.y] = null;
 | 
			
		||||
 
 | 
			
		||||
@@ -19,14 +19,14 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
        final ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (int[] xy : directions) {
 | 
			
		||||
            int loopX = this.position.x, loopY = this.position.y;
 | 
			
		||||
            while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) {
 | 
			
		||||
                loopX += xy[0];
 | 
			
		||||
                loopY += xy[1];
 | 
			
		||||
                boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse);
 | 
			
		||||
                final boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse);
 | 
			
		||||
                if (shouldBreak) {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,7 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
        // Kolla om man kan gå rakt frak
 | 
			
		||||
        for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
 | 
			
		||||
            Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
 | 
			
		||||
            final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
 | 
			
		||||
            boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse);
 | 
			
		||||
            if (shouldBreak) {
 | 
			
		||||
                break;
 | 
			
		||||
@@ -58,7 +58,7 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
        // Kolla ifall vi kan ta någon
 | 
			
		||||
        for (int pawnX : new int[]{-1, 1}) {
 | 
			
		||||
            // Position vi kollar just nu, snett upp åt höger & vänster
 | 
			
		||||
            Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
 | 
			
		||||
            final Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
 | 
			
		||||
            movable.addAll(addAttackMovesIfCan(pos, pieces));
 | 
			
		||||
        }
 | 
			
		||||
        return movable;
 | 
			
		||||
@@ -80,9 +80,9 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
            return movable;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Piece piece = pieces[pos.x][pos.y];
 | 
			
		||||
        final Piece potentialEnemy = pieces[pos.x][pos.y];
 | 
			
		||||
        // Ifall det är tomt här, gör ingenting
 | 
			
		||||
        if (piece != null && piece.isWhite() != this.isWhite()) {
 | 
			
		||||
        if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) {
 | 
			
		||||
            if (!isInSchack(pieces, pos)) {
 | 
			
		||||
                movable.add(pos);
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -27,13 +27,13 @@ public abstract class Piece {
 | 
			
		||||
     * Bild av pjäsen som ritas ut på bärdet
 | 
			
		||||
     */
 | 
			
		||||
    protected BufferedImage icon;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public Piece(boolean white, Point startingPosition) throws IOException {
 | 
			
		||||
        this.isWhite = white;
 | 
			
		||||
        this.position = startingPosition;
 | 
			
		||||
        setPieceIcon();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public Piece(boolean white) {
 | 
			
		||||
        this.isWhite = white;
 | 
			
		||||
    }
 | 
			
		||||
@@ -45,9 +45,9 @@ public abstract class Piece {
 | 
			
		||||
     * @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";
 | 
			
		||||
        final String className = this.getClass().getSimpleName();
 | 
			
		||||
        final String colorName = this.isWhite() ? "White" : "Black";
 | 
			
		||||
        final String fileName = colorName + className + ".png";
 | 
			
		||||
        InputStream is = getClass().getResourceAsStream("/img/" + fileName);
 | 
			
		||||
        icon = ImageIO.read(is);
 | 
			
		||||
    }
 | 
			
		||||
@@ -98,7 +98,7 @@ public abstract class Piece {
 | 
			
		||||
        if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        pieces[toMove.x][toMove.y] = this;
 | 
			
		||||
        pieces[position.x][position.y] = null;
 | 
			
		||||
        this.position = new Point(toMove);
 | 
			
		||||
@@ -118,8 +118,8 @@ public abstract class Piece {
 | 
			
		||||
        if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        Piece pieceToCheck = pieces[pos.x][pos.y];
 | 
			
		||||
 | 
			
		||||
        final Piece pieceToCheck = pieces[pos.x][pos.y];
 | 
			
		||||
 | 
			
		||||
        // Detta är en tom plats
 | 
			
		||||
        if (pieceToCheck == null) {
 | 
			
		||||
@@ -139,7 +139,7 @@ public abstract class Piece {
 | 
			
		||||
            movable.add(pos);
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -151,23 +151,23 @@ public abstract class Piece {
 | 
			
		||||
     */
 | 
			
		||||
    protected boolean isInSchack(Piece[][] pieces, Point pos) {
 | 
			
		||||
        // Kom ihåg vart vi var
 | 
			
		||||
        Point previousPosition = new Point(this.position);
 | 
			
		||||
        final Point previousPosition = new Point(this.position);
 | 
			
		||||
 | 
			
		||||
        // Kom ihåg motståndarpjäs
 | 
			
		||||
        Piece guyThatsAlreadyHere = pieces[pos.x][pos.y];
 | 
			
		||||
        final Piece guyThatsAlreadyHere = pieces[pos.x][pos.y];
 | 
			
		||||
 | 
			
		||||
        // Testa att flytta
 | 
			
		||||
        pieces[pos.x][pos.y] = this;
 | 
			
		||||
        pieces[previousPosition.x][previousPosition.y] = null;
 | 
			
		||||
        this.position = pos;
 | 
			
		||||
        
 | 
			
		||||
        boolean inSchack = isInSchack(pieces);
 | 
			
		||||
 | 
			
		||||
        final boolean inSchack = isInSchack(pieces);
 | 
			
		||||
 | 
			
		||||
        // Flytta tillbaka
 | 
			
		||||
        pieces[previousPosition.x][previousPosition.y] = this;
 | 
			
		||||
        pieces[pos.x][pos.y] = guyThatsAlreadyHere;
 | 
			
		||||
        this.position = previousPosition;
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        return inSchack;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -192,18 +192,17 @@ public abstract class Piece {
 | 
			
		||||
 | 
			
		||||
        // Kollar ifall kungen står i schack just nu
 | 
			
		||||
        for (Point enemyAttack : enemyAttacks) {
 | 
			
		||||
            Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
 | 
			
		||||
            final Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
 | 
			
		||||
            if (attackedPiece != null && attackedPiece.supremeRuler) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
 | 
			
		||||
//        return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -222,5 +221,5 @@ public abstract class Piece {
 | 
			
		||||
    public boolean isMoved() {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,7 @@ import javax.swing.UIManager;
 | 
			
		||||
 */
 | 
			
		||||
public class Schack {
 | 
			
		||||
 | 
			
		||||
    JFrame frame;
 | 
			
		||||
    final JFrame frame;
 | 
			
		||||
 | 
			
		||||
    public Schack() throws IOException {
 | 
			
		||||
        // Set theme
 | 
			
		||||
@@ -42,18 +42,18 @@ public class Schack {
 | 
			
		||||
        frame.setResizable(false);
 | 
			
		||||
 | 
			
		||||
        //  Might throw an IOException if the icon of the Pieces isn't embedded correctly
 | 
			
		||||
        Board board = new Board();
 | 
			
		||||
        final Board board = new Board();
 | 
			
		||||
        frame.setContentPane(board);
 | 
			
		||||
        frame.getContentPane().addMouseListener(board);
 | 
			
		||||
 | 
			
		||||
        // Create menu        
 | 
			
		||||
        JMenuBar menuBar = new JMenuBar();
 | 
			
		||||
        JMenu gameMenu = new JMenu("Game");
 | 
			
		||||
        JMenu connectMenu = new JMenu("Connect");
 | 
			
		||||
        JMenuItem connectToOpponent = new JMenuItem("Connect to opponent");
 | 
			
		||||
        JMenuItem showLocalIP = new JMenuItem("Show IP");
 | 
			
		||||
        JMenuItem askForRemi = new JMenuItem("Ask for remi");
 | 
			
		||||
        JMenuItem surrender = new JMenuItem("Surrender");
 | 
			
		||||
        final JMenuBar menuBar = new JMenuBar();
 | 
			
		||||
        final JMenu gameMenu = new JMenu("Game");
 | 
			
		||||
        final JMenu connectMenu = new JMenu("Connect");
 | 
			
		||||
        final JMenuItem connectToOpponent = new JMenuItem("Connect to opponent");
 | 
			
		||||
        final JMenuItem showLocalIP = new JMenuItem("Show IP");
 | 
			
		||||
        final JMenuItem askForRemi = new JMenuItem("Ask for remi");
 | 
			
		||||
        final JMenuItem surrender = new JMenuItem("Surrender");
 | 
			
		||||
 | 
			
		||||
        // Actions
 | 
			
		||||
        connectToOpponent.addActionListener((ActionEvent ae) -> {
 | 
			
		||||
@@ -83,7 +83,7 @@ public class Schack {
 | 
			
		||||
        });
 | 
			
		||||
        surrender.addActionListener((ActionEvent ae) -> {
 | 
			
		||||
            String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart";
 | 
			
		||||
            int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?");
 | 
			
		||||
            final int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?");
 | 
			
		||||
            if (choice == JOptionPane.YES_OPTION) {
 | 
			
		||||
                try {
 | 
			
		||||
                    board.restartGame();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user