Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("CuboA: " + args[0]);
            Console.WriteLine("CuboB: " + args[1]);

            RubikCube cubeA = new RubikCube();

            //cubeA.fromString("0|0|0|0|0|0|1|5|5|0|0|6|5|5|0|0|1|1|6|0|0|0|0|0|0|0|5|3|2|0|4|0|0|0|1|4|0|0|0|1|6|0|0|0|4|0|2|2|1|0|0|1|4|2|0|6|0|0|0|3|1|0|0|0|3|6|0|0|0|5|0|2|2|2|0|0|4|3|6|0|3|0|0|0|4|4|0|0|0|1|6|0|0|0|5|0|2|3|2|0|0|0|0|0|0|0|5|4|3|0|0|5|6|4|0|0|3|6|3|0|0|0|0|0|0|");
            cubeA.fromString(args[0]);

            RubikCube cubeB = new RubikCube();

            cubeB.fromString(args[1]);
            //cubeB.fromString("0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|6|0|0|0|5|0|2|2|2|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|6|0|0|0|5|0|2|2|2|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|6|0|0|0|5|0|2|0|2|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3|0|3|0|0|0|0|0|0|");

            AlgorithmCombinations alg = new AlgorithmCombinations(4);

            /*
             * RubikCube result = alg.resolveSingle(cubeA, cubeB, 9);
             *
             * if (result != null)
             * Console.WriteLine("OK!");
             * else
             * Console.WriteLine("None");
             *
             */
        }
Exemplo n.º 2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            this.palette = new PaletteUI(this, 20, 80);

            this.cubeA = new RubikCube();
            this.cubeA.setRandom();

            this.UICubeA = new RubikCubeUI(this.cubeA, 55, 80, this);

            this.cubeB = new RubikCube();
            this.cubeB.setMasterCube();

            this.UICubeB = new RubikCubeUI(this.cubeB, 340, 80, this);

            this.status   = new RubikCube();
            this.UIStatus = new RubikCubeUI(this.status, 625, 80, this);

            this.DoubleBuffered = true;
            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;

            cmbClipboard.SelectedIndex = 0;
            this.btnStop.Enabled       = false;
            this.btnSolution.Enabled   = false;

            cmbAlgorithm.Items.Add(new AlgorithmCombinations());
            cmbAlgorithm.SelectedIndex = 0;
        }
Exemplo n.º 3
0
    public void RunTest()
    {
        var rubik = new RubikCube(settings);

        rubik.UseCommand(rubikMonoBehaviour.Command);
        var node = new Node(rubik, settings);

        Debug.Log("Start ida");
        var watch = System.Diagnostics.Stopwatch.StartNew();

        var task = Task.Run(() => RubikIDA.IdaStar(node));

        if (!task.Wait(TimeSpan.FromSeconds(TimeOut)))
        {
            throw new Exception("Timed out");
        }

        watch.Stop();
        var elapsedMs = watch.ElapsedMilliseconds;

        var idaResults = task.Result;

        if (idaResults == null)
        {
            return;
        }

        rubikMonoBehaviour.Decision = idaResults.Value.path.ToArray()[0].Command();

        Debug.Log("Time " + TimeSpan.FromMilliseconds(elapsedMs).TotalSeconds);
        Debug.Log(rubikMonoBehaviour.Decision);
    }
Exemplo n.º 4
0
 private void SegCountBox_ValueChanged(object sender, EventArgs e)
 {
     RubikCube.N      = (int)SegCountBox.Value;
     RubikCube.Parent = null;
     RubikCube        = new RubikCube();
     RubikCube.Parent = tglView1.Context.Root;
     tglView1.Invalidate();
 }
Exemplo n.º 5
0
    private void CreateCubes()
    {
        cubesCount = cubesOrder * cubesOrder * cubesOrder;
        cubes      = new RubikCube[cubesCount];

        for (int i = 0; i < cubesCount; i++)
        {
            cubes[i] = new RubikCube(this, i);
        }
    }
Exemplo n.º 6
0
 private void drawCubeFaceLarge(int[, ,] cubex)
 {
     for (int l = 0; l < 3; l++)
     {
         Console.WriteLine("----------------------------");
         drawLine(RubikCube.getFace(cubex, 5), l);
         drawLine(RubikCube.getFace(cubex, 2), l);
         drawLine(RubikCube.getFace(cubex, 6), l);
         Console.WriteLine("|");
     }
 }
Exemplo n.º 7
0
        public void RubikCube_CreateInvalidSize_ThrowArgumentException(int size)
        {
            // 1. Prepare
            RubikCube c;

            // 2. Execute
            void a() => c = new RubikCube(size);

            // 3. Verify
            Assert.Throws <ArgumentException>(a);
        }
Exemplo n.º 8
0
        public void RubikCube_CreateAndCountBlocks_ShouldMatch(int size, int blocksCount)
        {
            // 1. Prepare
            // Nothing

            // 2. Execute
            RubikCube c     = new RubikCube(size);
            int       count = c.Blocks.Count();

            // 3. Verify
            Assert.Equal(blocksCount, count);
        }
Exemplo n.º 9
0
        public RubikCubeUI(RubikCube cube, int xi, int yi, Form f)
        {
            this.Cube = cube;

            // this.Cube.UpdateCube += new RubikCube.updateCubeHandler(updateCube);

            this.offsetX = xi;
            this.offsetY = yi;
            this.form    = f;

            // Calculo en memoria la posición de los cuadrados
            this.calcFaces();
        }
Exemplo n.º 10
0
 private void updateAlgStatus(RubikCube cube)
 {
     this.lOperations.Text = this.alg.Operations.ToString();
     if (cube != null)
     {
         status.fromMatrix(cube.ToMatrix());
     }
     else
     {
         status.clear();
     }
     this.Invalidate(new Rectangle(this.UIStatus.X, this.UIStatus.Y, this.UIStatus.Width, this.UIStatus.Height));
     this.Update();
 }
Exemplo n.º 11
0
 private void drawCubeFace(int[,,] cubex, int face)
 {
     Console.WriteLine("         ----------");
     for (int l = 0; l < 3; l++)
     {
         Console.Write("         ");
         for (int c = 0; c < 3; c++)
         {
             Console.Write("|" + getColorByNum(RubikCube.getFace(cubex, face)[l, c]));
             //Console.Write("|" + ((this.Cube.getFace(face)[l, c] < 10)?"0":"") + this.Cube.getFace(face)[l, c].ToString());
         }
         Console.WriteLine("|");
     }
 }
Exemplo n.º 12
0
        public void RubikCube_Rotate_CenterStayInPlace(string axisId)
        {
            // 1. Prepare
            RubikCube c               = new RubikCube(3);
            var       axis            = c.GetAxis(axisId);
            var       center          = c.Blocks.Where(b => b.Position.IsSameVector(axis.Vector)).First();
            var       initialPosition = center.Position;

            // 2. Execute
            c.RotateAround(axis, true);

            // 3. Verify
            Assert.Equal(initialPosition.X, center.Position.X, PRECISION_DOUBLE);
            Assert.Equal(initialPosition.Y, center.Position.Y, PRECISION_DOUBLE);
            Assert.Equal(initialPosition.Z, center.Position.Z, PRECISION_DOUBLE);
        }
Exemplo n.º 13
0
        private void button8_Click(object sender, EventArgs e)
        {
            this.ConsoleWrite("Testing cube viewpoints...", true);

            RubikCube nc = this.cubeA.Clone();

            nc.Equals(this.cubeA);

            for (int idx = 0; idx < 24; idx++)
            {
                this.status.fromMatrix(nc.viewPoints[idx]);
                this.Refresh();

                this.ConsoleWrite(string.Format("Test #{0} CubeA compare: {1}", idx.ToString(), this.cubeA.Equals(this.status).ToString()), false);
                System.Threading.Thread.Sleep(500);
                //MessageBox.Show("Next...");
            }
        }
Exemplo n.º 14
0
        public void RubikCube_RotateOnceOnSize2_FindExpectedFace(string axisId, bool isClockwise, string blockCoordinate, string checkedFaceId, string expectedBlockFace)
        {
            // 1. Prepare
            Cartesian3dCoordinate blockPosition = new Cartesian3dCoordinate(blockCoordinate);
            RubikCube             c             = new RubikCube(2);
            var axis             = c.GetAxis(axisId);
            var initialBlock     = c.Blocks.FirstOrDefault(b => b.Position.IsSameVector(blockPosition));
            var targetFaceVector = c.GetFace(checkedFaceId).Coordinates.Normal;

            // 2. Execute
            c.RotateAround(axis, isClockwise);
            var block = c.Blocks.FirstOrDefault(b => b.Position.IsSameVector(blockPosition));
            var face  = block.GetBlockFace(targetFaceVector);

            // 3. Verify
            Assert.NotEqual(initialBlock.Id, block.Id);
            Assert.NotNull(face);
            Assert.Equal(expectedBlockFace, face.Id);
        }
Exemplo n.º 15
0
        public double OnEvaluate(TChromosome specimen)
        {
            var cube        = new RubikCube(RubikCube);
            var bestFitness = double.MaxValue;

            for (int len = 0; len < specimen.Genes.Length; len++)
            {
                var move = new TMove();
                move.Decode((int)specimen.Genes[len]);
                cube.MakeMove(move);
                var fitness = cube.Evaluate();
                if (fitness < bestFitness)
                {
                    bestFitness = fitness;
                    (specimen as RubikGenome).MovesCount = len + 1;
                }
            }
            specimen.Fitness = bestFitness;
            return(bestFitness);
        }
Exemplo n.º 16
0
        public void RubikCube_CreateAndCountBlocksPerFace_ShouldMatch(int size, int blocksCount)
        {
            // 1. Prepare
            int i = 0;

            int[] results = new int[6];

            // 2. Execute
            RubikCube c = new RubikCube(size);

            foreach (RotationAxis axis in c.Axes)
            {
                results[i++] = c.GetBlocksForFace(axis.Vector).Count();
            }

            // 3. Verify
            for (i = 0; i < results.Length; ++i)
            {
                Assert.Equal(blocksCount, results[i]);
            }
        }
Exemplo n.º 17
0
        public void click(int x, int y, Color color)
        {
            for (int faceid = 0; faceid < 6; faceid++)
            {
                //int[,] face = MonzaRubikLib.RubikCube.getFace(cube, faceid + 1);

                for (int idx = 0; idx < 9; idx++)
                {
                    if (
                        this.squares[faceid, idx, 0] < x && x < this.squares[faceid, idx, 0] + Side &&
                        this.squares[faceid, idx, 1] < y && y < this.squares[faceid, idx, 1] + Side
                        )
                    {
                        int[] coords = MonzaRubikLib.RubikCube.getFaceCoords(faceid + 1, this.squares[faceid, idx, 2], this.squares[faceid, idx, 3]);
                        this.Cube.ToMatrix()[coords[0], coords[1], coords[2]] = RubikCube.getNumByColor(color);
                        this.status = string.Format("X:{0} Y:{1} Z:{2}", coords[0], coords[1], coords[2]);

                        //g.FillRectangle(new SolidBrush(this.getColorByNum(face[x, y])), this.squares[faceid, idx, 0], this.squares[faceid, idx, 1], Side, Side);
                        //g.DrawRectangle(p, this.squares[faceid, idx, 0], this.squares[faceid, idx, 1], Side, Side);
                        this.form.Invalidate(new Rectangle(this.squares[faceid, idx, 0], this.squares[faceid, idx, 1], Side, Side));
                    }
                }
            }
        }
Exemplo n.º 18
0
    public void RunTest()
    {
        var rubik = new RubikCube(settings);

        rubik.UseCommand(rubikMonoBehaviour.Command);
        var node = new Node(rubik, settings);

        Debug.Log("Start ida");
        var watch = System.Diagnostics.Stopwatch.StartNew();

        var idaResults = IdaStar(node);

        watch.Stop();
        var elapsedMs = watch.ElapsedMilliseconds;

        if (idaResults == null)
        {
            return;
        }
        rubikMonoBehaviour.Decision = idaResults.Value.path.ToArray()[0].Command();
        Debug.Log("Time " + TimeSpan.FromMilliseconds(elapsedMs).TotalSeconds);
        Debug.Log(idaResults.Value.bound);
        Debug.Log(rubikMonoBehaviour.Decision);
    }
Exemplo n.º 19
0
        public void draw(Graphics g, RubikCube cubep)
        {
            if (cubep == null)
            {
                cubep = new RubikCube();
            }

            Pen p = new Pen(Color.Black);

            int x, y;

            for (int faceid = 0; faceid < 6; faceid++)
            {
                int[,] face = MonzaRubikLib.RubikCube.getFace(cubep.ToMatrix(), faceid + 1); //MonzaRubikLib.RubikCube

                for (int idx = 0; idx < 9; idx++)
                {
                    x = this.squares[faceid, idx, 2];
                    y = this.squares[faceid, idx, 3];
                    g.FillRectangle(new SolidBrush(RubikCube.getColorByNum(face[x, y])), this.squares[faceid, idx, 0], this.squares[faceid, idx, 1], Side, Side);
                    g.DrawRectangle(p, this.squares[faceid, idx, 0], this.squares[faceid, idx, 1], Side, Side);
                }
            }
        }
Exemplo n.º 20
0
 private void Form1_Shown(object sender, EventArgs e)
 {
     RubikCube        = new RubikCube();
     RubikCube.Parent = tglView1.Context.Root;
 }
Exemplo n.º 21
0
    public CubesLayer CreateLayer(int width, LayerType layerType, float margin, Dictionary <CubeSide, int> sideToColorIndex, RubikCube parentCube)
    {
        var layer = Instantiate(layerPrefab);

        for (var i = 0; i < width * width; i++)
        {
            var rowNumber  = Mathf.Floor(i / width);
            var isEdgeRow  = rowNumber % (width - 1) == 0;
            var isFirstRow = isEdgeRow && rowNumber == 0;
            var isLastRow  = isEdgeRow && !isFirstRow;

            var isFirstInRow = i % (width) == 0;
            var isLastInRow  = (i + 1) - (width * (rowNumber + 1)) == 0;

            var cubeSides = new List <CubeSide>();

            if (isFirstInRow)
            {
                cubeSides.Add(CubeSide.Left);
            }
            else if (isLastInRow)
            {
                cubeSides.Add(CubeSide.Right);
            }

            if (isFirstRow)
            {
                cubeSides.Add(CubeSide.Top);
            }
            else if (isLastRow)
            {
                cubeSides.Add(CubeSide.Bottom);
            }

            if (layerType == LayerType.Front)
            {
                cubeSides.Add(CubeSide.Front);
            }
            else if (layerType == LayerType.Back)
            {
                cubeSides.Add(CubeSide.Back);
            }

            var coloredSides = new Dictionary <CubeSide, int>();
            foreach (var side in cubeSides)
            {
                coloredSides.Add(side, sideToColorIndex[side]);
            }

            var cube = cubeFactory.GetCube(coloredSides);

            cube.transform.SetParent(layer.transform);
            cube.transform.Translate((i - (width * rowNumber)) * (1 + MarginBetweenCubes), -rowNumber - (MarginBetweenCubes * rowNumber), margin);
            layer.LayerCubes.Add(cube);
        }

        layer.ParentCube = parentCube;
        return(layer);
    }
Exemplo n.º 22
0
 /// <summary>
 /// Create a new RubikCubeTextPresenter that will present the provided cube to the Console.
 /// </summary>
 /// <param name="cube">Cube that will be displayed in the Console.</param>
 public RubikCubeTextPresenter(RubikCube cube)
 {
     this.Cube = cube;
 }
Exemplo n.º 23
0
        void Solve()
        {
            var best = new RubikGenome();

            best.Fitness = HighScore;
            foreach (var solution in Solutions)
            {
                var genome = new RubikGenome();
                var cube   = new RubikCube(RubikCube);
                for (int i = 0; i < solution.Value.Count; i++)
                {
                    var move = solution.Value[i];
                    cube.MakeMove(move);
                    genome.Genes[i] = move.Encode();
                }
                genome.Fitness = cube.Evaluate();
                if (genome.Fitness < best.Fitness)
                {
                    best = genome;
                }
            }

            if (best.Fitness >= HighScore)
            {
                TChromosome.GenesLength = 30;
                TChromosome.MinGenes    = new double[TChromosome.GenesLength];
                TChromosome.MaxGenes    = new double[TChromosome.GenesLength];
                for (int i = 0; i < TChromosome.MaxGenes.Length; i++)
                {
                    TChromosome.MaxGenes[i] = 9 * RubikCube.N;
                }
                TGA <RubikGenome> .GenerationsCount = 50;
                TGA <RubikGenome> .MutationRatio    = 0.2;
                TGA <RubikGenome> .WinnerRatio      = 0.1;
                var ga = new TGA <RubikGenome>();
                ga.Evaluate = OnEvaluate;
                ga.Progress = OnProgress;
                ga.Execute();
                best = ga.Best;

                if (best.Fitness == 0)
                {
                    SaveSolution(best);
                }
            }

            if (best.Fitness < HighScore)
            {
                Moves.Clear();
                for (int i = 0; i < best.MovesCount; i++)
                {
                    var move = new TMove();
                    move.Decode((int)best.Genes[i]);
                    Moves.Add(move);
                }

                HighScore = best.Fitness;
            }

            MoveTimer.Start();
        }
Exemplo n.º 24
0
 public Node(RubikCube rubik, HeuristicSettings settings)
 {
     this.rubik    = rubik;
     this.settings = settings;
 }
Exemplo n.º 25
0
    void ParentRubikCubes_ToRotation(RotationAxis rotAxis)
    {
        RubikCube parent = CenterOfRotation(rotAxis);

        RotationAuxiliar.transform.localPosition = parent.transform.localPosition;

        if (rotAxis == RotationAxis.X_1_POS || rotAxis == RotationAxis.X_1_NEG)
        {
            for (int y = 0; y < 3; y++)
            {
                for (int z = 0; z < 3; z++)
                {
                    cubes[0, y, z].transform.parent = RotationAuxiliar.transform;
                }
            }
        }
        else if (rotAxis == RotationAxis.X_2_POS || rotAxis == RotationAxis.X_2_NEG)
        {
            for (int y = 0; y < 3; y++)
            {
                for (int z = 0; z < 3; z++)
                {
                    cubes[1, y, z].transform.parent = RotationAuxiliar.transform;
                }
            }
        }
        else if (rotAxis == RotationAxis.X_3_POS || rotAxis == RotationAxis.X_3_NEG)
        {
            for (int y = 0; y < 3; y++)
            {
                for (int z = 0; z < 3; z++)
                {
                    cubes[2, y, z].transform.parent = RotationAuxiliar.transform;
                }
            }
        }
        else if (rotAxis == RotationAxis.Z_1_POS || rotAxis == RotationAxis.Z_1_NEG)
        {
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    cubes[x, y, 0].transform.parent = RotationAuxiliar.transform;
                }
            }
        }
        else if (rotAxis == RotationAxis.Z_2_POS || rotAxis == RotationAxis.Z_2_NEG)
        {
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    cubes[x, y, 1].transform.parent = RotationAuxiliar.transform;
                }
            }
        }
        else if (rotAxis == RotationAxis.Z_3_POS || rotAxis == RotationAxis.Z_3_NEG)
        {
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    cubes[x, y, 2].transform.parent = RotationAuxiliar.transform;
                }
            }
        }
    }
Exemplo n.º 26
0
        //public RubikCube CubeB;


        public ConsoleUI()
        {
            this.CubeA = new RubikCube();
            //this.CubeB = new RubikCube(true);
        }