-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePanel.java
207 lines (175 loc) · 6.28 KB
/
GamePanel.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GamePanel extends Panel
{
private Game game;
private Colour player;
private GameController controller;
private BoardPanel boardPanel;
private Label statusLabel = new Label("", Label.CENTER);
private MoveList moveList;
private TextArea historyTextArea =
new TextArea("", 10, 20, TextArea.SCROLLBARS_VERTICAL_ONLY);
/*
private int
whitePositions = 0, whiteMoves = 0,
whiteCheckPositions = 0, whiteCheckMoves = 0,
blackPositions = 0, blackMoves = 0,
blackCheckPositions = 0, blackCheckMoves = 0;
*/
public GamePanel(GameController controller, Colour player,
ActionListener actionListener)
{
this.controller = controller;
this.player = player;
this.game = controller.game();
moveList = new MoveList(controller, this);
boardPanel = new BoardPanel(controller, this, player==Colour.BLACK);
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0; // First column
{
// Add the game board
constraints.gridy = 0; constraints.gridheight = 7;
gridbag.setConstraints(boardPanel, constraints);
add(boardPanel);
}
constraints.gridx = 1; // Second column
constraints.insets.left = constraints.insets.right = 8;
constraints.gridheight = 1;
{
// Add the game history
Component historyLabel = new Label("History:");
constraints.gridy = 0; constraints.weighty = 0.0;
constraints.insets.top = 4;
gridbag.setConstraints(historyLabel, constraints);
constraints.gridy = 1; constraints.weighty = 1.0;
constraints.insets.top = 0;
historyTextArea.setEditable(false);
historyTextArea.setBackground(SystemColor.text);
historyTextArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
gridbag.setConstraints(historyTextArea, constraints);
Button storeHistoryButton = new Button("Save...");
storeHistoryButton.setActionCommand("storeHistory");
storeHistoryButton.addActionListener(actionListener);
constraints.gridy = 2; constraints.weighty = 0.0;
constraints.insets.top = 0;
gridbag.setConstraints(storeHistoryButton, constraints);
add(historyLabel); add(historyTextArea); add(storeHistoryButton);
}
{
// Add the available moves
Component movesLabel = new Label("Available moves:");
constraints.gridy = 3; constraints.weighty = 0.0;
constraints.insets.top = 4;
gridbag.setConstraints(movesLabel, constraints);
constraints.gridy = 4; constraints.weighty = 1.0;
constraints.insets.top = 0;
gridbag.setConstraints(moveList, constraints);
add(movesLabel); add(moveList);
}
{
// Add the flip board checkbox label
constraints.gridy = 5; constraints.weighty = 0.0;
constraints.insets.top = 4;
Checkbox checkbox = new BoardFlipCheckbox(boardPanel, player==Colour.BLACK);
gridbag.setConstraints(checkbox, constraints);
add(checkbox);
}
{
// Add the status label
constraints.gridy = 6; constraints.weighty = 0.0;
constraints.insets.top = 4;
gridbag.setConstraints(statusLabel, constraints);
add(statusLabel);
}
{
// Add the quit button
constraints.insets.top = constraints.insets.bottom = 4;
constraints.gridy = 7; constraints.weighty = 0.0;
Component quitButton = new ResignButton(controller);
gridbag.setConstraints(quitButton, constraints);
add(quitButton);
}
processMove(null);
}
public void update(Graphics g)
{
// Don't clear background - this component is opaque
paint(g);
}
public void processMove(Move move)
{
// Update history
if(move != null)
{
String moveString
= game.lastMove().toString() + (game.check()?"+":" ");
int moves = game.getHistorySize();
historyTextArea.append((moves % 2) == 1 ?
(moves/2 + 1 < 10 ? " " : "") +
Integer.toString(moves/2 + 1) + ". " + moveString :
" " + moveString + "\n");
}
Collection availableMoves = game.moves();
// Update list of available moves
moveList.reset(availableMoves);
/* Update total number of moves (for game analysis) */
/*
if(game.player()==Colour.WHITE)
{
if(game.check())
{
whiteCheckMoves += availableMoves.size();
++whiteCheckPositions;
} else
{
whiteMoves += availableMoves.size();
++whitePositions;
}
} else
if(game.player()==Colour.BLACK)
{
if(game.check())
{
blackCheckMoves += availableMoves.size();
++blackCheckPositions;
} else
{
blackMoves += availableMoves.size();
++blackPositions;
}
}
System.out.println("Average number of available moves per position: (all, non-check, check)");
System.out.println("White: " + (whiteCheckPositions+whitePositions==0?"-":
Float.toString((whiteCheckMoves+whiteMoves)/(whiteCheckPositions+whitePositions))) +
" / " + (whitePositions==0?"-":Float.toString(whiteMoves/whitePositions)) +
" / " + (whiteCheckPositions==0?"-":Float.toString(whiteCheckMoves/whiteCheckPositions)));
System.out.println("Black: " + (blackCheckPositions+blackPositions==0?"-":
Float.toString((blackCheckMoves+blackMoves)/(blackCheckPositions+blackPositions))) +
" / " + (blackPositions==0?"-":Float.toString(blackMoves/blackPositions)) +
" / " + (blackCheckPositions==0?"-":Float.toString(blackCheckMoves/blackCheckPositions)));
*/
// Update game field view
if(move != null)
boardPanel.repaintForMove(move);
// Show current player and game state (maybe move state to Game?)
String player = (game.player() == Colour.WHITE ? "White" : "Black");
GameState state = game.state();
String stateString = "";
if(state == GameState.CHECK) stateString = "Check!";
if(state == GameState.CHECKMATE) stateString = "Checkmate!";
if(state == GameState.STALEMATE) stateString = "Stalemate.";
if(state == GameState.RESIGNED) stateString = "Resigned.";
if(state == GameState.DRAW) stateString = "Draw.";
statusLabel.setText(state == GameState.NORMAL ?
player + " to move" : player + " - " + stateString);
}
public boolean moveEnabled()
{
return ((!game.over())&&(game.player() == player));
}
}