-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
144 lines (128 loc) · 5.67 KB
/
GUI.java
File metadata and controls
144 lines (128 loc) · 5.67 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class GUI {
private JFrame frame;
private Game game;
private JTextArea gameArea;
private JTextField inputField;
private JButton playButton;
private JButton saveButton;
private JButton loadButton;
private JLabel statusLabel;
private int currentPlayerIndex;
public GUI(int numPlayers) {
game = new Game(numPlayers);
frame = new JFrame("Crazy 8s");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
gameArea = new JTextArea();
gameArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(gameArea);
inputField = new JTextField(10);
playButton = new JButton("Play");
playButton.addActionListener(new PlayButtonListener());
saveButton = new JButton("Save Game");
saveButton.addActionListener(new SaveButtonListener());
loadButton = new JButton("Load Game");
loadButton.addActionListener(new LoadButtonListener());
statusLabel = new JLabel("Player 1's turn");
JPanel panel = new JPanel();
panel.add(new JLabel("Enter card to play (e.g., '5 of Hearts'): "));
panel.add(inputField);
panel.add(playButton);
panel.add(saveButton);
panel.add(loadButton);
frame.getContentPane().add(BorderLayout.NORTH, statusLabel);
frame.getContentPane().add(BorderLayout.CENTER, scrollPane);
frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.setVisible(true);
currentPlayerIndex = 0; // Start with Player 1
updateGameArea();
}
private void updateGameArea() {
gameArea.setText("");
gameArea.append("Top card: " + game.getTopCard().toUnicode() + "\n\n");
for (int i = 0; i < game.getPlayers().size(); i++) {
Player player = game.getPlayer(i);
gameArea.append(player.getName() + "'s hand: ");
for (Card card : player.getHand()) {
gameArea.append(card.toUnicode() + " ");
}
gameArea.append("\n");
}
}
private class PlayButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String input = inputField.getText().trim();
String[] parts = input.split(" of ");
if (parts.length == 2) {
String rank = parts[0].trim();
String suit = parts[1].trim();
Card card = new Card(suit, rank);
Player currentPlayer = game.getPlayer(currentPlayerIndex);
if (game.isValidMove(card)) {
game.playTurn(currentPlayer, card);
currentPlayerIndex = (currentPlayerIndex + 1) % game.getPlayers().size();
game.setCurrentPlayerIndex(currentPlayerIndex);
statusLabel.setText("Player " + (currentPlayerIndex + 1) + "'s turn");
updateGameArea();
if (currentPlayer.hasWon()) {
JOptionPane.showMessageDialog(frame, currentPlayer.getName() + " has won the game!");
playButton.setEnabled(false);
}
} else {
JOptionPane.showMessageDialog(frame, "Invalid move. Please try again.");
}
inputField.setText("");
} else {
JOptionPane.showMessageDialog(frame,
"Invalid input. Please enter a card in the format 'Rank of Suit'.");
}
}
}
private class SaveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String filePath = JOptionPane.showInputDialog(frame,
"Enter file path to save the game (e.g., G:/VS code/CODE/Java/projects/savegame.dat):");
if (filePath != null && !filePath.trim().isEmpty()) {
filePath = filePath.trim();
try {
game.saveGame(filePath);
JOptionPane.showMessageDialog(frame, "Game saved successfully.");
} catch (IOException ex) {
JOptionPane.showMessageDialog(frame, "Failed to save game: " + ex.getMessage());
}
} else {
JOptionPane.showMessageDialog(frame, "Invalid file path. Please try again.");
}
}
}
private class LoadButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String filePath = JOptionPane.showInputDialog(frame, "Enter file path to load the game:");
if (filePath != null && !filePath.trim().isEmpty()) {
try {
Game loadedGame = Game.loadGame(filePath.trim());
game = loadedGame;
currentPlayerIndex = game.getCurrentPlayerIndex();
statusLabel.setText("Player " + (currentPlayerIndex + 1) + "'s turn");
updateGameArea();
JOptionPane.showMessageDialog(frame, "Game loaded successfully.");
} catch (IOException | ClassNotFoundException ex) {
JOptionPane.showMessageDialog(frame, "Failed to load game: " + ex.getMessage());
}
} else {
JOptionPane.showMessageDialog(frame, "Invalid file path. Please try again.");
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GUI(2));
}
}