/// <summary> /// PlaceWordInGrid /// Places the given word in the grid /// </summary> /// <param name="grid"></param> /// <param name="hiddenWord"></param> /// <returns></returns> public bool PlaceWordInGrid(char[,] grid, HiddenWord hiddenWord) { var placed = false; if (placed = PlacementValidator.IsWordPlacementValid(grid, hiddenWord)) { var gridLocation = new GridLocation(hiddenWord.Start); foreach (var letter in hiddenWord.Word) { grid[gridLocation.Row - 1, gridLocation.Column - 1] = letter; gridLocation = GridLocation.GetNextLetterGridLocation(gridLocation, hiddenWord.Direction); } // return true; } return(placed); }
/// <summary> /// GetWordPlacement /// Attempts to find a place in the grid for the given word /// </summary> /// <param name="grid"></param> /// <param name="word"></param> /// <returns></returns> public HiddenWord GetWordPlacement(char[,] grid, string word) { var startLocationFound = false; var entireGridSearched = false; var initialGridLocation = new GridLocation(_randomNumberService, grid); var gridLocation = new GridLocation(initialGridLocation); HiddenWord hiddenWord = null; // Randomize direction list Random rnd = new Random(); var directions = _directions.OrderBy(x => rnd.Next()).ToArray(); // move right and down, checking if word can fit into any direction. if so add it do { // check word can be placed in any of the directions foreach (var direction in directions) { hiddenWord = new HiddenWord() { Direction = direction, Start = gridLocation, Word = word }; if (startLocationFound = PlacementValidator.IsWordPlacementValid(grid, hiddenWord)) { break; } } if (!startLocationFound) { // Move to next position gridLocation = grid.GetNextGridLocation(gridLocation); entireGridSearched = gridLocation.Equals(initialGridLocation); } }while (!startLocationFound && !entireGridSearched); return(startLocationFound ? hiddenWord : null); }