improved display

This commit is contained in:
Lukian LEIZOUR 2024-03-03 15:39:50 +01:00
parent a5090c2bcf
commit 95b7286f0b
3 changed files with 299 additions and 92 deletions

View file

@ -1,15 +1,29 @@
// Antoine CRETUAL, Lukian LEIZOUR, 14/02/2024
import java.util.Scanner;
public class AppLaser {
public static void main(String [] args) {
// definitions of the starting position and the universe dimensions
boolean cli = false;
int start_i = 3;
int start_j = 1;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-cli":
cli = true;
break;
default:
System.out.println("Unknown argument " + args[i]);
System.exit(0);
}
}
int universe_width = 3;
int universe_height = 3;
int start_i = 0;
int start_j = 0;
int start_dir = 11;
int universe_width = 21;
int universe_height = 20;
int firstState_i = start_i;
int firstState_j = start_j;
@ -18,45 +32,204 @@ public class AppLaser {
else if (start_dir == 12) firstState_j = start_j + 1;
else if (start_dir == 13) firstState_j = start_j - 1;
// creation of the stack, the universe and the first state
if (cli == true) {
System.out.print("\033[H\033[2J");
System.out.flush();
Stack <Situation> stack = new Stack <Situation>();
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i, start_j, start_dir);
Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0);
Scanner scanner = new Scanner(System.in);
// obstacles creation
universe.addObstacle(2, 3);
universe.addObstacle(3, 3);
universe.addObstacle(2, 4);
universe.addObstacle(3, 4);
/*System.out.print("Enter the width of the universe : ");
universe_width = scanner.nextInt();
System.out.print("Enter the length of the universe : ");
universe_height = scanner.nextInt();
System.out.print("Enter the position of the start point in the first axis : ");
start_i = scanner.nextInt();
System.out.print("Enter the position of the start point in the seccond axis : ");
start_j = scanner.nextInt();
System.out.print("Enter the direction of the start point : ");
start_dir = scanner.nextInt();*/
System.out.print("\033[H\033[2J");
System.out.flush();
Stack <Situation> stack = new Stack <Situation>();
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir);
Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0);
universe.print(2, 4);
int choice;
int i = 0;
while (!universe.isSolved()) {
//System.out.printf("i: %d, j: %d, dir: %d, choice: %d, possible: %d\n", currentState.pos_i, currentState.pos_j, currentState.direction, currentState.nb_choix, universe.possibleChoices(currentState));
do {
// clear screen
if (universe.canEvolve(currentState)) {
stack.push(currentState.copy(universe.possibleChoices(currentState)));
currentState = universe.evolve(currentState);
System.out.print("\033[H\033[2J");
System.out.flush();
// print the universe
universe.print(2, 4);
// print the menu
System.out.println("\n1 - Resize universe");
System.out.println("2 - Change start position");
System.out.println("3 - Add obstacle");
System.out.println("4 - Resolve");
// prompt the user
System.out.print("\nChoice : ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the width of the universe : ");
int width = scanner.nextInt();
System.out.print("Enter the length of the universe : ");
int height = scanner.nextInt();
universe.changeUniverseDim(width, height);
break;
case 2:
break;
case 3:
int firstPos_i, firstPos_j, seccondPos_i, seccondPos_j;
System.out.print("\nFirst position of the obstacle i : ");
firstPos_i = scanner.nextInt();
System.out.print("First position of the obstacle j : ");
firstPos_j = scanner.nextInt();
System.out.print("Seccond position of the obstacle i : ");
seccondPos_i = scanner.nextInt();
System.out.print("Seccond position of the obstacle j : ");
seccondPos_j = scanner.nextInt();
for (int i = firstPos_i + 1; i < seccondPos_i + 2; i++) {
for (int j = firstPos_j + 1; j < seccondPos_j + 2; j++) {
universe.addObstacle(i, j);
}
}
break;
}
if (choice == 1) {
}
} while (choice != 2);
boolean display_progress = false, display_regress = false;
System.out.println("1 - display progress and regress");
System.out.println("2 - display progress");
System.out.println("3 - display regress");
System.out.println("4 - display nothing");
System.out.print("\nChoice : ");
choice = scanner.nextInt();
switch (choice) {
case 1:
display_progress = true;
display_regress = true;
break;
case 2:
display_progress = true;
break;
case 3:
display_regress = true;
break;
case 4:
break;
default:
break;
}
else if (stack.size() > 0) {
currentState = stack.pop();
universe.reset(currentState);
universe.print(universe_height + 6, 4);
} else {
break;
System.out.print("\033[?25l");
System.out.print("\033[H\033[2J");
System.out.flush();
universe.print(2, 4);
while (!universe.isSolved()) {
if (universe.canEvolve(currentState)) {
stack.push(currentState.copy(universe.possibleChoices(currentState)));
currentState = universe.evolve(currentState);
if (display_progress == true) universe.print(universe_height + 6, 4);
}
else if (stack.size() > 0) {
currentState = stack.pop();
universe.reset(currentState);
if (display_regress == true) universe.print(universe_height + 6, 4);
} else {
break;
}
}
//universe.print(universe_height + 6, 4);
System.out.println("\n\n");
universe.print(universe_height + 6, 4);
System.out.print("\033[?25h");
}
else {
// definitions of the starting position and the universe dimensions
start_i = 3;
start_j = 1;
start_dir = 11;
universe_width = 10;
universe_height = 10;
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;
// creation of the stack, the universe and the first state
Stack <Situation> stack = new Stack <Situation>();
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i, start_j, start_dir);
Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0);
// obstacles creation
universe.addObstacle(2, 3);
universe.addObstacle(3, 3);
universe.addObstacle(2, 4);
universe.addObstacle(3, 4);
System.out.print("\033[?25l");
System.out.print("\033[H\033[2J");
System.out.flush();
universe.print(2, 4);
while (!universe.isSolved()) {
if (universe.canEvolve(currentState)) {
stack.push(currentState.copy(universe.possibleChoices(currentState)));
currentState = universe.evolve(currentState);
}
else if (stack.size() > 0) {
currentState = stack.pop();
universe.reset(currentState);
universe.print(universe_height + 6, 4);
} else {
break;
}
//universe.print(universe_height + 6, 4);
}
System.out.println("\n\n");
universe.print(universe_height + 6, 4);
System.out.print("\033[?25h");
}
System.out.println("\n\n");
universe.print(universe_height + 6, 4);
}
}

View file

@ -37,6 +37,98 @@ public class Universe {
// Methods
public void print(int pos_i, int pos_j) {
int i, j;
for (i = 0; i < this.height; i++) {
for (j = 0; j < this.width; j++) {
System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H");
switch (this.grid[i][j]) {
case -1:
System.out.printf(" X");
break;
case 0:
System.out.printf(" ");
break;
case 1:
System.out.printf(" |");
break;
case 2:
System.out.printf(" -");
break;
case 3:
System.out.printf(" +");
break;
case 4:
System.out.printf(" /");
break;
case 5:
System.out.printf(" \\");
break;
case 10:
System.out.printf(" ^");
break;
case 11:
System.out.printf(" v");
break;
case 12:
System.out.printf(" >");
break;
case 13:
System.out.printf(" <");
break;
default:
System.out.printf("%2d", this.grid[i][j]);
}
}
System.out.print("\n");
}
}
public void resetUniverse() {
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; 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] != -1) {
this.grid[i][j] = 0;
}
}
}
}
public void changeUniverseDim(int width, int height) {
int [][] newgrid = new int[width][height];
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
newgrid[i][j] = 0;
}
}
for (int i = 0; j < height; j++) {
newgrid[i][0] = -1;
newgrid[i][width - 1] = -1;
}
for (int j = 0; j < width; j++) {
newgrid[height - 1][j] = -1;
newgrid[0][j] = -1;
}
this.grid = newgrid;
this.width = width;
this.height = height;
}
public void addObstacle(int pos_i, int pos_j) {
if (this.grid[pos_i][pos_j] == 0) {
this.grid[pos_i][pos_j] = -1;
@ -209,62 +301,4 @@ public class Universe {
public boolean isSolved() {
return this.boxes_to_fill == 0;
}
public void print(int pos_i, int pos_j) {
int i, j;
for (i = 0; i < this.height; i++) {
for (j = 0; j < this.width; j++) {
System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H");
switch (this.grid[i][j]) {
case -1:
System.out.printf(" X");
break;
case 0:
System.out.printf(" ");
break;
case 1:
System.out.printf(" |");
break;
case 2:
System.out.printf(" -");
break;
case 3:
System.out.printf(" +");
break;
case 4:
System.out.printf(" /");
break;
case 5:
System.out.printf(" \\");
break;
case 10:
System.out.printf(" ^");
break;
case 11:
System.out.printf(" v");
break;
case 12:
System.out.printf(" >");
break;
case 13:
System.out.printf(" <");
break;
default:
System.out.printf("%2d", this.grid[i][j]);
}
}
System.out.print("\n");
}
}
}

View file

@ -4,4 +4,4 @@ build:
run:
javac --source 1.8 --target 1.8 AppLaser.java
java AppLaser
java AppLaser -cli