Exemplo n.º 1
0
        static void Main(string[] args)
        {
            List <int?[]> listInitial = new List <int?[]>()
            {
                new int?[] { 4, 2, 7 },
                new int?[] { null, 8, 6 },
                new int?[] { 3, 5, 1 }
            };


            SizeNPuzzle initial = new SizeNPuzzle(listInitial);

            List <int?[]> listFinal = new List <int?[]>()
            {
                new int?[] { 1, 4, 7 },
                new int?[] { 2, 5, 8 },
                new int?[] { 3, 6, null }
            };

            List <ActionStateInterface> finals = new List <ActionStateInterface>()
            {
                new SizeNPuzzle(listFinal)
            };

            UninformedSearch problem = new UninformedSearch(initial, finals);

            List <CandidateState> result = problem.search();

            foreach (CandidateState state in result)
            {
                Console.WriteLine(state.ToString());
            }

            Console.ReadLine();
        }
Exemplo n.º 2
0
        public ActionStateInterface doAction(string pAction)
        {
            SizeNPuzzle state = (SizeNPuzzle)this.Clone();
            int         i, j = 0;

            for (i = 0; i < this.Matriz.Count; i++)
            {
                j = Array.FindIndex(this.Matriz[i], value => value != null && value != 1);

                if (j != -1)
                {
                    break;
                }
            }

            if (pAction == SizeNPuzzleActions.MOVEDOWN)
            {
                state.Matriz[i][j]     = this.Matriz[i + 1][j];
                state.Matriz[i + 1][j] = null;
            }
            else if (pAction == SizeNPuzzleActions.MOVEUP)
            {
                state.Matriz[i][j]     = this.Matriz[i - 1][j];
                state.Matriz[i - 1][j] = null;
            }
            else if (pAction == SizeNPuzzleActions.MOVELEFT)
            {
                state.Matriz[i][j]     = this.Matriz[i][j - 1];
                state.Matriz[i][j - 1] = null;
            }
            else if (pAction == SizeNPuzzleActions.MOVERIGHT)
            {
                state.Matriz[i][j]     = this.Matriz[i][j + 1];
                state.Matriz[i][j + 1] = null;
            }

            return(state);
        }
Exemplo n.º 3
0
        public override bool Equals(object?obj)
        {
            if (obj is null)
            {
                return(false);
            }
            if (obj.GetType() == this.GetType())
            {
                SizeNPuzzle o = (SizeNPuzzle)obj;
                if (this.Matriz.Count != o.Matriz.Count)
                {
                    return(false);
                }

                for (int i = 0; i < this.Matriz.Count; i++)
                {
                    if (this.Matriz[i].Length != o.Matriz[i].Length)
                    {
                        return(false);
                    }

                    for (int j = 0; j < this.Matriz[i].Length; j++)
                    {
                        if (this.Matriz[i][j] != o.Matriz[i][j])
                        {
                            return(false);
                        }
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }