Breaked out code into func that is inhereited

This commit is contained in:
loveb
2022-03-17 09:30:18 +01:00
parent 8a72456334
commit 16afa5dc8c
4 changed files with 91 additions and 349 deletions

View File

@@ -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<Point> validMoves(Piece[][] pieces) {
LinkedHashSet<Point> 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;
}
}