From 16afa5dc8ca32422ff1b350a50e1244b173cf22c Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 17 Mar 2022 09:30:18 +0100 Subject: [PATCH] Breaked out code into func that is inhereited --- src/schack/Bishop.java | 86 ++-------------- src/schack/Piece.java | 32 ++++-- src/schack/Queen.java | 220 ++++++++--------------------------------- src/schack/Rook.java | 102 +++---------------- 4 files changed, 91 insertions(+), 349 deletions(-) diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java index eccabc7..5febff7 100644 --- a/src/schack/Bishop.java +++ b/src/schack/Bishop.java @@ -19,108 +19,42 @@ public class Bishop extends Piece { // 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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; } + + @Override public String toString() { return "Bishop{" + "position=" + position + ", isWhite=" + isWhite + '}'; diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 82ec095..2c37720 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -51,21 +51,41 @@ public abstract class Piece extends Component { 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"); + } } + protected boolean checkMove(int x, int y, LinkedHashSet movable, Piece[][] pieces) { + Point pos = new Point(x, 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) { + return true; + } + + movable.add(pos); + return true; + + } catch (NullPointerException npe) { + // This is an empty spot + movable.add(pos); + } catch (Exception e) { + // This means that the player is at the edge + } + return false; + + } + @Override public String toString() { return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}'; diff --git a/src/schack/Queen.java b/src/schack/Queen.java index ccc6a17..5bedd20 100644 --- a/src/schack/Queen.java +++ b/src/schack/Queen.java @@ -9,215 +9,77 @@ public class Queen extends Piece { public Queen(boolean isWhite, Point point) throws IOException { super(isWhite, point); setPieceIcon("Queen"); - } + } @Override public LinkedHashSet validMoves(Piece[][] pieces) { 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); + // Vänster + for (int rookX = this.position.x - 1; rookX >= 0; rookX--) { + boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + if (shouldBreak) { 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++) { + boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + if (shouldBreak) { + break; + } + } + + // Ner + for (int rookY = this.position.y + 1; rookY <= 7; rookY++) { + boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + if (shouldBreak) { + break; + } + } + + // Upp + for (int rookY = this.position.y - 1; rookY >= 0; rookY--) { + boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + if (shouldBreak) { + break; + } + } + // Upp vänster + for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) { + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { + break; } } // 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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); + boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + if (shouldBreak) { 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; } } diff --git a/src/schack/Rook.java b/src/schack/Rook.java index ee86894..41ce68e 100644 --- a/src/schack/Rook.java +++ b/src/schack/Rook.java @@ -20,115 +20,41 @@ public class Rook extends Piece { //men jag är trög och har spenderat alldles förmycket tid på att vara trög :^) // 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); + for (int rookX = this.position.x - 1; rookX >= 0; rookX--) { + boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + if (shouldBreak) { 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); + for (int rookX = this.position.x + 1; rookX <= 7; rookX++) { + boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + if (shouldBreak) { 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); + for (int rookY = this.position.y + 1; rookY <= 7; rookY++) { + boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + if (shouldBreak) { 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); + for (int rookY = this.position.y - 1; rookY >= 0; rookY--) { + boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + if (shouldBreak) { break; - - } catch (NullPointerException npe) { - // This is an empty spot - movable.add(pos); - } catch (Exception e) { - // This means that the player is at the edge } - } System.out.println("Len of movable: " + movable.size()); return movable; } + + }