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

                HashSet <Action> actions = new LinkedHashSet <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);
            }
        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.º 5
0
        public override bool Equals(Object o)
        {
            if (this == o)
            {
                return(true);
            }
            if ((o == null) || (this.getClass() != o.getClass()))
            {
                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 Object result(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 Object result(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.º 8
0
	public EightPuzzleBoard(EightPuzzleBoard copyBoard) {
		this(copyBoard.getState());
	}
Ejemplo n.º 9
0
 public EightPuzzleBoard(EightPuzzleBoard copyBoard)
 {
     this(copyBoard.getState());
 }
        public double h(Object state)
        {
            EightPuzzleBoard board = (EightPuzzleBoard)state;

            return(getNumberOfMisplacedTiles(board));
        }
Ejemplo n.º 11
0
        public bool isGoalState(Object state)
        {
            EightPuzzleBoard board = (EightPuzzleBoard)state;

            return(board.Equals(goal));
        }