lecture de fichier +affichage debug
This commit is contained in:
parent
a13d298097
commit
98c2443099
BIN
Cell.class
Normal file
BIN
Cell.class
Normal file
Binary file not shown.
@ -17,5 +17,4 @@ public class Cell {
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
Grid.class
Normal file
BIN
Grid.class
Normal file
Binary file not shown.
58
Grid.java
58
Grid.java
@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
BIN
Sudoku.class
Normal file
BIN
Sudoku.class
Normal file
Binary file not shown.
24
Sudoku.java
24
Sudoku.java
@ -2,7 +2,7 @@ public class Sudoku {
|
||||
private Grid grid;
|
||||
|
||||
public Sudoku() {
|
||||
this.grid = new Grid(); //grille de base
|
||||
this.grid = new Grid();
|
||||
}
|
||||
|
||||
public Grid getGrid() {
|
||||
@ -10,19 +10,13 @@ public class Sudoku {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Sudoku sudoku = new Sudoku();
|
||||
sudoku.printGrid(); // Afficher la grille
|
||||
new SudokuUI(sudoku);
|
||||
}
|
||||
|
||||
public void printGrid() {
|
||||
for (int row = 0; row < 9; row++) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
int value = grid.getCell(row, col).getValue();
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
System.out.println();
|
||||
if (args.length > 0) {
|
||||
Sudoku sudoku = new Sudoku();
|
||||
sudoku.getGrid().loadGridFromFile(args[0]);
|
||||
// Pas besoin d'afficher la grille ici, car loadGridFromFile() le fait déjà
|
||||
new SudokuUI(sudoku.getGrid());
|
||||
} else {
|
||||
System.err.println("Usage: java Sudoku <grid_file>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
SudokuButtonListener.class
Normal file
BIN
SudokuButtonListener.class
Normal file
Binary file not shown.
@ -6,36 +6,33 @@ import java.awt.event.ActionListener;
|
||||
public class SudokuButtonListener implements ActionListener {
|
||||
private int row;
|
||||
private int col;
|
||||
private Sudoku sudoku;
|
||||
private Grid grid;
|
||||
private JButton[][] buttons;
|
||||
|
||||
public SudokuButtonListener(int row, int col, Sudoku sudoku, JButton[][] buttons) {
|
||||
public SudokuButtonListener(int row, int col, Grid grid, JButton[][] buttons) {
|
||||
this.row = row;
|
||||
this.col = col;
|
||||
this.sudoku = sudoku;
|
||||
this.grid = grid;
|
||||
this.buttons = buttons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String input = JOptionPane.showInputDialog("Enter a number:");
|
||||
if (input != null && input.length() > 0) {
|
||||
try {
|
||||
int num = Integer.parseInt(input);
|
||||
sudoku.getGrid().getCell(row, col).setValue(num);
|
||||
if (num == 0) {
|
||||
buttons[row][col].setText(""); // Empty cell if the number is 0
|
||||
} else {
|
||||
buttons[row][col].setText(String.valueOf(num));
|
||||
}
|
||||
if (!isValidMove(num, row, col)) {
|
||||
buttons[row][col].setForeground(Color.RED); // Set text color to red for invalid move
|
||||
} else {
|
||||
buttons[row][col].setForeground(Color.BLACK); // Reset text color
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Please enter a valid number.");
|
||||
try {
|
||||
int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number:"));
|
||||
grid.getCell(row, col).setValue(num);
|
||||
if (num == 0) {
|
||||
buttons[row][col].setText(""); // Empty cell if the number is 0
|
||||
} else {
|
||||
buttons[row][col].setText(String.valueOf(num));
|
||||
}
|
||||
if (!isValidMove(num, row, col)) {
|
||||
buttons[row][col].setForeground(Color.RED); // Set text color to red for invalid move
|
||||
} else {
|
||||
buttons[row][col].setForeground(Color.BLACK); // Reset text color
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Please enter a valid number.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +42,7 @@ public class SudokuButtonListener implements ActionListener {
|
||||
|
||||
private boolean isValidRow(int num, int row) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (sudoku.getGrid().getCell(row, i).getValue() == num && i != col) {
|
||||
if (grid.getCell(row, i).getValue() == num && i != col) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -54,7 +51,7 @@ public class SudokuButtonListener implements ActionListener {
|
||||
|
||||
private boolean isValidCol(int num, int col) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != row) {
|
||||
if (grid.getCell(i, col).getValue() == num && i != row) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -66,12 +63,11 @@ public class SudokuButtonListener implements ActionListener {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int row = i + boxStartRow;
|
||||
int col = j + boxStartCol;
|
||||
if (sudoku.getGrid().getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
|
||||
if (grid.getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
SudokuUI.class
Normal file
BIN
SudokuUI.class
Normal file
Binary file not shown.
@ -2,11 +2,11 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class SudokuUI extends JFrame {
|
||||
private Sudoku sudoku;
|
||||
private Grid grid;
|
||||
private JButton[][] buttons;
|
||||
|
||||
public SudokuUI(Sudoku sudoku) {
|
||||
this.sudoku = sudoku;
|
||||
public SudokuUI(Grid grid) {
|
||||
this.grid = grid;
|
||||
this.buttons = new JButton[9][9];
|
||||
|
||||
setTitle("Sudoku");
|
||||
@ -28,14 +28,13 @@ public class SudokuUI extends JFrame {
|
||||
}
|
||||
|
||||
private void createGridButtons(JPanel gridPanel) { // Création sudoku
|
||||
Grid grid = sudoku.getGrid();
|
||||
for (int row = 0; row < 9; row++) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
JButton button = new JButton();
|
||||
button.setFont(new Font("Arial", Font.BOLD, 24));
|
||||
button.setContentAreaFilled(false);
|
||||
button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
button.addActionListener(new SudokuButtonListener(row, col, sudoku, buttons));
|
||||
button.addActionListener(new SudokuButtonListener(row, col, grid, buttons));
|
||||
gridPanel.add(button);
|
||||
buttons[row][col] = button;
|
||||
|
||||
@ -45,5 +44,4 @@ public class SudokuUI extends JFrame {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
exemple.gri
Normal file
BIN
exemple.gri
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user