用Java swing gui 实现数独游戏的代码,要求1、要求产生指定形式的数独题目 2、在产生的数独题目上,程序产生数独的答案,将题目和答案保存在文件。 3、用户可按指定格式输入数独答案,程序可以检查答案是否正确,如
时间: 2023-12-03 19:44:56 浏览: 197
用Java实现的数独游戏的源代码
4星 · 用户满意度95%
下面是一个简单的 Java Swing GUI 实现数独游戏的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SudokuGame extends JFrame {
private JPanel gamePanel;
private JPanel buttonPanel;
private JButton newGameButton;
private JButton checkButton;
private JButton saveButton;
private JButton loadButton;
private JTextField[][] cells;
private int[][] solution;
private int[][] puzzle;
public SudokuGame() {
super("Sudoku Game");
// Create the game panel
gamePanel = new JPanel(new GridLayout(9, 9));
cells = new JTextField[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cells[i][j] = new JTextField();
cells[i][j].setHorizontalAlignment(JTextField.CENTER);
cells[i][j].setFont(new Font("Arial", Font.PLAIN, 20));
gamePanel.add(cells[i][j]);
}
}
// Create the button panel
buttonPanel = new JPanel(new GridLayout(1, 4));
newGameButton = new JButton("New Game");
newGameButton.addActionListener(new NewGameListener());
checkButton = new JButton("Check");
checkButton.addActionListener(new CheckListener());
saveButton = new JButton("Save");
saveButton.addActionListener(new SaveListener());
loadButton = new JButton("Load");
loadButton.addActionListener(new LoadListener());
buttonPanel.add(newGameButton);
buttonPanel.add(checkButton);
buttonPanel.add(saveButton);
buttonPanel.add(loadButton);
// Add the panels to the frame
add(gamePanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Set the size and visibility of the frame
setSize(500, 500);
setVisible(true);
}
private boolean generatePuzzle() {
solution = generateSolution();
puzzle = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
puzzle[i][j] = solution[i][j];
}
}
int count = 0;
while (count < 40) {
int i = (int) (Math.random() * 9);
int j = (int) (Math.random() * 9);
if (puzzle[i][j] != 0) {
puzzle[i][j] = 0;
count++;
}
}
return solvePuzzle(puzzle);
}
private int[][] generateSolution() {
int[][] board = new int[9][9];
solveBoard(board, 0, 0);
return board;
}
private boolean solvePuzzle(int[][] puzzle) {
int[][] board = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
board[i][j] = puzzle[i][j];
}
}
return solveBoard(board, 0, 0);
}
private boolean solveBoard(int[][] board, int row, int col) {
if (col == 9) {
col = 0;
row++;
if (row == 9) {
return true;
}
}
if (board[row][col] != 0) {
return solveBoard(board, row, col + 1);
}
for (int num = 1; num <= 9; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveBoard(board, row, col + 1)) {
return true;
}
board[row][col] = 0;
}
}
return false;
}
private boolean isValid(int[][] board, int row, int col, int num) {
for (int i = 0; i < 9; i++) {
if (board[row][i] == num || board[i][col] == num) {
return false;
}
}
int boxRow = row - row % 3;
int boxCol = col - col % 3;
for (int i = boxRow; i < boxRow + 3; i++) {
for (int j = boxCol; j < boxCol + 3; j++) {
if (board[i][j] == num) {
return false;
}
}
}
return true;
}
private boolean checkPuzzle() {
int[][] board = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
try {
board[i][j] = Integer.parseInt(cells[i][j].getText());
} catch (NumberFormatException e) {
return false;
}
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != solution[i][j]) {
return false;
}
}
}
return true;
}
private void savePuzzle() {
try {
PrintWriter writer = new PrintWriter(new FileWriter("sudoku.txt"));
writer.println("Puzzle:");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
writer.print(puzzle[i][j]);
}
writer.println();
}
writer.println("Solution:");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
writer.print(solution[i][j]);
}
writer.println();
}
writer.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error saving puzzle", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void loadPuzzle() {
try {
BufferedReader reader = new BufferedReader(new FileReader("sudoku.txt"));
String line;
for (int i = 0; i < 9; i++) {
line = reader.readLine();
for (int j = 0; j < 9; j++) {
puzzle[i][j] = line.charAt(j) - '0';
}
}
line = reader.readLine();
for (int i = 0; i < 9; i++) {
line = reader.readLine();
for (int j = 0; j < 9; j++) {
solution[i][j] = line.charAt(j) - '0';
}
}
reader.close();
updateCells();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error loading puzzle", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void updateCells() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (puzzle[i][j] == 0) {
cells[i][j].setText("");
} else {
cells[i][j].setText(Integer.toString(puzzle[i][j]));
}
}
}
}
private class NewGameListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (generatePuzzle()) {
updateCells();
} else {
JOptionPane.showMessageDialog(SudokuGame.this, "Error generating puzzle", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class CheckListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (checkPuzzle()) {
JOptionPane.showMessageDialog(SudokuGame.this, "Congratulations! You solved the puzzle!", "Congratulations", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(SudokuGame.this, "Sorry, your solution is incorrect.", "Incorrect Solution", JOptionPane.ERROR_MESSAGE);
}
}
}
private class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
savePuzzle();
}
}
private class LoadListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
loadPuzzle();
}
}
public static void main(String[] args) {
SudokuGame game = new SudokuGame();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
这个程序实现了数独游戏的基本功能,包括产生指定形式的数独题目、在产生的数独题目上,程序产生数独的答案、将题目和答案保存在文件、用户可按指定格式输入数独答案、程序可以检查答案是否正确。你可以根据自己的需要对程序进行修改和扩展。
阅读全文