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

@ -19,108 +19,42 @@ public class Bishop extends Piece {
// Upp vänster // Upp vänster
for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) { 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);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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 // Upp höger
for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) { for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) {
boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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 // Ner höger
for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) { for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) {
boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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 // Ner vänster
for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) { for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) {
boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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; return movable;
} }
@Override @Override
public String toString() { public String toString() {
return "Bishop{" + "position=" + position + ", isWhite=" + isWhite + '}'; return "Bishop{" + "position=" + position + ", isWhite=" + isWhite + '}';

View File

@ -51,21 +51,41 @@ public abstract class Piece extends Component {
public void move(Piece[][] pieces, Point toMove, Point selected) { public void move(Piece[][] pieces, Point toMove, Point selected) {
try { try {
System.out.println("toMove: " + toMove);
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove)); pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
pieces[selected.x][selected.y] = null; 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); this.position = new Point(toMove);
Board.printPieces(pieces); Board.printPieces(pieces);
} catch (Exception e) { } 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 @Override
public String toString() { public String toString() {
return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}'; return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}';

View File

@ -15,209 +15,71 @@ public class Queen extends Piece {
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
// Upp vänster // Vänster
for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) { for (int rookX = this.position.x - 1; rookX >= 0; rookX--) {
boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; break;
} }
}
movable.add(pos); // Höger
for (int rookX = this.position.x + 1; rookX <= 7; rookX++) {
boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces);
if (shouldBreak) {
break; break;
}
}
} catch (NullPointerException npe) { // Ner
// This is an empty spot for (int rookY = this.position.y + 1; rookY <= 7; rookY++) {
movable.add(pos); boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces);
} catch (Exception e) { if (shouldBreak) {
// This means that the player is at the edge 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 // Upp höger
for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) { for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) {
boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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 // Ner höger
for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) { for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) {
boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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 // Ner vänster
for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) { for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) {
boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces);
Point pos = new Point(bishopX, bishopY); if (shouldBreak) {
// 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; 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; return movable;
} }
} }

View File

@ -21,114 +21,40 @@ public class Rook extends Piece {
// Vänster // Vänster
for (int rookX = this.position.x - 1; rookX >= 0; rookX--) { for (int rookX = this.position.x - 1; rookX >= 0; rookX--) {
boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces);
Point pos = new Point(rookX,this.position.y); if (shouldBreak) {
// 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; 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 // Höger
for (int rookX = this.position.x + 1; rookX <= 7; rookX++) { for (int rookX = this.position.x + 1; rookX <= 7; rookX++) {
boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces);
Point pos = new Point(rookX,this.position.y); if (shouldBreak) {
// 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; 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 // Ner
for (int rookY = this.position.y + 1; rookY <= 7; rookY++) { for (int rookY = this.position.y + 1; rookY <= 7; rookY++) {
boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces);
Point pos = new Point(this.position.x,rookY); if (shouldBreak) {
// 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; 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 // Upp
for (int rookY = this.position.y - 1; rookY >= 0; rookY--) { for (int rookY = this.position.y - 1; rookY >= 0; rookY--) {
boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces);
Point pos = new Point(this.position.x,rookY); if (shouldBreak) {
// 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; 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
}
} }
System.out.println("Len of movable: " + movable.size()); System.out.println("Len of movable: " + movable.size());
return movable; return movable;
} }
} }