Commit 7fa5c498 authored by hamed's avatar hamed

threats of King is removed from list of possible movment

parent 610acdb4
package Main;
public class Cell implements Comparable<Cell>{
int x;
int y;
public Cell(int x,int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Cell o) {
if(o.x == x){
return o.y - y;
}
else {
return o.x-x;
}
}
}
......@@ -72,7 +72,7 @@ public class Main {
}
public static void printMoves(ArrayList<int[]> moves){
for(int m[] : moves){
System.out.print((char)(m[0]+'a')+""+(m[1]+1)+"\t");
System.out.print((char)(m[0]+'a')+""+(m[1]+1)+",");
}
System.out.println();
}
......
package Pieces;
import Main.Board;
import Main.Cell;
import java.util.ArrayList;
import java.util.HashSet;
public class King extends Piece {
......@@ -40,9 +42,26 @@ public class King extends Piece {
return false;
}
public HashSet<Cell> getThreatedPlaces(){
HashSet<Cell> retSet = new HashSet<>();
for (int i = 0 ; i < 8 ; i++){
for(int j =0 ; j < 8 ; j++){
Piece p = getBoard().getBoard()[i][j];
if(p!=null && (p.isWhite()!=isWhite())){
ArrayList<int[]> moves = p.moves();
for(int m[] : moves){
Cell c = new Cell(m[0],m[1]);
retSet.add(c);
}
}
}
}
return retSet;
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> move = new ArrayList<>();
HashSet<Cell> threats = getThreatedPlaces();
int x = getX();
int y = getY();
int posx[]={x,x,x+1,x+1,x+1,x-1,x-1,x-1};
......@@ -51,7 +70,10 @@ public class King extends Piece {
if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)){
if(getBoard().isEmpty(posx[i],posy[i]) || (getBoard().getColor(posx[i],posy[i])==2 && this.isWhite()) ||(getBoard().getColor(posx[i],posy[i])==1 && isBlack()) )
{
move.add(new int[]{posx[i], posy[i]});
Cell cmp = new Cell(posx[i],posy[i]);
if(!threats.contains(cmp)) {
move.add(new int[]{posx[i], posy[i]});
}
}
}
}
......
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