private void SearchFiguresOnCastle(ITile currentTile, CardinalDirection whereToGo) { CheckedTiles.Clear(); _starterWhereToGo = whereToGo; _firstTile = currentTile; CheckedTiles.Add(currentTile); if (IsEndOfCastle(currentTile)) { if (currentTile.GetTileSideByCardinalDirection(whereToGo).Landscape == Landscape.Castle) { CheckFigureOnTile(currentTile, (int)whereToGo); CalculateWithEndOfCastle(currentTile, whereToGo); } } else { for (int i = 0; i < 4; i++) { if (currentTile.Directions[i].Landscape == Landscape.Castle) { _foundEndofRoad.Clear(); _firstTile = GetEndOfCastle(currentTile, (CardinalDirection)i); if (_firstTile != null) { CheckedTiles.Clear(); CheckedTiles.Add(_firstTile); CheckFigureOnTile(_firstTile, (int)_whereToGoAfterEndFound); CalculateWithEndOfCastle(_firstTile, _whereToGoAfterEndFound); } break; } } } }
private void CalculateWithEndOfCastle(ITile currentTile, CardinalDirection whereToGo) { var neighborTile = TileUtils.GetNeighborTile(TileUtils.GetSurroundingTiles(currentTile), whereToGo); // Check if the castle is not finished if (neighborTile == null) { _figuresOnTiles.Clear(); return; } if (IsChecked(neighborTile, CheckedTiles) && !neighborTile.Position.Equals(_firstTile.Position)) { return; } CheckedTiles.Add(neighborTile); if (neighborTile.Position.Equals(_firstTile.Position)) { CheckFigureOnTile(neighborTile, (int)TileUtils.GetOppositeDirection(whereToGo)); return; } // If it is an EndOfCastle tile and it isn't the first tile... if (IsEndOfCastle(neighborTile) && _firstTile != neighborTile) { CheckFigureOnTile(neighborTile, (int)TileUtils.GetOppositeDirection(whereToGo)); return; } for (int i = 0; i < 4; i++) { if (neighborTile.Directions[i].Landscape == Landscape.Castle) { CheckFigureOnTile(neighborTile, i); } } for (int i = 0; i < 4; i++) { if (neighborTile.Directions[i].Landscape == Landscape.Castle && i != (int)TileUtils.GetOppositeDirection(whereToGo)) { CalculateWithEndOfCastle(neighborTile, (CardinalDirection)i); } } }
private ITile GetEndOfCastle(ITile currentTile, CardinalDirection whereToGo) { ITile neighborTile = TileUtils.GetNeighborTile(TileUtils.GetSurroundingTiles(currentTile), whereToGo); if (neighborTile == null || IsEndOfCastle(neighborTile) || _firstTile.Position.Equals(neighborTile.Position) || CheckedTiles.Contains(neighborTile)) { _whereToGoAfterEndFound = TileUtils.GetOppositeDirection(whereToGo); return(neighborTile); } CheckedTiles.Add(neighborTile); for (var i = 0; i < 4; i++) { if (neighborTile.Directions[i].Landscape == Landscape.Castle && i != (int)TileUtils.GetOppositeDirection(whereToGo)) { return(GetEndOfCastle(neighborTile, neighborTile.GetCardinalDirectionByIndex(i))); } } return(null); }
private bool CanPlaceFigureOnCastle(ITile currentTile, CardinalDirection whereToGo, bool firstCall) { ITile neighborTile = TileUtils.GetNeighborTile(TileUtils.GetSurroundingTiles(currentTile), whereToGo); if (firstCall) { _currentITile = currentTile; CheckedTiles.Clear(); _figuresOnTiles.Clear(); _canPlaceFigure = true; } if (_currentITile == null) { _canPlaceFigure = false; } if (IsChecked(_currentITile, CheckedTiles)) { return(_canPlaceFigure); } if (firstCall) { if (IsEndOfCastle(currentTile)) { return(CanPlaceFigureOnCastle(currentTile, whereToGo, false)); } else { List <bool> results = new List <bool>(); for (int i = 0; i < 4; i++) { if (currentTile.Directions[i].Landscape == Landscape.Castle && i != (int)whereToGo) { results.Add(CanPlaceFigureOnCastle(currentTile, (CardinalDirection)i, false)); } } return(results.All(e => e)); } } CheckedTiles.Add(currentTile); CheckFigureOnTile(currentTile, (int)whereToGo); if (neighborTile == null) { return(_canPlaceFigure); } if (IsEndOfCastle(neighborTile)) { CheckFigureOnTile(neighborTile, (int)TileUtils.GetOppositeDirection(whereToGo)); return(neighborTile.Directions[(int)TileUtils.GetOppositeDirection(whereToGo)].Figure == null && _canPlaceFigure); } for (int i = 0; i < 4; i++) { if (neighborTile.Directions[i].Figure != null && neighborTile.Directions[i].Landscape == Landscape.Castle) { CheckFigureOnTile(currentTile, i); _canPlaceFigure = false; } } var resultOfPlacementChecking = new List <bool>(); for (var i = 0; i < 4; i++) { if (neighborTile.Directions[i].Landscape == Landscape.Castle && i != (int)TileUtils.GetOppositeDirection(whereToGo)) { CheckFigureOnTile(neighborTile, i); resultOfPlacementChecking.Add(CanPlaceFigureOnCastle(neighborTile, neighborTile.GetCardinalDirectionByIndex(i), false)); } } return(resultOfPlacementChecking.All(e => e)); }