1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class Knight extends Piece {
public Knight(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isValidMove(int dx, int dy) {
ArrayList<int[]> moves = moves();
for (int m[] : moves){
if(m[0]==dx && m[1] == dy){
return true;
// break;
}
}
return false;
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> move = new ArrayList<>();
int x = getX();
int y = getY();
int posx[]={x+1,x+1,x+2,x+2,x-1,x-1,x-2,x-2};
int posy[]={y-2,y+2,y-1,y+1,y-2,y+2,y-1,y+1};
for(int i=0;i<8;i++) {
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]});
}
}
return move;
}
@Override
public String toString() {
return "A";
}
}