Change the following code so that the player can see only the neighboring fields at a distance of 3 units.   public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");    private final ImageIcon emptyIcon = new ImageIcon("data/images/empty.png");     public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {        this.labyrinth = labyrinth;        this.player = player;        this.dragon = dragon;         setFocusable(true);        addKeyListener(new KeyAdapter() {            @Override            public void keyPressed(KeyEvent e) {                char move = switch (e.getKeyCode()) {                    case KeyEvent.VK_W -> 'W';                    case KeyEvent.VK_S -> 'S';                    case KeyEvent.VK_A -> 'A';                    case KeyEvent.VK_D -> 'D';                    default -> ' ';                };                 if (move != ' ') {                    player.move(move, labyrinth);                    dragon.move(labyrinth);                    repaint();                    checkGameState();                }            }        });    }     private void checkGameState() {        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");            System.exit(0);        }         if (Math.abs(player.getX() - dragon.getX()) <= 1 &&            Math.abs(player.getY() - dragon.getY()) <= 1) {            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");            System.exit(0);        }    }     @Override    protected void paintComponent(Graphics g) {        super.paintComponent(g);        int cellSize = Math.min(getWidth() / labyrinth.getSize(), getHeight() / labyrinth.getSize());         for (int i = 0; i < labyrinth.getSize(); i++) {            for (int j = 0; j < labyrinth.getSize(); j++) {                if (labyrinth.getCell(i, j).isWall()) {                    wallIcon.paintIcon(this, g, j * cellSize, i * cellSize);                } else if(!labyrinth.getCell(i, j).isWall()){                    emptyIcon.paintIcon(this, g, j * cellSize, i * cellSize);                }            }        }         playerIcon.paintIcon(this, g, player.getY() * cellSize, player.getX() * cellSize);        dragonIcon.paintIcon(this, g, dragon.getY() * cellSize, dragon.getX() * cellSize);    }        @Override    public Dimension getPreferredSize() {        return new Dimension(320, 320);    }}   public class Labyrinth {    private final int size;    private final Cell[][] grid;      public Labyrinth(int size) {        if (size < 2) {            throw new IllegalArgumentException("Labyrinth size must be at least 2x2.");        }        this.size = size;        this.grid = new Cell[size][size];        generateLabyrinth();    } }

EBK JAVA PROGRAMMING
8th Edition
ISBN:9781305480537
Author:FARRELL
Publisher:FARRELL
Chapter16: Graphics
Section: Chapter Questions
Problem 16RQ
icon
Related questions
Question
100%

Change the following code so that the player can see only the neighboring fields at a distance of 3 units.

 

public class GameGUI extends JPanel {
    private final Labyrinth labyrinth;
    private final Player player;
    private final Dragon dragon;

    private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");
    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");
    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");
    private final ImageIcon emptyIcon = new ImageIcon("data/images/empty.png");


    public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {
        this.labyrinth = labyrinth;
        this.player = player;
        this.dragon = dragon;

        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                char move = switch (e.getKeyCode()) {
                    case KeyEvent.VK_W -> 'W';
                    case KeyEvent.VK_S -> 'S';
                    case KeyEvent.VK_A -> 'A';
                    case KeyEvent.VK_D -> 'D';
                    default -> ' ';
                };

                if (move != ' ') {
                    player.move(move, labyrinth);
                    dragon.move(labyrinth);
                    repaint();
                    checkGameState();
                }
            }
        });
    }

    private void checkGameState() {
        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {
            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");
            System.exit(0);
        }

        if (Math.abs(player.getX() - dragon.getX()) <= 1 &&
            Math.abs(player.getY() - dragon.getY()) <= 1) {
            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");
            System.exit(0);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int cellSize = Math.min(getWidth() / labyrinth.getSize(), getHeight() / labyrinth.getSize());

        for (int i = 0; i < labyrinth.getSize(); i++) {
            for (int j = 0; j < labyrinth.getSize(); j++) {
                if (labyrinth.getCell(i, j).isWall()) {
                    wallIcon.paintIcon(this, g, j * cellSize, i * cellSize);
                } else if(!labyrinth.getCell(i, j).isWall()){
                    emptyIcon.paintIcon(this, g, j * cellSize, i * cellSize);
                }
            }
        }

        playerIcon.paintIcon(this, g, player.getY() * cellSize, player.getX() * cellSize);
        dragonIcon.paintIcon(this, g, dragon.getY() * cellSize, dragon.getX() * cellSize);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 320);
    }
}

 

public class Labyrinth {
    private final int size;
    private final Cell[][] grid;

     public Labyrinth(int size) {
        if (size < 2) {
            throw new IllegalArgumentException("Labyrinth size must be at least 2x2.");
        }
        this.size = size;
        this.grid = new Cell[size][size];
        generateLabyrinth();
    }

}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT