コード例 #1
0
        /// <summary>
        /// Apply a move and update the map with the new state.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public void ApplyMove(Coordinates source, Coordinates destination)
        {
            Tile sourceTile      = GetTileByCoordinates(source);
            Tile destinationTile = GetTileByCoordinates(destination);

            switch (destinationTile.Content)
            {
            case TileContent.Cheese:
                Cheese.RemoveAll(tile => tile.Position.Equals(destination));     // Removes the cheese eaten
                break;

            case TileContent.Rat:
                Rats.RemoveAll(tile => tile.Position.Equals(destination));     // Removes the rat captured
                break;
            }

            switch (sourceTile.Content)
            {
            case TileContent.Cat:
                Cats[Cats.FindIndex(tile => tile.Equals(sourceTile))] = destinationTile;     // Change the cat position
                break;

            case TileContent.Rat:
                Rats[Rats.FindIndex(tile => tile.Position.Equals(sourceTile.Position))] = destinationTile;     // Change the rat position
                break;
            }

            // Move the source's content to the destination's content
            destinationTile.Content = sourceTile.Content;
            sourceTile.Content      = TileContent.Empty;

            if (Exits.Contains(destinationTile) && destinationTile.Content == TileContent.Rat)
            {
                // A rat reached the exit
                Rats.RemoveAll(tile => tile.Position.Equals(destination)); // Removes the escaped rat
                destinationTile.Content = TileContent.Empty;               // Empty the tile
            }
        }