-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.java
177 lines (150 loc) · 5.12 KB
/
GameBoard.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Write a description of class gameboard here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GameBoard extends Actor implements Observer, Subject
{
private static GameBoard instance;
private GameBoard() {
// Set up to observe GamePieces
{
// Set to observe all GamePieces
subject = new GamePiece();
// Attach observer to all GamePieces
subject.attach(this);
}
//maze[][]={{1,1,1,1},{1,0,0,1},{1,0,0,1},{1,0,0,1}};
// maze[0][]=[1,1,1,1];
gi = new GreenfootImage( 420, 441);
gi.clear();
gi.setColor( java.awt.Color.BLACK ) ;
gi.fill() ;
//gi.setColor( java.awt.Color.BLACK ) ;
World wr = getWorld();
setImage(gi);
//gi.scale(240,320);
ppf.setOptions("gridSize=21");
pdf.setOptions("gridSize=21");
ff.setOptions("gridSize=21,fruit=cherry");
}
public static GameBoard getInstance() {
if (instance == null) {
instance = new GameBoard();
}
return instance;
}
protected String observerState;
protected GamePiece subject;
private static String subjectState;
private static ArrayList<Observer> observers = new ArrayList<Observer>() ;
GreenfootImage gi;
static int maze[][]= {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,6,2,2,3,3,2,2,2,1,2,2,2,2,2,2,2,1,6,1},
{1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,1,2,1,0,1},
{1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,1,2,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,1},
{1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1},
{1,0,7,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,3,1},
{1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,0,1,1,0,1},
{0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,0,1},
{0,0,0,1,0,1,1,1,0,0,0,1,1,0,1,5,1,1,0,1},
{0,0,0,1,0,1,0,0,4,0,0,1,0,0,0,0,0,0,0,1},
{1,1,1,1,0,1,0,1,1,0,1,1,0,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0},
{1,1,1,1,2,1,0,1,1,1,1,1,0,1,1,0,1,0,0,0},
{0,0,0,1,2,1,0,0,0,0,0,0,0,0,1,0,1,1,1,1},
{0,0,0,1,2,1,0,1,1,1,1,1,0,1,1,0,0,0,0,1},
{1,1,1,1,2,2,2,2,2,1,2,2,2,2,2,0,1,1,0,1},
{1,2,2,2,2,2,1,2,1,1,1,2,1,2,1,0,1,0,0,1},
{1,2,1,1,1,1,1,2,1,2,1,2,1,2,1,0,1,0,1,1},
{1,6,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,6,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
//{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}
};
/* the coordinator */
int x,y;
/* Reset flag */
boolean reset = true;
PowerPelletFactory ppf = new PowerPelletFactory();
PacDotFactory pdf = new PacDotFactory();
FruitFactory ff = new FruitFactory();
/**
* Act - do whatever the gameboard wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
/* If reset is not true, don't draw the map again!*/
if(reset){
drawMap();
reset = false;
}
}
public void superpw(){
World wr = getWorld();
boolean isWeak = true;
Pinky pinky = Pinky.getPinky();
Clyde clyde = Clyde.getClyde();
Blinky blinky= Blinky.getBlinky();
Inky inky = Inky.getInky();
pinky.setStrategy(isWeak);
clyde.setStrategy(isWeak);
blinky.setStrategy(isWeak);
inky.setStrategy(isWeak);
}
/* Draw the map again*/
public void drawMap(){
x=10;y=10;
World wr = getWorld();
for(int i=0;i<21;i++){
for(int j=0;j<20;j++){
switch(maze[i][j]){
case 1: wr.addObject(new Brick(),x+j*21,y+i*21);break;
case 2: wr.addObject(pdf.buildOrder(),x+j*21,y+i*21);break;
case 3: wr.addObject(ff.buildOrder(),x+j*21,y+i*21);break;
case 4: //wr.addObject(new Blinky(),x+j*21,y+i*21);break;
case 5: //wr.addObject(new Clyde(),x+j*21,y+i*21);break;
case 6: wr.addObject(ppf.buildOrder(),x+j*21,y+i*21);break;
case 7: //wr.addObject(new Pinky(),x+j*21,y+i*21);break;
default: break;
}
}
}
}
public void update() {
observerState = subject.getState();
subjectState = observerState;
// do nothing
//showState();
notifyObservers();
}
public void showState()
{
System.out.println( "Observer: " + this.getClass().getName() + " = " + observerState);
}
public void notifyObservers()
{
for (Observer obj : observers)
{
obj.update();
}
}
public String getState() {
return subjectState ;
}
public void setState(String status) {
subjectState = status ;
notifyObservers();
}
public void attach(Observer obj) {
observers.add(obj);
}
public void detach(Observer obj) {
observers.remove(obj) ;
}
}