WPF桌面应用实例(四):写一个数独解题器

4 篇文章 0 订阅

数独是以前经常玩的游戏,很锻炼逻辑能力,今天写了一个数独解题器,基本可以在一分钟内解决问题,很方便。

以下是代码:

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,84,40,275" Content="OneStep"/>
        <Button Name="RestartBtn" Height="50" Width="100" Margin="430,210,40,145" Content="Restart"/>
    </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;
        }

        public MainWindow()
        {
            InitializeComponent();
            Create81Grids();
            DrawBorders();
            InitSudokuArr();
            SolveBtn.Click += SolveBtn_Click;
            RestartBtn.Click += RestartBtn_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.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] {
                { 0, 0, 0, 0, 0, 0, 2, 0, 0 },
                { 0, 1, 2, 0, 0, 6, 0, 9, 0 },
                { 0, 3, 0, 2, 5, 0, 0, 0, 1 },
                { 0, 0, 8, 1, 0, 0, 0, 4, 0 },
                { 0, 0, 5, 0, 7, 0, 3, 0, 0 },
                { 0, 4, 0, 0, 0, 2, 9, 0, 0 },
                { 2, 0, 0, 0, 3, 4, 0, 7, 0 },
                { 0, 7, 0, 9, 0, 0, 6, 5, 0 },
                { 0, 0, 3, 0, 0, 0, 0, 0, 0 },
            };
            for (int i = 0; i < textBoxList.Count; i++)
                if (numArr[i / 9, i % 9] != 0)
                    textBoxList[i].Text = numArr[i / 9, i % 9].ToString();
                else
                    textBoxList[i].Text = "";
        }

        private void RestartBtn_Click(object sender, RoutedEventArgs e)//游戏还原
        {
            for (int i = 0; i < textBoxList.Count; i++)
            {
                textBoxList[i].Foreground = Brushes.Black;
            }
            InitSudokuArr();
        }

        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);
                    }
                }
            }

            List<List<FillNum>> Grid9List = Splict9GridList(possibleNumsList);
            for (int i = 0; i < Grid9List.Count; i++)
            {
                List<FillNum> gridList = Grid9List[i];//代表某个九宫格的可填数据
                int guessNum = 1;
                while (guessNum < 10) {
                    int activeCount = 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];
                            activeCount++;
                        }
                    }

                    if (activeCount == 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 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);
        }
    }
}

以下是通过解题器解出的数独:

黑色为初始题目数据,绿色为解题器计算数据,蓝色为玩家输入的猜测数据。

需要注意的是,输入数字之后,需要按Enter键确认。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值