added save management

This commit is contained in:
Lukian LEIZOUR 2024-03-05 17:59:54 +01:00
parent de8f816d66
commit 9becc62ff7
5 changed files with 149 additions and 16 deletions

3
.gitignore vendored
View file

@ -1,3 +1,2 @@
*.class
*.jar
saves/*.txt
*.jar

View file

@ -2,8 +2,12 @@
import java.util.Scanner;
import java.time.Instant;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.util.Set;
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class AppLaser {
public static void main(String [] args) {
@ -65,9 +69,11 @@ public class AppLaser {
System.out.println("\n1 - Resize universe");
System.out.println("2 - Change start position");
System.out.println("3 - Add obstacle");
System.out.println("4 - Save universe");
System.out.println("5 - Load universe");
System.out.println("6 - Resolve");
System.out.println("4 - Reset universe");
System.out.println("5 - Reset obstacles");
System.out.println("6 - Save universe");
System.out.println("7 - Load universe");
System.out.println("8 - Resolve");
System.out.println("0 - Exit");
// prompt the user
@ -130,6 +136,17 @@ public class AppLaser {
break;
case 4:
universe.resetUniverse();
break;
case 5:
universe.resetUniverseObstacles();
break;
case 6:
System.out.print("\nHow do you want to name your universe : ");
String name = "";
@ -137,26 +154,68 @@ public class AppLaser {
name = scanner.nextLine();
}
universe.save(name);
break;
case 7:
Set<String> files = Stream.of(new File("./saves").listFiles()).filter(file -> !file.isDirectory()).map(File::getName).collect(Collectors.toSet());
System.out.println("\nAvailable files : \n");
for (String element : files) {
System.out.println(element.replace(".txt", ""));
}
System.out.print("\nWhat universe do you want load ? : ");
String name2 = "";
while (name2.isEmpty()) {
name2 = scanner.nextLine();
}
try {
File file = new File("./saves/" + name + ".txt");
BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name2 + ".txt"));
universe_height = Integer.valueOf(reader.readLine());
universe_width = Integer.valueOf(reader.readLine());
start_dir = Integer.valueOf(reader.readLine());
start_i = Integer.valueOf(reader.readLine());
start_j = Integer.valueOf(reader.readLine());
file.createNewFile();
FileWriter writer = new FileWriter("./saves/" + name + ".txt");
universe.changeUniverseDim(universe_width, universe_height);
for (int i = 0; i < universe_height + 2; i++) {
for (int j = 0; j < universe_width + 2; j++) {
writer.write(universe.grid[i][j]);
firstState_i = start_i;
firstState_j = start_j;
if (start_dir == 10) firstState_i = start_i - 1;
else if (start_dir == 11) firstState_i = start_i + 1;
else if (start_dir == 12) firstState_j = start_j + 1;
else if (start_dir == 13) firstState_j = start_j - 1;
currentState = new Situation(firstState_i, firstState_j, start_dir, 0);
universe.changeUniverseStart(start_i, start_j, start_dir);
while (true) {
try {
int pos1 = Integer.valueOf(reader.readLine());
int pos2 = Integer.valueOf(reader.readLine());
universe.addObstacle(pos1, pos2);
}
catch (Exception e) {
break;
}
writer.write("\n");
}
writer.close();
}
catch (Exception e) {}
break;
case 6:
case 8:
boolean display_progress = false, display_regress = false;
System.out.println("\n1 - display progress and regress");

View file

@ -1,9 +1,12 @@
// Antoine CRETUAL, Lukian LEIZOUR, 14/02/2024
import java.io.File;
import java.io.FileWriter;
public class Universe {
// Atributes
public int[][] grid;
private int[][] grid;
private int width, height;
private int boxes_to_fill;
@ -105,6 +108,18 @@ public class Universe {
}
}
public void resetUniverseObstacles() {
for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) {
if (this.grid[i][j] != 10 && this.grid[i][j] != 11 && this.grid[i][j] != 12 && this.grid[i][j] != 13 && this.grid[i][j] != 0) {
this.grid[i][j] = 0;
}
}
}
this.boxes_to_fill = ((this.height - 2) * (this.width - 2)) - 1;
}
public void changeUniverseDim(int width, int height) {
int [][] newgrid = new int[width][height];
@ -329,4 +344,30 @@ public class Universe {
public boolean isSolved() {
return this.boxes_to_fill == 0;
}
public void save(String fileName) {
try {
File file = new File("./saves/" + fileName + ".txt");
file.createNewFile();
FileWriter writer = new FileWriter("./saves/" + fileName + ".txt");
writer.write(this.height + "\n" + this.width + "\n");
for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) {
if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12 || this.grid[i][j] == 13) writer.write(this.grid[i][j] + "\n" + i + "\n" + j + "\n");
}
}
for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) {
if (this.grid[i][j] == -1) writer.write(i + "\n" + j + "\n");
}
}
writer.close();
}
catch (Exception e) {}
}
}

5
saves/default.txt Normal file
View file

@ -0,0 +1,5 @@
5
5
11
1
1

29
saves/rightBlock.txt Normal file
View file

@ -0,0 +1,29 @@
12
12
10
6
1
1
8
1
9
1
10
2
8
2
9
2
10
3
8
3
9
3
10
4
8
4
9
4
10