当前位置:网站首页>"I can't understand Sudoku, so I choose to play Sudoku."

"I can't understand Sudoku, so I choose to play Sudoku."

2022-06-24 08:57:00 Foulove

one day , Xiaoming was browsing the Li Kou website with relish , I was pleasantly surprised to find a topic that seemed very easy to start ( The root of all evil T T).

  So Xiaoming wrote the problem-solving ideas with interest .

Opened as usual Unity Ready to start doing .

.( draw UI)

..( Write the script , Use constantly produces 1~9 The method of random numbers , Fill in the blanks one by one , To solve Sudoku )

...( Hanging components )

Yes .

The test link :

1 Empty tests ( That's all right. )

2 Blank random numbers ( That's all right. )

81 Empty ( No response )

Maybe Untiy What front-end programmers fear most is not full screen error reporting , But the code is OK , No problem with the engine setup , But I can't run .( Core algorithm logic error T T)

So , At a time when I was thinking hard about how to solve Sudoku , A pat on the thigh , Why can't I play Sudoku , It's not easy (— —).

So he came . Since we can't solve Sudoku, let's make a Sudoku game and solve it by ourselves ( Confidence in fans ).

Solve Sudoku successfully :

  Fail to solve Sudoku :

Draft function :( Because I found that we usually want to finish Sudoku , Always used to use a draft sheet to check , Most of the software on the market is not available , I added this function , It is also an innovation )

  Partial source code :

Sudoke class :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class Sudoke : MonoBehaviour
{
    public GameObject[] AllGrid;// All squares 
    public GameObject m_Panel;
    public GameObject m_RawImage;
    public GameObject m_Paper;
    public GameObject m_Paper_1;
    public GameObject tici;// Questions 
    public Button m_SolveBtn;// Sudoku button 
    public Button Paper;// Paper button 
   
    
    [Header(" The value of the lattice ")]
    public List<string> GridValue = new List<string>();
    public bool canWrite;


     [SerializeField]
    private char[] SudokeAllNum;// All the numbers 
    private GameObject m_Grid;   
    private string m_GridValue;// The value of the lattice 
    private string randomNum;
    
    public void Awake()
    {
        tici.GetComponent<Text>().text = " The first 1 topic ";
        SetGridValue();
        m_SolveBtn.onClick.AddListener(GetGridValue);
        Paper.onClick.AddListener(UsePaper);
        Exam();
        m_RawImage.gameObject.SetActive(false);
        m_Paper_1.gameObject.SetActive(true);
        AddLimit();
    }

    public void Update()
    {        
        #region  Row store 
        RowStorage(NumsManager.instance.Row_0, 0, 8);
        RowStorage(NumsManager.instance.Row_1, 9, 17);
        RowStorage(NumsManager.instance.Row_2, 18, 26);
        RowStorage(NumsManager.instance.Row_3, 27, 35);
        RowStorage(NumsManager.instance.Row_4, 36, 44);
        RowStorage(NumsManager.instance.Row_5, 45, 53);
        RowStorage(NumsManager.instance.Row_6, 54, 62);
        RowStorage(NumsManager.instance.Row_7, 63, 71);
        RowStorage(NumsManager.instance.Row_8, 72, 80);
        #endregion
        #region  Column store 
        ColStorage(NumsManager.instance.Col_0, 0);
        ColStorage(NumsManager.instance.Col_1, 1);
        ColStorage(NumsManager.instance.Col_2, 2);
        ColStorage(NumsManager.instance.Col_3, 3);
        ColStorage(NumsManager.instance.Col_4, 4);
        ColStorage(NumsManager.instance.Col_5, 5);
        ColStorage(NumsManager.instance.Col_6, 6);
        ColStorage(NumsManager.instance.Col_7, 7);
        ColStorage(NumsManager.instance.Col_8, 8);
        #endregion
        #region  Palace storage 
        BoxStorage(NumsManager.instance.Box_0, 0, 1, 2);
        BoxStorage(NumsManager.instance.Box_1, 3, 4, 5);
        BoxStorage(NumsManager.instance.Box_2, 6, 7, 8);
        BoxStorage(NumsManager.instance.Box_3, 27, 28, 29);
        BoxStorage(NumsManager.instance.Box_4, 30, 31, 32);
        BoxStorage(NumsManager.instance.Box_5, 33, 34, 35);
        BoxStorage(NumsManager.instance.Box_6, 54, 55, 56);
        BoxStorage(NumsManager.instance.Box_7, 57, 58, 59);
        BoxStorage(NumsManager.instance.Box_8, 60, 61, 62);
        #endregion      
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
    /// <summary>
    ///  Lattice dynamic assignment 
    /// </summary>
    private void SetGridValue()
    {
        for (int i = 0; i < AllGrid.Length; i++)
        {
            m_Grid = m_Panel.transform.GetChild(i).gameObject;
            AllGrid.SetValue(m_Grid, i);
        }
    }
    /// <summary>
    ///  Get the value of the grid 
    /// </summary>x 
    private void GetGridValue()
    {
        GridValue.Clear();// Refresh the values in the list 
        for (int i = 0; i < AllGrid.Length; i++)
        {
            m_Grid = m_Panel.transform.GetChild(i).gameObject;
            m_GridValue = m_Grid.GetComponent<InputField>().text;
            if (m_GridValue!=null)
            {
                GridValue.Add(m_GridValue);// The list stores the values entered in the grid 
            }         
        }
        
        
       
    }
    /// <summary>
    ///  Row store 
    /// </summary>
    /// <param name="list"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    public void RowStorage(List<string> list, int min, int max)
    {
        list.Clear();
        for (int i = min; i <= max; i++)
        {
            list.Add(AllGrid[i].GetComponent<InputField>().text);
        }
    }
    /// <summary>
    ///  Column store 
    /// </summary>
    /// <param name="list"></param>
    /// <param name="index"></param>
    public void ColStorage(List<string> list,int index)
    {
        list.Clear();
        for (int i = 0; i < 9; i++)
        {
            list.Add(AllGrid[index].GetComponent<InputField>().text);
            index += 9;
        }
    }
    /// <summary>
    ///  Palace storage 
    /// </summary>
    /// <param name="list"></param>
    /// <param name="startNum"></param>
    /// <param name="midNum"></param>
    /// <param name="lastNum"></param>
    public void BoxStorage(List<string> list,int startNum,int midNum,int lastNum)
    {
        list.Clear();
        for (int i = 0; i < 3; i++)
        {
            list.Add(AllGrid[startNum].GetComponent<InputField>().text);
            list.Add(AllGrid[midNum].GetComponent<InputField>().text);
            list.Add(AllGrid[lastNum].GetComponent<InputField>().text);
            startNum += 9;
            midNum += 9;
            lastNum += 9;
        }
    }
  
    /// <summary>
    ///  Add questions 
    /// </summary>
    /// <param name="orderNum"></param>
    /// <param name="examNum"></param>
    public void AddExam(int orderNum,string examNum)
    {
        for (int i = 0; i < AllGrid.Length; i++)
        {
            AllGrid[orderNum].GetComponent<InputField>().text = examNum;
            AllGrid[orderNum].GetComponent<InputField>().interactable = false;
        }
    }
    /// <summary>
    ///  Write the test questions 
    /// </summary>
    public void Exam()
    {
        // The first question is 
        AddExam(3, "9");
        AddExam(4, "5");
        AddExam(5, "7");
        AddExam(6, "3");
        AddExam(7, "8");
        AddExam(8, "2");
        AddExam(15, "5");
        AddExam(16, "4");
        AddExam(17, "1");
        AddExam(12, "6");
        AddExam(13, "2");
        AddExam(14, "3");
        AddExam(21, "1");
        AddExam(22, "8");
        AddExam(23, "4");
        AddExam(24, "7");
        AddExam(29, "4");
        AddExam(30, "8");
        AddExam(32, "5");
        AddExam(38, "5");
        AddExam(40, "6");
        AddExam(41, "2");
        AddExam(48, "4");
        AddExam(50, "1");
        AddExam(51, "6");
        AddExam(54, "7");
        AddExam(57, "3");
        AddExam(60, "2");
        AddExam(61, "6");
        AddExam(63, "4");
        AddExam(64, "2");
        AddExam(66, "5");
        AddExam(69, "9");
        AddExam(71, "3");
        AddExam(72, "6");
        AddExam(73, "5");
        AddExam(78, "4");
        AddExam(79, "1");
    }
    /// <summary>
    ///  Realize the draft making operation 
    /// </summary>
    public void UsePaper()
    {
        if (canWrite == true)
        {
            m_RawImage.gameObject.SetActive(false);
            m_Paper.gameObject.SetActive(false);
            m_Paper_1.gameObject.SetActive(true);
            canWrite = false;
        }
        else
        {
            m_RawImage.gameObject.SetActive(true);
            m_Paper_1.gameObject.SetActive(false);
            m_Paper.gameObject.SetActive(true);
            canWrite = true;
        }
    }

    /// <summary>
    ///  Set that each input box can only input one character 
    /// </summary>
    public void AddLimit()
    {
        for (int i = 0; i < AllGrid.Length; i++)
        {
            AllGrid[i].GetComponent<InputField>().characterLimit = 1;
        }
    }

    
}

NumsManager class :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NumsManager : MonoBehaviour
{
    public static NumsManager instance;
    public List<string> Col_0, Col_1, Col_2, Col_3, Col_4, Col_5, Col_6
        , Col_7 , Col_8 = new List<string>();// Nine columns 

    public List<string> Row_0, Row_1, Row_2, Row_3, Row_4, Row_5, Row_6
        , Row_7, Row_8 = new List<string>();// Nine elements 

    public List<string> Box_0, Box_1, Box_2, Box_3, Box_4, Box_5, Box_6
        , Box_7, Box_8 = new List<string>();// Nine palaces 
    public List<string> OriginalAsw = new List<string>() {"1","2","3","4","5","6","7","8","9"};
    public int completeNum = 0;
    public GameObject OK;// success 
    public GameObject NoOK;// Failure 
    public Button completeBtn;// Finish button 

    NumsManager()
    {
        instance = this;
    }

    void Awake()
    {
        completeBtn.onClick.AddListener(TestOkorNot);
    }

    public void Update()
    {
        // Click anywhere to continue the game 
        if (Input.GetMouseButtonDown(0))
        {
            NoOK.gameObject.SetActive(false);
        }
    }

    /// <summary>
    ///  Detection system 
    /// </summary>
    public void TestOkorNot()
    {
        completeNum = 0;
        TestAsw(Col_0);
        TestAsw(Col_1);
        TestAsw(Col_2);
        TestAsw(Col_3);
        TestAsw(Col_4);
        TestAsw(Col_5);
        TestAsw(Col_6);
        TestAsw(Col_7);
        TestAsw(Col_8);
        TestAsw(Row_0);
        TestAsw(Row_1);
        TestAsw(Row_2);
        TestAsw(Row_3);
        TestAsw(Row_4);
        TestAsw(Row_5);
        TestAsw(Row_6);
        TestAsw(Row_7);
        TestAsw(Row_8);
        TestAsw(Box_0);
        TestAsw(Box_1);
        TestAsw(Box_2);
        TestAsw(Box_3);
        TestAsw(Box_4);
        TestAsw(Box_5);
        TestAsw(Box_6);
        TestAsw(Box_7);
        TestAsw(Box_8);
        if (completeNum == 243)//3*9*9 This is the completion condition 
        {
            OK.gameObject.SetActive(true);
        }
        else
        {
            NoOK.gameObject.SetActive(true);           
        }
    }
    /// <summary>
    ///  Detection principle 
    /// </summary>
    /// <param name="nowAswList"></param>
    public void TestAsw(List<string> nowAswList)
    {       
        for (int i = 0; i < OriginalAsw.Count; i++)
        {
            for (int s = 0; s < nowAswList.Count; s++)
            {
                nowAswList.Sort();
                if (nowAswList[s]==OriginalAsw[i])
                {
                    completeNum += 1;
                }
            }
        }
    }
}

      At present, the blogger has successfully graduated , So there is more time to share the technology blog . Looking back on the four years of College , There are still many feelings , But if you stay in one place forever , I can't enjoy the scenery behind , So just stride forward (2022 A message from a graduate of ).

原网站

版权声明
本文为[Foulove]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240657469797.html