-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChessBoard.java
112 lines (112 loc) · 3.99 KB
/
ChessBoard.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
import org.code.playground.*;
import java.io.FileNotFoundException;
public class ChessBoard {
public static Board myChessBoard = Playground.board;
private static Piece[][] pieces = new Piece[2][16];
private static Move[][] moves = new Move[8][8];
private static Piece clickedPiece;
private static boolean playerHasMove;
public ChessBoard() throws FileNotFoundException, PlaygroundException {
String[] pieceTypes = {"Rook", "Knight", "Bishop", "Queen", "King", "Bishop", "Knight", "Rook"};
for (int i=0; i < 8; i++) {
pieces[0][i] = new Piece(pieceTypes[i], "White", 0, i);
pieces[0][8+i] = new Piece("Pawn", "White", 1, i);
pieces[1][i] = new Piece(pieceTypes[i], "Black", 7, i);
pieces[1][8+i] = new Piece("Pawn", "Black", 6, i);
}
showPlayableBoard();
myChessBoard.start();
}
public static Piece[][] getPiecesArray() {
return pieces;
}
public static Piece getClicked() {
return clickedPiece;
}
public static boolean getPlayerHasMove() {
return playerHasMove;
}
public static Piece getPieceAt (int row, int col) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 16; j++) {
if (pieces[i][j].getRow() == row && pieces[i][j].getCol() == col) {
return pieces[i][j];
}
}
}
return null;
}
public static void setClicked (Piece piece) {
clickedPiece = piece;
}
public static void setPlayerHasMove (boolean x) {
playerHasMove = x;
}
public static boolean isEmpty (int row, int col) {
return getPieceAt(row, col) == null;
}
public static boolean isFree (int row, int col, String color) {
return isEmpty(row, col) || !getPieceAt(row, col).getColor().equals(color);
}
public static void moveClickedPiece (int row, int col) throws FileNotFoundException {
clickedPiece.move(true, row, col);
showPlayableBoard();
}
public static void removePiece (Piece piece, Piece killedBy) {
myChessBoard.removeClickableImage(piece.getButton());
}
public static void getPossibleMoves (Piece piece) throws FileNotFoundException {
for (int r=0; r<8; r++) {
for (int c=0; c<8; c++) {
if (piece.validMove(r, c)) {
moves[r][c] = new Move(r, c);
playerHasMove = true;
}
}
}
}
public static void showPlayableBoard() throws FileNotFoundException {
myChessBoard.setBackgroundImage("Chessboard.png");
for (Piece[] arr : pieces) {
for (Piece piece : arr) {
if (piece.getType() != null) {
myChessBoard.addClickableImage(piece.getButton());
}
}
}
}
public static void showPossibleMoves (Piece piece) throws FileNotFoundException {
clearPossibleMoves();
getPossibleMoves(piece);
for (int r=0; r<8; r++) {
for (int c=0; c<8; c++) {
if (moves[r][c] != null) {
myChessBoard.addClickableImage(moves[r][c]);
}
}
}
}
public static void clearPossibleMoves() {
for (int r=0; r<8; r++) {
for (int c=0; c<8; c++) {
if (moves[r][c] != null) {
myChessBoard.removeClickableImage(moves[r][c]);
moves[r][c] = null;
}
}
}
}
public static void showOverlay (String winner) throws FileNotFoundException {
switch (winner) {
case "White": myChessBoard.addImageItem(new ImageItem("White-King.png", 0, 0, myChessBoard.getWidth(), myChessBoard.getHeight()));
break;
case "Black": myChessBoard.addImageItem(new ImageItem("Black-King.png", 0, 0, myChessBoard.getWidth(), myChessBoard.getHeight()));
break;
case "Draw": {
myChessBoard.addImageItem(new ImageItem("White-King.png", 0, 0, myChessBoard.getWidth() / 2, myChessBoard.getHeight() / 2));
myChessBoard.addImageItem(new ImageItem("Black-King.png", myChessBoard.getWidth() / 2, myChessBoard.getHeight() / 2, myChessBoard.getWidth() / 2, myChessBoard.getHeight() / 2));
}
}
myChessBoard.addImageItem(new ImageItem("Transparent.png", 0, 0, myChessBoard.getWidth(), myChessBoard.getHeight()));
}
}