From 044270c81314b4c11ddd23d6d876684e8f7cf3d6 Mon Sep 17 00:00:00 2001 From: KoCoder Date: Tue, 14 Apr 2026 17:31:00 +0200 Subject: [PATCH] New: AI Generated GUI --- src/Cat.java | 7 +++- src/CatchTheMouse.java | 35 +++------------- src/GameGUI.java | 91 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 src/GameGUI.java diff --git a/src/Cat.java b/src/Cat.java index aa9def0..d6e2d63 100644 --- a/src/Cat.java +++ b/src/Cat.java @@ -25,9 +25,14 @@ public class Cat extends Player{ int posX = Integer.parseInt(inputArr[0]); int posY = Integer.parseInt(inputArr[1]); + movePlayer(posX, posY); + } + + public void movePlayer(int posX, int posY) throws IOException { if (!gArea.validatePosition(new Position(posX,posY))) { System.out.println("Invalid input!"); - System.exit(1); + if (scanner != null && scanner.hasNext()) System.exit(1); // Only exit if in console mode + return; } Position p = gArea.playingField[posX][posY]; diff --git a/src/CatchTheMouse.java b/src/CatchTheMouse.java index 62f0e29..c64c5ee 100644 --- a/src/CatchTheMouse.java +++ b/src/CatchTheMouse.java @@ -1,37 +1,14 @@ import java.io.IOException; -import java.util.Scanner; + public class CatchTheMouse { public static void main(String[] args) throws IOException { - - Scanner scanner = new Scanner(System.in); - int width; - int height; - int round = 0; - - System.out.println("Welcome to Catch the Mouse"); + System.out.println("Starting GUI..."); Game game = Game.getInstance(10, 10); - try { - while(!game.gameOver()) - { - game.writer.newLine(); - game.writer.write("Round " + round); - game.writer.newLine(); - game.printField(); - game.mouse.movePlayer(); - game.cat.movePlayer(); - Thread.sleep(1000); - - round++; - } - game.writer.write("Game end"); - game.writer.flush(); - - System.out.println("Congrats! You caught the mouse!"); - } catch (Exception e) { - game.writer.flush(); - e.printStackTrace(); - } + // Launch GUI + javax.swing.SwingUtilities.invokeLater(() -> { + new GameGUI(game); + }); } } diff --git a/src/GameGUI.java b/src/GameGUI.java new file mode 100644 index 0000000..f570401 --- /dev/null +++ b/src/GameGUI.java @@ -0,0 +1,91 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; + +public class GameGUI extends JFrame { + private final int SIZE = 10; + private JButton[][] buttons = new JButton[SIZE][SIZE]; + private Game game; + private int round = 0; + + public GameGUI(Game game) { + this.game = game; + setTitle("Catch the Mouse - Sprint 2 GUI"); + setSize(600, 600); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(new GridLayout(SIZE, SIZE)); + + initializeGrid(); + updateGrid(); + + setLocationRelativeTo(null); + setVisible(true); + } + + private void initializeGrid() { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + JButton button = new JButton(); + button.setFont(new Font("Arial", Font.BOLD, 20)); + final int r = i; + final int c = j; + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + handleMove(r, c); + } + }); + buttons[i][j] = button; + add(button); + } + } + } + + private void handleMove(int x, int y) { + try { + if (game.gameOver()) return; + + // Log Round + game.writer.write("Round " + round); + game.writer.newLine(); + + // 1. Mouse moves first (as per our improved strategy) + game.mouse.movePlayer(); + + // 2. Tom moves to clicked position + game.cat.movePlayer(x, y); + + round++; + updateGrid(); + + if (game.gameOver()) { + game.writer.flush(); + JOptionPane.showMessageDialog(this, "Congrats! You caught the mouse!", "Game Over", JOptionPane.INFORMATION_MESSAGE); + System.exit(0); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void updateGrid() { + Position[][] field = game.gArea.getPlayingField(); + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + char ch = field[i][j].displayChar; + buttons[i][j].setText(String.valueOf(ch)); + + // Styling + if (ch == DisplayChars.TOM.getCharacter()) { + buttons[i][j].setForeground(Color.BLUE); + } else if (ch == DisplayChars.JERRY.getCharacter()) { + buttons[i][j].setForeground(Color.RED); + } else { + buttons[i][j].setForeground(Color.BLACK); + } + } + } + } +}