mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
Bishop validmoves funkar
This commit is contained in:
parent
06f37e4097
commit
ff54c28a81
@ -15,47 +15,111 @@ public class Bishop extends Piece {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||||
LinkedHashSet<Point> unmovable = new LinkedHashSet<>();
|
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||||
LinkedHashSet<Point> perhapsMovable = new LinkedHashSet<>();
|
|
||||||
|
|
||||||
// // Ner höger
|
// Upp vänster
|
||||||
// for (int x = 0, y = 0; x < 8 && x > -8; x++, y++) {
|
for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) {
|
||||||
// System.out.println("x: " + x + ", y: " + y);
|
|
||||||
// }
|
Point pos = new Point(bishopX, bishopY);
|
||||||
// // Upp höger
|
|
||||||
// for (int x = 0, y = 0; x < 8 && x > -8; x++, y--) {
|
// Instead of checking index and null, try-catch
|
||||||
// System.out.println("x: " + x + ", y: " + y);
|
try {
|
||||||
// }
|
Piece p = pieces[pos.x][pos.y];
|
||||||
// // Ner vänster
|
System.out.println(p);
|
||||||
// for (int x = 0, y = 0; x < 8 && x > -8; x--, y--) {
|
// If this piece is the same team as ours, skip
|
||||||
// System.out.println("x: " + x + ", y: " + y);
|
if (p.isWhite == this.isWhite) {
|
||||||
// }
|
break;
|
||||||
// // Upp vänster
|
}
|
||||||
// for (int x = 0, y = 0; x < 8 && x > -8; x--, y++) {
|
|
||||||
// System.out.println("x: " + x + ", y: " + y);
|
movable.add(pos);
|
||||||
// }
|
break;
|
||||||
return new LinkedHashSet<>();
|
|
||||||
}
|
} catch (NullPointerException npe) {
|
||||||
|
// This is an empty spot
|
||||||
|
movable.add(pos);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// This means that the player is at the edge
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// Ner höger
|
|
||||||
for (int x = 0, y = 0; x < 8 && x > -8; x++, y++) {
|
|
||||||
System.out.println("x: " + x + ", y: " + y);
|
|
||||||
}
|
}
|
||||||
// Upp höger
|
|
||||||
for (int x = 0, y = 0; x < 8 && x > -8; x++, y--) {
|
// Upp höger
|
||||||
System.out.println("x: " + x + ", y: " + y);
|
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
|
// Ner vänster
|
||||||
for (int x = 0, y = 0; x < 8 && x > -8; x--, y--) {
|
for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) {
|
||||||
System.out.println("x: " + x + ", y: " + y);
|
|
||||||
}
|
|
||||||
// Upp vänster
|
|
||||||
for (int x = 0, y = 0; x < 8 && x > -8; x--, y++) {
|
|
||||||
System.out.println("x: " + x + ", y: " + y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return movable;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -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 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 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 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 Rook(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 Bishop(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)},
|
{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, new King(false, new Point(5, 7))},
|
||||||
{null, null, null, null, null, null, null, null},
|
{null, null, null, null, null, null, null, null},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user