-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
152 lines (143 loc) · 4.28 KB
/
GUI.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package maze;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* Group Members: Connor Allen, Cameron White, Liel van der Hoeven
* @author Liel van der Hoeven
*/
public class GUI extends JFrame
{
Grid grid = new Grid();
Maze main = new Maze();
private final int w = 100;
private final int h = 100;
JFrame frame = new JFrame("MAZE");
JButton[][] buttons = new JButton[w][h];
char maze[][] = grid.getGrid();
int x = 1, y = 3;
private String rpos;
public void createMaze()
{
//Create the frame
frame.setLayout(new GridLayout(w,h));
frame.setDefaultCloseOperation(3);
frame.pack();
frame.setSize(1300, 700);
for(int x = 0; x < w; x++)
{
for(int y = 0; y < h; y++)
{
buttons[x][y] = new JButton();
frame.add(buttons[x][y]);
buttons[x][y].setSize(7, 7);
buttons[x][y].setBorderPainted(false);
//buttons[x][y].setVisible(true);
}
}
frame.setVisible(true);
updateColor();
}
public void updateColor()
{
for(int x=0; x<w; x++)
{
for(int y=0; y<h; y++)
{
if(maze[x][y] == 'w')
{
buttons[x][y].setBackground(Color.BLACK);
buttons[x][y].setForeground(Color.BLACK);
}
else if(maze[x][y] == 'p')
{
buttons[x][y].setBackground(Color.ORANGE);
buttons[x][y].setForeground(Color.ORANGE);
}
else if(maze[x][y] == 'r')
{
buttons[x][y].setBackground(Color.RED);
buttons[x][y].setForeground(Color.RED);
}
else
{
buttons[x][y].setBackground(Color.MAGENTA);
buttons[x][y].setForeground(Color.MAGENTA);
}
}
}
//update maze
}
public void updateColorn(char[][] test)
{
for(int x=0; x<w; x++)
{
for(int y=0; y<h; y++)
{
if(test[x][y] == 'p')
{
buttons[x][y].setBackground(Color.ORANGE);
buttons[x][y].setForeground(Color.ORANGE);
}
else if(test[x][y] == 'r')
{
buttons[x][y].setBackground(Color.RED);
buttons[x][y].setForeground(Color.RED);
}
}
}
//update maze
}
public String getString(String oldpos)
{
moveRat(oldpos);
try
{
String topl, top, topr, left, middle, right, bottoml, bottom, bottomr;
topl = String.valueOf(maze[x-1][y-1]);
top = String.valueOf(maze[x-1][y]);
topr = String.valueOf(maze[x-1][y+1]);
left = String.valueOf(maze[x][y-1]);
middle = String.valueOf(maze[x][y]);
right = String.valueOf(maze[x][y+1]);
bottoml = String.valueOf(maze[x+1][y-1]);
bottom = String.valueOf(maze[x+1][y]);
bottomr = String.valueOf(maze[x+1][y+1]);
rpos = topl + top + topr + left + middle + right + bottoml + bottom + bottomr;
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.err.println("It seems as if the rat steped out-of-bounds or successfully finished the maze");
}
return rpos;
}
public void moveRat(String oldrpos)
{
if(oldrpos.indexOf('r') == 1)
{
maze[x][y] = 'p';
x--;
maze[x][y] = 'r';
}
if(oldrpos.indexOf('r') == 3)
{
maze[x][y] = 'p';
y--;
maze[x][y] = 'r';
}
if(oldrpos.indexOf('r') == 5)
{
maze[x][y] = 'p';
y++;
maze[x][y] = 'r';
}
if(oldrpos.indexOf('r') == 7)
{
maze[x][y] = 'p';
x++;
maze[x][y] = 'r';
}
main.update(maze);
}
}