116 lines
3.8 KiB
Java
116 lines
3.8 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.io.*;
|
|
import java.util.List;
|
|
import java.util.Arrays;
|
|
import java.util.ArrayList;
|
|
import java.util.Scanner;
|
|
|
|
public class HangmanGUI extends JFrame {
|
|
private HangmanGame game;
|
|
private JLabel wordLabel, historyLabel, statusLabel, imageLabel;
|
|
private JTextField input;
|
|
private JButton restartBtn;
|
|
private JCheckBoxMenuItem showHistoryItem;
|
|
private List<String> words;
|
|
|
|
public HangmanGUI() {
|
|
setTitle("Swing Hangman");
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
setSize(400, 500);
|
|
setLayout(new BorderLayout());
|
|
|
|
loadWords();
|
|
game = new HangmanGame(words);
|
|
|
|
// === Top Menu ===
|
|
JMenuBar menuBar = new JMenuBar();
|
|
JMenu settingsMenu = new JMenu("Settings");
|
|
|
|
JMenu triesMenu = new JMenu("Tries");
|
|
int[] triesOptions = {3,6,9,12};
|
|
for (int n : triesOptions) {
|
|
JMenuItem item = new JMenuItem(n + " tries");
|
|
item.addActionListener(e -> { game.setMaxErrors(n); resetGame(); });
|
|
triesMenu.add(item);
|
|
}
|
|
showHistoryItem = new JCheckBoxMenuItem("Show history", true);
|
|
showHistoryItem.addActionListener(e -> historyLabel.setVisible(showHistoryItem.isSelected()));
|
|
|
|
settingsMenu.add(triesMenu);
|
|
settingsMenu.add(showHistoryItem);
|
|
menuBar.add(settingsMenu);
|
|
setJMenuBar(menuBar);
|
|
|
|
// === Panels ===
|
|
JPanel top = new JPanel(new GridLayout(4, 1));
|
|
wordLabel = new JLabel("", SwingConstants.CENTER);
|
|
wordLabel.setFont(new Font("Monospaced", Font.BOLD, 24));
|
|
historyLabel = new JLabel("", SwingConstants.CENTER);
|
|
statusLabel = new JLabel("Tries left: " + (game.getMaxErrors() - game.getErrors()), SwingConstants.CENTER);
|
|
imageLabel = new JLabel(new ImageIcon("img/hang0.png"), SwingConstants.CENTER);
|
|
|
|
input = new JTextField();
|
|
input.setHorizontalAlignment(JTextField.CENTER);
|
|
input.addActionListener(e -> handleGuess());
|
|
restartBtn = new JButton("Restart");
|
|
restartBtn.addActionListener(e -> resetGame());
|
|
|
|
top.add(wordLabel);
|
|
top.add(historyLabel);
|
|
top.add(statusLabel);
|
|
top.add(input);
|
|
|
|
add(top, BorderLayout.NORTH);
|
|
add(imageLabel, BorderLayout.CENTER);
|
|
add(restartBtn, BorderLayout.SOUTH);
|
|
|
|
refresh();
|
|
setVisible(true);
|
|
}
|
|
|
|
private void loadWords() {
|
|
words = new ArrayList<>();
|
|
File f = new File("words.txt");
|
|
if (f.exists()) {
|
|
try (Scanner sc = new Scanner(f)) {
|
|
while (sc.hasNext()) words.add(sc.next().trim());
|
|
} catch (Exception e) { e.printStackTrace(); }
|
|
}
|
|
if (words.isEmpty()) {
|
|
words = Arrays.asList("JAVA", "SWING", "SQUIRREL", "PYTHON", "FLUFFY");
|
|
}
|
|
}
|
|
|
|
private void handleGuess() {
|
|
String text = input.getText().trim();
|
|
if (text.length() == 0) return;
|
|
char c = text.charAt(0);
|
|
game.guess(c);
|
|
input.setText("");
|
|
refresh();
|
|
}
|
|
|
|
private void refresh() {
|
|
wordLabel.setText(game.getMaskedWord());
|
|
historyLabel.setText("History: " + game.getHistory());
|
|
statusLabel.setText("Tries left: " + (game.getMaxErrors() - game.getErrors()));
|
|
imageLabel.setIcon(new ImageIcon("img/hang" + Math.min(game.getErrors(), 9) + ".png"));
|
|
|
|
if (game.isWon()) {
|
|
JOptionPane.showMessageDialog(this, "Solved! Word was: " + game.getWord());
|
|
} else if (game.isLost()) {
|
|
JOptionPane.showMessageDialog(this, "Game Over! Word was: " + game.getWord());
|
|
}
|
|
}
|
|
|
|
private void resetGame() {
|
|
game = new HangmanGame(words);
|
|
refresh();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SwingUtilities.invokeLater(HangmanGUI::new);
|
|
}
|
|
}
|