Commit 6240c6aa authored by Omid Sayfun's avatar Omid Sayfun

KingCheck Added

parent b81456de
...@@ -187,34 +187,41 @@ public class Board{ ...@@ -187,34 +187,41 @@ public class Board{
for(Piece p : this.blackPieces){ for(Piece p : this.blackPieces){
all.add(p); all.add(p);
} }
boolean found = false; Piece found = null;
boolean toBusy = false; boolean toBusy = false;
Piece attack = null;
if( color.equals("W") ){ if( color.equals("W") ){
selector = this.whitePieces; selector = this.whitePieces;
enemy = this.blackPieces; enemy = this.blackPieces;
found = isTakenByWhite(fromArray[0] - '0', fromArray[1]); found = takenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]); toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
}else{ }else{
selector = this.blackPieces; selector = this.blackPieces;
enemy = this.whitePieces; enemy = this.whitePieces;
found = isTakenByBlack(fromArray[0] - '0', fromArray[1]); found = takenByBlack(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]); toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
attack = takenByWhite(toArray[0] - '0', toArray[1]);
} }
if( found && !toBusy ){// Found and to is not busy if( found != null && !toBusy ){// Found and to is not busy
for(Piece p : selector){ if( found.getY() == fromArray[0] - '0' && found.getX() == fromArray[1] ){ // Find Piece in select
if( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){ // Find Piece in select
// To Do: Check if pathway is busy if( kingCheck(all, selector, enemy) && !(found instanceof King) ){
// To Do: Check if king is checked and have to move king
// To Do: Attack
if( p.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
if( p.checkWay(all, toArray[1], toArray[0] - '0') ){ return false;
}else{
if( found.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
if( found.checkWay(all, toArray[1], toArray[0] - '0') ){
p.setY(toArray[0] - '0'); if( attack != null ){
p.setX(toArray[1]);
enemy.remove(attack);
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true; return true;
}else{ }else{
return false; return false;
...@@ -238,4 +245,27 @@ public class Board{ ...@@ -238,4 +245,27 @@ public class Board{
return false; return false;
} }
} }
public boolean kingCheck(ArrayList<Piece> all, ArrayList<Piece> base, ArrayList<Piece> oponent){
// find king
int kingY = 0;
char kingX = 'A';
for(Piece p : base){
if( p instanceof King){
kingY = p.y;
kingX = p.x;
}
}
for(Piece p : oponent){
if( p.canMove(kingX, kingY) ){ // Check if move is valid
if( p.checkWay(all, kingX, kingY) ){
return true;
}
}
}
return false;
}
} }
...@@ -33,11 +33,20 @@ public class Pawn extends Piece{ ...@@ -33,11 +33,20 @@ public class Pawn extends Piece{
}else{ }else{
return false; return false;
} }
}else if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
} }
return false; return false;
} }
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}else if( Math.abs(this.y - y) == 1 ){
return true;
}else{
int yShift = 0; int yShift = 0;
if( this.y - y != 0 ){ if( this.y - y != 0 ){
...@@ -57,4 +66,5 @@ public class Pawn extends Piece{ ...@@ -57,4 +66,5 @@ public class Pawn extends Piece{
} }
return true; return true;
} }
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment