-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
115 lines (106 loc) · 3.05 KB
/
View.java
File metadata and controls
115 lines (106 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class View extends JFrame{
private int [][] maze =
{ {0,0,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,1,0,1,0,0,0,0,0,1},
{1,0,1,0,0,0,1,0,1,1,1,0,1},
{1,0,0,0,1,1,1,0,0,0,0,0,1},
{1,0,1,0,0,0,0,0,1,1,1,0,1},
{1,0,1,0,1,1,1,0,1,0,0,0,1},
{1,0,1,0,1,0,0,0,1,1,1,0,1},
{1,0,1,0,1,1,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,0,3}
};
private final List<Integer> path=new ArrayList<Integer>();
private int pindex;
public View() {
setTitle("Simple Maze Solver");
setSize(640,480);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Dfs.searchPath(maze, 1, 1, path);
// pindex=path.size()-2;
// System.out.println(path);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for(int i=0;i<maze.length;i++) {
for(int j=0;j<maze[0].length;j++) {
Color color;
switch(maze[i][j]) {
case 1 : color=Color.BLACK;
break;
case 3 : color=Color.RED;
break;
default : color=Color.WHITE;
break;
}
g.setColor(Color.BLACK);
g.drawRect(35*(j+1),35*(i+1),35,35);
g.setColor(color);
g.fillRect(35*(j+1)+1,35*(i+1)+1,35,35);
}
}
RatInMaze rat=new RatInMaze(maze.length,maze[0].length);
rat.solveMaze(maze,0,0,maze.length-1,maze[0].length-1);
for(int i=0;i<rat.sol.length;i++) {
for(int j=0;j<rat.sol[0].length;j++) {
System.out.print(rat.sol[i][j]+" ");
if(rat.sol[i][j]==1 && maze[i][j]!=3) {
g.setColor(Color.GREEN);
g.fillRect(35*(j+1),35*(i+1),35,35);
}
}
System.out.println();
}
// for(int p=0;p<path.size();p+=2) {
// int px=path.get(p);
// int py=path.get(p+1);
// g.setColor(Color.GREEN);
// g.fillRect(px*30,py*30,30,30);
// }
// int px=path.get(pindex);
// int py=path.get(pindex+1);
// g.setColor(Color.RED);
// g.fillOval(px*30,py*30,30,30);
//
}
// @Override
// protected void processKeyEvent(KeyEvent ke) {
// if (ke.getID() != KeyEvent.KEY_PRESSED) {
// return;
// }
// if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
// pindex -= 2;
// if (pindex < 0) {
// pindex = 0;
// }
// }
// else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
// pindex += 2;
// if (pindex > path.size() - 2) {
// pindex = path.size() - 2;
// }
// }
// repaint();
// }
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
View view=new View();
view.setVisible(true);
}
});
}
}