-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessSlot.java
132 lines (124 loc) · 3.55 KB
/
ChessSlot.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import javafx.scene.layout.StackPane;
import javafx.scene.layout.GridPane;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
public class ChessSlot {
boolean hasPiece;
int verticalVal;
int horizontalVal;
boolean isSelected;
GridPane grid;
Piece piece;
String name;
StackPane space;
Stage stage;
ChessSlot highlightedSlot = null;
boolean kingKilled = false;
boolean kingisWhite;
public ChessSlot(Stage primaryStage, GridPane gridP, String name,int horz,int vert) {
stage = primaryStage;
grid = gridP;
space = new StackPane();
space.setMinSize(75,75);
gridP.add(space, horz, vert);
piece = null;
name = null;
verticalVal = vert;
horizontalVal = horz;
isSelected = false;
}
public void addPiece(Object add) {
if(add instanceof Piece) {
Piece addPiece = (Piece)add;
space.getChildren().add(addPiece.getImageView());
hasPiece = true;
piece = addPiece;
addPiece.setSlot(this);
}
}
public void removePiece(Object remove) {
if(remove instanceof Piece) {
Piece removePiece = (Piece)remove;
space.getChildren().remove(removePiece.getImageView());
hasPiece = false;
piece = null;
}
}
Rectangle highlightRec = new Rectangle(60, 60, Color.DODGERBLUE);
public void highlight() {
space.getChildren().add(highlightRec);
if(hasPiece) {
space.getChildren().remove(piece.getImageView());
space.getChildren().add(piece.getImageView());
}
}
public void removeHighlight() {
space.getChildren().remove(highlightRec);
}
Rectangle selectRec = new Rectangle(60, 60, Color.ORANGERED);
public void select(ChessSlot origin) {
highlightedSlot = origin;
isSelected = true;
space.getChildren().add(selectRec);
if(hasPiece) {
space.getChildren().remove(piece.getImageView());
space.getChildren().add(piece.getImageView());
}
}
public void deSelect() {
isSelected = false;
highlightedSlot = null;
space.getChildren().remove(selectRec);
}
public void move(ChessSlot newSlot) {
if(newSlot.hasPiece()) {
if(newSlot.getPiece() instanceof King) {
kingKilled = true;
if(newSlot.getPiece().isWhite()) {
kingisWhite = true;
} else {
kingisWhite = false;
}
}
newSlot.removePiece(newSlot.getPiece());
}
piece.move(newSlot);
newSlot.addPiece(piece);
removePiece(piece);
newSlot.deSelect();
this.removeHighlight();
}
public StackPane getPane() {
return space;
}
public int getHorz() {
return horizontalVal;
}
public int getVert() {
return verticalVal;
}
public boolean hasPiece() {
return hasPiece;
}
public Piece getPiece() {
return piece;
}
public boolean isSelected() {
return isSelected;
}
public ChessSlot getHighlightSlot() {
return highlightedSlot;
}
public boolean kingKilled() {
return kingKilled;
}
public boolean kingisWhite() {
return kingisWhite;
}
public void kingReset() {
kingKilled = false;
}
}