-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.java
More file actions
106 lines (87 loc) · 3.05 KB
/
Piece.java
File metadata and controls
106 lines (87 loc) · 3.05 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//---------------------------imports--------------------------------
import java.util.*;
//handle gui later
//------------------------class + methods--------------------------------
public abstract class Piece{
//---------------------------position handling--------------------------------
protected int col;
protected int row;
protected final boolean isWhite;
protected List<int []> possibleMoves;
public boolean isSelected;//if piece is tapped we set it to true and calc moves and display validated moves
//if 1 piece is selected change all other are to not selected
//------------------------GUI handling--------------------------------
protected int size;
//------------------------methods--------------------------------
public Piece(int row, int col, boolean isWhite){
this.row = row;
this.col = col;
this.isWhite = isWhite;
this.isSelected=false;
this.size=64;
this.possibleMoves=new ArrayList<int[]>();
}
//returns position of the piece assuming 0,0 is the start of the board
public int[] getPos(){
return new int[] {col*size,row*size};//returns {x,y}
}
public int[] getRawPos(){
return new int[]{row,col};
}
public int getCol() {
return this.col;
}
public int getRow() {
return this.row;
}
public boolean getIsWhite() {
return this.isWhite;
}
public List<int[]> getMoves() {
//removed the board and the calc moves because thy happen before every move.
return this.possibleMoves;
}
public void setMoves(List<int[]> possibleMoves){
this.possibleMoves= possibleMoves;
}
public void validateMoves(Board b){
//checking for pinned pieces
List<int[]> moves=this.possibleMoves;
List<int[]> remove=new ArrayList<>();
int[] currPos;
//check what moves to remove:
for(int i=0;i<moves.size();i++){
//move the piece without drawing
int[] mov=moves.get(i);
currPos=this.getRawPos();
Piece p=b.getCell(mov[0], mov[1]).getPiece();
b.move(currPos, mov);
if(b.isCheck(this.isWhite)){
remove.add(mov);
}
//move the piece back
b.move(mov,currPos);
if(p!=null){
b.getCell(mov[0], mov[1]).setPiece(p);
}
}
//remove the invalid moves
for(int i=0;i<remove.size();i++){
if(remove.get(i)!=null){
moves.remove(remove.get(i));
}
}
this.setMoves(moves);
}
public abstract void calculateMoves(Board b);
public abstract void draw();
public abstract char toChar();
public void drawValidMoves(){
//foreach loop of moves, draw circle with low opacity
}
//in the board class add a listener to all cells in the board that sends move events
public void move(int row,int col){
this.row=row;
this.col=col;
}//add the eating mechanism in the subclasses
}