|
#region File and License Information
/*
<File>
<Copyright>Copyright � 2007, Daniel Vaughan. All rights reserved.</Copyright>
<License see="prj:///Documentation/License.txt"/>
<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
<CreationDate>2008-06-09 11:54:02Z</CreationDate>
<LastSubmissionDate>$Date: $</LastSubmissionDate>
<Version>$Revision: $</Version>
</File>
*/
#endregion
namespace DanielVaughan.Sokoban
{
public partial class Actor
{
/// <summary>
/// Tries to move in the direction of the specified move.
/// </summary>
/// <param name="move">The move indicating where to go.</param>
/// <returns><code>true</code> if the move completed
/// successfully, <code>false</code> otherwise.</returns>
internal bool DoMove(Move move)
{
lock (moveLock)
{
return DoMoveAux(move);
}
}
internal bool DoMoveAux(Move move)
{
bool result = false;
Location moveLocation = Location.GetAdjacentLocation(move.Direction);
if (Level.InBounds(moveLocation))
{
Cell toCell = Level[moveLocation];
Cell fromCell = Level[Location];
CellContents toCellContents = toCell.CellContents;
if (toCell.CanEnter)
{ /* Empty cell. */
if (!move.Undo)
{ /* Regular move - nominal case. */
result = toCell.TrySetContents(this);
if (result)
{
Move newMove = new Move(move.Direction.GetOppositeDirection()) { Undo = true };
moves.Push(newMove);
MoveCount++;
}
}
else if (move.PushedContents != null)
{ /* Is an undo and there was contents. */
toCell.TrySetContents(this);
result = fromCell.TrySetContents(move.PushedContents);
if (result)
{
MoveCount--;
}
}
else
{ /* Is an undo and there wasn't contents. */
result = toCell.TrySetContents(this);
if (result)
{
MoveCount--;
}
}
}
else if (toCell.TryPushContents(move.Direction))
{ /* Wasn't able to enter, but could push contents. */
if (!move.Undo)
{
Move newMove = new Move(move.Direction.GetOppositeDirection()) { Undo = true, PushedContents = toCellContents };
moves.Push(newMove);
}
result = toCell.TrySetContents(this);
if (result)
{
MoveCount++;
}
}
}
return result;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.
Previously Daniel was a nine-time Microsoft MVP and co-founder of
Outcoder, a Swiss software and consulting company.
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
Daniel is the developer behind several acclaimed mobile apps including
Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably
Codon.
Would you like Daniel to bring value to your organisation?
Please contact
Blog |
Twitter
Xamarin Experts
Windows 10 Experts