lecture de fichier +affichage debug

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

BIN
Cell.class Normal file

Binary file not shown.

@ -18,4 +18,3 @@ public class Cell {
this.value = value; this.value = value;
} }
} }

BIN
Grid.class Normal file

Binary file not shown.

@ -1,30 +1,60 @@
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Grid { public class Grid {
private Cell[][] cells; private Cell[][] cells;
public Grid() { public Grid() {
cells = new Cell[9][9]; cells = new Cell[9][9];
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) { for (int column = 0; column < 9; column++) {
cells[i][j] = new Cell(); cells[i][column] = new Cell();
} }
} }
} }
public Grid(Grid grid) { public void loadGridFromFile(String fileName) {
cells = new Cell[9][9]; 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 i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) { for (int column = 0; column < 9; column++) {
cells[i][j] = new Cell(grid.cells[i][j].getValue()); 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

Binary file not shown.

@ -2,7 +2,7 @@ public class Sudoku {
private Grid grid; private Grid grid;
public Sudoku() { public Sudoku() {
this.grid = new Grid(); //grille de base this.grid = new Grid();
} }
public Grid getGrid() { public Grid getGrid() {
@ -10,19 +10,13 @@ public class Sudoku {
} }
public static void main(String[] args) { public static void main(String[] args) {
Sudoku sudoku = new Sudoku(); if (args.length > 0) {
sudoku.printGrid(); // Afficher la grille Sudoku sudoku = new Sudoku();
new SudokuUI(sudoku); sudoku.getGrid().loadGridFromFile(args[0]);
} // Pas besoin d'afficher la grille ici, car loadGridFromFile() le fait déjà
new SudokuUI(sudoku.getGrid());
public void printGrid() { } else {
for (int row = 0; row < 9; row++) { System.err.println("Usage: java Sudoku <grid_file>");
for (int col = 0; col < 9; col++) {
int value = grid.getCell(row, col).getValue();
System.out.print(value + " ");
}
System.out.println();
} }
} }
} }

BIN
SudokuButtonListener.class Normal file

Binary file not shown.

@ -6,36 +6,33 @@ import java.awt.event.ActionListener;
public class SudokuButtonListener implements ActionListener { public class SudokuButtonListener implements ActionListener {
private int row; private int row;
private int col; private int col;
private Sudoku sudoku; private Grid grid;
private JButton[][] buttons; 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.row = row;
this.col = col; this.col = col;
this.sudoku = sudoku; this.grid = grid;
this.buttons = buttons; this.buttons = buttons;
} }
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String input = JOptionPane.showInputDialog("Enter a number:"); try {
if (input != null && input.length() > 0) { int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number:"));
try { grid.getCell(row, col).setValue(num);
int num = Integer.parseInt(input); if (num == 0) {
sudoku.getGrid().getCell(row, col).setValue(num); buttons[row][col].setText(""); // Empty cell if the number is 0
if (num == 0) { } else {
buttons[row][col].setText(""); // Empty cell if the number is 0 buttons[row][col].setText(String.valueOf(num));
} 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.");
} }
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) { private boolean isValidRow(int num, int row) {
for (int i = 0; i < 9; i++) { 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; return false;
} }
} }
@ -54,7 +51,7 @@ public class SudokuButtonListener implements ActionListener {
private boolean isValidCol(int num, int col) { private boolean isValidCol(int num, int col) {
for (int i = 0; i < 9; i++) { 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; return false;
} }
} }
@ -66,7 +63,7 @@ public class SudokuButtonListener implements ActionListener {
for (int j = 0; j < 3; j++) { for (int j = 0; j < 3; j++) {
int row = i + boxStartRow; int row = i + boxStartRow;
int col = j + boxStartCol; 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 false;
} }
} }
@ -74,4 +71,3 @@ public class SudokuButtonListener implements ActionListener {
return true; return true;
} }
} }

BIN
SudokuUI.class Normal file

Binary file not shown.

@ -2,11 +2,11 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
public class SudokuUI extends JFrame { public class SudokuUI extends JFrame {
private Sudoku sudoku; private Grid grid;
private JButton[][] buttons; private JButton[][] buttons;
public SudokuUI(Sudoku sudoku) { public SudokuUI(Grid grid) {
this.sudoku = sudoku; this.grid = grid;
this.buttons = new JButton[9][9]; this.buttons = new JButton[9][9];
setTitle("Sudoku"); setTitle("Sudoku");
@ -28,14 +28,13 @@ public class SudokuUI extends JFrame {
} }
private void createGridButtons(JPanel gridPanel) { // Création sudoku private void createGridButtons(JPanel gridPanel) { // Création sudoku
Grid grid = sudoku.getGrid();
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++) {
JButton button = new JButton(); JButton button = new JButton();
button.setFont(new Font("Arial", Font.BOLD, 24)); button.setFont(new Font("Arial", Font.BOLD, 24));
button.setContentAreaFilled(false); button.setContentAreaFilled(false);
button.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 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); gridPanel.add(button);
buttons[row][col] = button; buttons[row][col] = button;
@ -46,4 +45,3 @@ public class SudokuUI extends JFrame {
} }
} }
} }

BIN
exemple.gri Normal file

Binary file not shown.