gpt4 book ai didi

java - 图形用户界面 : Highlight the Path It Takes for A Maze?

转载 作者:行者123 更新时间:2023-12-02 06:04:59 25 4
gpt4 key购买 nike

我正在开发一个合作伙伴项目,我的合作伙伴为游戏创建一个解算器类,而我的部分是创建 MazeGUI。

当您单击 Solve JButton 时,它应该突出显示它在 GUI 本身上所采取的路径,但它只突出显示它“结束”的位置,即最右下角,而不是因为它是“W”,所以不应该可访问。如果有解决方案,“F”将变成“RIP”,如果迷宫没有解决方案,则应该有一个 JLabel 表示迷宫无法解决。

如何在解决按钮的 ActionListener 下的代码中输入所有这些内容?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MazeGUI {
String appName = "Zombie Attack!";
JLabel appNameLabel;

private JFrame frame;
private JPanel buttonPanel, solvePanel;
private JButton solveButton;
// private JLabel noSolutionLabel;

private final int rowCount = 10;
private final int colCount = 10;
private final int startRow = 0;
private final int startCol = 1;
private final int endRow = rowCount - 1;
private final int endCol = colCount - 2;

String[][] map;

private void createAndShowGui() {
frame = new JFrame("Zombie Attack!");

solveButton = new JButton("Solve");

map = new String[rowCount][colCount];
buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(rowCount, colCount, 2, 2));

appNameLabel = new JLabel(appName);
for (int rows = 0; rows < rowCount; rows++) {
for (int columns = 0; columns < colCount; columns++) {

map[rows][columns] = " ";

final JLabel jlabel = new JLabel("");
jlabel.setBackground(Color.BLACK);

if (rows == startRow && columns == startCol) {
jlabel.setBackground(Color.BLACK);
jlabel.setForeground(Color.MAGENTA);
jlabel.setText("S");
map[startRow][startCol] = "S";
}

if (rows == endRow && columns == endCol) {
jlabel.setBackground(Color.BLACK);
jlabel.setForeground(Color.MAGENTA);
jlabel.setText("F");
map[endRow][endCol] = "F";
}

if (!(rows == startRow && columns == startCol || rows == endRow
&& columns == endCol)) {
if (rows == 0 || rows == rowCount - 1 || columns == 0
|| columns == colCount - 1) {
jlabel.setBackground(Color.LIGHT_GRAY);
map[rows][columns] = "W";
}
}


final int rc = rows;
final int cc = columns;


jlabel.addMouseListener(new MouseListener() {
boolean clicked = false;

@Override
public void mouseClicked(MouseEvent e) {
if (clicked == false) {
clicked = true;
jlabel.setBackground(Color.LIGHT_GRAY);
map[rc][cc] = "W";


} else {
clicked = false;
jlabel.setBackground(Color.BLACK);
map[rc][cc] = "";

}
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {
}

});

solveButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent event) {

MazeSolver solver;
solver = new MyMazeSolver();
solver.solve(map);
jlabel.setBackground(Color.RED);
jlabel.setForeground(Color.GREEN);
jlabel.setText("RIP");

for (int i = 0; i < map.length; i++){
for (int j = 0; j < map[0].length; j++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
});
System.out.print(map[rows][columns] + " ");
buttonPanel.add(jlabel);

jlabel.setOpaque(true);
}
System.out.println();
}
frame.add(appNameLabel, BorderLayout.NORTH);
frame.add(buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(500, 500);
frame.add(solveButton, BorderLayout.SOUTH);

frame.setVisible(true);
}

public static void main(String[] args) {
MazeGUI maze = new MazeGUI();
maze.createAndShowGui();
}
}

这是求解器的界面:

public interface MazeSolver {
public String[][] solve(String[][] map);
}

这是我合作伙伴的代码(坐标类):

public class Coordinate {

private int row;
private int col;

public Coordinate(int row, int col) {
this.row = row;
this.col = col;
}

public int getRow() {
return row;
}

public int getCol() {
return col;
}

@Override
public String toString() {
return "Coordinate [row=" + row + ", col=" + col + "]";
}
}

MyMazeSolver 类:

import java.util.ArrayList;

public class MyMazeSolver implements MazeSolver {
private static ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();

private int row;
private int col;

@Override
public String[][] solve(String[][] map) {
startingPos(map);
//for (int i = 0; i < 15; i++) {
do{
makeMove(map);

if (nextToF(map, row, col)) {
System.out.println(row + "" + col);
map[row][col] = "X";
} else {

if (isExplorable(map, row, col)) {
map[row][col] = "D";
} else {
map[row][col] = "X";
}

if(deadEnd(map, row, col)){

}
}
addCoordinates(row, col);
//}
}while(!isEnded(map));
print(map);

// delete printCoordinates
printCoordinates();
return map;
}

public void startingPos(String[][] map) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (map[i][j].contains("S")) {
row = i;
col = j;
}
}
}
}

public boolean clear(String[][] map, int row, int col) {


if (row <= 0 || col <= 0 ||row > map.length) {
return false;
}
if (col <= 0 || col > map[0].length) {
return false;
}

if ("S".equals(map[row][col])) {
return false;
}
if ("W".equals(map[row][col])) {
return false;
}
if ("X".equals(map[row][col])) {
return false;
}
if ("D".equals(map[row][col])) {
return false;
}

return true;
}

public void makeMove(String[][] map) {
if (clear(map, row + 1, col)) {
row++;
} else if (clear(map, row, col - 1)) {
col--;
} else if (clear(map, row, col + 1)) {
col++;
} else if (clear(map, row, col)) {
row--;
}

}

public boolean explorable(String[][] map, int row, int col) {
if (row > map.length) {
return false;
}
if (col > map[0].length) {
return false;
}

if (map[row][col].equals("S")) {
return false;
}
if (map[row][col].equals("W")) {
return false;
}
if (map[row][col].equals("X")) {
return false;
}
if (map[row][col].equals("D")) {
return false;
}

return true;
}

public boolean isExplorable(String[][] map, int row, int col) {
int squares = 0;

if (explorable(map, row + 1, col)) {
squares++;
}
if (explorable(map, row, col - 1)) {
squares++;
}
if (explorable(map, row, col + 1)) {
squares++;
}
if (explorable(map, row - 1, col)) {
squares++;
}

if (squares > 1) {
return true;
} else {
return false;
}
}

public void addCoordinates(int row, int col) {
coordinates.add(new Coordinate(row, col));
}

public void printCoordinates() {
for (int i = 0; i < coordinates.size(); i++) {

System.out.println(coordinates.get(i));
}
}

public boolean nextToF(String[][] map, int row, int col) {
if ("F".equals(map[row + 1][col])) {
// row++;
map[row + 1][col] = ("RIP");
return true;
} else if ("F".equals(map[row][col - 1])) {
// col--;
map[row][col - 1] = "RIP";
return true;
} else if ("F".equals(map[row][col + 1])) {
// col++;
map[row][col + 1] = "RIP";
return true;
} else if ("F".equals(map[row - 1][col])) {
// row--;
map[row - 1][col] = "RIP";
return true;
}
return false;
}

public boolean deadEnd(String[][] map, int row, int col) {
int deadEnds = 0;

if (row > map.length) {
deadEnds++;
}
if (col > map[0].length) {
deadEnds++;
}
if (map[row][col].equals("S")) {
deadEnds++;
}
if (map[row][col].equals("W")) {
deadEnds++;
}
if (map[row][col].equals("X")) {
deadEnds++;
}
if (map[row][col].equals("D")) {
deadEnds++;
}

if (deadEnds == 4) {
return true;
} else {
return false;
}

}

public void findD(){

}

public boolean isEnded(String[][] map) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map.length; j++) {
if (map[i][j].equals("RIP")) {
return true;
}
}
}
return false;
}

public void print(String[][] map) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
System.out.print(map[i][j]);

}
System.out.println();
}
}

}

最佳答案

I call his class under the solveButton's ActionListener but I don't know how to update the String[][] map when you click on a JLabel. For example if I click a JLabel at (5,5) the map will update map[5][5] to "W" instead of an empty String.

这里的问题是,您所依赖的信息不会改变。

public void mouseClicked(MouseEvent e) {
if (clicked == false) {
clicked = true;
jlabel.setBackground(Color.LIGHT_GRAY);
map[rowCount - 1][colCount - 1] = "W";

} else {
clicked = false;
jlabel.setBackground(Color.BLACK);
//map[rowCount - 1][colCount -1 ] = "";

}
}

在您的代码中,您依赖于 rowCountcolCount,它们不会(也可能不应该)改变,而只会影响最后一个元素。 JLabel 和 map 条目之间没有关系。

您需要做的是提供某种链接。在这种情况下,我通常会使用某种 Map,键入 JLabel 并维护循环 map 所需的引用值。

例如...

String[][] map;
// 1
private Map<JLabel, Point> labelMap;
//...
private void createAndShowGui() {
//...
map = new String[rowCount][colCount];
// 2
labelMap = new HashMap<>(rowCount * colCount);
//...
// 3
labelMap.put(jlabel, new Point(rows - 1, columns - 1));
jlabel.addMouseListener(new MouseListener() {
  1. 创建一个 Map 类型的新实例变量
  2. 使用 HashMap 创建 labelMap 的新实例
  3. /与给定标签关联

然后在你的MouseListener中,你需要获取被点击的JLabel,获取关联的/ code> 为 map 数组并进行更新...

jlabel.addMouseListener(new MouseListener() {
boolean clicked = false;

@Override
public void mouseClicked(MouseEvent e) {
// Get the source of the event
JLabel label = (JLabel)e.getComponent();
// Get the map indices associated with the given label
Point point = labelMap.get(label);

// Flip the clicked state
clicked = !clicked;
// Update the state accordingly...
if (clicked) {
jlabel.setBackground(Color.LIGHT_GRAY);
map[point.x][point.y] = "W";
} else {
jlabel.setBackground(Color.BLACK);
map[point.x][point.y] = "";
}
}

Also, when you click on the Solve JButton, it is supposed to highlight the path it takes, but it only highlights where it ends. If there is a Solution, "F" will turn into "RIP", and if there is no solution to the maze, it should have a JLabel saying the maze is unsolvable.

有一系列问题导致这不起作用,您应该已经看到异常发生时抛出的情况。

我遇到的第一个问题是一个 NullPointerException ,它是由您的 clear 方法在这一行 if (map[row][col].equals (“S”))){

这立即告诉我 map[row][col] 的值为 null。不知道它是否应该,并不关心,因为它很容易修复使用...

if ("S".equals(map[row][col]))) {

您需要对此方法中的其他 if 语句执行此操作。

我遇到的第二个问题是 ArrayIndexOutOfBoundsException,它是由您的 makeMove 方法引起的

} else if (clear(map, row, col - 1)) {
//...
} else if (clear(map, row - 1, col)) {

这里的主要问题是,当colrow0时会发生什么?您最终不会为此进行边界检查......

您可以使用以下方法在 clear 方法中修复此问题

public boolean clear(String[][] map, int row, int col) {
if (row < 0 || row > map.length) {
return false;
}
if (col < 0 || col > map[0].length) {
return false;
}

例如...

您的nextToF方法导致了类似的问题

关于java - 图形用户界面 : Highlight the Path It Takes for A Maze?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22366080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com