Random Cube Algorithm

本来我就是魔方爱好者,所有是魔友当然少不了pc端的计时器,以下是魔方打乱公式算法代码,语言:c#;

public static string rndCube( int max ); 是获取随机打算字符串的入口函数;

max就是打乱长度了。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

////

////code by:chouhom

////

namespace ConsoleApplication1

{

    class Program

    {

        static string[] rndMove = new string[] { "R", "L", "F", "B", "U", "D" };

        static List<string> arr = new List<string>();

        static string[] Arr;

        static Random r = new Random();

        /// <summary>

        /// slice

        /// </summary>

        /// <param name="move1"></param>

        /// <param name="move2"></param>

        /// <returns></returns>

        public static bool slice(string move1, string move2)

        {

            int i = Convert.ToInt32(move1);

            int k = Convert.ToInt32(move2);

            if (i / 2 == k / 2)

                return true;

            else return false;

        }

        /// <summary>

        /// random filter

        /// </summary>

        /// <param name="tmpRnd">int.tostring</param>

        /// <returns>false is yes</returns>

        public static bool checkmove(string tmpRnd)

        {

            Arr = arr.ToArray();

            if (Arr.Length > 0 && tmpRnd == Arr[Arr.Length - 1])

                return true;

            else

            {

                if (Arr.Length > 1 && tmpRnd == Arr[Arr.Length - 2] && slice(tmpRnd, Arr[Arr.Length - 1]))

                    return true;

                else return false;

            }

        }

        /// <summary>

        /// create random cube string

        /// </summary>

        /// <param name="max">length</param>

        /// <returns>randomCube string</returns>

        public static string rndCube(int max)

        {

            string move = "";

            string tmpRnd;

            max++;

            for (int i = 0; i < max; i++)

            {

                do { 

                    tmpRnd = r.Next(6).ToString(); 

                }

                while (checkmove(tmpRnd));

                arr.Add(tmpRnd);

            }

            for (int i = 0; i < Arr.Length; i++)

            {

                int tmp = r.Next(5);

                if (tmp == 4) { move += rndMove[Convert.ToInt32(Arr[i])] + "2 "; }

                else if (tmp == 2 || tmp == 3)

                { move += rndMove[Convert.ToInt32(Arr[i])] + "' "; }

                else

                { move += rndMove[Convert.ToInt32(Arr[i])] + " "; }

            }

            return move;

        }

        static void Main(string[] args)

        {

            Console.Write(rndCube(15));

            Console.ReadKey();

        }

    }

}

 

你可能感兴趣的:(Algorithm)