Backtrack迷宫游戏怎么放定时器?

时间:2016-05-31 08:11:28

标签: java backtracking maze

import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class Backtrack
{
    private JFrame frame;

    private JPanel panel;

    private JTable table;

    public Backtrack()

    {
        table = new JTable(5, 5){

            DefaultTableCellRenderer render = new DefaultTableCellRenderer();

            {
                render.setHorizontalAlignment(SwingConstants.CENTER);
            }

            @Override
            public TableCellRenderer getCellRenderer(int arg0, int arg1)
            {
                return render;
            }

        };
        table.setRowHeight(55);
        table.setCellSelectionEnabled(false);
        table.setFont(new Font("Segoe UI", Font.BOLD, 32));
        table.setValueAt("X", 1, 0);
        table.setValueAt("X", 1, 2);
        table.setValueAt("X", 1, 3);
        table.setValueAt("X", 3, 1);
        table.setValueAt("X", 4, 0);
        table.setValueAt("X", 4, 2);
        table.setValueAt("X", 2, 3);
        table.setValueAt("X", 3, 4);

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(500, 350));
        panel.add(table);

        frame = new JFrame("Backtracking Algorithm");
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(500, 300);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        solve(0, 0, 5);
    }

    public boolean solve(int row, int col, int n)
    {
        table.setValueAt("O", row, col);

        try
        {
            Thread.sleep(500);
        }
        catch (Exception ex){}

        if (row == n - 1 && col == n - 1)
        {
            return true;
        }

        //table.setValueAt("", row, col);

        if (validMove(row, col - 1, n))
        {
            if (solve(row, col - 1, n))
            {
                return true;
            }
        }

        if (validMove(row - 1, col, n))
        {
            if (solve(row - 1, col, n))
            {
                return true;
            }
        }

        if (validMove(row, col + 1, n))
        {
            if (solve(row, col + 1, n))
            {
                return true;
            }
        }

        if (validMove(row + 1, col, n))
        {
            if (solve(row + 1, col, n))
            {
                return true;
            }
        }

        table.setValueAt("", row, col);
        return false;
    }

    public boolean validMove(int row, int col, int n)
    {
        if (row > n - 1 || col > n - 1 || row < 0 || col < 0)
        {
            return false;
        }

        if (table.getValueAt(row, col) != null)
        {
            return false;
        }

        return true;

    }

    public static void main(String[] args)
    {
        Backtrack backtrack = new Backtrack();
    }
}

1 个答案:

答案 0 :(得分:0)

对于你可以实现的计时器

  

org.apache.commons.lang.time.StopWatch

  

Java.lang.System.nanoTime()

对于得分,您只需声明一个变量,并在solve()返回true

时递增它