Exemple #1
0
        private int GetNumberOfMisplacedTiles(EightPuzzleBoard board)
        {
            var 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);
        }
        public bool Equals(EightPuzzleBoard other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            for (var i = 0; i < 8; i++)
            {
                if (this.GetPositionOf(i) != other.GetPositionOf(i))
                {
                    return(false);
                }
            }

            return(true);
        }
            public ISet<IAction> Actions(object state) 
            {
                EightPuzzleBoard board = (EightPuzzleBoard) state;

                var actions = new HashedSet<IAction>();

                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 object Result(object s, IAction 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 EightPuzzleBoard(EightPuzzleBoard copyBoard) : this(copyBoard.State)
 {
 }