private void CheckNeighborPossibleMove(Direction direction) { GemController neighbor = GetNeighbor(direction); if ((needToCheckNeighborPossibleMove & direction) == direction && neighbor != null && neighbor.IsActive) { bool havesamepossiblematch = false; foreach (PossibleMatch pm in neighbor.PossibleMatches) { if (PossibleMatches.Contains(pm)) { havesamepossiblematch = true; } } if (havesamepossiblematch) { neighbor.GetNeighbor(direction)?.CheckForPossibleMove(); } else { neighbor.CheckForPossibleMove(); } } needToCheckNeighborPossibleMove &= (~direction); }
public static Fixture GenerateFixture() { Fixture fixture = new Fixture(); PossibleMatches possibleMatches = new PossibleMatches(); Round firstRound = RoundGenerator.GenerateRound(); fixture.Rounds.Add(firstRound); PrintRound(fixture, firstRound); foreach (Match m in firstRound.Matches) { possibleMatches.Matches.Remove(m); } int attemptsToGenerateRound = 0; while (fixture.Rounds.Count < MAX_ROUNDS) { Round possibleRound = RoundGenerator.GenerateRound(); if (!fixture.ContainsRound(possibleRound)) { attemptsToGenerateRound = 0; fixture.Rounds.Add(possibleRound); PrintRound(fixture, possibleRound); foreach (Match m in possibleRound.Matches) { possibleMatches.Matches.Remove(m); } } else { attemptsToGenerateRound++; if (attemptsToGenerateRound > 1000) { Round remainingRound = RoundGenerator.GenerateRoundFromList(possibleMatches.Matches); fixture.Rounds.Add(remainingRound); PrintRound(fixture, remainingRound, true); attemptsToGenerateRound = 0; } } } RoundGenerator.CleanList(); if (!fixture.IsValid()) { PrintInvalidFixtureMessage(); } return(fixture); }
public void CheckMatches() { List <List <GridObject> > matches = Matches(); if (matches.Count() > 0) { audio.PlayOneShot(matchSound); Debug.Log("checking matches"); // Add the scores foreach (List <GridObject> matchSequence in matches) { score.AddForMatch(matchSequence); } // Then destroy the objects foreach (List <GridObject> matchSequence in matches) { foreach (GridObject gridObject in matchSequence) { grid.RemoveObject(gridObject); } } grid.OnGridObjectsDestroyed(); } else if (!PossibleMatches().HasMatches()) { Debug.Log("no matches"); audio.PlayOneShot(noMovesSound); grid.ResetBoard(); } else { score.matchesCombo = 0; } cachedMatches = null; CachedPossibleMatches = null; score.EndSequence(); //PossibleMatches (); }
public PossibleMatches PossibleMatches() { if (CachedPossibleMatches == null) { CachedPossibleMatches = new PossibleMatches(); foreach (List <Vector3> row in grid.Successions()) { bool isAtEndOfMatch = false; PossibleMatch currentMatch = new PossibleMatch(this); PossibleMatch nextMatch = null; foreach (Vector3 point in row) { GridObject currentObject = grid.ObjectAt(point); if (nextMatch == null || !nextMatch.MatchObjects.Contains(currentObject)) { bool currentMatchFromNextMatch = false; if (nextMatch != null) { if (nextMatch.MatchObjects.Count > 0) { Debug.Log("setting current match from next match. next match start " + nextMatch.MatchObjects.First().Position() + " next match end: " + nextMatch.MatchObjects.Last().Position() + " current object: " + currentObject.Position()); } else { Debug.Log("EMPTY: setting current match from next match EMPTY"); } currentMatch = nextMatch; nextMatch = null; currentMatchFromNextMatch = true; } if (currentMatch.Matches(currentObject)) { currentMatch.AddMatch(currentObject); } else { isAtEndOfMatch = true; } int trailingIndex = row.IndexOf(point); if (row.Count() == trailingIndex) { isAtEndOfMatch = true; } // split if (isAtEndOfMatch) { bool foundSplit = false; if (trailingIndex + 1 < row.Count()) { GridObject nextNextObj = grid.ObjectAt(row.ElementAt(trailingIndex + 1)); if (currentMatch.Matches(nextNextObj)) { PossibleMatch splitMatch = currentMatch.Duplicate(); splitMatch.Switch.Inhibitor = currentObject; splitMatch.AddMatch(nextNextObj); List <GridObject> perpMatches = new List <GridObject> (); foreach (GridObject matchObj in grid.AdjacentGridObjects(currentObject)) { if (splitMatch.Matches(matchObj)) { perpMatches.Add(matchObj); } } // if we have a perpendicular match, see if there's enough for a split if (perpMatches.Count() > 0) { int sequenceIndex = trailingIndex; bool isAtEnd = false; nextMatch = new PossibleMatch(this); nextMatch.AddMatch(nextNextObj); while (!isAtEnd) { sequenceIndex = sequenceIndex + 1; if (sequenceIndex < row.Count()) { nextNextObj = grid.ObjectAt(row.ElementAt(sequenceIndex)); if (splitMatch.Matches(nextNextObj)) { splitMatch.AddMatch(nextNextObj); nextMatch.AddMatch(nextNextObj); } else { isAtEnd = true; } } else { isAtEnd = true; } } if (splitMatch.HasPotentialPossibleMatch()) { foundSplit = true; foreach (GridObject matchObj in perpMatches) { PossibleMatch perpMatch = splitMatch.Duplicate(); perpMatch.Switch.Trigger = matchObj; CachedPossibleMatches.Add(perpMatch); } } } } } if (currentMatch.HasPotentialPossibleMatch()) { // leading if (!currentMatchFromNextMatch) { int leadingIndex = row.FindIndex(pt => pt == currentMatch.MatchObjects.First().Position()) - 1; if (leadingIndex >= 0) { GridObject leadingObj = grid.ObjectAt(row.ElementAt(leadingIndex)); foreach (GridObject matchObj in grid.AdjacentGridObjects(leadingObj)) { if (currentMatch.Matches(matchObj)) { PossibleMatch leadingMatch = currentMatch.Duplicate(); leadingMatch.Switch.Inhibitor = leadingObj; leadingMatch.Switch.Trigger = matchObj; CachedPossibleMatches.Add(leadingMatch); } } } } // trailing if (!foundSplit) { foreach (GridObject matchObj in grid.AdjacentGridObjects(currentObject)) { if (currentMatch.Matches(matchObj)) { PossibleMatch trailingMatch = currentMatch.Duplicate(); trailingMatch.Switch.Inhibitor = currentObject; trailingMatch.Switch.Trigger = matchObj; CachedPossibleMatches.Add(trailingMatch); } } } } isAtEndOfMatch = false; currentMatch = new PossibleMatch(this); currentMatch.AddMatch(currentObject); } } } // have to check if the final match is big enough } } return(CachedPossibleMatches); }