From a13d2980978829826d45b710af256b198c281b41 Mon Sep 17 00:00:00 2001 From: Vincent <xefal77@gmail.com> Date: Sun, 28 Apr 2024 01:40:14 +0200 Subject: [PATCH] Ajout Grille + Swing UI Grille Sudoku --- Cell.java | 21 +++++++++++ Grid.java | 30 +++++++++++++++ Makefile | 35 ++++++++++++++++++ Sudoku.java | 28 ++++++++++++++ SudokuButtonListener.java | 77 +++++++++++++++++++++++++++++++++++++++ SudokuUI.java | 49 +++++++++++++++++++++++++ 6 files changed, 240 insertions(+) create mode 100644 Cell.java create mode 100644 Grid.java create mode 100644 Makefile create mode 100644 Sudoku.java create mode 100644 SudokuButtonListener.java create mode 100644 SudokuUI.java diff --git a/Cell.java b/Cell.java new file mode 100644 index 0000000..9bad4e1 --- /dev/null +++ b/Cell.java @@ -0,0 +1,21 @@ +public class Cell { + private int value; + + public Cell() { + this.value = 0; + } + + // Constructeur prenant une valeur comme argument + public Cell(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } +} + diff --git a/Grid.java b/Grid.java new file mode 100644 index 0000000..7875772 --- /dev/null +++ b/Grid.java @@ -0,0 +1,30 @@ +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(); + } + } + } + + public Grid(Grid 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(grid.cells[i][j].getValue()); + } + } + } + + + public Cell getCell(int ligne, int col) { + return cells[ligne][col]; + } + +} + + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1707241 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# List of Java source files +SRCS := Cell.java Grid.java Sudoku.java SudokuButtonListener.java SudokuUI.java + +# Directory to store compiled class files +BUILD_DIR := build + +# Java compiler +JAVAC := javac + +# Java interpreter +JAVA := java + +# Main class to run +MAIN_CLASS := Sudoku + +# Default target +.PHONY: all +all: compile + +# Create the build directory +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +# Compile Java source files +.PHONY: compile +compile: $(addprefix $(BUILD_DIR)/, $(SRCS:.java=.class)) + +# Rule to compile Java source files +$(BUILD_DIR)/%.class: %.java | $(BUILD_DIR) + $(JAVAC) -d $(BUILD_DIR) $ + +# Clean up compiled class files +.PHONY: clean +clean: + rm -rf $(BUILD_DIR) diff --git a/Sudoku.java b/Sudoku.java new file mode 100644 index 0000000..fc22ab5 --- /dev/null +++ b/Sudoku.java @@ -0,0 +1,28 @@ +public class Sudoku { + private Grid grid; + + public Sudoku() { + this.grid = new Grid(); //grille de base + } + + public Grid getGrid() { + return grid; + } + + 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(); + } + } +} + diff --git a/SudokuButtonListener.java b/SudokuButtonListener.java new file mode 100644 index 0000000..cf3064d --- /dev/null +++ b/SudokuButtonListener.java @@ -0,0 +1,77 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class SudokuButtonListener implements ActionListener { + private int row; + private int col; + private Sudoku sudoku; + private JButton[][] buttons; + + public SudokuButtonListener(int row, int col, Sudoku sudoku, JButton[][] buttons) { + this.row = row; + this.col = col; + this.sudoku = sudoku; + 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."); + } + } + } + + private boolean isValidMove(int num, int row, int col) { + return isValidRow(num, row) && isValidCol(num, col) && isValidBox(num, row - row % 3, col - col % 3); + } + + private boolean isValidRow(int num, int row) { + for (int i = 0; i < 9; i++) { + if (sudoku.getGrid().getCell(row, i).getValue() == num && i != col) { + return false; + } + } + return true; + } + + private boolean isValidCol(int num, int col) { + for (int i = 0; i < 9; i++) { + if (sudoku.getGrid().getCell(i, col).getValue() == num && i != row) { + return false; + } + } + return true; + } + + private boolean isValidBox(int num, int boxStartRow, int boxStartCol) { + for (int i = 0; i < 3; i++) { + 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)) { + return false; + } + } + } + return true; + } +} + diff --git a/SudokuUI.java b/SudokuUI.java new file mode 100644 index 0000000..ea418c5 --- /dev/null +++ b/SudokuUI.java @@ -0,0 +1,49 @@ +import javax.swing.*; +import java.awt.*; + +public class SudokuUI extends JFrame { + private Sudoku sudoku; + private JButton[][] buttons; + + public SudokuUI(Sudoku sudoku) { + this.sudoku = sudoku; + this.buttons = new JButton[9][9]; + + setTitle("Sudoku"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + JPanel gridPanel = new JPanel(new GridLayout(9, 9)); + gridPanel.setBackground(Color.WHITE); + gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + + // Boutons de la grille + createGridButtons(gridPanel); + + int preferredSize = 50 * 9; // 50 est la taille approximative d'un bouton + gridPanel.setPreferredSize(new Dimension(preferredSize, preferredSize)); + + add(gridPanel); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } + + 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)); + gridPanel.add(button); + buttons[row][col] = button; + + // Mettez à jour le texte des boutons avec les valeurs de la grille Sudoku + int value = grid.getCell(row, col).getValue(); + buttons[row][col].setText(value == 0 ? "" : String.valueOf(value)); + } + } + } +} +