forked from gjy03/project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickController.java
More file actions
57 lines (47 loc) · 1.86 KB
/
ClickController.java
File metadata and controls
57 lines (47 loc) · 1.86 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
package controller;
import model.ChessComponent;
import view.Chessboard;
public class ClickController {
private final Chessboard chessboard;
private ChessComponent first;
public ClickController(Chessboard chessboard) {
this.chessboard = chessboard;
}
public void onClick(ChessComponent chessComponent) {
if (first == null) {
if (handleFirst(chessComponent)) {
chessComponent.setSelected(true);
first = chessComponent;
first.repaint();
}
} else {
if (first == chessComponent) { // 再次点击取消选取
chessComponent.setSelected(false);
ChessComponent recordFirst = first;
first = null;
recordFirst.repaint();
} else if (handleSecond(chessComponent)) {
//repaint in swap chess method.
chessboard.swapChessComponents(first, chessComponent);
chessboard.swapColor();
first.setSelected(false);
first = null;
}
}
}
/**
* @param chessComponent 目标选取的棋子
* @return 目标选取的棋子是否与棋盘记录的当前行棋方颜色相同
*/
private boolean handleFirst(ChessComponent chessComponent) {
return chessComponent.getChessColor() == chessboard.getCurrentColor();
}
/**
* @param chessComponent first棋子目标移动到的棋子second
* @return first棋子是否能够移动到second棋子位置
*/
private boolean handleSecond(ChessComponent chessComponent) {
return chessComponent.getChessColor() != chessboard.getCurrentColor() &&
first.canMoveTo(chessboard.getChessComponents(), chessComponent.getChessboardPoint());
}
}