public HashSet <Action> actions(System.Object state)
            {
                EightPuzzleBoard board = (EightPuzzleBoard)state;

                HashSet <Action> actions = new HashSet <Action>();

                if (board.canMoveGap(EightPuzzleBoard.UP))
                {
                    actions.Add(EightPuzzleBoard.UP);
                }
                if (board.canMoveGap(EightPuzzleBoard.DOWN))
                {
                    actions.Add(EightPuzzleBoard.DOWN);
                }
                if (board.canMoveGap(EightPuzzleBoard.LEFT))
                {
                    actions.Add(EightPuzzleBoard.LEFT);
                }
                if (board.canMoveGap(EightPuzzleBoard.RIGHT))
                {
                    actions.Add(EightPuzzleBoard.RIGHT);
                }

                return(actions);
            }
Ejemplo n.º 2
0
 public BidirectionalEightPuzzleProblem(EightPuzzleBoard initialState) : base(initialState, EightPuzzleFunctionFactory.getActionsFunction(),
                                                                              EightPuzzleFunctionFactory.getResultFunction(),
                                                                              new DefaultGoalTest(new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 })))
 {
     reverseProblem = new Problem(new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }),
                                  EightPuzzleFunctionFactory.getActionsFunction(), EightPuzzleFunctionFactory.getResultFunction(),
                                  new DefaultGoalTest(initialState));
 }
        public BidirectionalEightPuzzleProblem(EightPuzzleBoard initialState)
            : base(initialState, EightPuzzleFunctionFactory.getActionsFunction(),
				EightPuzzleFunctionFactory.getResultFunction(),
				new DefaultGoalTest(new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 })))
        {
            reverseProblem = new Problem(new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }),
                EightPuzzleFunctionFactory.getActionsFunction(), EightPuzzleFunctionFactory.getResultFunction(),
                new DefaultGoalTest(initialState));
        }
Ejemplo n.º 4
0
        public double h(Object state)
        {
            EightPuzzleBoard board = (EightPuzzleBoard)state;
            int retVal             = 0;

            for (int i = 1; i < 9; i++)
            {
                XYLocation loc = board.getLocationOf(i);
                retVal += evaluateManhattanDistanceOf(i, loc);
            }
            return(retVal);
        }
        private int getNumberOfMisplacedTiles(EightPuzzleBoard board)
        {
            int numberOfMisplacedTiles = 0;

            if (!(board.getLocationOf(0).Equals(new XYLocation(0, 0))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(1).Equals(new XYLocation(0, 1))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(2).Equals(new XYLocation(0, 2))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(3).Equals(new XYLocation(1, 0))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(4).Equals(new XYLocation(1, 1))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(5).Equals(new XYLocation(1, 2))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(6).Equals(new XYLocation(2, 0))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(7).Equals(new XYLocation(2, 1))))
            {
                numberOfMisplacedTiles++;
            }
            if (!(board.getLocationOf(8).Equals(new XYLocation(2, 2))))
            {
                numberOfMisplacedTiles++;
            }
            return(numberOfMisplacedTiles);
        }
 private int getNumberOfMisplacedTiles(EightPuzzleBoard board)
 {
     int numberOfMisplacedTiles = 0;
     if (!(board.getLocationOf(0).Equals(new XYLocation(0, 0))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(1).Equals(new XYLocation(0, 1))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(2).Equals(new XYLocation(0, 2))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(3).Equals(new XYLocation(1, 0))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(4).Equals(new XYLocation(1, 1))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(5).Equals(new XYLocation(1, 2))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(6).Equals(new XYLocation(2, 0))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(7).Equals(new XYLocation(2, 1))))
     {
     numberOfMisplacedTiles++;
     }
     if (!(board.getLocationOf(8).Equals(new XYLocation(2, 2))))
     {
     numberOfMisplacedTiles++;
     }
     return numberOfMisplacedTiles;
 }
Ejemplo n.º 7
0
        public bool equals(Object o)
        {
            if (this == o)
            {
                return(true);
            }
            if ((o == null) || (this.GetType() != o.GetType()))
            {
                return(false);
            }
            EightPuzzleBoard aBoard = (EightPuzzleBoard)o;

            for (int i = 0; i < 8; i++)
            {
                if (this.getPositionOf(i) != aBoard.getPositionOf(i))
                {
                    return(false);
                }
            }
            return(true);
        }
            public System.Object result(System.Object s, Action a)
            {
                EightPuzzleBoard board = (EightPuzzleBoard)s;

                if (EightPuzzleBoard.UP.Equals(a) &&
                    board.canMoveGap(EightPuzzleBoard.UP))
                {
                    EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                    newBoard.moveGapUp();
                    return(newBoard);
                }
                else if (EightPuzzleBoard.DOWN.Equals(a) &&
                         board.canMoveGap(EightPuzzleBoard.DOWN))
                {
                    EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                    newBoard.moveGapDown();
                    return(newBoard);
                }
                else if (EightPuzzleBoard.LEFT.Equals(a) &&
                         board.canMoveGap(EightPuzzleBoard.LEFT))
                {
                    EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                    newBoard.moveGapLeft();
                    return(newBoard);
                }
                else if (EightPuzzleBoard.RIGHT.Equals(a) &&
                         board.canMoveGap(EightPuzzleBoard.RIGHT))
                {
                    EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                    newBoard.moveGapRight();
                    return(newBoard);
                }

                // The Action is not understood or is a NoOp
                // the result will be the current state.
                return(s);
            }
Ejemplo n.º 9
0
 public EightPuzzleBoard(EightPuzzleBoard copyBoard) : this(copyBoard.getState())
 {
 }
            public System.Object result(System.Object s, Action a)
            {
                EightPuzzleBoard board = (EightPuzzleBoard) s;

                if (EightPuzzleBoard.UP.Equals(a)
                && board.canMoveGap(EightPuzzleBoard.UP))
                {
                EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                newBoard.moveGapUp();
                return newBoard;
                }
                else if (EightPuzzleBoard.DOWN.Equals(a)
                  && board.canMoveGap(EightPuzzleBoard.DOWN))
                {
                EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                newBoard.moveGapDown();
                return newBoard;
                }
                else if (EightPuzzleBoard.LEFT.Equals(a)
                  && board.canMoveGap(EightPuzzleBoard.LEFT))
                {
                EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                newBoard.moveGapLeft();
                return newBoard;
                }
                else if (EightPuzzleBoard.RIGHT.Equals(a)
                  && board.canMoveGap(EightPuzzleBoard.RIGHT))
                {
                EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
                newBoard.moveGapRight();
                return newBoard;
                }

                // The Action is not understood or is a NoOp
                // the result will be the current state.
                return s;
            }
        public bool isGoalState(Object state)
        {
            EightPuzzleBoard board = (EightPuzzleBoard)state;

            return(board.Equals(goal));
        }
Ejemplo n.º 12
0
 public EightPuzzleBoard(EightPuzzleBoard copyBoard)
     : this(copyBoard.getState())
 {
 }
        public double h(Object state)
        {
            EightPuzzleBoard board = (EightPuzzleBoard)state;

            return(getNumberOfMisplacedTiles(board));
        }