2024-04-30 12:20:16 +02:00
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2024-04-28 01:40:14 +02:00
|
|
|
public class Grid {
|
|
|
|
private Cell[][] cells;
|
|
|
|
|
|
|
|
public Grid() {
|
|
|
|
cells = new Cell[9][9];
|
2024-04-30 22:50:48 +02:00
|
|
|
for (int ligne = 0; ligne < 9; ligne++) {
|
2024-04-30 12:20:16 +02:00
|
|
|
for (int column = 0; column < 9; column++) {
|
2024-04-30 22:50:48 +02:00
|
|
|
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-04-30 22:50:48 +02:00
|
|
|
char digit = line.charAt(column);
|
|
|
|
int value = Character.getNumericValue(digit);
|
|
|
|
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);
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("Error: " + e.getMessage());
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:50:48 +02:00
|
|
|
public Cell getCell(int ligne, int column) {
|
|
|
|
return cells[ligne][column];
|
2024-04-30 12:20:16 +02:00
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
|
2024-04-30 22:50:48 +02:00
|
|
|
public void setCell(int ligne, int column, int value) {
|
|
|
|
cells[ligne][column].setValue(value);
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 12:20:16 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2024-04-30 22:50:48 +02:00
|
|
|
for (int ligne = 0; ligne < 9; ligne++) {
|
2024-04-30 12:20:16 +02:00
|
|
|
for (int column = 0; column < 9; column++) {
|
2024-04-30 22:50:48 +02:00
|
|
|
int value = cells[ligne][column].getValue();
|
2024-04-30 12:20:16 +02:00
|
|
|
sb.append(value).append(" ");
|
|
|
|
}
|
|
|
|
sb.append("\n");
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2024-04-30 22:50:48 +02:00
|
|
|
}
|