Algorithm Game-Sudoku
博客专区 > Wayen007 的博客 > 博客详情
Algorithm Game-Sudoku
Wayen007 发表于8个月前
Algorithm Game-Sudoku
  • 发表于 8个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: A game about Sudoku, but I just implement it, when the game has only one solution. If you have a hobby, you can continue to implement it.

Sudoku Solver:

  • Write a program to solve a Sudoku puzzle by filling the empty cells.
  • Empty cells are indicated by the character '.'.
  • You may assume that there will be only one unique solution.

          

  • Answer :

          

...and its solution numbers marked in red.

C# Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sudoku
{
    class Suduku
    {
        /// <summary>
        /// Keep all of values related every time after running the programme.
        /// </summary>
        private static string str;

        /// <summary>
        /// Keep all of array's data
        ///
        /// </summary>
        private static string arrayStr;

        static void Main(string[] args)
        {
            string[] a = new string[9];
            string[,] arr = new string[,] {
                {"5","3",".",".","7",".",".",".","." },
                {"6",".",".","1","9","5",".",".","." },
                {".","9","8",".",".",".",".","6","." },
                {"8",".",".",".","6",".",".",".","3" },
                {"4",".",".","8",".","3",".",".","1" },
                {"7",".",".",".","2",".",".",".","6" },
                {".","6",".",".",".",".","2","8","." },
                {".",".",".","4","1","9",".",".","5" },
                {".",".",".",".","8",".",".","7","9" }
            };

            // If key is true, "while" cyscle will continue to run. Otherwise, the process will jump out of cycle.
            bool key = true;

            // This array keep results after running once 'while' cycle.
            string[,] result = new string[8, 8];
            result = arr;

            Console.WriteLine("   -------Sudoku Game Question--------");
            result = arr;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write("   " + result[i, j]);
                }
                Console.Write("\n\n");
            }
            Console.Write("   Let's play this game!\n\n");

            // "while" cycle that it can deal with datas and replace '.';
            while (key)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (arr[i, j] == ".")
                        {
                            Array.Clear(a, 0, a.Length);

                            // X stand for rows, Y stand for columns.
                            int X = i / 3;
                            int Y = j / 3;
                            for (int i1 = 0; i1 < 3; i1++)
                            {
                                for (int j1 = 0; j1 < 3; j1++)
                                {
                                    a = isExist(a, arr[X * 3 + i1, Y * 3 + j1]);
                                }
                            }
                            // Check row's values existed
                            for (int m = 0; m < 9; m++)
                            {
                                a = isExist(a, arr[i, m]);
                            }
                            // Check column's values existed
                            for (int n = 0; n < 9; n++)
                            {
                                a = isExist(a, arr[n, j]);
                            }

                            for (int k = 0; k < 9; k++)
                            {
                                if (a[k] != "")
                                {
                                    str += a[k];
                                }
                                else
                                {
                                    str += "";
                                }
                            }

                            //Console.WriteLine("Verify this string:{0}", str);
                            if (str.Length != 8)
                            {
                                str = string.Empty;
                                continue;
                            }
                            else
                            {
                                if (!str.Contains("1"))
                                {
                                    result[i, j] = "1";
                                }
                                if (!str.Contains("2"))
                                {
                                    result[i, j] = "2";
                                }
                                if (!str.Contains("3"))
                                {
                                    result[i, j] = "3";
                                }
                                if (!str.Contains("4"))
                                {
                                    result[i, j] = "4";
                                }
                                if (!str.Contains("5"))
                                {
                                    result[i, j] = "5";
                                }
                                if (!str.Contains("6"))
                                {
                                    result[i, j] = "6";
                                }
                                if (!str.Contains("7"))
                                {
                                    result[i, j] = "7";
                                }
                                if (!str.Contains("8"))
                                {
                                    result[i, j] = "8";
                                }
                                if (!str.Contains("9"))
                                {
                                    result[i, j] = "9";
                                }
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                }

                // Clean up this string
                arrayStr = string.Empty;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        arrayStr += result[i, j];
                    }
                }

                // According to key value, whether the cycle continue to run.
                if (arrayStr.Contains("."))
                {
                    key = true;
                }
                else
                {
                    key = false;
                }
            }

            // Print Sudoku Game Result
            Console.WriteLine("   -------Sudoku Game Result--------");
            result = arr;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write("   " + result[i, j]);
                }
                Console.Write("\n\n");
            }
            Console.ReadKey();


        }

        /// <summary>
        /// Create a new array to store row's or column's values.
        /// </summary>
        /// <param name="a1">store row's or column's values</param>
        /// <param name="v">array arr's data</param>
        /// <returns></returns>
        private static string[] isExist(string[] a, string v)
        {
            switch (v)
            {
                case "1": a[0] = v; break;
                case "2": a[1] = v; break;
                case "3": a[2] = v; break;
                case "4": a[3] = v; break;
                case "5": a[4] = v; break;
                case "6": a[5] = v; break;
                case "7": a[6] = v; break;
                case "8": a[7] = v; break;
                case "9": a[8] = v; break;
                default: break;
            }
            return a;
        }
    }
}

Running results:

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 4
码字总数 1466
×
Wayen007
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: