-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatscrewGUI.java
277 lines (272 loc) · 9.97 KB
/
RatscrewGUI.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* Write a description of class RatscrewGUI here.
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class RatscrewGUI extends JFrame
{
protected static final int DEFAULT_HEIGHT = 500 * 3;
protected static final int DEFAULT_WIDTH = 800 * 3;
protected static final int CARD_WIDTH = 73;
protected static final int CARD_HEIGHT = 97;
/** The board (Board subclass). */
protected Ratscrew game;
/** The main panel containing the game components. */
protected CardPanel panel;
/**
* Initialize the GUI.
* @param gameBoard is a <code>Board</code> subclass.
*/
public RatscrewGUI(int players) {
game = new Ratscrew(players);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* Initialize the display.
*/
protected void initDisplay() {
this.setTitle("Egyptian Ratscrew");
this.setSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
panel.setLayout(null);
panel.setPreferredSize(
new Dimension(DEFAULT_WIDTH - 20, DEFAULT_HEIGHT - 20));
panel.initPanel();
getContentPane().add(panel);
panel.setFocusable(true);
panel.requestFocusInWindow();
panel.setVisible(true);
}
/**
* Draw the display (cards and messages).
*/
public void repaint() {
if (game.gameOver()){
panel.getWinMsg().setText("Player " + game.gameWinner() + " wins");
panel.getWinMsg().setVisible(true);
}
for (int i = 0; i < panel.previousCards.length; i++){
panel.previousCards[i].setBounds(getWidth()/2 + (CARD_WIDTH/2) - (CARD_WIDTH*i/2), getHeight()/2 - (CARD_HEIGHT/2), CARD_WIDTH, CARD_HEIGHT);
}
panel.statusMsg.setBounds(getWidth()/2, getHeight()/2 - CARD_HEIGHT/2 - 30, 100, 30);
panel.winMsg.setBounds(getWidth()/2, getHeight()/2 + CARD_HEIGHT/2 + 30, 100, 30);
panel.refresh();
panel.repaint();
}
/**
* Deal with the user clicking on something other than a button or a card.
*/
protected void signalError() {
Toolkit t = panel.getToolkit();
t.beep();
}
/**
* Run the game.
*/
public void displayGame() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
/**
* Attempt to put down a card of a player
* @param player the index of the player who is attempting to draw
*/
protected void draw(int player){
if (!game.draw(player)){
if (game.claimable()){
panel.getStatusMsg().setText("P" + (game.getFaceCardPlayer() + 1) + " claims");
} else {
panel.getStatusMsg().setText("P" + (game.getCurrentPlayer() + 1) + " draws");
}
panel.getStatusMsg().setVisible(true);
} else panel.getStatusMsg().setVisible(false);
}
/**
* Attempt to give all center pile cards to a player
* @param player the index of the player attempting a claim
*/
protected void claim(int player){
if (!game.claim(player)){
if (game.claimable()) panel.getStatusMsg().setText("P" + (game.getFaceCardPlayer() + 1) + " claims");
else panel.getStatusMsg().setText("False Claim by P" + (player+1));
panel.getStatusMsg().setVisible(true);
}
else panel.getStatusMsg().setVisible(false);
}
/**
* An abstract panel class for the abstract GUI class
*/
protected abstract class CardPanel extends JPanel{
private Ratscrew game;
private JLabel[] previousCards;
private JLabel[] playerCards;
private JLabel[] playerData;
private JLabel statusMsg;
private JLabel winMsg;
/**
* Constructor for the card panel class
*/
protected CardPanel(RatscrewGUI frame){
this.game = frame.game;
}
/**
* Draws all player cards and data labels and
* the labels to display the last 3 cards of the center pile
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
}
/**
* Initializes the panel components
*/
protected void initPanel(){
previousCards = new JLabel[3];
playerCards = new JLabel[game.getPlayers()];
playerData = new JLabel[game.getPlayers()];
for (int j = 0; j < previousCards.length ; j++){
previousCards[j] = new JLabel();
this.add(previousCards[j]);
}
for (int i = 0; i < playerCards.length; i++){
playerCards[i] = new JLabel();
playerData[i] = new JLabel();
playerData[i].setFont(new Font("SansSerif", Font.BOLD, 20));
this.add(playerCards[i]);
this.add(playerData[i]);
}
statusMsg = new JLabel();
winMsg = new JLabel();
this.add(statusMsg);
statusMsg.setVisible(false);
winMsg.setFont(new Font("SansSerif", Font.BOLD, 20));
winMsg.setForeground(Color.BLACK);
this.add(winMsg);
winMsg.setVisible(false);
}
public void refresh(){
for (int i = 0; i < previousCards.length; i++){
String cardImageFileName;
if (game.getCardPile().size()>i){
cardImageFileName = imageFileName(game.getCardPile().get(game.getCardPile().size()-(1+i)));
}
else cardImageFileName = imageFileName(null);
URL imageURL = getClass().getResource(cardImageFileName);
if (imageURL !=null){
ImageIcon icon = new ImageIcon(imageURL);
previousCards[i].setIcon(icon);
previousCards[i].setVisible(cardImageFileName!=imageFileName(null));
}else {
throw new RuntimeException(
"Card image not found: \"" + cardImageFileName + "\"");
}
}
for (int i = 0; i < playerCards.length; i++){
playerData[i].setText("P" + (i+1) + ": " + game.deckSize(i) + " cards");
playerCards[i].setIcon(new ImageIcon(getClass().getResource(imageFileName(null))));
playerData[i].setVisible(true);
playerCards[i].setVisible(game.deckSize(i)>0 && game.getCurrentPlayer()==i);
}
}
/**
* Returns the image that corresponds to the input card.
* Image names have the format "[Rank][Suit].GIF" or "[Rank][Suit]S.GIF",
* for example "aceclubs.GIF" or "8heartsS.GIF". The "S" indicates that
* the card is selected.
*
* @param c Card to get the image for
* @param isSelected flag that indicates if the card is selected
* @return String representation of the image
*/
private String imageFileName(Card c, boolean isSelected) {
String str = "cards/";
if (c == null) {
return "cards/back1.GIF";
}
str += c.rank() + c.suit();
if (isSelected) {
str += "S";
}
str += ".GIF";
return str;
}
/**
* Returns the image file name corresponding to the card,
*/
private String imageFileName(Card c){
return imageFileName(c, false);
}
/**
* Getter for the game object
*/
protected Ratscrew getGame(){
return game;
}
/**
* Getter for a previous card on the center
*/
protected JLabel getPreviousCard(int index){
if (previousCards.length > index && !(index<0)) return previousCards[index];
return null;
}
/**
* Getter for the deck of a specific player
* @param player the player whose card label is to be returned
*/
protected JLabel getPlayerCards(int index){
if (playerCards.length > index && !(index<0)) return playerCards[index];
return null;
}
/**
* Getter for the data label of a specific player
*/
protected JLabel getPlayerData(int index){
if (playerData.length > index && !(index<0)) return playerData[index];
return null;
}
/**
* Getter for the win message label
*/
protected JLabel getWinMsg(){ return winMsg;}
/**
* Getter for the status message label
*/
protected JLabel getStatusMsg(){return statusMsg;}
/**
* Return the number of cards on the center
*/
protected int pileSize(){return previousCards.length;}
/**
* Return the number of players playing the game
*/
protected int players(){return playerCards.length;}
}
/**
* Getter for the gui's game member object
*/
public Ratscrew getGame(){return game;}
/**
* Return the panel member of the gui
*/
public CardPanel getPanel(){return panel;}
}