-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveLoader.java
More file actions
63 lines (60 loc) · 2.15 KB
/
SaveLoader.java
File metadata and controls
63 lines (60 loc) · 2.15 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
package JavaRacer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Locale;
import java.util.Scanner;
public class SaveLoader {
public void loadSave(GenerationAlgorithm genAlg){
try {
File saveFile = new File("source/save.txt");
Scanner scanner = new Scanner(saveFile).useLocale(Locale.US);
if(scanner.hasNext()){
genAlg.generationNumber = scanner.nextInt();
genAlg.instructionSize = scanner.nextInt();
genAlg.lastIncreasePoints = scanner.nextDouble();
scanner.nextLine();
String[] tempInstructions = scanner.nextLine().replaceAll("[\\[\\]]", "").split(", ");
System.out.println(tempInstructions.length);
for (String string : tempInstructions) {
int instruct = Integer.parseInt(string);
for(Agent agent : genAlg.agents){
agent.instructions.add(instruct);
}
}
}
else{
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void save(GenerationAlgorithm genAlg){
try {
PrintStream saveFile = new PrintStream(new File("source/save.txt"));
saveFile.println(genAlg.generationNumber);
saveFile.println(genAlg.instructionSize);
saveFile.println(genAlg.lastIncreasePoints);
saveFile.print(genAlg.agents[genAlg.eliteIndexes.get(0)].instructions.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean hasSaveFile(){
try {
File saveFile = new File("source/save.txt");
Scanner scanner = new Scanner(saveFile);
if(scanner.hasNext()){
scanner.close();
return true;
}
scanner.close();
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
}