Not working check
parent
e680a72a75
commit
a12831513d
|
@ -2,7 +2,7 @@ package chess;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
|
|
||||||
enum BoardStatus {
|
public enum BoardStatus {
|
||||||
None,
|
None,
|
||||||
WhiteInCheck,
|
WhiteInCheck,
|
||||||
BlackInCheck
|
BlackInCheck
|
||||||
|
@ -45,11 +45,43 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean movePiece(int x, int y, int newX, int newY){
|
public boolean movePiece(int x, int y, int newX, int newY){
|
||||||
System.out.println(x + " " + y + " || " + newX + " " + newY);
|
|
||||||
ChessPiece selectedPiece;
|
ChessPiece selectedPiece;
|
||||||
// make sure the place we are moving from has a piece
|
// make sure the place we are moving from has a piece
|
||||||
if ((selectedPiece = get(x, y)) == null)
|
if ((selectedPiece = get(x, y)) == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// check for check
|
||||||
|
if (status != BoardStatus.None){
|
||||||
|
// when the king is in check no piece can move.
|
||||||
|
if (!(selectedPiece instanceof King))
|
||||||
|
return false;
|
||||||
|
if (status == BoardStatus.WhiteInCheck){
|
||||||
|
// don't allow the wrong king to be moved
|
||||||
|
if (!selectedPiece.isWhite)
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// don't allow the wrong king to be moved
|
||||||
|
if (selectedPiece.isWhite)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// prevent moves which are invalid.
|
||||||
|
for (int i = 0; i < board.length; i++){
|
||||||
|
for (int j = 0; j < board.length; j++){
|
||||||
|
var p = get(i, j);
|
||||||
|
// make sure that we only check moves on pieces of the opposite color
|
||||||
|
if (p != null && p.isWhite != selectedPiece.isWhite){
|
||||||
|
var moves = p.getMoves();
|
||||||
|
for (Move m : moves){
|
||||||
|
// don't allow for king to move to pieces which would still put him in danger.
|
||||||
|
if (m.getX() == newX && m.getY() == newY)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply move like normal.
|
||||||
var moves = selectedPiece.getMoves();
|
var moves = selectedPiece.getMoves();
|
||||||
for (Move m : moves){
|
for (Move m : moves){
|
||||||
// reject the moves that don't correspond to where we want to move to.
|
// reject the moves that don't correspond to where we want to move to.
|
||||||
|
@ -73,16 +105,11 @@ public class Board {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the danger status of all the pieces. Pieces which are threatened are marked as in danger
|
||||||
|
*/
|
||||||
public void updateDangerStatus(){
|
public void updateDangerStatus(){
|
||||||
// reset dangers
|
status = BoardStatus.None;
|
||||||
for (int i = 0; i < board.length; i++){
|
|
||||||
for (int j = 0; j < board.length; j++){
|
|
||||||
var p = get(i, j);
|
|
||||||
if (p != null)
|
|
||||||
p.setInDanger(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// calculate all the pieces that are in danger now
|
|
||||||
for (int i = 0; i < board.length; i++){
|
for (int i = 0; i < board.length; i++){
|
||||||
for (int j = 0; j < board.length; j++){
|
for (int j = 0; j < board.length; j++){
|
||||||
var p = get(i, j);
|
var p = get(i, j);
|
||||||
|
@ -90,19 +117,16 @@ public class Board {
|
||||||
var moves = p.getMoves();
|
var moves = p.getMoves();
|
||||||
for (Move m : moves){
|
for (Move m : moves){
|
||||||
var pieceInDanger = get(m);
|
var pieceInDanger = get(m);
|
||||||
if (pieceInDanger != null)
|
// make sure that the piece isn't on the same team. White cannot endanger white.
|
||||||
pieceInDanger.setInDanger(true);
|
if (pieceInDanger != null && pieceInDanger.isWhite != p.isWhite) {
|
||||||
|
// we only care if the king is in danger.
|
||||||
|
if (pieceInDanger instanceof King) {
|
||||||
|
status = pieceInDanger.isWhite() ? BoardStatus.WhiteInCheck : BoardStatus.BlackInCheck;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check for check
|
|
||||||
for (int i = 0; i < board.length; i++){
|
|
||||||
for (int j = 0; j < board.length; j++){
|
|
||||||
var p = get(i, j);
|
|
||||||
if (p instanceof King)
|
|
||||||
if (p.isInDanger)
|
|
||||||
status = p.isWhite() ? BoardStatus.WhiteInCheck : BoardStatus.BlackInCheck;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ public class Display extends JFrame implements MouseListener {
|
||||||
@Override
|
@Override
|
||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
super.paint(g);
|
super.paint(g);
|
||||||
|
g.drawString(b.getStatus() == Board.BoardStatus.BlackInCheck ? "Black in check" : b.getStatus() == Board.BoardStatus.WhiteInCheck ? "White in check" : "Board Normal", 600, 700);
|
||||||
for (int i = 0; i < b.size(); i++){
|
for (int i = 0; i < b.size(); i++){
|
||||||
for (int j = 0; j < b.size(); j++){
|
for (int j = 0; j < b.size(); j++){
|
||||||
var p = b.get(i,j);
|
var p = b.get(i,j);
|
||||||
|
|
Loading…
Reference in New Issue