-
Notifications
You must be signed in to change notification settings - Fork 0
/
WarGameGUI.java
207 lines (183 loc) · 5.71 KB
/
WarGameGUI.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 javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class WarGameGUI extends JFrame
{
private WarGame war;
private ImageIcon cardBackImage;
private ImageIcon[][] cardImages = new ImageIcon[4][13];
private JLabel playerHand;
private JLabel playerPile;
private JLabel computerPile;
private JLabel computerHand;
private JLabel statusMessage;
private JPanel topPanel;
private JPanel bottomPanel;
private JButton nextStepButton;
private JCheckBox userInput;
private final int RANKS = 13;
private final int SUITS = 4;
private String rank;
/**
constructs a war
@throws FileNotFoundException if images are unfound
*/
public WarGameGUI() throws FileNotFoundException
{
war = new WarGame();
try
{
cardBackImage = new ImageIcon(ImageIO.read(new File("cardpics/back.jpg")));
}
catch (IOException ex)
{
throw new FileNotFoundException("Card image: back.jpg not found.");
}
for(int i = 0; i < SUITS; i++)
{
for(int j = 2; j < RANKS; j++)
{
if(i == 0)
rank = "s";
else if (i == 1)
rank = "h";
else if (i == 2)
rank ="d";
else if (i == 3)
rank = "c";
String fileName = "cardpics/"+j+rank+".jpg";
try
{
cardImages[i][j] = new ImageIcon(ImageIO.read(new File(fileName)));
}
catch (IOException ex)
{
throw new FileNotFoundException("Card image: "+j+rank+".jpg not found.");
}
}
}
playerHand = new JLabel(cardBackImage);
computerHand = new JLabel(cardBackImage);
// Leave the piles blank for now.
playerPile = new JLabel();
computerPile = new JLabel();
setLayout(new GridLayout(2,1));
topPanel = new JPanel(new GridLayout(1,4));
topPanel.add(playerHand);
topPanel.add(playerPile);
topPanel.add(computerPile);
topPanel.add(computerHand);
bottomPanel = new JPanel(new GridLayout(2,1));
statusMessage = new JLabel("Welcome to WAR!");
nextStepButton = new JButton("Begin Game");
userInput = new JCheckBox("Would you like to play?");
nextStepButton.addActionListener(new ButtonListener());
bottomPanel.add(userInput);
bottomPanel.add(statusMessage);
bottomPanel.add(nextStepButton);
this.add(topPanel);
this.add(bottomPanel);
}
/**
checks for new card images each time cards are played
*/
private void updateCardImages()
{
if(war.getHumanPlayer().getHumanDeck().size() != 0)
playerHand.setIcon(cardBackImage);
else
playerHand.setIcon(null);
playerHand.revalidate();
if(!war.getHumanPlayer().getHumanDeck().isEmpty())
playerPile.setIcon(cardImages[war.getHumanPlayer().getHumanDeck().get(0).getSuit()][war.getHumanPlayer().getHumanDeck().get(0).getRank()]);
else
playerPile.setIcon(null);
playerPile.revalidate();
if(war.getAIPlayer().getPlayerDeck().size() != 0)
computerHand.setIcon(cardBackImage);
else
computerHand.setIcon(null);
computerHand.revalidate();
if(!war.getAIPlayer().getPlayerDeck().isEmpty())
computerPile.setIcon(cardImages[war.getAIPlayer().getPlayerDeck().get(0).getSuit()][war.getAIPlayer().getPlayerDeck().get(0).getRank()]);
else
computerPile.setIcon(null);
computerPile.revalidate();
}
/**
tells the Player how many cards are in each hand after each turn
*/
private void updateStatusMessage()
{
String newMessage = "<html>"+war.getStatusMessage()
+ "<br>Your hand size: " + war.getHumanPlayer().getHumanDeck().size()
+ "<br>Computer's hand size: " + war.getAIPlayer().getPlayerDeck().size()
+ "</html>";
try
{
statusMessage.setText(newMessage);
}
catch (NullPointerException ex)
{
return; //Work around a Java bug...
}
}
/**
creates actions performed when Button is pressed
*/
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
nextStepButton.setText("Continue Game");
if(userInput.isSelected())
{
war.setResponse(true);
//userInput.setSelected(false);
//nextStepButton.setEnabled(false);
}
else
war.setResponse(false);
updateCardImages();
updateStatusMessage();
war.step();
war.setResponse(false);
updateCardImages();
if (war.getGameState() != 0)
{
// Figure out who won
if (war.getGameState() > 0)
{
javax.swing.JOptionPane.showMessageDialog(null, "The computer won.");
}
else //g.getGameState() > 0
{
javax.swing.JOptionPane.showMessageDialog(null, "You win!");
}
// And finish up.
updateStatusMessage(); // To get the "You win!"/"Computer Wins." message
nextStepButton.setEnabled(false); // Disable the continue button.
}
}
}
public static void main(String[] args)
{
WarGUI wgui;
try
{
wgui = new WarGUI();
}
catch (FileNotFoundException ex)
{
javax.swing.JOptionPane.showMessageDialog(null,ex.toString());
return;
}
wgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wgui.setSize(700,500);
wgui.validate();
wgui.setVisible(true);
}
}