-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.java
More file actions
56 lines (48 loc) · 2.07 KB
/
GameController.java
File metadata and controls
56 lines (48 loc) · 2.07 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
import javafx.scene.layout.StackPane;
import java.util.Objects;
public class GameController {
public static boolean isGameFinished = false;
public static boolean isWhiteTurn = true;
private static Piece selectedPiece = null;
public static void makeMove(Tile tile, StackPane cell) {
cell.setOnMouseClicked(e -> {
if (isGameFinished) {
return;
}
if (selectedPiece == null) {
if (isWhiteTurn) {
if (tile.getPiece() != null && Objects.equals(tile.getPiece().getColour(), "white")) {
selectedPiece = tile.getPiece();
System.out.println("Selected Piece " + selectedPiece.getName() + " on Tile " + selectedPiece.getTile().getRow() + selectedPiece.getTile().getColumn());
} else {
System.out.println("white's turn");
}
} else {
if (tile.getPiece() != null && Objects.equals(tile.getPiece().getColour(), "black")) {
selectedPiece = tile.getPiece();
System.out.println("Selected Piece " + selectedPiece.getName() + " on Tile " + selectedPiece.getTile().getRow() + selectedPiece.getTile().getColumn());
} else {
System.out.println("black's turn");
}
}
} else {
if (selectedPiece.isLegalMove(tile)) {
isWhiteTurn = !isWhiteTurn;
}
selectedPiece.move(tile);
selectedPiece = null;
if (Piece.isKingCaptured) {
isGameFinished = true;
}
}
});
}
public static MoveResult performMove(Piece piece, Tile target) {
if (!piece.isLegalMove(target)){
return new MoveResult(piece, null, target, false);
}
Piece captured = target.getPiece();
piece.move(target);
return new MoveResult(piece,captured,target, true);
}
}