/// <summary> /// Move the specified tile from its current square to the empty square; /// returns true if the tile moves, false if tile can't be moved. /// </summary> public bool MoveTile(int tileId) { // Can't move the "empty" tile: if (tileId == 0) { return(false); } // Find specified tile: SquareIndex selectedSquare = Tiles[tileId]; // Calculate required movement from current square // to the empty square: MoveVector movement = SquareIndex.Subtract(EmptySquare, selectedSquare); // See if movement is an orthogonal step: foreach (MoveVector orthogonalStep in MoveVector.OrthogonalSteps) { if (movement == orthogonalStep) { ExchangeTiles(selectedSquare, EmptySquare); return(true); } } // Can't move the tile from the specified square // to the empty square in a single orthogonal step: return(false); }
public bool CanMove(Move move) { bool fCanMove = false; SquareIndex selectedSquare = this.EmptySquare.Plus(MoveVector.OrthogonalSteps[(int)move]); int tileId = this[selectedSquare]; if (SquareIsOnTheBoard(selectedSquare)) { // Can't move the "empty" tile: if (tileId != 0) { // Find specified tile: SquareIndex currentSquare = Tiles[tileId]; // Calculate required movement from current square // to the empty square: MoveVector movement = SquareIndex.Subtract(EmptySquare, currentSquare); // See if movement is an orthogonal step: foreach (MoveVector orthogonalStep in MoveVector.OrthogonalSteps) { if (movement == orthogonalStep) { fCanMove = true; break; } } } } return(fCanMove); }