/// <summary> /// Checks the values for a tile at a specific tileGridPosition. /// If one value doesn't match the value within the tiles name false is returned, /// while one null-value of the tileValueGrid values is allowed. /// </summary> /// <param name="tile">The tile whose values should be checked.</param> /// <param name="tileGridPosition">The tiles position within the tileGrid.</param> /// <returns>True, if values matching (one tileValueGrid value can be null), false if not.</returns> private bool CheckValueGridAtTileGridPosition(TriominoTile tile, Point tileGridPosition) { List<int?> valuesToCheck = this.GetValueGridValuesFromNameGridPositions(tileGridPosition, tile.Orientation.ToArrayTileOrientation()); // if more than two values are null, this triomino tile // isn't added adjacent to another tile (except for the first tile). if (this.NumbTilesOnBoard > 0 && valuesToCheck.Where(v => !v.HasValue).Count() > 1) { return false; } string[] nameParts = tile.GetArrayName().Split('-'); for (int i = 0; i < valuesToCheck.Count; i++) { int intValue = int.Parse(nameParts[i]); if (valuesToCheck[i].HasValue && valuesToCheck[i] != intValue) { return false; } } return true; }
private bool AddTile(TriominoTile tile, Point tileGridPosition) { if (this.tileGrid[tileGridPosition.Y, tileGridPosition.X] != null) { throw new ArgumentException($"Cannot add tile {tile.Name} at position(x,y): ({tileGridPosition.X}, {tileGridPosition.Y}). Place is taken by tile: '{this.tileGrid[tileGridPosition.Y, tileGridPosition.X].Name}'"); } if (!this.CheckValueGridAtTileGridPosition(tile, tileGridPosition)) { return false; } this.tileGrid[tileGridPosition.Y, tileGridPosition.X] = tile; this.AddValuesToValueGrid(tile.GetArrayName(), tileGridPosition, tile.Orientation.ToArrayTileOrientation()); return true; }