Ajout Sauvegarder grille dans Grid.java

This commit is contained in:
Vincent TEISSIER 2024-05-04 16:11:35 +02:00
parent d3612a14aa
commit f5d9276a33

@ -1,24 +1,17 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Grid {
private Cell[][] cell;
private Cell[][] cells;
public Grid() {
cell = new Cell[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cell[i][j] = new Cell();
}
}
}
public Grid(Grid grid) {
cell = new Cell[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cell[i][j] = new Cell(grid.cell[i][j].getValue());
cells = new Cell[9][9];
for (int ligne = 0; ligne < 9; ligne++) {
for (int column = 0; column < 9; column++) {
cells[ligne][column] = new Cell();
}
}
}
@ -34,7 +27,7 @@ public class Grid {
for (int column = 0; column < 9; column++) {
char number = line.charAt(column);
int value = Character.getNumericValue(number);
cell[ligne][column].setValue(value);
cells[ligne][column].setValue(value);
}
}
System.out.println("Success");
@ -43,15 +36,31 @@ public class Grid {
System.err.println("Error: " + e.getMessage());
}
}
public void saveGridToFile(String fileName) {
try (DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName))) {
for (int ligne = 0; ligne < 9; ligne++) {
StringBuilder line = new StringBuilder();
for (int column = 0; column < 9; column++) {
int value = cells[ligne][column].getValue();
line.append(value);
}
output.writeInt(Integer.parseInt(line.toString()));
}
System.out.println("Grille sauvegardée avec succès dans " + fileName);
} catch (IOException e) {
System.err.println("Erreur lors de la sauvegarde de la grille: " + e.getMessage());
}
}
public Cell getCell(int ligne, int col) {
return cell[ligne][col];
return cells[ligne][col];
}
public void copyFrom(Grid second_grid) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
this.cell[row][col].setValue(second_grid.cell[row][col].getValue());
this.cells[row][col].setValue(second_grid.cells[row][col].getValue());
}
}
}
@ -59,7 +68,7 @@ public class Grid {
public void printGrid() {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
System.out.print(cell[row][col].getValue() + " ");
System.out.print(cells[row][col].getValue() + " ");
}
System.out.println();
}