Beispiel #1
0
        public ProblemAction[] Actions(ProblemState <T> state)
        {
            var(rowNumber, columnNumber) = state.FindEmpty();

            var actions = new List <ProblemAction>();

            if (columnNumber > 0)
            {
                actions.Add(ProblemAction.Left);
            }

            if (columnNumber < state.GetLength(0) - 1)
            {
                actions.Add(ProblemAction.Right);
            }

            if (rowNumber > 0)
            {
                actions.Add(ProblemAction.Up);
            }

            if (rowNumber < state.GetLength(1) - 1)
            {
                actions.Add(ProblemAction.Down);
            }

            return(actions.ToArray());
        }
Beispiel #2
0
        protected override ProblemState <T> FormulateGoal(ProblemState <T> state)
        {
            var values = state.ToValues();

            Array.Sort(values);

            return(values.ToProblemState(state.GetLength(0), state.GetLength(1)));
        }
Beispiel #3
0
        public static bool StateEquals <T>(this ProblemState <T> left, ProblemState <T> right)
        {
            for (int i = 0; i < left.GetLength(0); i++)
            {
                for (int j = 0; j < left.GetLength(1); j++)
                {
                    if (!Equals(left[i, j], right[i, j]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #4
0
        public static ProblemState <T> ToProblemState <T>(this PuzzleBoard <T> perceptedObj)
        {
            var perceptedState = new ProblemState <T>(
                perceptedObj.Pieces.GetLength(0), perceptedObj.Pieces.GetLength(1));

            for (int i = 0; i < perceptedState.GetLength(0); i++)
            {
                for (int j = 0; j < perceptedState.GetLength(1); j++)
                {
                    perceptedState[i, j] = perceptedObj.Pieces[i, j].Value;
                }
            }

            return(perceptedState);
        }
Beispiel #5
0
        public static T[] ToValues <T>(this ProblemState <T> state)
        {
            int rows = state.GetLength(0);

            var values = new T[rows * state.GetLength(1)];

            for (int i = 0; i < state.GetLength(0); i++)
            {
                for (int j = 0; j < state.GetLength(1); j++)
                {
                    values[rows * i + j] = state[i, j];
                }
            }

            return(values);
        }
Beispiel #6
0
        public static PuzzleBoard <T> ToPuzzleBoard <T>(this ProblemState <T> problemState)
        {
            var state = new PuzzleBoard <T>(new PuzzlePiece <T> [problemState.GetLength(0), problemState.GetLength(1)]);

            for (int i = 0; i < problemState.GetLength(0); i++)
            {
                for (int j = 0; j < problemState.GetLength(0); j++)
                {
                    state.Pieces[i, j] = new PuzzlePiece <T> {
                        Value = problemState[i, j]
                    };
                }
            }

            return(state);
        }
Beispiel #7
0
        public static double StraightLineDistance(
            ProblemState <int> stateSrc, ProblemState <int> stateDest)
        {
            double totalDist = 0;

            for (int i = 0; i < stateDest.GetLength(0); i++)
            {
                for (int j = 0; j < stateDest.GetLength(1); j++)
                {
                    totalDist += StraightLineDistance(
                        stateSrc.FindValueIndex(stateDest[i, j]),
                        (i, j));
                }
            }

            return(totalDist);
        }
Beispiel #8
0
        public ProblemState <T> Result(ProblemState <T> state, ProblemAction action)
        {
            var(rowNumber, columnNumber) = state.FindEmpty();

            var transitionState = new ProblemState <T>(state.GetLength(0), state.GetLength(1));

            ProblemState <T> .Copy(state, transitionState, state.Length);

            int row = rowNumber, col = columnNumber;

            if (action == ProblemAction.Up)
            {
                row = rowNumber - 1;
            }

            if (action == ProblemAction.Down)
            {
                row = rowNumber + 1;
            }

            if (action == ProblemAction.Left)
            {
                col = columnNumber - 1;
            }

            if (action == ProblemAction.Right)
            {
                col = columnNumber + 1;
            }

            transitionState[row, col] = state[rowNumber, columnNumber];

            transitionState[rowNumber, columnNumber] = state[row, col];

            return(transitionState);
        }