Ritar ut nu iaf + fixat en del med king o piece

This commit is contained in:
lov3b 2022-03-01 23:00:38 +01:00
parent 623310de3e
commit 7311bea07b
4 changed files with 124 additions and 17 deletions

View File

@ -4,37 +4,73 @@ import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.JPanel; import javax.swing.JPanel;
public class Board extends JPanel { public class Board extends JPanel {
public static final int SCALE = 100;
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
public Board() { public Board() throws IOException {
this.pieces = initPieces();
setPreferredSize(new Dimension(800, 800)); setPreferredSize(new Dimension(800, 800));
} }
private ArrayList<Piece> initPieces() throws IOException {
// White pieces
ArrayList<Piece> pieces = new ArrayList<>();
ArrayList<Piece> whites = (ArrayList) Stream.of(
new King(true, new Point(4, 7))
).collect(Collectors.toList());
// Black pieces
ArrayList<Piece> 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) { public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
drawSquares(g2); drawSquares(g2);
pieces.forEach(p -> p.draw(g2));
Piece p = pieces.get(1);
LinkedHashSet<Point> 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) { private void drawSquares(Graphics2D g2) {
g2.scale(100, 100);
g2.setBackground(Color.WHITE); g2.setBackground(Color.WHITE);
g2.setColor(Color.BLACK); g2.setColor(Color.DARK_GRAY);
for (int i = 0; i < 8; i += 2) { for (int i = 0; i < 8; i += 2) {
for (int j = 0; j < 8; j += 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 i = 1; i < 8; i += 2) {
for (int j = 1; j < 8; j += 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);
} }
} }

View File

@ -1,8 +1,69 @@
package schack; 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 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<Piece> pieces) {
return true; return true;
} }
@Override
public LinkedHashSet<Point> legalMoves(ArrayList<Piece> pieces) {
LinkedHashSet<Point> unmovable = new LinkedHashSet<>();
LinkedHashSet<Point> 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;
}
} }

View File

@ -1,18 +1,31 @@
package schack; package schack;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
public class Piece { public abstract class Piece extends Component {
public Point position; public Point position;
public boolean white;
public boolean castled = false;
protected BufferedImage icon;
public boolean isValidMove(Point p, ArrayList<Piece> pieces) { public Piece(boolean white, Point startingPosition) {
return true; this.white = white;
this.position = startingPosition;
} }
void draw() { public abstract LinkedHashSet<Point> legalMoves(ArrayList<Piece> pieces);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
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);
} }
} }

View File

@ -2,17 +2,14 @@ package schack;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JOptionPane; 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 Dimension size = new Dimension(800, 800);
public Schack() { public Schack() throws IOException {
setTitle("Schack"); setTitle("Schack");
setAlwaysOnTop(true); setAlwaysOnTop(true);
setResizable(false); 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(); new Schack();
} }