mirror of
https://github.com/lov3b/Schack.git
synced 2025-02-23 20:20:07 +01:00
rook funkar inte
This commit is contained in:
parent
fa60ce0174
commit
ed07eecccb
@ -18,7 +18,7 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
private Piece[][] pieces = new Piece[8][8];
|
private Piece[][] pieces = new Piece[8][8];
|
||||||
private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
|
private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
|
||||||
private Point selectedPiece = new Point();
|
private Point selectedPiece = new Point();
|
||||||
private Color moveableColor = new Color(200, 200, 200);
|
private Color moveableColor = new Color(255, 191, 0);
|
||||||
|
|
||||||
public Board() throws IOException {
|
public Board() throws IOException {
|
||||||
|
|
||||||
@ -193,7 +193,6 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
Piece p = pieces[mouseCoordinateX][mouseCoordinateY];
|
Piece p = pieces[mouseCoordinateX][mouseCoordinateY];
|
||||||
LinkedHashSet validMoves = p.validMoves(pieces);
|
LinkedHashSet validMoves = p.validMoves(pieces);
|
||||||
System.out.println("valid moves " + validMoves);
|
System.out.println("valid moves " + validMoves);
|
||||||
moveableColor = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
|
|
||||||
validMovesToDraw.addAll(validMoves);
|
validMovesToDraw.addAll(validMoves);
|
||||||
System.out.println("valid moves to draw " + validMovesToDraw);
|
System.out.println("valid moves to draw " + validMovesToDraw);
|
||||||
|
|
||||||
|
@ -16,6 +16,4 @@ public class Horse extends Piece {
|
|||||||
return new LinkedHashSet<>();
|
return new LinkedHashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@ public class Pawn extends Piece {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||||
|
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||||
|
|
||||||
|
|
||||||
return new LinkedHashSet<>();
|
return new LinkedHashSet<>();
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package schack;
|
|||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
|
||||||
public class Rook extends Piece {
|
public class Rook extends Piece {
|
||||||
@ -12,41 +13,126 @@ public class Rook extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||||
//Behöver skriva att om rookX = this.position.x så ska vi istället loopa igenom
|
//Behöver skriva att om rookX = this.position.x så ska vi istället loopa igenom
|
||||||
//int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++
|
//int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++
|
||||||
//men jag är trög och har spenderat alldles förmycket tid på att vara trög :^)
|
//men jag är trög och har spenderat alldles förmycket tid på att vara trög :^)
|
||||||
for (int rookX = 0-this.position.x; rookX < 8-this.position.x; rookX++) {
|
|
||||||
|
|
||||||
if (this.position.y == 0 && rookX == 0) {
|
// Vänster
|
||||||
continue;
|
for (int rookX = this.position.x; rookX >= 0; rookX--) {
|
||||||
}
|
Point pos = new Point(this.position.x - rookX, this.position.y);
|
||||||
Point pos = new Point(this.position.x + rookX, this.position.y);
|
|
||||||
|
|
||||||
// Instead of checking index and null, try-catch
|
// Instead of checking index and null, try-catch
|
||||||
try {
|
try {
|
||||||
Piece p = pieces[pos.x][pos.y];
|
Piece p = pieces[pos.x][pos.y];
|
||||||
System.out.println(p);
|
System.out.println(p);
|
||||||
// If this piece is the same team as ours, skip
|
// If this piece is the same team as ours, skip
|
||||||
if (p.isWhite == this.isWhite) {
|
if (p.isWhite == this.isWhite) {
|
||||||
continue;
|
break;
|
||||||
}
|
|
||||||
movable.add(pos);
|
|
||||||
|
|
||||||
} catch (NullPointerException npe) {
|
|
||||||
// This is an empty spot
|
|
||||||
movable.add(pos);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// This means that the player is at the edge
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 = 1; rookX <= this.position.x + rookX; rookX++) {
|
||||||
|
Point pos = new Point(this.position.x + 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// for (int rookY = 0 - this.position.y; rookY < 8 - this.position.y; rookY++) {
|
||||||
|
// if (this.position.y == 0 && rookY == 0) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// Point pos = new Point(this.position.x, this.position.y + 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) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// movable.add(pos);
|
||||||
|
//
|
||||||
|
// } catch (NullPointerException npe) {
|
||||||
|
// // This is an empty spot
|
||||||
|
// movable.add(pos);
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// // This means that the player is at the edge
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // En lista för att kolla alla vi har lagt till innan en pjäs av samma färg.
|
||||||
|
// HashSet<Point> toBeRemoved = new HashSet();
|
||||||
|
//
|
||||||
|
// for (int rookX = 0 - this.position.x; rookX < 8 - this.position.x; rookX++) {
|
||||||
|
//
|
||||||
|
// if (this.position.y == 0 && rookX == 0) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// Point pos = new Point(this.position.x + rookX, this.position.y);
|
||||||
|
//
|
||||||
|
// // Instead of checking index and null, try-catch
|
||||||
|
// try {
|
||||||
|
// Piece p = pieces[pos.x][pos.y];
|
||||||
|
// System.out.println(p);
|
||||||
|
//
|
||||||
|
// // Funkar bara åt vänster
|
||||||
|
// if (pieces[pos.x + 1][pos.y] != null && pieces[pos.x + 1][pos.y] != this) {
|
||||||
|
// toBeRemoved.add(pos);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // If this piece is the same team as ours, skip
|
||||||
|
// if (p.isWhite == this.isWhite) {
|
||||||
|
// movable.removeAll(toBeRemoved);
|
||||||
|
// toBeRemoved.clear();
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
//// toBeRemoved.add(pos);
|
||||||
|
// movable.add(pos);
|
||||||
|
//
|
||||||
|
// } 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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user