有一天,小明同学在津津有味的逛力扣网站的时候,惊喜的发现了一个看似非常好下手的题目(万恶之源 T T)。
? 于是小明同学又津津有味的写了解题思路。
像往常一样打开了Unity准备开始做。
。(画UI)
。。(写脚本,使用不停产生1~9的随机数的办法,往一个一个空填,来达到解数独的目的)
。。。(挂组件)
好耶。
测试环节:
1个空测试(没问题)
2个空填随机数(没问题)
81个空(没反应)
也许Untiy前端程序员最怕的不是满屏的报错,而是代码没问题,引擎设置也没问题,但是跑不起来。(核心算法逻辑出错 T T)
于是乎,在某一次冥思苦想怎么解数独的时候,一拍大腿,为啥俺不能做个数独游戏,这不简单多了(— —)。
所以他来了。既然咱解不了数独就做个数独游戏自己解吧(迷之自信)。
成功解数独:
?失败解数独:
打草稿功能:(因为发现我们平常在想完成数独的时候,总是习惯使用一草稿张来验算,而市面上大多软件都没有,我就加入了这个功能,也勉强算是一种创新吧)
?部分源代码:
Sudoke类:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class Sudoke : MonoBehaviour
{
public GameObject[] AllGrid;//全部格子
public GameObject m_Panel;
public GameObject m_RawImage;
public GameObject m_Paper;
public GameObject m_Paper_1;
public GameObject tici;//题次
public Button m_SolveBtn;//解数独按钮
public Button Paper;//纸按钮
[Header("格子的值")]
public List<string> GridValue = new List<string>();
public bool canWrite;
[SerializeField]
private char[] SudokeAllNum;//全部的数
private GameObject m_Grid;
private string m_GridValue;//格子的值
private string randomNum;
public void Awake()
{
tici.GetComponent<Text>().text = "第1题";
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 行存储
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 列存储
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 宫存储
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>
/// 格子动态赋值
/// </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>
/// 获取到格子的值
/// </summary>x
private void GetGridValue()
{
GridValue.Clear();//刷新列表中的值
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);//列表存储格子中输入的值
}
}
}
/// <summary>
/// 行存储
/// </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>
/// 列存储
/// </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>
/// 宫存储
/// </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>
/// 添加试题
/// </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>
/// 写入试题
/// </summary>
public void Exam()
{
//第一题
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>
/// 实现打草稿操作
/// </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>
/// 设置每一个输入框只能输入一个字符
/// </summary>
public void AddLimit()
{
for (int i = 0; i < AllGrid.Length; i++)
{
AllGrid[i].GetComponent<InputField>().characterLimit = 1;
}
}
}
NumsManager类:
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>();//九列
public List<string> Row_0, Row_1, Row_2, Row_3, Row_4, Row_5, Row_6
, Row_7, Row_8 = new List<string>();//九行
public List<string> Box_0, Box_1, Box_2, Box_3, Box_4, Box_5, Box_6
, Box_7, Box_8 = new List<string>();//九宫
public List<string> OriginalAsw = new List<string>() {"1","2","3","4","5","6","7","8","9"};
public int completeNum = 0;
public GameObject OK;//成功
public GameObject NoOK;//失败
public Button completeBtn;//完成按钮
NumsManager()
{
instance = this;
}
void Awake()
{
completeBtn.onClick.AddListener(TestOkorNot);
}
public void Update()
{
//鼠标点击任意位置继续游戏
if (Input.GetMouseButtonDown(0))
{
NoOK.gameObject.SetActive(false);
}
}
/// <summary>
/// 检测系统
/// </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即为完成条件
{
OK.gameObject.SetActive(true);
}
else
{
NoOK.gameObject.SetActive(true);
}
}
/// <summary>
/// 检测原理
/// </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;
}
}
}
}
}
? ? ?目前博主也是顺利毕业了,所以有更多时间来分享技术博客了。回首大学四年的时光,还是有诸多感慨啊,但是永远驻足在一个地方的话,就欣赏不了后面的风景了,所以只管大步的朝前方走去吧(2022的一个毕业生寄语)。
|