-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.java
149 lines (131 loc) · 4.72 KB
/
App.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
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.HashMap;
public class App {
JFrame frame = new JFrame(); //creates frame
JButton[][] grid;
Random rand = new Random();
Cheese close = null;
private App(int width, int length, int food) throws InterruptedException {
Cheese[] cheeses = new Cheese[food];
HashMap<Integer, Mouse> mouseMoves = new HashMap<>();
//Builds UI
frame.setLayout(new GridLayout(width,length)); //set layout
grid=new JButton[width][length]; //allocate the size of grid
for(int y=0; y<length; y++) {
for (int x = 0; x < width; x++) {
grid[x][y] = new JButton(); //creates new button
grid[x][y].setPreferredSize(new Dimension(40, 40));
frame.add(grid[x][y]); //adds button to grid
}
}
//Places random cheese
for(int i=0; i<food; i++){
int a = rand.nextInt(12);
int b = rand.nextInt(12);
grid[a][b].setBackground(Color.yellow);
Cheese c = new Cheese(a,b);
cheeses[i]=c;
}
//Places random Cat and mouse
int mx = rand.nextInt(12);
int cx = rand.nextInt(12);
int my = rand.nextInt(12);
int cy = rand.nextInt(12);
grid[cx][cy].setBackground(Color.blue);
grid[mx][my].setBackground(Color.green);
Mouse m = new Mouse(mx,my);
Cat c = new Cat(cx,cy);
//Copy cheese spots before they're overwritten
Cheese[] cheddar = cheeses;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(405,430);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
//Euclidean travel of mouse. Stores moves in Hashmap in order
eatNext(cheeses,m);
int mm = 0;
//Find route of mouse and add to hashmap
//grid[m.Xcor][m.Ycor].setBackground(Color.lightGray);
mouseMoves.put(mm, m);
while(true){
if(close == null)eatNext(cheeses,m);
m = moveMouse(m, close);
mm++;
mouseMoves.put(mm,new Mouse(m.Xcor,m.Ycor));
System.out.println("X:"+m.Xcor+" Y:"+m.Ycor);
eatCheese(m,cheeses);
int lost = 0;
for (int i=0; i < cheeses.length; i++){
if (cheeses[i].Ycor == 100) lost++;
}
if (lost == cheeses.length)break;
}
System.out.println("Mouse Moves "+ mouseMoves.size());
Search s = new Search(c,mouseMoves, cheddar);
ArrayList<Cat> cats;
//Find route of cat to catch mouse
cats = s.bfs();
System.out.println("win length: "+cats.size());
//Update UI to show capture
for(int i=0; i<=cats.size()-1;i++) {
//Moves mouse
m = mouseMoves.get(i);
c = cats.get(i);
update(m, c);
}
}
//Finds closest cheese to mouse
public void eatNext(Cheese[] cheeses, Mouse m){
double near = 0;
double temp;
for (Cheese ch:cheeses) {
if (ch != null) {
temp = closest(m.Xcor, m.Ycor, ch.Xcor, ch.Ycor);
if (near == 0 || near > temp) {
near = temp;
close = ch;
}
}
}
}
// Calculates closest cheese to mouse
public double closest(int x1, int y1, int x2,int y2) {
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
//Moves mouse towards closest cheese
public Mouse moveMouse(Mouse m, Cheese c){
Mouse move = m;
if(m.Xcor < c.Xcor) move.moveRight();
if(m.Xcor > c.Xcor) move.moveLeft();
if(m.Ycor < c.Ycor) move.moveDown();
if(m.Ycor > c.Ycor) move.moveUp();
return move;
}
//Updates UI
public void update(Mouse m,Cat c) throws InterruptedException {
grid[c.Xcor][c.Ycor].setBackground(Color.blue);
grid[m.Xcor][m.Ycor].setBackground(Color.green);
Thread.sleep(500);
grid[m.Xcor][m.Ycor].setBackground(Color.lightGray);
Thread.sleep(200);
grid[c.Xcor][c.Ycor].setBackground(Color.blue);
Thread.sleep(500);
grid[c.Xcor][c.Ycor].setBackground(Color.gray);
}
//Sets eaten cheese to null
public void eatCheese(Mouse m,Cheese[] cs){
for (Cheese ch : cs) {
if (ch.Xcor == m.Xcor && ch.Ycor == m.Ycor) {
ch.Xcor = 100;
ch.Ycor = 100;
close = null;
}
}
}
public static void main(String[] args) throws InterruptedException {
new App(13, 13, 3);
}
}