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 {
|
2024-05-04 14:59:38 +02:00
|
|
|
private Cell[][] cell;
|
2024-04-28 01:40:14 +02:00
|
|
|
|
|
|
|
public Grid() {
|
2024-05-04 14:59:38 +02:00
|
|
|
cell = new Cell[9][9];
|
2024-05-04 00:13:44 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
for (int j = 0; j < 9; j++) {
|
2024-05-04 14:59:38 +02:00
|
|
|
cell[i][j] = new Cell();
|
2024-05-04 00:13:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Grid(Grid grid) {
|
2024-05-04 14:59:38 +02:00
|
|
|
cell = new Cell[9][9];
|
2024-05-04 00:13:44 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
for (int j = 0; j < 9; j++) {
|
2024-05-04 14:59:38 +02:00
|
|
|
cell[i][j] = new Cell(grid.cell[i][j].getValue());
|
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);
|
|
|
|
cell[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-05-04 00:13:44 +02:00
|
|
|
public Cell getCell(int ligne, int col) {
|
2024-05-04 14:59:38 +02:00
|
|
|
return cell[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 14:59:38 +02:00
|
|
|
this.cell[row][col].setValue(second_grid.cell[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 14:59:38 +02:00
|
|
|
System.out.print(cell[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 00:13:44 +02:00
|
|
|
}
|