使用Java编写一个数独游戏
时间: 2023-07-20 16:16:54 浏览: 106
好的,我可以为您提供一个简单的Java数独游戏代码示例:
```java
import java.util.Scanner;
public class SudokuGame {
private int[][] board;
private static final int BOARD_SIZE = 9;
private static final int BOX_SIZE = 3;
public SudokuGame() {
board = new int[BOARD_SIZE][BOARD_SIZE];
}
public void startGame() {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Sudoku Game!");
System.out.println("Please enter the numbers for each row, use 0 for empty cells.");
System.out.println("Example for row 1: 530070000 (means 5 3 0 0 7 0 0 0 0)");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.printf("Enter row %d: ", i + 1);
String input = scanner.nextLine();
while (!validateInput(input)) {
System.out.println("Invalid input, please enter again.");
System.out.printf("Enter row %d: ", i + 1);
input = scanner.nextLine();
}
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = Character.getNumericValue(input.charAt(j));
}
}
System.out.println("Game starts now!");
printBoard();
while (!isBoardCompleted()) {
System.out.print("Enter row: ");
int row = scanner.nextInt();
System.out.print("Enter column: ");
int column = scanner.nextInt();
System.out.print("Enter number: ");
int number = scanner.nextInt();
while (!isValidMove(row - 1, column - 1, number)) {
System.out.print("Invalid move, please try again.\nEnter row: ");
row = scanner.nextInt();
System.out.print("Enter column: ");
column = scanner.nextInt();
System.out.print("Enter number: ");
number = scanner.nextInt();
}
board[row - 1][column - 1] = number;
printBoard();
}
System.out.println("Congratulations, you win!");
}
private boolean validateInput(String input) {
if (input.length() != BOARD_SIZE) {
return false;
}
for (int i = 0; i < BOARD_SIZE; i++) {
if (!Character.isDigit(input.charAt(i))) {
return false;
}
}
return true;
}
private boolean isBoardCompleted() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
private boolean isValidMove(int row, int column, int number) {
if (board[row][column] != 0) {
return false;
}
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[row][i] == number || board[i][column] == number) {
return false;
}
}
int boxRow = row / BOX_SIZE * BOX_SIZE;
int boxColumn = column / BOX_SIZE * BOX_SIZE;
for (int i = boxRow; i < boxRow + BOX_SIZE; i++) {
for (int j = boxColumn; j < boxColumn + BOX_SIZE; j++) {
if (board[i][j] == number) {
return false;
}
}
}
return true;
}
private void printBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
if (i % BOX_SIZE == 0) {
System.out.println(" -------------------------");
}
for (int j = 0; j < BOARD_SIZE; j++) {
if (j % BOX_SIZE == 0) {
System.out.print("| ");
}
System.out.print(board[i][j] == 0 ? " " : Integer.toString(board[i][j]));
System.out.print(' ');
}
System.out.println("|");
}
System.out.println(" -------------------------");
}
public static void main(String[] args) {
SudokuGame game = new SudokuGame();
game.startGame();
}
}
```
这个程序使用控制台进行输入和输出,首先要求用户输入一个数独游戏的初始状态,然后程序会依次提示用户输入每一步的行、列和数字,并判断这一步是否合法,直到用户成功填完整个数独。
阅读全文