From 7311bea07be146808dbe8668e651cb9d19abce41 Mon Sep 17 00:00:00 2001 From: lov3b Date: Tue, 1 Mar 2022 23:00:38 +0100 Subject: [PATCH] Ritar ut nu iaf + fixat en del med king o piece --- src/schack/Board.java | 46 ++++++++++++++++++++++++++---- src/schack/King.java | 63 +++++++++++++++++++++++++++++++++++++++++- src/schack/Piece.java | 23 +++++++++++---- src/schack/Schack.java | 9 ++---- 4 files changed, 124 insertions(+), 17 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 0da936c..ab98d02 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -4,37 +4,73 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Point; +import java.io.IOException; import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.swing.JPanel; public class Board extends JPanel { + public static final int SCALE = 100; ArrayList pieces = new ArrayList<>(); - public Board() { + public Board() throws IOException { + + this.pieces = initPieces(); setPreferredSize(new Dimension(800, 800)); + } + private ArrayList initPieces() throws IOException { + // White pieces + ArrayList pieces = new ArrayList<>(); + ArrayList whites = (ArrayList) Stream.of( + new King(true, new Point(4, 7)) + ).collect(Collectors.toList()); + + // Black pieces + ArrayList blacks = (ArrayList) Stream.of( + new King(false, new Point(4, 0)) + ).collect(Collectors.toList()); + + pieces.addAll(whites); + pieces.addAll(blacks); + + return pieces; + } + + @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; drawSquares(g2); + pieces.forEach(p -> p.draw(g2)); + + Piece p = pieces.get(1); + LinkedHashSet legal = p.legalMoves(pieces); + g2.setColor(Color.yellow); + legal.forEach(point -> g2.fillOval(point.x * SCALE, point.y * SCALE, SCALE, SCALE)); + + System.out.println(legal.size()); } private void drawSquares(Graphics2D g2) { - g2.scale(100, 100); + g2.setBackground(Color.WHITE); - g2.setColor(Color.BLACK); + g2.setColor(Color.DARK_GRAY); for (int i = 0; i < 8; i += 2) { for (int j = 0; j < 8; j += 2) { - g2.fillRect(i, j, 1, 1); + g2.fillRect(i * SCALE, j * SCALE, 1 * SCALE, 1 * SCALE); } } for (int i = 1; i < 8; i += 2) { for (int j = 1; j < 8; j += 2) { - g2.fillRect(i, j, 1, 1); + g2.fillRect(i * SCALE, j * SCALE, 1 * SCALE, 1 * SCALE); } } diff --git a/src/schack/King.java b/src/schack/King.java index 93fb16b..c4372f9 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -1,8 +1,69 @@ package schack; +import java.awt.Graphics2D; +import java.awt.Point; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import javax.imageio.ImageIO; + public final class King extends Piece { - public boolean isSeen() { + public King(boolean white, Point startingPosition) throws IOException { + super(white, startingPosition); + String colorName = white ? "White" : "Black"; + String fileName = "resized" + colorName + "King.png"; + String path = Paths.get("icons", fileName).toString(); + System.out.println(path); + icon = ImageIO.read(new File(fileName)); + } + + @Override + public void draw(Graphics2D g2) { + super.draw(g2); + } + + public boolean isSeen(ArrayList pieces) { return true; } + + @Override + public LinkedHashSet legalMoves(ArrayList pieces) { + LinkedHashSet unmovable = new LinkedHashSet<>(); + LinkedHashSet perhapsMovable = new LinkedHashSet<>(); + for (Piece piece : pieces) { + Point p = piece.position; + + // Ifall en pjäs står runt omkring kungen går det inte att flytta dit + if (Math.abs(p.x - this.position.x) == 1 + && Math.abs(p.y - this.position.y) == 1) { + unmovable.add(p); + } + + } + + // Lägg till tiles kring kungen + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 3; y++) { + if (y == 1 && x == 1) { + continue; + } // Ifall det är utanför planen, skippa tror inte det funkar + else if (x + this.position.x > 8 + || x + this.position.x < 0 + || y + this.position.y > 8 + || y + this.position.y < 0) { + continue; + } + perhapsMovable.add( + new Point(this.position.x - 1 + x, this.position.y - 1 + y) + ); + } + } + + perhapsMovable.removeAll(unmovable); + return perhapsMovable; + } } diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 73e1430..0307136 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -1,18 +1,31 @@ package schack; +import java.awt.Component; +import java.awt.Graphics2D; import java.awt.Point; +import java.awt.image.BufferedImage; +import java.awt.image.ImageObserver; import java.util.ArrayList; +import java.util.LinkedHashSet; -public class Piece { +public abstract class Piece extends Component { public Point position; + public boolean white; + public boolean castled = false; + protected BufferedImage icon; - public boolean isValidMove(Point p, ArrayList pieces) { - return true; + public Piece(boolean white, Point startingPosition) { + this.white = white; + this.position = startingPosition; } - void draw() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public abstract LinkedHashSet legalMoves(ArrayList pieces); + + public void draw(Graphics2D g2) { + + g2.drawImage(icon, position.x * Board.SCALE, position.y * Board.SCALE, (ImageObserver) this); +// g2.drawImage(icon, 4 * Board.SCALE, 6* Board.SCALE, (ImageObserver) this); } } diff --git a/src/schack/Schack.java b/src/schack/Schack.java index 5ef0712..c51dc9f 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -2,17 +2,14 @@ package schack; import java.awt.Dimension; import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +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; import javax.swing.JMenuItem; import javax.swing.JOptionPane; -import javax.swing.JPopupMenu; /** * @@ -22,7 +19,7 @@ public class Schack extends JFrame { public Dimension size = new Dimension(800, 800); - public Schack() { + public Schack() throws IOException { setTitle("Schack"); setAlwaysOnTop(true); setResizable(false); @@ -70,7 +67,7 @@ public class Schack extends JFrame { } - public static void main(String[] args) { + public static void main(String[] args) throws IOException { new Schack(); }