mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-04 07:00:21 +01:00 
			
		
		
		
	Lektion 3
Nu kan man gå med kung
This commit is contained in:
		@@ -56,10 +56,6 @@ public class Bishop extends Piece {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void move(Piece[][] pieces) {
 | 
			
		||||
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
 
 | 
			
		||||
@@ -5,15 +5,20 @@ import java.awt.Dimension;
 | 
			
		||||
import java.awt.Graphics;
 | 
			
		||||
import java.awt.Graphics2D;
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.awt.event.MouseEvent;
 | 
			
		||||
import java.awt.event.MouseListener;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
 | 
			
		||||
public class Board extends JPanel {
 | 
			
		||||
public class Board extends JPanel implements MouseListener {
 | 
			
		||||
 | 
			
		||||
    public static final int SIZE_OF_TILE = 100;
 | 
			
		||||
    private Piece[][] pieces = new Piece[8][8];
 | 
			
		||||
    private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
 | 
			
		||||
    private Point selectedPiece = new Point();
 | 
			
		||||
    private Color moveableColor = new Color(200, 200, 200);
 | 
			
		||||
 | 
			
		||||
    public Board() throws IOException {
 | 
			
		||||
 | 
			
		||||
@@ -30,7 +35,7 @@ public class Board extends JPanel {
 | 
			
		||||
            {new Bishop(false, new Point(2, 0)), null, null, null, null, null, null, new Bishop(true, new Point(2, 7))},
 | 
			
		||||
            {new Queen(false, new Point(3, 0)), null, null, null, null, null, null, new Queen(true, new Point(3, 7))},
 | 
			
		||||
            {new King(false), null, null, null, null, null, null, new King(true)},
 | 
			
		||||
            {null, null, null, null, null, null, null, null},
 | 
			
		||||
            {null, null, null, null, null, null, null, new King(false, new Point(5, 7))},
 | 
			
		||||
            {null, null, null, null, null, null, null, null},
 | 
			
		||||
            {null, null, null, null, null, null, null, null}
 | 
			
		||||
        };
 | 
			
		||||
@@ -70,41 +75,48 @@ public class Board extends JPanel {
 | 
			
		||||
        Graphics2D g2 = (Graphics2D) g;
 | 
			
		||||
        drawSquares(g2);
 | 
			
		||||
 | 
			
		||||
        // måla alla ställen man kan gå¨till
 | 
			
		||||
        validMovesToDraw.forEach(point -> {
 | 
			
		||||
            if (point != null) {
 | 
			
		||||
                g2.setColor(moveableColor);
 | 
			
		||||
                g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        // Draw piece
 | 
			
		||||
        Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
 | 
			
		||||
            if (piece != null) {
 | 
			
		||||
                piece.draw(g2);
 | 
			
		||||
            }
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        // Check valid moves method
 | 
			
		||||
//        Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
 | 
			
		||||
//            if (piece != null) {
 | 
			
		||||
//                // Draw eglible moves
 | 
			
		||||
//                LinkedHashSet<Point> validMoves = piece.validMoves(pieces);
 | 
			
		||||
//                Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random()));
 | 
			
		||||
        //        Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
 | 
			
		||||
        //            if (piece != null) {
 | 
			
		||||
        //                // Draw eglible moves
 | 
			
		||||
        //                LinkedHashSet<Point> validMoves = piece.validMoves(pieces);
 | 
			
		||||
        //                Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random()));
 | 
			
		||||
        //                g2.setColor(c);
 | 
			
		||||
        //                validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
 | 
			
		||||
        //                System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size());
 | 
			
		||||
        //            }
 | 
			
		||||
        //        }));
 | 
			
		||||
 | 
			
		||||
        // draw valid moves
 | 
			
		||||
//        for (int y = 0; y < pieces.length; y++) {
 | 
			
		||||
//            for (int x = 0; x < pieces.length; x++) {
 | 
			
		||||
//                Piece p = pieces[y][x];
 | 
			
		||||
//                if (p == null) {
 | 
			
		||||
//                    continue;
 | 
			
		||||
//                }
 | 
			
		||||
//
 | 
			
		||||
//                LinkedHashSet<Point> validMoves = p.validMoves(pieces);
 | 
			
		||||
//                Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
 | 
			
		||||
//                g2.setColor(c);
 | 
			
		||||
//                validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
 | 
			
		||||
//                System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size());
 | 
			
		||||
//            }
 | 
			
		||||
//        }));
 | 
			
		||||
        // Check valid moves method
 | 
			
		||||
        for (int y = 0; y < pieces.length; y++) {
 | 
			
		||||
            for (int x = 0; x < pieces.length; x++) {
 | 
			
		||||
                Piece p = pieces[y][x];
 | 
			
		||||
                if (p == null) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                LinkedHashSet<Point> validMoves = p.validMoves(pieces);
 | 
			
		||||
                Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
 | 
			
		||||
                g2.setColor(c);
 | 
			
		||||
                validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
//        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void printPieces(Piece[][] pieces) {
 | 
			
		||||
    public static void printPieces(Piece[][] pieces) {
 | 
			
		||||
        System.out.println("");
 | 
			
		||||
        for (int loopX = 0; loopX < pieces.length; loopX++) {
 | 
			
		||||
            for (int loopY = 0; loopY < pieces.length; loopY++) {
 | 
			
		||||
@@ -149,4 +161,60 @@ public class Board extends JPanel {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void mouseClicked(MouseEvent mouseEvent) {
 | 
			
		||||
        int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
 | 
			
		||||
        int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
 | 
			
		||||
 | 
			
		||||
        Point clicked = new Point(mouseCoordinateX, mouseCoordinateY);
 | 
			
		||||
 | 
			
		||||
        // Ifall vi har tryckt på en pjäs och sedan ska gå dit
 | 
			
		||||
        if (validMovesToDraw.contains(clicked)) {
 | 
			
		||||
            try {
 | 
			
		||||
                Piece p = pieces[selectedPiece.x][selectedPiece.y];
 | 
			
		||||
                p.move(pieces, clicked, selectedPiece);
 | 
			
		||||
                validMovesToDraw.clear();
 | 
			
		||||
                System.out.println("came here");
 | 
			
		||||
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
 | 
			
		||||
            selectedPiece = new Point(clicked);
 | 
			
		||||
            validMovesToDraw.clear();
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        System.out.println("X: " + mouseCoordinateX + ", Y: " + mouseCoordinateY);
 | 
			
		||||
        try {
 | 
			
		||||
            Piece p = pieces[mouseCoordinateX][mouseCoordinateY];
 | 
			
		||||
            LinkedHashSet validMoves = p.validMoves(pieces);
 | 
			
		||||
            System.out.println("valid moves " + validMoves);
 | 
			
		||||
            moveableColor = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
 | 
			
		||||
            validMovesToDraw.addAll(validMoves);
 | 
			
		||||
            System.out.println("valid moves to draw " + validMovesToDraw);
 | 
			
		||||
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            validMovesToDraw.clear();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void mousePressed(MouseEvent e) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void mouseReleased(MouseEvent e) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void mouseEntered(MouseEvent e) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void mouseExited(MouseEvent e) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								src/schack/Horse.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/schack/Horse.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
 | 
			
		||||
public class Horse extends Piece {
 | 
			
		||||
 | 
			
		||||
    public Horse(boolean isWhite, boolean isLeft, Point startingPosition) throws IOException {
 | 
			
		||||
        super(isWhite, startingPosition);
 | 
			
		||||
        setPieceIcon("Horse");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
 | 
			
		||||
        return new LinkedHashSet<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
   
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -13,10 +13,7 @@ public final class King extends Piece {
 | 
			
		||||
 | 
			
		||||
    public King(boolean isWhite, Point startingPosition) throws IOException {
 | 
			
		||||
        super(isWhite, startingPosition);
 | 
			
		||||
        String colorName = isWhite ? "White" : "Black";
 | 
			
		||||
        String fileName = colorName + "King.png";
 | 
			
		||||
        InputStream is = King.class.getResourceAsStream("../img/" + fileName);
 | 
			
		||||
        icon = ImageIO.read(is);
 | 
			
		||||
        setPieceIcon("King");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public King(boolean isWhite) throws IOException {
 | 
			
		||||
@@ -64,12 +61,6 @@ public final class King extends Piece {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void move(Piece[][] pieces) {
 | 
			
		||||
        eglibleForCastling = false;
 | 
			
		||||
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
     @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return "Piece{" + "eglibleForCastling=" + eglibleForCastling + "position=" + position + ", isWhite=" + isWhite + '}';
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -22,10 +22,6 @@ public class Pawn extends Piece {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void move(Piece[][] pieces) {
 | 
			
		||||
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
 | 
			
		||||
    }
 | 
			
		||||
@Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return "Pawn{" + "position=" + position + ", isWhite=" + isWhite + '}';
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ public abstract class Piece extends Component {
 | 
			
		||||
    public boolean isWhite;
 | 
			
		||||
    protected BufferedImage icon;
 | 
			
		||||
 | 
			
		||||
    public Piece(boolean isWhite, Point startingPosition) {
 | 
			
		||||
    public Piece(boolean isWhite, Point startingPosition) throws IOException {
 | 
			
		||||
        this.isWhite = isWhite;
 | 
			
		||||
        this.position = startingPosition;
 | 
			
		||||
    }
 | 
			
		||||
@@ -48,7 +48,23 @@ public abstract class Piece extends Component {
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public abstract void move(Piece[][] pieces);
 | 
			
		||||
    public void move(Piece[][] pieces, Point toMove, Point selected) {
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            System.out.println("toMove: " + toMove);
 | 
			
		||||
 | 
			
		||||
            pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
 | 
			
		||||
            pieces[selected.x][selected.y] = null;
 | 
			
		||||
            // varför funkar det nu? det borde inte funka nu.
 | 
			
		||||
            System.out.println("equals: " + selected.equals(this.position));
 | 
			
		||||
 | 
			
		||||
            this.position = new Point(toMove);
 | 
			
		||||
            Board.printPieces(pieces);
 | 
			
		||||
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            System.out.println("jmgfmhyfhm");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								src/schack/Queen.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/schack/Queen.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
 | 
			
		||||
public class Queen extends Piece {
 | 
			
		||||
 | 
			
		||||
    public Queen(boolean isWhite, Point point) throws IOException {
 | 
			
		||||
        super(isWhite,point);
 | 
			
		||||
        setPieceIcon("Queen");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
 | 
			
		||||
        return new LinkedHashSet<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
   
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								src/schack/Rook.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/schack/Rook.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
 | 
			
		||||
public class Rook extends Piece {
 | 
			
		||||
 | 
			
		||||
    public Rook(boolean isWhite, Point startingPosition) throws IOException {
 | 
			
		||||
        super(isWhite, startingPosition);
 | 
			
		||||
        setPieceIcon("Rook");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
 | 
			
		||||
        return new LinkedHashSet<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
@@ -1,10 +1,11 @@
 | 
			
		||||
package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Dimension;
 | 
			
		||||
import java.awt.event.ActionEvent;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.InetAddress;
 | 
			
		||||
import java.net.UnknownHostException;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
import javax.swing.JFrame;
 | 
			
		||||
import javax.swing.JMenu;
 | 
			
		||||
import javax.swing.JMenuBar;
 | 
			
		||||
@@ -15,13 +16,16 @@ import javax.swing.JOptionPane;
 | 
			
		||||
 *
 | 
			
		||||
 * @author Love Billenius & Simon Hansson
 | 
			
		||||
 */
 | 
			
		||||
public class Schack extends JFrame {
 | 
			
		||||
public class Schack extends JFrame implements Runnable {
 | 
			
		||||
 | 
			
		||||
    public Schack() throws IOException {
 | 
			
		||||
        setTitle("Schack");
 | 
			
		||||
        setAlwaysOnTop(false);
 | 
			
		||||
        setResizable(false);
 | 
			
		||||
        setContentPane(new Board());
 | 
			
		||||
        Board board = new Board();
 | 
			
		||||
        setContentPane(board);
 | 
			
		||||
        getContentPane().addMouseListener(board);
 | 
			
		||||
        Thread thread = new Thread(this);
 | 
			
		||||
 | 
			
		||||
        // Create menu        
 | 
			
		||||
        JMenuBar menuBar = new JMenuBar();
 | 
			
		||||
@@ -62,6 +66,7 @@ public class Schack extends JFrame {
 | 
			
		||||
        pack();
 | 
			
		||||
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 | 
			
		||||
        setVisible(true);
 | 
			
		||||
        this.run();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -70,4 +75,18 @@ public class Schack extends JFrame {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void run() {
 | 
			
		||||
 | 
			
		||||
        while (true) {
 | 
			
		||||
            try {
 | 
			
		||||
                Thread.sleep(12);
 | 
			
		||||
            } catch (InterruptedException ex) {
 | 
			
		||||
                Logger.getLogger(Schack.class.getName()).log(Level.SEVERE, null, ex);
 | 
			
		||||
            }
 | 
			
		||||
            repaint();
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user