WPF桌面應用實例(四):寫一個數獨解題器

數獨是以前經常玩的遊戲,很鍛鍊邏輯能力,今天寫了一個數獨解題器,基本可以在一分鐘內解決問題,很方便。

以下是代碼:

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鍵確認。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章