Exemplo n.º 1
0
    void Start()
    {
        _cube.init(transform);
        _cubies.init(transform);
        _solver.init(_cubies, _cube);

        if (startState == "Random")
        {
            string[]      choices = { "F", "F", "F2",
                                      "B",      "B", "B2",
                                      "R",      "R", "R2",
                                      "L",      "L", "L2",
                                      "U",      "U", "U2",
                                      "D",      "D", "D2" };
            int           maxMoves = 10;
            System.Random random   = new System.Random();
            for (int i = 0; i < maxMoves; i++)
            {
                int    idx = random.Next(0, choices.Length);
                string cmd = choices[idx];
                _cube.turn(cmd);
                Debug.Log(cmd);
            }
        }
        else
        {
            char[]   delimiterChars = { ' ' };
            string[] words          = startState.Split(delimiterChars);
            for (int w = 0; w < words.Length; w++)
            {
                string word = words[w];
                _cube.turn(word);
            }
        }

        for (int i = 0; i < _cubies.GetNumCubes(); i++)
        {
            CubeInfo.Cubie cubie = _cubies.GetCubeInfo(i);
            if (_cubies.IsCenterCube(i))
            {
                _cubies.EnableColor(cubie, true);
            }
            else
            {
                _cubies.EnableColor(cubie, false);
            }
        }

        _tasks.Add(new CubeTask(SolveTopMiddle, _cubies.TopMiddleSolved, "top row", "middle cubes"));
        _tasks.Add(new CubeTask(SolveTopCorners, _cubies.TopCornersSolved, "top row", "corner cubes"));
        _tasks.Add(new CubeTask(SolveMiddleMiddles, _cubies.MiddleMiddleSolved, "middle row", "middle cubes"));
        _tasks.Add(new CubeTask(SolveOneCornerPosition, _cubies.BottomOneCornerCorrect, "bottom row", "choose anchor corner"));
        _tasks.Add(new CubeTask(SolveBottomCornerPositions, _cubies.BottomCornersCorrectPositions, "bottom row", "corner cube positions"));
        _tasks.Add(new CubeTask(SolveBottomMiddlePositions, _cubies.BottomMiddlesCorrectPositions, "bottom row", "middle cube positions"));
        _tasks.Add(new CubeTask(SolveBottomCornerOri, _cubies.BottomCornersCorrectOri, "bottom row", "twirl corners"));
        _tasks.Add(new CubeTask(SolveBottomMiddleOri, _cubies.BottomMiddlesCorrectOri, "bottom row", "flip middles"));
    }
Exemplo n.º 2
0
    // Solve for cubie given the passed constraints
    private int Search(CubeInfo.Cubie c, List <CubeInfo.Cubie> constraints,
                       ArrayList steps, int stepNum, ScoreStateFn scoreFn,
                       ref List <string> path, ref List <string> bestPath)
    {
        int score = scoreFn(c, constraints);

        if (stepNum >= steps.Count)
        {
            //Debug.Log("  score " + score + " " + path[0]);
            if (score > 0)
            {
                // bonus for empty moves (e.g. fewer moves is better)
                foreach (string s in path)
                {
                    if (s == "")
                    {
                        score += 10;
                    }
                }
                //string spath = "";
                //foreach (string s in path) spath += s + " ";
                //Debug.Log("SCORE " + score + " " + spath + " " + path.Count);
            }
            bestPath = new List <string>(path);
            return(score);
        }

        string[] turns = (string[])steps[stepNum];
        for (int i = 0; i < turns.Length; i++)
        {
            List <Transform> list   = new List <Transform>();
            Vector3          center = new Vector3(0, 0, 0);
            Vector3          axis   = new Vector3(0, 0, 0);
            float            amount = 0.0f;

            string turn = turns[i];
            if (turn != "")
            {
                if (turn == "X")
                {
                    turn = _cube.Reverse((string)path[path.Count - 2]);
                }
                _cube.CmdToTurn(turn, out list, out center, out axis, out amount);
                _cube.turn(list, center, axis, amount); // un-apply move
            }
            path.Add(turn);
            List <string> bestChild = new List <string>();
            int           tmp       = Search(c, constraints, steps, stepNum + 1, scoreFn, ref path, ref bestChild);
            if (tmp > score)
            {
                score    = tmp;
                bestPath = new List <string>(bestChild);
            }
            if (turn != "")
            {
                _cube.turn(list, center, axis, -amount);             // un-apply move
            }
            path.RemoveAt(path.Count - 1);
        }
        return(score);
    }