lecture de fichier +affichage debug

This commit is contained in:
2024-04-30 12:20:16 +02:00
parent a13d298097
commit 98c2443099
11 changed files with 80 additions and 63 deletions

View File

@@ -1,30 +1,60 @@
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Grid {
private Cell[][] cells;
public Grid() {
cells = new Cell[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cells[i][j] = new Cell();
for (int column = 0; column < 9; column++) {
cells[i][column] = new Cell();
}
}
}
public Grid(Grid grid) {
cells = new Cell[9][9];
public void loadGridFromFile(String fileName) {
try (DataInputStream input = new DataInputStream(new FileInputStream(fileName))) {
for (int i = 0; i < 9; i++) {
String line = String.valueOf(input.readInt());
for (int column = 0; column < 9; column++) {
int value;
if (column < line.length()) {
char digit = line.charAt(column);
value = Character.getNumericValue(digit);
} else {
value = 0;
}
cells[i][column].setValue(value);
}
}
System.out.println("Success");
System.out.println(this);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
public Cell getCell(int i, int column) {
return cells[i][column];
}
public void setCell(int i, int column, int value) {
cells[i][column].setValue(value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cells[i][j] = new Cell(grid.cells[i][j].getValue());
for (int column = 0; column < 9; column++) {
int value = cells[i][column].getValue();
sb.append(value).append(" ");
}
sb.append("\n");
}
return sb.toString();
}
public Cell getCell(int ligne, int col) {
return cells[ligne][col];
}
}