public void addWord(GridWord word) { if (words == null) { words = new List <GridWord>(); } words.Add(word); }
private bool tryToAddWordToGrid(string word) { int randomDirectionIndex = (int)Math.Floor(UnityEngine.Random.Range(0f, Directions.Count - 1)); //randomDirectionIndex = indexTestCount; //indexTestCount++; Vector2 direction = Directions[randomDirectionIndex]; int xMin = direction.x == -1 ? word.Length - 1 : 0; // 1 or word.Length int xMax = direction.x == 1 ? gridWidth - word.Length : gridWidth - 1; //gridWidth or GridWidth - word.Length int yMin = direction.y == -1 ? word.Length - 1 : 0; //1 or word.Length int yMax = direction.y == 1 ? gridHeight - word.Length + 1 : gridHeight - 1; //gridHeight or GridHeight - word.Length GameObject checkLetter; bool wordPlaced = false; int gridX = 0; int gridY = 0; int foundX = 0; int foundY = 0; for (int x = xMin; x <= xMax; x++) { for (int y = yMin; y <= yMax; y++) { if (!wordPlaced) { gridX = x; gridY = y; wordPlaced = true; for (int li = 0; li < word.Length; li++) { if (gridY < GridLetters.Count && gridX < GridLetters[gridY].Count) { checkLetter = GridLetters[gridY][gridX]; GridLetter gridLetter = checkLetter.GetComponent <GridLetter>(); if (gridLetter.isRandomLetter || gridLetter.letter == word[li]) { //wordPlaced = true; } else { wordPlaced = false; } gridX += (int)direction.x; gridY += (int)direction.y; } else { wordPlaced = false; } } if (wordPlaced) { foundX = x; foundY = y; } } } } int wordPlaceX = 0; int wordPlaceY = 0; if (wordPlaced) { wordPlaceX = foundX; wordPlaceY = foundY; GridWord gridWord = new GridWord(word, foundX, foundY, direction); for (int li = 0; li < word.Length; li++) { GridLetter gridLetter = GridLetters[wordPlaceY][wordPlaceX].GetComponent <GridLetter>(); gridLetter.letter = word[li]; gridLetter.displayLetter.GetComponent <Text>().text = word[li].ToString().ToUpper(); gridLetter.isRandomLetter = false; if (li == 0 || li == word.Length - 1) { gridLetter.isEndLetter = true; } gridWord.addLetter(gridLetter); gridLetter.addWord(gridWord); wordPlaceX += (int)direction.x; wordPlaceY += (int)direction.y; } GridWords.Add(gridWord); } return(wordPlaced); }