-2

How to add multiple finish line in traverse? example the player needs to go to the 1st finish line, then to next, then up to the last. this is the code. i want to have 1 up to 4 traversing point. this is the code. i dunno where to put the statement.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MazeSearchGUI extends JPanel implements ActionListener, Runnable {
    private static final long serialVersionUID = 1L;
    private final int WALL = 0; // A wall position value
    private final int EMPTY = 1; // An empty position value
    private final int TRIED = 3; // Previously tried path value
    private final int PATH = 7; // Successful path value
    private final int HEADERSZ = 60; // Height in header area
    private int width, height; // Width and Height
    private Image brick; // Brick images for walls
    private Image flag; // Flag finish line images
    private Image mole; // Flag finish line images
    private int brickWidth, brickHeight; // Brick dimensions
    private int flagWidth, flagHeight; // Flag image dimensions
    private int moleWidth, moleHeight; // Flag image dimensions
    private Color backCol = Color.white; // Header background color
    private Color mazeCol = Color.yellow; // Maze background color
    private Color triedCol = Color.lightGray; // Tried path color
    private Color successCol = Color.green; // Successful path color
    private Timer timer; // Timer to start game
    private int startDelay = 3000; // Delay to start traversal
    private int stepDelay = 1000; // Delay between each step
    private static JPanel mainPanel; // Main game panel
    private static String headLabel; // String at top
    private static boolean success = false; // Flags successful path
    private static int xPos, yPos; // Current position
    // Data structure of Grid
    private static int[][] grid = { { 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1      },
        { 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1 },
        { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1 },
        { 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1 },
        { 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };

    // No Arg Constructor - the thread process runs this
    public MazeSearchGUI() {
    }

    // One Arg Constructor - main panel process runs this
    public MazeSearchGUI(String label) {
        headLabel = label;
        backCol = new Color(255, 182, 193);
        xPos = 0;
        yPos = 0;
        brick = new ImageIcon("brick.png").getImage();
        flag = new ImageIcon("flag.png").getImage();
        mole = new ImageIcon("mole.png").getImage();
        MediaTracker track = new MediaTracker(this);
        track.addImage(brick, 0);
        track.addImage(flag, 0);
        track.addImage(mole, 0);
        try {
            track.waitForAll();
        } catch (InterruptedException e) {
        }
        brickWidth = brick.getWidth(null);
        brickHeight = brick.getHeight(null);
        flagWidth = flag.getWidth(null);
        flagHeight = flag.getHeight(null);
        moleWidth = mole.getWidth(null);
        moleHeight = mole.getHeight(null);
        width = brickWidth * grid[0].length;
        height = brickHeight * grid.length + HEADERSZ;
        setBackground(backCol);
        setPreferredSize(new Dimension(width, height));
        setFocusable(true);
        timer = new Timer(startDelay, this);
        timer.start();
    }
    // runs once just to start the traversal thread
    public void actionPerformed(ActionEvent event) {
        timer.stop();
        (new Thread(new MazeSearchGUI())).start();
    }

    // starts the traversal thread
    public void run() {
        if (traverse(0, 0)) {
            success = true;
            headLabel = "I Found A Way Out!";
            mainPanel.repaint();
            // JOptionPane.showMessageDialog(null,"A successful path was found!");
        } else {
            headLabel = "There's No Way Out!";
            mainPanel.repaint();
            // JOptionPane.showMessageDialog(null,"There is no way out of this maze!");
        }
    }

    // Updates the window
    public void paintComponent(Graphics page) {
        super.paintComponent(page);
        page.setColor(backCol);
        page.fillRect(0, 0, width, HEADERSZ);
        page.setColor(mazeCol);
        page.fillRect(0, HEADERSZ, width, height - HEADERSZ);
        page.setColor(Color.black);
        page.setFont(new Font("Arial", Font.BOLD, 24));
        FontMetrics metrics = page.getFontMetrics();
        int labelWidth = metrics.stringWidth(headLabel);
        page.drawString(headLabel, (width - labelWidth) / 2, HEADERSZ - 20);
        page.drawImage(flag, grid[0].length * brickWidth
                - (brickWidth + flagWidth) / 2, height
                - (brickHeight + flagHeight) / 2, null);
        for (int row = 0; row < grid.length; ++row) {
            for (int col = 0; col < grid[row].length; ++col) {
                if (grid[row][col] == WALL)
                    page.drawImage(brick, col * brickWidth, row * brickHeight
                        + HEADERSZ, null);
                else if (success) {
                    if (grid[row][col] == PATH) {
                        page.setColor(successCol);
                        page.fillRect(col * brickWidth, row * brickHeight
                            + HEADERSZ, brickWidth, brickHeight);
                    }
                } else if (grid[row][col] == TRIED
                        && (row != yPos || col != xPos)) {
                    page.setColor(triedCol);
                    page.fillRect(col * brickWidth, row * brickHeight
                            + HEADERSZ, brickWidth, brickHeight);
                }
            }
        }
        page.drawImage(mole, (xPos + 1) * brickWidth - (brickWidth + moleWidth)
                / 2, HEADERSZ + (yPos + 1) * brickHeight
                - (brickHeight + moleHeight) / 2, null);
    }

    // Attempts to recursively traverse the maze. Inserts special
    // characters indicating locations that have been tried and that
    // eventually become part of the solution.
    public boolean traverse(int row, int col) {
        boolean done = false;
        if (valid(row, col)) {
            pause(stepDelay); // half second waits
            xPos = col;
            yPos = row;
            grid[row][col] = TRIED; // this cell has been tried
            mainPanel.repaint();
            if (row == grid.length - 1 && col == grid[0].length - 1)
                done = true; // the maze is solved
            else {
                done = traverse(row + 1, col); // down
                if (!done)
                    done = traverse(row, col + 1); // right
                if (!done)
                    done = traverse(row - 1, col); // up
                if (!done)
                    done = traverse(row, col - 1); // left
            }
            if (done) // this location is part of the final path
                grid[row][col] = PATH;
        }
        return done;
    }

    // Determines if a specific location is valid.
    private boolean valid(int row, int col) {
        // check if cell is in the bounds of the matrix
        if (row >= 0 && row < grid.length && col >= 0 && col < grid[row].length)
        // check if cell is not blocked and not previously tried
        if (grid[row][col] == EMPTY)
            return true;
        return false;
    }

    public void pause(long millisecs) {
        try {
            Thread.sleep(millisecs);
        } catch (InterruptedException e) {
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Maze Search GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel = new MazeSearchGUI("Trying To Traverse This Maze...");
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }
}
Vivek Singh
  • 2,047
  • 11
  • 24
  • identify your problem more clearly, otherwise difficult for one to go through your complete code – SSH Oct 29 '15 at 08:09
  • Your code block has 179 lines of unindented code. Nobody will read it through. Narrow it down. – Antti29 Oct 29 '15 at 08:19

1 Answers1

0

Your code seems pretty procedural (meaning you have lots of code in a single class, without having different objects with state and behaviour). Applying changes will be very difficult and complex. Try to write object oriented code with different classes, each having their own state and responsibility.

This way you could have one class for the character sprite, one for the grid, one for the traversing points. If you want the points to have a certain order in which they need to be visited, an option would be to implement the state pattern.

Have a look at this https://en.wikipedia.org/wiki/State_pattern and this http://gameprogrammingpatterns.com/state.html for the state pattern.

For more specific answers, please reduce your code example to the minimum needed to understand your question, and provide more information about the game itself.

Raimund Krämer
  • 1,255
  • 1
  • 11
  • 29