public void FindMatches() { matchFound = false; for (int j = 0; j < grid.gridSize.y; j++) { for (int i = 0; i < grid.gridSize.x; i++) { var cont = grid.tokens[j, i].GetComponent <SwapPuzzleTokenController>(); if (!cont.isMatched) { cont.isChecking = true; TokenChain chain = FindMatches(cont, true); if (chain.IsMatchFound()) { matchFound = true; tokenChains.Add(chain); } matchFound = chain.IsMatchFound() || matchFound; } } } foreach (MaskToken tok in grid.tokens) { tok.GetComponent <SwapPuzzleTokenController>().isChecking = false; } }
public void Add(TokenChain other) { foreach (SwapPuzzleTokenController token in other.tokens) { tokens.Add(token); } }
public Scanner(TokenChain tokenchain) //上级编译器必须将token链传入 { //TokenChain tokenchain = new TokenChain(); this.tokenchain = tokenchain; file = new ScannerFile(); state = ScannerState.Failed_To_Open_File; //此时并没有打开文件 }
public TokenChain FindMatches(SwapPuzzleTokenController cont, bool mustAddToChain = false) { TokenChain chain = new TokenChain(); bool isAdded = !mustAddToChain; Vector2Int[] directions = new Vector2Int[] { Vector2Int.right, Vector2Int.up, Vector2Int.left, Vector2Int.down }; foreach (Vector2Int dir in directions) { Vector2Int nearPos = cont.token.gridPosition + dir; if (grid.IsInsideGrid(nearPos)) { var near = grid.tokens[nearPos.y, nearPos.x].GetComponent <SwapPuzzleTokenController>(); // check nearest token if (!near.isChecking && near.token.elementId == cont.token.elementId) { Vector2Int furtherPos = nearPos + dir; Vector2Int backPos = cont.token.gridPosition - dir; if (grid.IsInsideGrid(furtherPos)) { var further = grid.tokens[furtherPos.y, furtherPos.x].GetComponent <SwapPuzzleTokenController>(); if (further.token.elementId == cont.token.elementId) { if (!isAdded) { isAdded = true; chain.Add(cont); } cont.isMatched = true; near.isMatched = true; further.isMatched = true; bool checkNear = false, checkFurther = false; if (!near.isChecking) { chain.Add(near); near.isChecking = true; checkNear = true; } if (!further.isChecking) { chain.Add(further); further.isChecking = true; checkFurther = true; } if (checkNear) { chain.Add(FindMatches(near)); } if (checkFurther) { chain.Add(FindMatches(further)); } } } if (grid.IsInsideGrid(backPos)) { var back = grid.tokens[backPos.y, backPos.x].GetComponent <SwapPuzzleTokenController>(); if (back.token.elementId == cont.token.elementId) { if (!isAdded) { isAdded = true; chain.Add(cont); } cont.isMatched = true; near.isMatched = true; back.isMatched = true; bool checkNear = false, checkBack = false; if (!near.isChecking) { chain.Add(near); near.isChecking = true; checkNear = true; } if (!back.isChecking) { chain.Add(back); back.isChecking = true; checkBack = true; } if (checkNear) { chain.Add(FindMatches(near)); } if (checkBack) { chain.Add(FindMatches(back)); } } } } } } return(chain); }