mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
Breaked out code into func that is inhereited
This commit is contained in:
parent
8a72456334
commit
16afa5dc8c
@ -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 + '}';
|
||||
|
@ -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 + '}';
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user