Ajout Grille + Swing UI Grille Sudoku
This commit is contained in:
commit
a13d298097
21
Cell.java
Normal file
21
Cell.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
30
Grid.java
Normal file
30
Grid.java
Normal file
@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
35
Makefile
Normal file
35
Makefile
Normal file
@ -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)
|
28
Sudoku.java
Normal file
28
Sudoku.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
77
SudokuButtonListener.java
Normal file
77
SudokuButtonListener.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
49
SudokuUI.java
Normal file
49
SudokuUI.java
Normal file
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user