python实现数独游戏 java简单实现数独游戏
作者:root__1024 时间:2022-10-18 00:46:29
使用python和java实现数独游戏,有比较才有收获哦。
1、Python版
#--coding:utf-8--
import random
import itertools
from copy import deepcopy
def make_board(m=3):
numbers = list(range(1,m**2+1))
#可能出现的数字为1-9
board = None
#board是数度二维列表
while board is None:
board = get_board(m,numbers)
return board
def get_board(m,numbers):
n = m**2
board = [[None for _ in range(n)] for _ in range(n)]
for i,j in itertools.product(range(n),repeat=2):
i0,j0 = i-i%m,j-j%m
# i,j分别代表的是我们的行和列
# i0和j0代表的是board[i][j]所在的区块的起始位置
random.shuffle(numbers)
#shuffle() 方法将序列的所有元素随机排序。
for x in numbers:
if(x not in board[i]
and all(row[j]!=x for row in board)
and all(x not in row[j0:j0+m]
for row in board[i0:i])):
board[i][j] = x
break
else:#当循环正常结束时会执行else
return None
return board
def print_board(board,m=3):
numbers = list(range(1,m**2+1))
#每一行随机把5个数字变成None
omit = 5 #omit变量掌控着每一行被抹去的数字个数
challange = deepcopy(board)
for i,j in itertools.product(range(omit),range(m**2)):
x = random.choice(numbers) - 1
challange[x][j] = None
spacer = "++-----+-----+-----++-----+-----+-----++-----+-----+-----++"
print(spacer.replace('-','='))
for i,line in enumerate(challange):
print("|| {} | {} | {} || {} | {} | {} || {} | {} | {} ||"
.format(*(cell or ' ' for cell in line)))
#format()函数中的 * 号,则是将所有的 cell 的不同值放入一个元组 tuple 之中,方便format函数调用
if(i+1)%3==0:
print(spacer.replace('-','='))
else:
print(spacer)
return challange
Board = make_board()
print_board(Board)
效果:
2、Java版
package com.jimo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Shudu {
public String[][] make_board(int m) {
String[][] board = null;
int n = m * m;
List<Integer> numbers = new ArrayList<>();
for (int i = 1; i <= n; i++) {
numbers.add(i);
}
while (board == null) {
board = get_board(m, numbers);
}
return board;
}
private String[][] get_board(int m, List<Integer> numbers) {
int n = m * m;
String[][] board = new String[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
boolean ok = true;
// 打乱顺序
Collections.shuffle(numbers);
for (int x = 0; x < n; x++) {
if (isOk(numbers.get(x) + "", board, i, j, m)) {
board[i][j] = numbers.get(x) + "";
ok = false;
break;
}
}
if (ok) {
return null;
}
}
}
return board;
}
public void print_board(String[][] board, int m) {
int n = m * m;
int hard = 5;
String[][] b = new String[n][n];
System.arraycopy(board, 0, b, 0, board.length);
Random rd = new Random();
for (int i = 0; i < hard; i++) {
for (int j = 0; j < n; j++) {
int x = rd.nextInt(9);
b[x][j] = null;
}
}
String spacer = "+-----+-----+-----+-----+-----+-----+-----+-----+-----+";
System.out.println(spacer);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (null == board[i][j]) {
System.out.print("| |");
} else {
System.out.print("| " + b[i][j] + " |");
}
}
System.out.println();
System.out.println(spacer);
}
}
/**
* 判断行列和9格里是否重复
*
* @param x
* @param board
* @return
*/
private boolean isOk(String x, String[][] board, int i, int j, int m) {
int i0 = i - i % m, j0 = j - j % m;
int n = m * m;
for (int k = 0; k < n; k++) {
if (x.equals(board[i][k]) || x.equals(board[k][j])) {
return false;
}
}
for (int k = i0; k < i; k++) {
for (int g = j0; g < j0 + m; g++) {
if (board[k][g].equals(x)) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
Shudu s = new Shudu();
int m = 3;
String[][] b = s.make_board(m);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
s.print_board(b, m);
}
}
效果:
3 4 7 6 5 2 8 1 9
5 9 6 8 3 1 4 2 7
2 1 8 9 7 4 5 3 6
1 3 4 7 9 5 6 8 2
7 5 2 1 8 6 9 4 3
6 8 9 4 2 3 1 7 5
4 7 5 3 1 9 2 6 8
8 2 1 5 6 7 3 9 4
9 6 3 2 4 8 7 5 1
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || || || 6 || 5 || || 8 || 1 || 9 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 5 || 9 || 6 || || || || 4 || || 7 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 2 || || || 9 || 7 || 4 || 5 || || 6 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 1 || 3 || 4 || 7 || 9 || || 6 || || |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || 5 || 2 || 1 || || || || 4 || |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || || || || 2 || 3 || || 7 || |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 4 || 7 || || || 1 || 9 || || 6 || 8 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || 2 || 1 || 5 || 6 || 7 || || || 4 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 9 || 6 || || || || || || 5 || 1 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
可以看出java的代码量是python的两倍。
来源:https://blog.csdn.net/jimo_lonely/article/details/54707918
标签:python,java,数独
0
投稿
猜你喜欢
Java验证时间格式是否正确方法类项目实战
2021-05-30 02:15:15
Android Studio打包jar及aar包的方法
2023-06-18 15:59:53
kotlin延迟初始化和密封类详细讲解
2022-07-10 01:53:52
c# 基于wpf,开发OFD电子文档阅读器
2023-09-08 00:10:02
Android使用RSA加密实现接口调用时的校验功能
2023-11-06 15:24:51
android通过拼音搜索中文的功能实现代码
2022-03-12 04:48:17
使用log4j MDC实现日志追踪
2022-01-13 13:20:01
C#反射应用实例
2023-11-03 14:47:46
C#图像处理之图像均值方差计算的方法
2023-12-10 17:35:33
Jenkins Pipeline 部署 SpringBoot 应用的教程详解
2022-09-26 14:06:27
Java超详细分析泛型与通配符
2023-07-28 08:34:26
一行命令同时修改maven项目中多个module的版本号的方法
2023-09-13 09:43:10
C# 在PDF中创建和填充域
2022-05-14 05:49:23
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
2021-12-29 08:15:07
Java实现输出数字三角形实例代码
2023-08-25 02:09:51
C#切换鼠标左右键习惯无需控制面板中修改
2021-08-06 04:44:44
Java实现AOP功能的封装与配置的小框架实例代码
2022-12-27 10:57:45
Java语法中Lambda表达式无法抛出异常的解决
2022-10-13 01:04:43
android用java动态增添删除修改布局
2021-07-04 12:48:38
C#数据结构与算法揭秘五 栈和队列
2023-09-13 13:31:47