Change the following code so that when player wins the game, the game continues by creating new GameGUI with the same player. However the player's starting position is same, everything else should be reseted. public static void main(String[] args) {        Labyrinth labyrinth = new Labyrinth(10);        Player player = new Player(9, 0);        Random rand = new Random();        Dragon dragon = new Dragon(rand.nextInt(10), 9);        JFrame frame = new JFrame("Labyrinth Game");        GameGUI gui = new GameGUI(labyrinth, player, dragon);         frame.setLayout(new BorderLayout());        frame.setSize(600, 600);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.add(gui, BorderLayout.CENTER);        frame.pack();        frame.setResizable(false);        frame.setVisible(true);    } public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;    private Timer timer;    private long elapsedTime;     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;        String playerName = JOptionPane.showInputDialog("Enter your name:");        player.setName(playerName);         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) {            player.setScore(player.getScore() + 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) {            String name = player.getName();            int score = player.getScore();             Object[] options = {"Quit", "Restart"};            int option = JOptionPane.showOptionDialog(this, "The dragon caught you! Game Over.", "Game Over", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);            if (option == JOptionPane.YES_OPTION) {                System.exit(0);            } else if (option == JOptionPane.NO_OPTION) {                //restartGame();            }        }    }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Question
100%

Change the following code so that when player wins the game, the game continues by creating new GameGUI with the same player. However the player's starting position is same, everything else should be reseted.

public static void main(String[] args) {
        Labyrinth labyrinth = new Labyrinth(10);
        Player player = new Player(9, 0);
        Random rand = new Random();
        Dragon dragon = new Dragon(rand.nextInt(10), 9);
        JFrame frame = new JFrame("Labyrinth Game");
        GameGUI gui = new GameGUI(labyrinth, player, dragon);

        frame.setLayout(new BorderLayout());
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(gui, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);    }

public class GameGUI extends JPanel {
    private final Labyrinth labyrinth;
    private final Player player;
    private final Dragon dragon;
    private Timer timer;
    private long elapsedTime;

    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;
        String playerName = JOptionPane.showInputDialog("Enter your name:");
        player.setName(playerName);

        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) {
            player.setScore(player.getScore() + 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) {
            String name = player.getName();
            int score = player.getScore();

            Object[] options = {"Quit", "Restart"};
            int option = JOptionPane.showOptionDialog(this, "The dragon caught you! Game Over.", "Game Over", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            if (option == JOptionPane.YES_OPTION) {
                System.exit(0);
            } else if (option == JOptionPane.NO_OPTION) {
                //restartGame();
            }
        }
    }

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education