diff --git a/src/schack/Board.java b/src/schack/Board.java index a2d98fe..f382a32 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -33,7 +33,7 @@ public class Board extends JPanel implements MouseListener { {new Rook(false, new Point(0, 0)), null, null, null, null, null, null, new Rook(true, new Point(0, 7))}, {new Horse(false, true, new Point(1, 0)), null, null, null, null, null, null, new Horse(true, true, new Point(1, 7))}, {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, new Bishop(false, new Point(3, 4)), null, null, new Queen(true, new Point(3, 7))}, + {new Queen(false, new Point(3, 0)), null, null, null, new Queen(false, new Point(3, 4)), 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, new King(false, new Point(5, 7))}, {null, null, null, null, null, null, null, null}, diff --git a/src/schack/DiagonalWalk.java b/src/schack/DiagonalWalk.java deleted file mode 100644 index 0d76dd6..0000000 --- a/src/schack/DiagonalWalk.java +++ /dev/null @@ -1,9 +0,0 @@ -package schack; - -public interface DiagonalWalk { - - default void walDiagonal(Piece[][] pieces){ - - }; - -} diff --git a/src/schack/Queen.java b/src/schack/Queen.java index bf29ce8..1a85088 100644 --- a/src/schack/Queen.java +++ b/src/schack/Queen.java @@ -7,14 +7,217 @@ import java.util.LinkedHashSet; public class Queen extends Piece { public Queen(boolean isWhite, Point point) throws IOException { - super(isWhite,point); + super(isWhite, point); setPieceIcon("Queen"); } @Override public LinkedHashSet validMoves(Piece[][] pieces) { - return new LinkedHashSet<>(); - } + LinkedHashSet movable = new LinkedHashSet<>(); - + // Upp vänster + for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) { + + Point pos = new Point(bishopX, bishopY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + + // Upp höger + for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) { + + Point pos = new Point(bishopX, bishopY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + // Ner höger + for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) { + + Point pos = new Point(bishopX, bishopY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + // Ner vänster + for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) { + + Point pos = new Point(bishopX, bishopY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + }// Vänster + for (int rookX = this.position.x - 1; rookX >= 0; rookX--) { + + Point pos = new Point(rookX, this.position.y); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + + // Höger + for (int rookX = this.position.x + 1; rookX <= 7; rookX++) { + + Point pos = new Point(rookX, this.position.y); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + + // Ner + for (int rookY = this.position.y + 1; rookY <= 7; rookY++) { + + Point pos = new Point(this.position.x, rookY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + + // Upp + for (int rookY = this.position.y - 1; rookY >= 0; rookY--) { + + Point pos = new Point(this.position.x, rookY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + System.out.println(p); + // If this piece is the same team as ours, skip + if (p.isWhite == this.isWhite) { + break; + } + + movable.add(pos); + break; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + + } + return movable; + } }