示例#1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var position = SokobanPosition.LoadFromFile(defaultLevel);
            //position.GetSuccessors();
            //this.Draw(canvas, position);
            //return;

            var solver = new Solver <SokobanPosition>();

            this.solution = solver.AStar(position);
            string str = "";

            if (solution.FinalPosition != null)
            {
                this.index = 0;
                this.path  = SokobanUtil.GetFullPath(solution.GetPath().ToArray());
                this.DrawStep();
                str += string.Format("time: {0}\n", solution.Time);
                str += string.Format("pushes: {0}\n", solution.GetPath().Count - 1);
                str += string.Format("expanded: {0}\n", solution.ExpandedNodesCount);
                str += string.Format("visited: {0}\n", solution.VisitedNodesCount);
                str += string.Format("all steps: {0}\n", SokobanUtil.GetFullPath(solution.GetPath().ToArray()).Count - 1);
            }
            else
            {
                str = "Solution not found";
            }

            textBox.Clear();
            textBox.Text = str;
        }
    void SolveListener()
    {
        //读取地图符号
        List <Level> stagelist    = GameObject.Find("Level").GetComponent <Levels>().levels;
        int          index        = GameObject.Find("Level").GetComponent <Levels>().CurrentLevel - 1;
        Level        currentStage = stagelist[index];
        string       stageDef     = "";

        for (int _y = 0; _y < currentStage.SizeY; _y++)
        {
            for (int _x = 0; _x < currentStage.SizeX; _x++)
            {
                char s = currentStage.levelDef[_x * currentStage.SizeY + _y][0];
                stageDef += s;
                if (_x == currentStage.SizeX - 1)
                {
                    stageDef += Environment.NewLine;
                }
            }
        }
        Debug.Log(stageDef);
        FileStream stream = File.Open("temp.txt", FileMode.OpenOrCreate, FileAccess.Write);

        stream.Seek(0, SeekOrigin.Begin);
        stream.SetLength(0);
        stream.Close();
        StreamWriter sw = new StreamWriter("temp.txt", true, System.Text.Encoding.GetEncoding("gb2312"));

        sw.WriteLine(stageDef);
        sw.Flush();
        sw.Close();
        //算出路径
        var position = SokobanPosition.LoadFromFile("temp.txt");
        var solver   = new Solver <SokobanPosition>();
        Solution <SokobanPosition> solution = solver.AStar(position);

        if (solution.FinalPosition != null)
        {
            path = SokobanUtil.GetFullPath(solution.GetPath().ToArray());
            SolveText.GetComponent <Text>().text = "Solution: \n" + path;
        }
        else
        {
            SolveText.GetComponent <Text>().text = "Solution not found.";
        }
        //按钮 播放动画 ↑↓←→
        Button btn2 = GameObject.Find("UI/Button2").GetComponent <Button>();

        btn2.interactable = true;
        btn2.onClick.AddListener(StartAnimate);
    }