-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
184 lines (155 loc) · 3.56 KB
/
Copy pathMap.java
File metadata and controls
184 lines (155 loc) · 3.56 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
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
178
179
180
181
182
183
184
package model;
import java.io.BufferedReader;
import entity.IElement;
import entity.IMap;
import entity.mobile.MobileElementsFactory;
import entity.motionless.MotionlessElementsFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
/**
* <h1>The Map Class.</h1>
*
* @author NGANDO DAVID LANCE
* @version 0.1
*/
class Map extends Observable implements IMap {
/** The width. */
private int width;
/** The height. */
private int height;
/** The on the map. */
private IElement[][] onTheMap;
private boolean isCorrect = true;
private List<Integer> hasChanged = new ArrayList<Integer>();
/**
* Instantiates a new map with the content of the file fileName.
*
* @param fileName the file name where the map is
* @throws IOException Signals that an I/O exception has occurred.
*/
Map(final String fileName) throws IOException {
super();
this.loadFile(fileName);
}
/**
* Loads file.
*
* @param fileName the file name
* @throws IOException Signals that an I/O exception has occurred.
*/
private void loadFile(final String fileName) throws IOException {
final BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line;
try {
line = buffer.readLine();
this.setWidth(Integer.parseInt(line));
line = buffer.readLine();
this.setHeight(Integer.parseInt(line));
} catch (Exception | Error e) {
isCorrect = false;
}
if (isCorrect) {
this.onTheMap = new IElement[this.getWidth()][this.getHeight()];
int y = 0;
line = buffer.readLine();
while (line != null) {
if (line.toCharArray().length == this.width) {
for (int x = 0; x < line.toCharArray().length; x++) {
if (line.toCharArray()[x] == 'R' || line.toCharArray()[x] == 'O' || line.toCharArray()[x] == 'G' || line.toCharArray()[x] == '*') {
this.setOnTheMapXY(MobileElementsFactory.getFromFileSymbol(line.toCharArray()[x]), x, y);
} else {
this.setOnTheMapXY(MotionlessElementsFactory.getFromFileSymbol(line.toCharArray()[x]), x, y);
}
}
} else {
this.isCorrect = false;
}
line = buffer.readLine();
y++;
}
buffer.close();
if (y != this.height) {
this.isCorrect = false;
}
}
}
/*
* Gets element on the map
*
*/
@Override
public final IElement getOnTheMapXY(final int x, final int y) {
return this.onTheMap[x][y];
}
/**
* Sets the on the map XY.
*
* @param element the element
* @param x the x
* @param y the y
*/
public void setOnTheMapXY(IElement element, int x, int y) {
this.onTheMap[x][y] = element;
this.hasChanged.add(x);
this.hasChanged.add(y);
}
/*
* Sets the mobile has changed
*
*/
@Override
public final void setMobileHasChanged() {
this.setChanged();
this.notifyObservers();
}
/*
* Gets the width
*
*/
@Override
public final int getWidth() {
return this.width;
}
/**
* Sets the width.
*
* @param width the new width
*/
private void setWidth(final int width) {
this.width = width;
}
/*
* Gets the height
*
*/
@Override
public final int getHeight() {
return this.height;
}
/**
* Sets the height.
*
* @param height the new height
*/
private void setHeight(final int height) {
this.height = height;
}
/*
* Gets the observable
*
*/
@Override
public Observable getObservable() {
return this;
}
public boolean isCorrect() {
return isCorrect;
}
public List<Integer> getHasChanged() {
return hasChanged;
}
}