New: AI Generated GUI

This commit is contained in:
2026-04-14 17:31:00 +02:00
parent 42dcd2cc26
commit 044270c813
3 changed files with 103 additions and 30 deletions

View File

@@ -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];

View File

@@ -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);
});
}
}

91
src/GameGUI.java Normal file
View File

@@ -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);
}
}
}
}
}