-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatAndMouseGame.java
99 lines (83 loc) · 2.44 KB
/
CatAndMouseGame.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
import java.awt.*;
import javax.swing.*;
public class CatAndMouseGame extends Thread {
long delay;
SwingApplet a;
RLPolicy policy;
CatAndMouseWorld world;
static final int GREEDY=0, SMART=1; // type of mouse to use
int mousetype = SMART;
public boolean gameOn = false, single=false, gameActive, newInfo = false;
public CatAndMouseGame(SwingApplet s, long delay, CatAndMouseWorld w, RLPolicy policy) {
world = w;
a=s;
this.delay = delay;
this.policy = policy;
}
/* Thread Functions */
public void run() {
System.out.println("--Game thread started");
// start game
try {
while(true) {
while(gameOn) {
gameActive = true;
resetGame();
SwingUtilities.invokeLater(a); // draw initial state
runGame();
gameActive = false;
newInfo = true;
SwingUtilities.invokeLater(a); // update state
sleep(delay);
}
sleep(delay);
}
} catch (InterruptedException e) {
System.out.println("interrupted.");
}
System.out.println("== Game finished.");
}
public void runGame() {
while(!world.endGame()) {
//System.out.println("Game playing. Making move.");
int action=-1;
if (mousetype == GREEDY) {
action = world.mouseAction();
} else if (mousetype == SMART) {
action = policy.getBestAction(world.getState());
} else {
System.err.println("Invalid mouse type:"+mousetype);
}
world.getNextState(action);
//a.updateBoard();
SwingUtilities.invokeLater(a);
try {
sleep(delay);
} catch (InterruptedException e) {
System.out.println("interrupted.");
}
}
a.mousescore += world.mousescore;
a.catscore += world.catscore;
// turn off gameOn flag if only single game
if (single) gameOn = false;
}
public void interrupt() {
super.interrupt();
System.out.println("(interrupt)");
}
/* end Thread Functions */
public void setPolicy(RLPolicy p) { policy = p; }
public Dimension getMouse() { return new Dimension(world.mx, world.my); }
public Dimension getCat() { return new Dimension(world.cx, world.cy); }
public Dimension getCheese() { return new Dimension(world.chx, world.chy); }
public Dimension getHole() { return new Dimension(world.hx, world.hy); }
public boolean[][] getWalls() { return world.walls; }
public void makeMove() {
world.moveMouse();
world.moveCat();
}
public void resetGame() {
world.resetState();
}
}