2024-04-30 12:20:16 +02:00
|
|
|
import java.io.DataInputStream;
|
2024-05-04 16:11:35 +02:00
|
|
|
import java.io.DataOutputStream;
|
2024-05-04 16:54:22 +02:00
|
|
|
import java.io.EOFException;
|
2024-04-30 12:20:16 +02:00
|
|
|
import java.io.FileInputStream;
|
2024-05-04 16:54:22 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2024-05-04 16:11:35 +02:00
|
|
|
import java.io.FileOutputStream;
|
2024-04-30 12:20:16 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2024-04-28 01:40:14 +02:00
|
|
|
public class Grid {
|
2024-05-04 16:11:35 +02:00
|
|
|
private Cell[][] cells;
|
2024-04-28 01:40:14 +02:00
|
|
|
|
|
|
|
public Grid() {
|
2024-05-04 16:11:35 +02:00
|
|
|
cells = new Cell[9][9];
|
|
|
|
for (int ligne = 0; ligne < 9; ligne++) {
|
|
|
|
for (int column = 0; column < 9; column++) {
|
|
|
|
cells[ligne][column] = new Cell();
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 12:20:16 +02:00
|
|
|
public void loadGridFromFile(String fileName) {
|
|
|
|
try (DataInputStream input = new DataInputStream(new FileInputStream(fileName))) {
|
2024-04-30 22:50:48 +02:00
|
|
|
for (int ligne = 0; ligne < 9; ligne++) {
|
2024-04-30 12:20:16 +02:00
|
|
|
String line = String.valueOf(input.readInt());
|
2024-04-30 22:50:48 +02:00
|
|
|
int length = line.length();
|
|
|
|
if (length < 9) {
|
|
|
|
line = "000000000".substring(length) + line; // Ajoute les zéros au début si nécessaire
|
|
|
|
}
|
2024-04-30 12:20:16 +02:00
|
|
|
for (int column = 0; column < 9; column++) {
|
2024-05-04 14:59:38 +02:00
|
|
|
char number = line.charAt(column);
|
|
|
|
int value = Character.getNumericValue(number);
|
2024-05-04 16:11:35 +02:00
|
|
|
cells[ligne][column].setValue(value);
|
2024-04-30 12:20:16 +02:00
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
2024-04-30 12:20:16 +02:00
|
|
|
System.out.println("Success");
|
|
|
|
System.out.println(this);
|
2024-05-04 16:54:22 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
System.err.println("Erreur : Fichier non trouvé: " + e.getMessage());
|
|
|
|
} catch (EOFException e) {
|
|
|
|
System.err.println("Erreur : Fin de fichier atteinte prématurément: " + e.getMessage());
|
2024-04-30 12:20:16 +02:00
|
|
|
} catch (IOException e) {
|
2024-05-04 16:54:22 +02:00
|
|
|
System.err.println("Erreur d'entrée/sortie: " + e.getMessage());
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-04 16:54:22 +02:00
|
|
|
|
2024-05-04 16:11:35 +02:00
|
|
|
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);
|
2024-05-04 16:54:22 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
System.err.println("Erreur : Fichier non trouvé: " + e.getMessage());
|
2024-05-04 16:11:35 +02:00
|
|
|
} catch (IOException e) {
|
2024-05-04 16:54:22 +02:00
|
|
|
System.err.println("Erreur d'entrée/sortie: " + e.getMessage());
|
2024-05-04 16:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
public Cell getCell(int ligne, int col) {
|
2024-05-04 16:11:35 +02:00
|
|
|
return cells[ligne][col];
|
2024-04-30 12:20:16 +02:00
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
|
2024-05-04 14:59:38 +02:00
|
|
|
public void copyFrom(Grid second_grid) {
|
2024-05-04 00:13:44 +02:00
|
|
|
for (int row = 0; row < 9; row++) {
|
|
|
|
for (int col = 0; col < 9; col++) {
|
2024-05-04 16:11:35 +02:00
|
|
|
this.cells[row][col].setValue(second_grid.cells[row][col].getValue());
|
2024-05-04 00:13:44 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
public void printGrid() {
|
|
|
|
for (int row = 0; row < 9; row++) {
|
|
|
|
for (int col = 0; col < 9; col++) {
|
2024-05-04 16:11:35 +02:00
|
|
|
System.out.print(cells[row][col].getValue() + " ");
|
2024-04-30 12:20:16 +02:00
|
|
|
}
|
2024-05-04 00:13:44 +02:00
|
|
|
System.out.println();
|
2024-04-30 12:20:16 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-04 16:54:22 +02:00
|
|
|
}
|