- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
Latest commit
160 lines (126 loc) · 4.52 KB
/
Copy pathGame.java
File metadata and controls
160 lines (126 loc) · 4.52 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
packagechess;
importjavafx.application.Application;
importjavafx.event.*;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.control.TextField;
importjavafx.scene.layout.HBox;
importjavafx.scene.layout.Pane;
importjavafx.scene.layout.StackPane;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.GridPane;
importjavafx.scene.layout.VBox;
importjavafx.stage.Stage;
importjavafx.scene.image.*;
importjava.util.List;
importjavafx.collections.*;
publicclassGameextendsApplicationimplementsListChangeListener {
// Create board and button objects
privateChessBoardboard = newChessBoard();
privateButton[] whiteTakenSquare = newButton[16];
privateButton[] blackTakenSquare = newButton[16];
@Override
publicvoidstart(StageprimaryStage) {
BorderPaneroot = newBorderPane(); // Root branch
GridPanegrid = newGridPane(); // Holds buttons for the game board grid
PaneleftVBox = newVBox(); // Holds the taken white pieces
PanerightVBox = newVBox(); // Holds the taken black pieces
board.addTakenObserver(this);
SquareEventHandlerhandle = newSquareEventHandler(board);
// Create buttons for left Vertical squares (Taken White Pieces)
for (inti = 0; i < 16; i++) {
whiteTakenSquare[i] = newButton("");
whiteTakenSquare[i].setOnAction(handle);
whiteTakenSquare[i].setStyle("-fx-background-color: grey; -fx-border-color:white;");
whiteTakenSquare[i].setMinSize(90, 45);
leftVBox.getChildren().add(whiteTakenSquare[i]);
}
// Create button for right Vertical squares (Taken Black Pieces)
for (inti = 0; i < 16; i++) {
blackTakenSquare[i] = newButton("");
blackTakenSquare[i].setOnAction(handle);
blackTakenSquare[i].setStyle("-fx-background-color: grey; -fx-border-color:white;");
blackTakenSquare[i].setMinSize(90, 45);
rightVBox.getChildren().add(blackTakenSquare[i]);
}
CoordinateBoardcoordinates[] = newCoordinate[64]; // Used to keep track of grid coordinates
ButtonGbtn[][] = newButton[8][8]; // Create grid of square buttons
for (intc = 0; c < 8; c++) {
introwCor = 7;
inti = 0;
for (intr = 0; r < 8; r++) {
Boardcoordinates[i] = newCoordinate(c, rowCor - r); // Create coordinate for place on grid
Gbtn[c][r] = newButton("" + Boardcoordinates[i].name()); // Create new button with coordinate text
Gbtn[c][r].setOnAction(handle);
// Initialize color for respective button
if ((c + r) % 2 != 0) {
Gbtn[c][r].setStyle("-fx-background-color: white;");
} else {
Gbtn[c][r].setStyle("-fx-background-color: grey;");
}
Gbtn[c][r].setMinSize(90, 90); // Set size of grid button
grid.add(Gbtn[c][r], c, r); // Add button to grid
}
}
// Set graphics for initial game board
for (Button[] button : Gbtn) {
for (ButtonbuttonInside : button) {
Piecepiece = board.getSquare(newCoordinate(buttonInside.getText())).getPiece();
if (piece != null) {
buttonInside.setGraphic(newImageView(newImage("file:src/chess/images/" + piece.getImageName())));
}
}
}
// Arrange scene graph
root.setCenter(grid);
root.setLeft(leftVBox);
root.setRight(rightVBox);
Scenescene = newScene(root, 900, 720); // Set scene size
primaryStage.setTitle("Chess");
primaryStage.setScene(scene);
primaryStage.show();
}
publicstaticvoidmain(String[] args) {
launch(args);
}
@Override
publicvoidonChanged(Changec) {
while (c.next()) {
if (c.wasAdded()) {
intindex = c.getFrom();
List<Piece> pieces = c.getAddedSubList();
for (Piecep : pieces) {
// Check for piece color and set appropriate graphic
if (pieces.get(0).getColour() == ChessColour.WHITE) {
whiteTakenSquare[index]
.setGraphic(newImageView(newImage("file:src/chess/images/" + p.getImageName())));
} else {
blackTakenSquare[index]
.setGraphic(newImageView(newImage("file:src/chess/images/" + p.getImageName())));
}
}
}
}
}
}
classSquareEventHandlerimplementsEventHandler {
privateChessBoardboard = newChessBoard();
privatebooleanfirstClick;
privateButtonfirstButton;
SquareEventHandler(ChessBoardboard) {
this.board = board;
firstClick = true;
}
@Override
publicvoidhandle(Eventevent) {
Buttonb = (Button) event.getSource();
if (firstClick != true && board.move(newCoordinate(firstButton.getText()), newCoordinate(b.getText()))) {
b.setGraphic(firstButton.getGraphic());
firstButton.setGraphic(null);
firstClick = true;
} else {
firstButton = b;
firstClick = false;
}
}
}