Example #1
0
    // Undo move
    public void UndoClicked()
    {
        if (moves.Count == 0)
        {
            return;
        }

        audioSource.PlayOneShot(sfx[0]);

        // restore the move
        TMove cell = moves[moves.Count - 1];

        // empties the related cell
        cellsStates[cell.cellIdx] = 2;

        // empties the related button
        cell.button.image.sprite = sprites[2];

        // current player is the player of that move
        currentPlayer = cell.player;

        // undo presence computations,
        // by adding the inverse of the correct value
        UpdatePresence(cell.cellIdx, (currentPlayer == 0 ? -1 : 1));

        // remove the move from history
        moves.RemoveAt(moves.Count - 1);

        // certainly there is no winner yet
        gameEnded    = false;
        logText.text = "Next: player " + (currentPlayer + 1);
        replayButton.SetActive(false);
        SetParticles(false);
    }
Example #2
0
        public void MakeMove(TMove move)
        {
            Select(move);
            var group = new TObject3D();
            var angle = (move.Angle + 1) * 90;

            if (move.Axis == 0)
            {
                group.RotateX(angle);
            }
            else if (move.Axis == 1)
            {
                group.RotateY(angle);
            }
            else
            {
                group.RotateZ(angle);
            }

            for (int i = 0; i < Selection.Count; i++)
            {
                var cubic = Selection[i];
                cubic.MultMatrix(group.Transform);
                Cubiks[cubic.Z, cubic.Y, cubic.X] = cubic;
                cubic.UpdateState();
            }
        }
        private void RemoveGene(int i)
        {
            Array.Copy(Genes, i + 1, Genes, i, GenesLength - (i + 1));
            var move = new TMove();

            move.Decode((int)Genes[GenesLength - 1]);
            move.Axis = (move.Axis + 1) % 3;
            Genes[GenesLength - 1] = move.Encode();
        }
Example #4
0
 private void button1_Click(object sender, EventArgs e)
 {
     for (int i = 0; i < 25; i++)
     {
         var code = Rnd.Next(9 * RubikCube.N);
         var move = new TMove();
         move.Decode(code);
         Moves.Add(move);
     }
     MoveTimer.Start();
 }
Example #5
0
 public void Select(TMove move)
 {
     Selection = new List <TCubik>();
     for (int i = 0; i < N; i++)
     {
         for (int j = 0; j < N; j++)
         {
             var idx = new int [3];
             idx[move.Axis % 3]       = move.SegNo;
             idx[(move.Axis + 1) % 3] = i;
             idx[(move.Axis + 2) % 3] = j;
             var cubik = Cubiks[idx[2], idx[1], idx[0]];
             Selection.Add(cubik);
         }
     }
 }
Example #6
0
        public void LoadSolutions()
        {
            var fs     = new FileStream(SolutionPath, FileMode.OpenOrCreate);
            var reader = new BinaryReader(fs);

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                var key        = reader.ReadString();
                var movesCount = reader.ReadInt32();
                var moves      = new List <TMove>();
                for (int i = 0; i < movesCount; i++)
                {
                    var move = new TMove();
                    move.Decode(reader.ReadInt32());
                    moves.Add(move);
                }
                Solutions.Add(key, moves);
            }
            fs.Close();
        }
Example #7
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);
        }
        public void Correct()
        {
            for (int i = 0; i < GenesLength; i++)
            {
                var move = new TMove();
                move.Decode((int)Genes[i]);

                for (int j = i - 1; j >= 0; j--)
                {
                    var pMove = new TMove();
                    pMove.Decode((int)Genes[j]);

                    if (move.Axis != pMove.Axis)
                    {
                        break;
                    }

                    if (move.SegNo == pMove.SegNo)
                    {
                        RemoveGene(i);
                        i--;
                        var angle = (pMove.Angle + move.Angle + 2) % 4;

                        if (angle == 0)
                        {
                            RemoveGene(j);
                            i--;
                        }
                        else
                        {
                            pMove.Angle = angle - 1;
                            Genes[j]    = pMove.Encode();
                        }

                        break;
                    }
                }
            }
        }
Example #9
0
 /// <summary>
 /// Returns a new Transition with the specified move to perform and a hash representing the resulting gamestate.
 /// </summary>
 /// <param name="move">The move to perform to execute the transition.</param>
 /// <param name="hash">The hash code of the resulting gamestate after performing the move.</param>
 public Transition(TMove move, long hash)
 {
     Move = move;
     Hash = hash;
 }
Example #10
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();
        }
Example #11
0
		public TComputerMoveHandler(TMove move) {
			this.tmpMove = new TMove();
			showMove = move;
			this.move = move;
		}
Example #12
0
		public override void install() {
			base.install();

			engine.go((m) => {
				Console.WriteLine("MOVE: " + m);
				uninstall();

				var move = new TMove(m);
				if (move.isNone) {
					Console.WriteLine("CHECK MATE!");
					app.player.play("mate");
					Thread.Sleep(10000);
					return;
				}
				var handler = new TComputerMoveHandler(move);
				handler.install();
				app.player.play("computermove");
			});
		}
Example #13
0
		public TOwnMoveHandler() {
			tmpMove = new TMove();
			showMove = tmpMove;
		}
Example #14
0
 public Transition(TMove move, long hash)
 {
     this.move = move;
     this.hash = hash;
 }