public HashSet <Coordinate> GetValidPlayableSpaces() { if (ValidPlayableSpacesCache != null && ValidPlayableSpacesCache.Count > 0) { return(ValidPlayableSpacesCache); } // For every played card, add every neighboring coordinate to a HashSet (no duplicates) HashSet <Coordinate> neighborsOfHappyCoordinates = new HashSet <Coordinate>(GetHappyCoordinates().SelectMany(coordinate => coordinate.GetNeighbors())); HashSet <Coordinate> neighborsOfNotHappyCoordinates = new HashSet <Coordinate>(GetNotHappyCoordinates().SelectMany(coordinate => coordinate.GetNeighbors())); HashSet <Coordinate> finalCut = new HashSet <Coordinate>(); // For each considered Coordinate, if all the following is true, consider that spot valid: // - there are no cards on that spot // - there are no happy cards neighboring that spot foreach (Coordinate consideredCoordinate in neighborsOfNotHappyCoordinates) { if (PlayedCards.ContainsKey(consideredCoordinate)) { continue; } if (neighborsOfHappyCoordinates.Contains(consideredCoordinate)) { continue; } finalCut.Add(consideredCoordinate); } ValidPlayableSpacesCache = finalCut; return(finalCut); }
public bool ShouldCoordinateBeIncompletable(Coordinate forCoordinate) { if (!PlayedCards.ContainsKey(forCoordinate)) { Debug.LogError($"Asked if coordinate {forCoordinate.ToString()} should be incompleteable, but that coordinate isn't in this play field."); return(false); } int requiredNeighbors = PlayedCards[forCoordinate].FaceValue; int occuppiedNeighbors = OccuppiedNeighborsAtCoordinate(forCoordinate); if (requiredNeighbors == occuppiedNeighbors) { return(false); // Presumably already happy } HashSet <Coordinate> validSpaces = GetValidPlayableSpaces(); int playableSpotNeighbors = forCoordinate.GetNeighbors().Count(neighbor => validSpaces.Contains(neighbor)); if (occuppiedNeighbors + playableSpotNeighbors < requiredNeighbors) { return(true); } return(false); }
public void SetCard(CardData forCard, Coordinate onCoordinate) { ValidPlayableSpacesCache = null; NotHappyCoordinatesCache.Add(onCoordinate); HappyCoordinatesAreDirty = true; if (PlayedCards.ContainsKey(onCoordinate)) { PlayedCards[onCoordinate] = forCard; } else { PlayedCards.Add(onCoordinate, forCard); } }
public bool ShouldCoordinateBeHappy(Coordinate forCoordinate) { if (!PlayedCards.ContainsKey(forCoordinate)) { Debug.LogError($"Asked if coordinate {forCoordinate.ToString()} should be happy, but that coordinate isn't in this play field."); return(false); } int neighbors = OccuppiedNeighborsAtCoordinate(forCoordinate); if (PlayedCards[forCoordinate].FaceValue == neighbors) { return(true); } return(false); }
public int OccuppiedNeighborsAtCoordinate(Coordinate forCoordinate) { int answer = forCoordinate.GetNeighbors().Count(neighbor => PlayedCards.ContainsKey(neighbor)); return(answer); }