Ajout Sauvegarder grille dans Grid.java
This commit is contained in:
parent
d3612a14aa
commit
f5d9276a33
45
Grid.java
45
Grid.java
@ -1,24 +1,17 @@
|
|||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Grid {
|
public class Grid {
|
||||||
private Cell[][] cell;
|
private Cell[][] cells;
|
||||||
|
|
||||||
public Grid() {
|
public Grid() {
|
||||||
cell = new Cell[9][9];
|
cells = new Cell[9][9];
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int ligne = 0; ligne < 9; ligne++) {
|
||||||
for (int j = 0; j < 9; j++) {
|
for (int column = 0; column < 9; column++) {
|
||||||
cell[i][j] = new Cell();
|
cells[ligne][column] = 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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,7 +27,7 @@ public class Grid {
|
|||||||
for (int column = 0; column < 9; column++) {
|
for (int column = 0; column < 9; column++) {
|
||||||
char number = line.charAt(column);
|
char number = line.charAt(column);
|
||||||
int value = Character.getNumericValue(number);
|
int value = Character.getNumericValue(number);
|
||||||
cell[ligne][column].setValue(value);
|
cells[ligne][column].setValue(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Success");
|
System.out.println("Success");
|
||||||
@ -43,15 +36,31 @@ public class Grid {
|
|||||||
System.err.println("Error: " + e.getMessage());
|
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) {
|
public Cell getCell(int ligne, int col) {
|
||||||
return cell[ligne][col];
|
return cells[ligne][col];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void copyFrom(Grid second_grid) {
|
public void copyFrom(Grid second_grid) {
|
||||||
for (int row = 0; row < 9; row++) {
|
for (int row = 0; row < 9; row++) {
|
||||||
for (int col = 0; col < 9; col++) {
|
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() {
|
public void printGrid() {
|
||||||
for (int row = 0; row < 9; row++) {
|
for (int row = 0; row < 9; row++) {
|
||||||
for (int col = 0; col < 9; col++) {
|
for (int col = 0; col < 9; col++) {
|
||||||
System.out.print(cell[row][col].getValue() + " ");
|
System.out.print(cells[row][col].getValue() + " ");
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user