void PopualteGrid() { _CrosswordSpaceGrid = new CrosswordSpace[_GridAxisCount, _GridAxisCount]; float spaceSize = _GridLayout.GetComponent <RectTransform>().sizeDelta.x / (float)_GridAxisCount; _GridLayout.cellSize = new Vector2(spaceSize, spaceSize); for (int i = 0; i < _GridAxisCount; i++) { for (int j = 0; j < _GridAxisCount; j++) { CrosswordSpace space = Instantiate(_CrosswordSpace) as CrosswordSpace; space.name = "Space " + j + " - " + i; space.transform.SetParent(_GridLayout.transform); space.Init(spaceSize); _CrosswordSpaceGrid[j, i] = space; } } }
IEnumerator SearchForPlacementRoutine(WordQuestionPair word) { bool wordPlaced = false; CrosswordSpace currentSpace = _CrosswordSpaceGrid[0, 0]; #region Search along previouis words //_PlacedWords = _PlacedWords.OrderByDescending(n => n._Word.Length).ToList(); for (int i = 0; i < _PlacedWords.Count; i++) { WordQuestionPair placedWord = _PlacedWords[i]; for (int j = 0; j < placedWord._Word.Length; j++) { if (placedWord._Alignment == Alignment.Across) { // Search for placements across currentSpace = _CrosswordSpaceGrid[placedWord._XPos + j, placedWord._YPos]; currentSpace.Highlight(true); wordPlaced = TryPlaceWordDown(word, placedWord._XPos + j, placedWord._YPos); yield return(new WaitForSeconds(_RoutineWaitTime)); currentSpace.Highlight(false); } else { // Search for placements down currentSpace = _CrosswordSpaceGrid[placedWord._XPos, placedWord._YPos + j]; currentSpace.Highlight(true); wordPlaced = TryPlaceWordAcross(word, placedWord._XPos, placedWord._YPos + j); yield return(new WaitForSeconds(_RoutineWaitTime)); currentSpace.Highlight(false); } if (wordPlaced) { break; } } if (wordPlaced) { break; } } #endregion /* #region Search left/top edges of previouis words * * //_PlacedWords = _PlacedWords.OrderByDescending(n => n._Word.Length).ToList(); * * for (int i = 0; i < _PlacedWords.Count; i++) * { * WordQuestionPair placedWord = _PlacedWords[i]; * for (int j = 0; j < placedWord._Word.Length; j++) * { * if (placedWord._Alignment == Alignment.Across && placedWord._YPos > 0) * { * // Search for placements across * currentSpace = _CrosswordSpaceGrid[placedWord._XPos + j, placedWord._YPos-1]; * currentSpace.Highlight(true); * * wordPlaced = TryPlaceWordDown(word, placedWord._XPos + j, placedWord._YPos-1); * * yield return new WaitForSeconds(_RoutineWaitTime); * currentSpace.Highlight(false); * if (wordPlaced) break; * } * else if (placedWord._Alignment == Alignment.Down && placedWord._XPos > 0) * { * // Search for placements downward * currentSpace = _CrosswordSpaceGrid[placedWord._XPos - 1, placedWord._YPos + j]; * currentSpace.Highlight(true); * * wordPlaced = TryPlaceWordAcross(word, placedWord._XPos - 1, placedWord._YPos + j); * * yield return new WaitForSeconds(_RoutineWaitTime); * currentSpace.Highlight(false); * } * * if (wordPlaced) break; * } * * if (wordPlaced) break; * } * #endregion * #region Search right/bottom edges of previouis words * * for (int i = 0; i < _PlacedWords.Count; i++) * { * WordQuestionPair placedWord = _PlacedWords[i]; * for (int j = 0; j < placedWord._Word.Length; j++) * { * if (placedWord._Alignment == Alignment.Across && placedWord._YPos < _GridAxisCount - 2) * { * // Search for placements across * currentSpace = _CrosswordSpaceGrid[placedWord._XPos + j, placedWord._YPos + 1]; * currentSpace.Highlight(true); * * wordPlaced = TryPlaceWordDown(word, placedWord._XPos + j, placedWord._YPos + 1); * * yield return new WaitForSeconds(_RoutineWaitTime); * currentSpace.Highlight(false); * if (wordPlaced) break; * } * else if(placedWord._Alignment == Alignment.Down && placedWord._XPos < _GridAxisCount - 2) * { * // Search for placements downward * currentSpace = _CrosswordSpaceGrid[placedWord._XPos + 1, placedWord._YPos + j]; * currentSpace.Highlight(true); * * wordPlaced = TryPlaceWordAcross(word, placedWord._XPos + 1, placedWord._YPos + j); * * yield return new WaitForSeconds(_RoutineWaitTime); * currentSpace.Highlight(false); * } * * if (wordPlaced) break; * } * * if (wordPlaced) break; * } * #endregion */ int randXOffset = Random.Range(0, _GridAxisCount); int randYOffset = Random.Range(0, _GridAxisCount); if (!wordPlaced) { // Search within the space that can fit the word for (int y = 0; y < _GridAxisCount; y++) { for (int x = 0; x < _GridAxisCount; x++) { int xOffset = x + randXOffset; if (xOffset >= _GridAxisCount) { xOffset -= _GridAxisCount; } int yOffset = y + randYOffset; if (yOffset >= _GridAxisCount) { yOffset -= _GridAxisCount; } // Highlight current space we are searching currentSpace = _CrosswordSpaceGrid[xOffset, yOffset]; currentSpace.Highlight(true); wordPlaced = TryPlaceWordAcross(word, xOffset, yOffset); if (!wordPlaced) { wordPlaced = TryPlaceWordDown(word, x, y); } yield return(new WaitForSeconds(_RoutineWaitTime)); currentSpace.Highlight(false); // If the word is placed then return and stop searching if (wordPlaced) { break; } } currentSpace.Highlight(false); // If the word is placed then return and stop searching if (wordPlaced) { break; } } } }
IEnumerator TryPlaceRandom(WordQuestionPair word) { print("Trying to place random: " + word._Word); bool wordPlaced = false; CrosswordSpace currentSpace = _CrosswordSpaceGrid[0, 0]; int randXOffset = Random.Range(0, _GridAxisCount); int randYOffset = Random.Range(0, _GridAxisCount); if (!wordPlaced) { // Search within the space that can fit the word for (int y = 0; y < _GridAxisCount; y++) { for (int x = 0; x < _GridAxisCount; x++) { int xOffset = x + randXOffset; if (xOffset >= _GridAxisCount) { xOffset -= _GridAxisCount; } int yOffset = y + randYOffset; if (yOffset >= _GridAxisCount) { yOffset -= _GridAxisCount; } // Highlight current space we are searching currentSpace = _CrosswordSpaceGrid[xOffset, yOffset]; currentSpace.Highlight(true); wordPlaced = TryPlaceWordAcross(word, xOffset, yOffset); if (!wordPlaced) { wordPlaced = TryPlaceWordDown(word, x, y); } yield return(new WaitForSeconds(_RoutineWaitTime)); currentSpace.Highlight(false); // If the word is placed then return and stop searching if (wordPlaced) { break; } } currentSpace.Highlight(false); // If the word is placed then return and stop searching if (wordPlaced) { break; } } } }