-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataStorage.java
347 lines (317 loc) · 11 KB
/
DataStorage.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.*;
public class DataStorage implements Serializable{
private Map[] map;
private SoundEffect[] soundEffect;
private Tutorial tutorial;
private Charts chart;
private Skin[] skins;
//private Color chosenColor;
private String chosenColor;
private Map[][] storedMaps;
private DashboardData d = new DashboardData();
private int noTimes = 0;
public DataStorage() {
storedMaps = new Map[3][15];
storedMaps[0] = getMaps(6);
storedMaps[1] = getMaps(8);
storedMaps[2] = getMaps(10);
//map = new Map[15];
//map[0] = new Map(6,3,5,1);
//map[1] = new Map(8,3,7,1);
//map[2] = new Map(10,3,9,1);
/*
//==========================SOUND EFFECTS=======================================
soundEffect = new SoundEffect[2];
File soundFolder = new File("/Users/Desktop/soundFolder/"); //update the path
File[] listOfFiles = soundFolder.listFiles();
for (File file : listOfFiles) {
int i = 0;
if (file.isFile()) {
soundEffect[i] = new SoundEffect(file.getPath(), 50);
}
i++;
}
//==========================TUTORIAL============================================
String tutorialLoc = "./ / / / "; //path to the directory where tutorial is saved
tutorial = new Tutorial(tutorialLoc, ".jpg");
//==========================SKINS===============================================
skins = new Skin[5];
File skinFolder = new File("/Users/Desktop/skinFolder/"); //update the path
listOfFiles = skinFolder.listFiles();
for (File file : listOfFiles) {
int i = 0;
BufferedImage image = null;
try {
image = ImageIO.read(new File(file.getPath()));
} catch( Exception e) {}
if (file.isFile()) {
skins[i] = new Skin(i, image, true);
}
i++;
}
*/
//===========================CHOSEN COLOR=======================================
chosenColor = "LIGHT_GRAY";
}
//returns maps
public Map[] getMaps(int dimension){
Map[] dimensionMaps = new Map[15];
List<Car> perMapCarList = new ArrayList<>();
Car car ;
boolean firstConfig = true;
String maps = "";
switch(dimension){
case 6: maps = "./storage/6x6dim.csv"; break;
case 8: maps = "./storage/8x8dim.csv"; break;
case 10: maps = "./storage/10x10dim.csv"; break;
}
int mapIndex = 0;
int skinnerIndex = 0;
boolean player = false;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(maps));
String line = reader.readLine();
do {
player = false;
int skinNo = skinnerIndex%10;
skinnerIndex++;
String[] array = line.split(",");
int index = Integer.parseInt(array[0]);
int direction = Integer.parseInt(array[1]);
int startx = Integer.parseInt(array[2]);
int endx = Integer.parseInt(array[3]);
int starty = Integer.parseInt(array[4]);
int endy = Integer.parseInt(array[5]);
int length = Integer.parseInt(array[6]);
if (index == 0){
player = true;
if (!firstConfig){
mapIndex++;
Car[] carArray = new Car[perMapCarList.size()]; //perMapCarList.toArray(new Integer(perMapCarList.size()));
int indexer =0;
for (Car tmp : perMapCarList) {
//System.out.println("Indexer " + indexer);
carArray[indexer] = tmp.clone();
indexer++;
}
int finishX = carArray[0].getX();
Map newMap = new Map();
newMap.setCars(carArray);
newMap.setDimension(dimension);
newMap.setLevel(mapIndex);
newMap.setExitCoordinates(finishX,dimension-1);
//System.out.println("The finish: " + finishX + " " + (dimension-1));
newMap.initMap(finishX,dimension-1);
dimensionMaps[mapIndex-1] = newMap.clone();
perMapCarList = new ArrayList<>();
//System.out.println(newMap);
//return dimensionMaps;
}
firstConfig = false;
}
car = new Car();
car.setImageLocation("/img/"+skinNo);
car.setHorizontalX(startx,endx);
car.setVerticalY(starty,endy );
car.setLength(length);
car.setCarDirection(direction);
car.setPlayer(player);
car.setCarSkin(skinNo);
perMapCarList.add(car.clone());
line = reader.readLine();
} while (line != null);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
mapIndex++;
Car[] carArray = new Car[perMapCarList.size()]; //perMapCarList.toArray(new Integer(perMapCarList.size()));
int indexer =0;
for (Car tmp : perMapCarList) {
//System.out.println("Indexer " + indexer);
carArray[indexer] = tmp.clone();
indexer++;
}
int finishX = carArray[0].getX();
Map newMap = new Map();
newMap.setCars(carArray);
newMap.setDimension(dimension);
newMap.setLevel(mapIndex);
newMap.setExitCoordinates(finishX,dimension-1);
//System.out.println("The finish: " + finishX + " " + (dimension-1));
newMap.initMap(finishX,dimension-1);
dimensionMaps[mapIndex-1] = newMap.clone();
perMapCarList = new ArrayList<>();
for (int i = mapIndex; i < dimensionMaps.length; i++){
dimensionMaps[i] = dimensionMaps[i%mapIndex].clone();
}
return dimensionMaps;
}
public Map getMap(int dimension,int level) {
//System.out.println("My level is : " + level);
if (dimension == 6){
return storedMaps[0][level];
}
else if (dimension == 8){
return storedMaps[1][level];
}
else {
return storedMaps[2][level];
}
}
public void resetNoTimes(){
noTimes = 0;
}
//returns soundEffect
public SoundEffect[] getSoundEffects(){
return soundEffect;
}
//return tutorial file
public Tutorial getTutorial(){
return tutorial;
}
//return chart
public Charts getCharts(){
return chart;
}
//return skins
public Skin[] getSkins(){
return skins;
}
// return chosenColor, default at first
public String getChosenColor(){
return chosenColor;
}
public boolean[] getOpenMapArray(int dimension){
int fileLine = 0;
switch(dimension){
case 6: fileLine = 0; break;
case 8: fileLine = 1; break;
case 10: fileLine = 2; break;
}
BufferedReader reader;
int LineIndex = 0;
boolean[] openMaps = new boolean[15];
try {
reader = new BufferedReader(new FileReader(
"./storage/MapsLocked.txt"));
String line = reader.readLine();
do {
//System.out.println((line.split(",")));
// read next line
//System.out.println(line);
if (fileLine == LineIndex){
String[] array = line.split(",");
for (int i = 0 ; i < array.length; i++){
if (array[i].equals("0")){
openMaps[i] = false;
}else {
openMaps[i] = true;
}
}
}
line = reader.readLine();
LineIndex++;
} while (line != null);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return openMaps;
}
public void updateOpenMaps(int dimension, boolean[] openMaps){
int fileLine = 0;
switch(dimension){
case 6: fileLine = 0; break;
case 8: fileLine = 1; break;
case 10: fileLine = 2; break;
}
BufferedReader reader;
int LineIndex = 0;
String[] readOpenMaps = new String[3];
try {
reader = new BufferedReader(new FileReader(
"./storage/MapsLocked.txt"));
String line = reader.readLine();
do {
if (fileLine == LineIndex){
String tmp = "";
for (int i = 0 ; i < openMaps.length; i++){
System.out.println(openMaps[i]);
if (openMaps[i]){
tmp += "1,";
}else {
tmp += "0,";
}
}
readOpenMaps[LineIndex] = tmp;
} else {
readOpenMaps[LineIndex] = line;
}
line = reader.readLine();
LineIndex++;
} while (line != null);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try (PrintWriter out = new PrintWriter("./storage/MapsLocked.txt")) {
for (int i = 0; i < readOpenMaps.length; i++){
System.out.println(readOpenMaps[i]);
out.println(readOpenMaps[i].substring(0,readOpenMaps[i].length()-1));
}
}catch (IOException e) {
e.printStackTrace();
}
}
public String getSkin(){
BufferedReader reader;
String output = "./img/0";
try {
reader = new BufferedReader(new FileReader(
"./storage/skin.txt"));
String line = reader.readLine();
output = line;
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("My sotrage " + output);
return output;
}
public DashboardData updateDashboard(){
int i = 0;
try {
BufferedReader br = new BufferedReader(new FileReader("./storage/starsFile.txt"));
String s = "";
String[] chunks;
s = br.readLine();
while (s != null) {
chunks = s.split(",");
i += Integer.parseInt(chunks[2]);
s += s + "\n";
s = br.readLine();
}
}
catch(IOException e) {
e.printStackTrace();
}
d.setNoOfStars(i);
return d;
}
/*public static void main(String[] args){
DataStorage tmp = new DataStorage();
/* Map[] test = tmp.getMaps(6);
for (int i = 0; i < test.length; i++){
System.out.println("Map " + i + " " +test[i]);
System.out.println();
}
Map tmpe = tmp.getMap(6,0);
System.out.println("After the cal :");
System.out.println(tmpe);
}*/
}