数独解题器强化版

news/2024/11/15 1:34:22/

经过实际测试,在解决高难度数独时,解题器的效果仍旧不理想,所以添加了数字提示功能。

另外添加了自定义数独数据功能。

MainWindow.xaml

<Window x:Class="SudokuSolver.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:SudokuSolver"mc:Ignorable="d"Title="SudokuSolver" Height="450" Width="600"><Grid><UniformGrid Name="InputGrid" Height="360" Rows="9" Columns="9" Margin="40,30,185,20"/><UniformGrid Name="BorderPanel" Height="360" Rows="3" Columns="3" Margin="40,30,185,20"/><Button Name="SolveBtn" Height="50" Width="100" Margin="430,70,40,290" Content="OneStep"/><Button Name="RestartBtn" Height="50" Width="100" Margin="430,130,40,190" Content="Restart"/><Button Name="CustomBtn" Height="50" Width="100" Margin="430,210,40,110" Content="Clear"/><Button Name="SaveBtn" Height="50" Width="100" Margin="430,300,40,50" Content="Save"/><Button Name="Tips" Height="50" Width ="50" Content="Tips" Margin="520,10,10,340"/></Grid></Window>

MainWindow.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace SudokuSolver
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{private int[,] numArr = new int[9, 9];private List<TextBox> textBoxList = new List<TextBox>();private struct FillNum {public int row;public int col;public List<int> possibleNums;}private bool isCustom = false;private int[,] customNumArr = new int[9, 9];private List<FillNum> tipsList = new List<FillNum>();public MainWindow(){InitializeComponent();Create81Grids();DrawBorders();InitSudokuArr();SolveBtn.Click += SolveBtn_Click;RestartBtn.Click += RestartBtn_Click;CustomBtn.Click += CustomBtn_Click;SaveBtn.Click += SaveBtn_Click;Tips.Click += Tips_Click;}private void Create81Grids() {//创建格子for (int i = 0; i < 81; i++){TextBox textBox = new TextBox();textBox.Height = 30;textBox.Width = 30;textBox.FontSize = 25;textBox.Foreground = Brushes.Black;textBox.TextAlignment = TextAlignment.Center;textBox.Name = "box_"+i.ToString();textBox.KeyDown += TextBox_KeyDown;textBoxList.Add(textBox);InputGrid.Children.Add(textBox);}}private void TextBox_KeyDown(object sender, KeyEventArgs e){TextBox textBox = sender as TextBox;textBox.Foreground = Brushes.Blue;int index = int.Parse(textBox.Name.Split('_')[1]);numArr[index / 9, index % 9] = int.Parse(textBox.Text);}private void DrawBorders() {//绘制边框for (int i = 0; i < 9; i++){Border border = new Border();border.Height = 120;border.Width = 120;border.BorderThickness = new Thickness(3, 3, 3, 3);border.BorderBrush = Brushes.Black;BorderPanel.Children.Add(border);}}private void InitSudokuArr() {//数独数据初始化numArr = new int[9,9] {{ 7, 1, 3, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 5, 0, 3 },{ 0, 0, 6, 0, 3, 8, 0, 7, 0 },{ 9, 7, 0, 0, 0, 6, 0, 0, 8 },{ 0, 0, 0, 9, 0, 2, 0, 0, 0 },{ 2, 0, 0, 1, 0, 0, 0, 5, 6 },{ 0, 5, 0, 4, 6, 0, 8, 0, 0 },{ 8, 0, 2, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 6, 1, 7 },};for (int i = 0; i < textBoxList.Count; i++)if (numArr[i / 9, i % 9] != 0)textBoxList[i].Text = numArr[i / 9, i % 9].ToString();elsetextBoxList[i].Text = "";}private void CustomBtn_Click(object sender, RoutedEventArgs e)//清除初始数据{numArr = new int[9, 9] {{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 6 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },};for (int i = 0; i < textBoxList.Count; i++)textBoxList[i].Text = "";}private void SaveBtn_Click(object sender, RoutedEventArgs e)//保存自定义初始数据{isCustom = true;for (int i = 0; i < textBoxList.Count; i++)if (textBoxList[i].Text != "")numArr[i / 9, i % 9] = int.Parse(textBoxList[i].Text);elsenumArr[i / 9, i % 9] = 0;customNumArr = numArr;}private void RestartBtn_Click(object sender, RoutedEventArgs e)//游戏还原{if (isCustom){numArr = customNumArr;for (int i = 0; i < textBoxList.Count; i++)if (numArr[i / 9, i % 9] != 0 && textBoxList[i].Foreground == Brushes.Black)textBoxList[i].Text = numArr[i / 9, i % 9].ToString();elsetextBoxList[i].Text = "";}else {for (int i = 0; i < textBoxList.Count; i++){textBoxList[i].Foreground = Brushes.Black;}InitSudokuArr();}}bool isShowTips = false;private void Tips_Click(object sender, RoutedEventArgs e){if (isShowTips){for (int i = 0; i < textBoxList.Count; i++){if (textBoxList[i].Foreground == Brushes.Red){textBoxList[i].Text = "";textBoxList[i].FontSize = 25;textBoxList[i].Foreground = Brushes.Black;}}isShowTips = false;}else{for (int i = 0; i < tipsList.Count; i++){FillNum fillNum = tipsList[i];if (fillNum.possibleNums.Count > 0){int index = fillNum.row * 9 + fillNum.col;if (textBoxList[index].Text == ""){textBoxList[index].FontSize = 10;textBoxList[index].Foreground = Brushes.Red;for (int j = 0; j < fillNum.possibleNums.Count; j++){textBoxList[index].Text += fillNum.possibleNums[j];}}}}isShowTips = true;}}private void SolveBtn_Click(object sender, RoutedEventArgs e)//点击解决按钮{CheckWrong();//找出可填数据List<FillNum> possibleNumsList = new List<FillNum>();for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){int num = numArr[i, j];if (num == 0) {FillNum fillNum = new FillNum();fillNum.row = i;fillNum.col = j;fillNum.possibleNums = FindPossibleNums(i, j);possibleNumsList.Add(fillNum);}}}tipsList = possibleNumsList;List<List<FillNum>> Grid9List = Splict9GridList(possibleNumsList);List<List<FillNum>> Row9List = Splict9RowList(possibleNumsList);List<List<FillNum>> Col9List = Splict9ColList(possibleNumsList);for (int i = 0; i < Grid9List.Count; i++){List<FillNum> gridList = Grid9List[i];//代表某个九宫格的可填数据List<FillNum> rowList = Row9List[i];List<FillNum> colList = Col9List[i];int guessNum = 1;while (guessNum < 10) {int activeCountG = 0;int activeCountR = 0;int activeCountC = 0;   FillNum fillFlag = new FillNum();//判断九宫格for (int j = 0; j < gridList.Count; j++){List<int> possibleNums = gridList[j].possibleNums;if (possibleNums.Contains(guessNum)){fillFlag = gridList[j];activeCountG++;}}if (activeCountG == 1)FillNumber(fillFlag.row, fillFlag.col, guessNum);//判断行for (int k = 0; k < rowList.Count; k++){List<int> possibleNums = rowList[k].possibleNums;if (possibleNums.Contains(guessNum)){fillFlag = rowList[k];activeCountR++;}}if (activeCountR == 1)FillNumber(fillFlag.row, fillFlag.col, guessNum);//判断列for (int l = 0; l < colList.Count; l++){List<int> possibleNums = colList[l].possibleNums;if (possibleNums.Contains(guessNum)){fillFlag = colList[l];activeCountC++;}}if (activeCountC == 1)FillNumber(fillFlag.row, fillFlag.col, guessNum);guessNum++;}}}private List<int> FindPossibleNums(int row, int col) {//找出格子可填数字List<int> optionNums = new List<int>{1,2,3,4,5,6,7,8,9};CheckOneRow(row, ref optionNums);CheckOneCol(col, ref optionNums);CheckOneGrid(row, col, ref optionNums);if (optionNums.Count == 1)FillNumber(row, col, optionNums[0]);return optionNums;}private void CheckOneRow(int row, ref List<int> nums) {//检查一行for (int i = 0; i < 9; i++){int num = numArr[row, i];if (nums.Contains(num))nums.Remove(num);}}private void CheckOneCol(int col, ref List<int> nums) {//检查一列for (int i = 0; i < 9; i++){int num = numArr[i, col];if (nums.Contains(num))nums.Remove(num);}}private void CheckOneGrid(int row, int col, ref List<int> nums) {//检查九宫格int startI = row / 3 * 3;int startJ = col / 3 * 3;for (int i = startI; i < startI+3; i++){for (int j = startJ; j < startJ+3; j++){int num = numArr[i, j];if (nums.Contains(num))nums.Remove(num);}}}private void CheckWrong() {//检查异常//检查行列for (int i = 0; i < 9; i++){List<int> allNumsRow = new List<int>();List<int> allNumsCol = new List<int>();bool isRowSame = false;bool isColSame = false;int sameRowIndex = -1;int sameColIndex = -1;for (int j = 0; j < 9; j++){int numR = numArr[i, j];if (!allNumsRow.Contains(numR))allNumsRow.Add(numR);else if(numR != 0) {isRowSame = true;sameRowIndex = i+1;}int numC = numArr[j, i];if (!allNumsCol.Contains(numC))allNumsCol.Add(numC);else if (numC != 0){isColSame = true;sameColIndex = i+1;}}if (isRowSame)MessageBox.Show("Wrong: Row" + sameRowIndex);if  (isColSame)MessageBox.Show("Wrong: Col" + sameColIndex);}//检查九宫格for (int i = 0; i < 9; i+=3){for (int j = 0; j < 9; j+=3){List<int> allNumsGrid = new List<int>();bool isGridSame = false;for (int x = i; x < i+3; x++){for (int y = j; y < j+3; y++){int num = numArr[x, y];if (!allNumsGrid.Contains(num))allNumsGrid.Add(num);else if(num != 0)isGridSame = true;}}if (isGridSame)MessageBox.Show("Wrong: Grid" + GetGridIndex(i,j));}}}private List<List<FillNum>> Splict9GridList(List<FillNum> orginalList) {//可能填的数字按九宫格划分List<List<FillNum>> resList = new List<List<FillNum>>();for (int i = 0; i < 9; i++){resList.Add(new List<FillNum>());}for (int i = 0; i < orginalList.Count; i++){FillNum fillNum = orginalList[i];int gridIndex = GetGridIndex(fillNum.row, fillNum.col);resList[gridIndex].Add(fillNum);}return resList;}private List<List<FillNum>> Splict9RowList(List<FillNum> orginalList)//可能填的数字按行划分{List<List<FillNum>> resList = new List<List<FillNum>>();for (int i = 0; i < 9; i++){resList.Add(new List<FillNum>());}for (int i = 0; i < orginalList.Count; i++){FillNum fillNum = orginalList[i];int rowIndex = fillNum.row;resList[rowIndex].Add(fillNum);}return resList;}private List<List<FillNum>> Splict9ColList(List<FillNum> orginalList)//可能填的数字按列划分{List<List<FillNum>> resList = new List<List<FillNum>>();for (int i = 0; i < 9; i++){resList.Add(new List<FillNum>());}for (int i = 0; i < orginalList.Count; i++){FillNum fillNum = orginalList[i];int colIndex = fillNum.col;resList[colIndex].Add(fillNum);}return resList;}private int GetGridIndex(int i, int j) {//获取所在九宫格索引if (i < 3 && j < 3) return 0;else if (i < 3 && j < 6) return 1;else if (i < 3) return 2;else if (i < 6 && j < 3) return 3;else if (i < 6 && j < 6) return 4;else if (i < 6) return 5;else if (j < 3) return 6;else if (j < 6) return 7;else return 8;}private void FillNumber(int i, int j, int num) {//填充数据numArr[i,j] = num;textBoxList[i * 9 + j].Foreground = Brushes.Green;textBoxList[i * 9 + j].Text = num.ToString();//MessageBox.Show((i + 1) + "行" + (j + 1) + "列解决:" + num);}}
}

以下是实际效果截图:

点击Clear按钮可以清除原始数据,然后用户输入数据,点击Save保存数据。

点击Tips按钮可以显示红色提示文本,点击第二次可以隐藏。

另外优化了求解算法,可以再更少的步骤得出结果。

 

 


http://www.ppmy.cn/news/563447.html

相关文章

【开源】完美破解九宫格(数独)游戏

数独是一种比较费时费脑的游戏&#xff0c;一般难度的数独玩下来也得1个小时左右&#xff0c;本人是伪数独爱好者&#xff0c;碰到难点的数独需要花上若干个小时&#xff0c;于是偷懒写了一套破解程序&#xff0c;特拿出来分享&#xff0c;希望有人喜欢。 思路&#xff1a; 1、…

经典数独游戏+数独求解器—纯C语言实现

“心常乐数独小游戏”&#xff08;以下简称“本软件”&#xff09;是一款windows平台下的数独游戏软件。 本软件是开源、免费软件。 本软件使用纯C语言编写&#xff0c;MinGW编译&#xff0c;NSIS打包。 本软件主要特性如下&#xff1a; 支持“闯关模式”和“选关模式” 支…

python实现简易数独小游戏

起源 既然“数独”有一个字是“数”&#xff0c;人们也往往会联想到数学&#xff0c;那就不妨从大家都知道的数学家欧拉说起&#xff0c;但凡想了解数独历史的玩家在网络、书籍中搜索时&#xff0c;共同会提到的就是欧拉的“拉丁方块&#xff08;Latin square&#xff09;”。…

1.数独游戏(生成题目解唯一)

前几天在玩数独游戏的时候&#xff0c;产生了一个大胆的想法&#xff1a; 数独app上的题目都是现成的&#xff0c;干脆自己写一个可以随机生成数独的程序算了 一、需求提出&#xff1a; 1.随机生成数独题目&#xff0c;要求该题目有解&#xff08;有一个解最好&#xff09;&a…

(附代码)数独大作业【读取数独,解数独,生成唯一解数独(随机,特定形状,不同难度生成),玩数独】

注:未经同意不要转载。 上学期简单的做了一个数独程序&#xff0c;实现了一些功能&#xff0c;想简单的为大家提供的思路。 为了避免某些情况出现&#xff0c;具体代码暂时先不发了&#xff0c;有不太懂的地方可以评论提问啊。 下面是我的具体报告&#xff1a; 一&#xff…

让程序自动玩数独游戏让你秒变骨灰级数独玩家

程序自动填网页数独游戏 有个玩数独游戏的网站&#xff1a;https://www.sudoku.name/index-cn.php 当然这类玩数独游戏的网站很多&#xff0c;现在我们先以该网站为例进行演示。 玩过的都非常清楚数独的基本规则&#xff1a; 数字 1-9 在每一行只能出现一次。数字 1-9 在每一…

项目集活动—项目集交付阶段活动

项目集交付阶段活动包括协调和管理项目集实际交付所需执行的项目集活动。这些活动包括围绕 变更控制、报告和信息发布所开展的活动&#xff0c;以及围绕成本、采购、质量和风险所开展的活动。 这些活动提供了贯穿整个项目集生命周期的支持活动和流程&#xff0c;旨在提供项目集…

为什么要做数据治理

在数聚股份看来&#xff0c;数据治理是管理企业数据资源、提高数据价值的一种方法。它是一种综合性的管理方式&#xff0c;旨在帮助企业提高数据管理效率&#xff0c;加强数据安全管理&#xff0c;优化数据质量。随着企业数据规模的不断增大&#xff0c;数据治理越来越成为企业…