使用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的两倍。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,java,数独
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python实现数独游戏 java简单实现数独游戏”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]