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

KingCheck Added

parent b81456de
......@@ -187,34 +187,41 @@ public class Board{
for(Piece p : this.blackPieces){
all.add(p);
}
boolean found = false;
Piece found = null;
boolean toBusy = false;
Piece attack = null;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
found = isTakenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
selector = this.whitePieces;
enemy = this.blackPieces;
found = takenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
found = isTakenByBlack(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
selector = this.blackPieces;
enemy = this.whitePieces;
found = takenByBlack(fromArray[0] - '0', fromArray[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( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){ // Find Piece in select
if( found.getY() == fromArray[0] - '0' && found.getX() == fromArray[1] ){ // Find Piece in select
// To Do: Check if pathway is busy
// 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( kingCheck(all, selector, enemy) && !(found instanceof King) ){
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
p.setY(toArray[0] - '0');
p.setX(toArray[1]);
if( found.checkWay(all, toArray[1], toArray[0] - '0') ){
if( attack != null ){
enemy.remove(attack);
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true;
}else{
return false;
......@@ -238,4 +245,27 @@ public class Board{
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,28 +33,38 @@ public class Pawn extends Piece{
}else{
return false;
}
}else if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int yShift = 0;
if( this.y - y != 0 ){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( y != this.y + i * yShift ){
return true;
}else if( Math.abs(this.y - y) == 1 ){
return true;
}else{
int yShift = 0;
if( this.y - y != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( y != this.y + i * yShift ){
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
i++;
}
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
i++;
}
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
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