private bool TryFillingWord(DirectionInGrid direction, LocationOnGrid location, string word, Puzzle notYetCompletelyFilledPuzzle) { var targetSpaceInGrid = notYetCompletelyFilledPuzzle.GetWordOfLengthInDirectionAtLocation(word.Length, direction, location); var okToPutWord = true; for (int i = 0; i < word.Length; i++) { var cWord = word[i]; var cTarget = targetSpaceInGrid[i]; if ((cTarget == ' ') || (cTarget == cWord)) { //okToPutWord remains true; } else { okToPutWord = false; break; } } if (!okToPutWord) { return(false); } notYetCompletelyFilledPuzzle.PutWordAtLocationInDirection(word, location, direction); return(true); }
public Puzzle GeneratePuzzleKey(Puzzle notYetCompletelyFilledPuzzle) { int iMax = notYetCompletelyFilledPuzzle.iMax; int jMax = notYetCompletelyFilledPuzzle.jMax; var wordList = notYetCompletelyFilledPuzzle.WordsInThePuzzleDesired; var wordsSuccessfullyFilled = new List <string>(); int iMaxTrialForSameWord = 10000; foreach (var word in wordList) { int iTrialForSameWord = 0; bool wordNotFilled = true; while ((iTrialForSameWord < iMaxTrialForSameWord) && wordNotFilled) { iTrialForSameWord++; //Pick a random location var location = HelpWithRandomPick.GetRandomLocation(iMax, jMax); //Pick a random direction DirectionInGrid direction = new DirectionInGrid(); var dirCounter = 0; direction.SetToRandomDirection(); while ((dirCounter < 8) && wordNotFilled) { if (IsSpaceEnough(direction, location, word, notYetCompletelyFilledPuzzle)) { if (TryFillingWord(direction, location, word, notYetCompletelyFilledPuzzle)) { wordNotFilled = false; notYetCompletelyFilledPuzzle.WordsInThePuzzleFilled.Add(word); var puzzleSolutionWord = new PuzzleSolutionWord(word, direction, location); notYetCompletelyFilledPuzzle.PuzzleSolutionSet.Add(word, puzzleSolutionWord); continue; //done with direction switching } else { direction.SetToNextDirection(); dirCounter++; } } else { direction.SetToNextDirection(); dirCounter++; } } //Keep trying for same word } } return(notYetCompletelyFilledPuzzle); }
public LocationOnGrid AfterSoManyStepsInDirection(int nSteps, DirectionInGrid direction) { int iLoc = i; int jLoc = j; int iNext = 0; int jNext = 0; switch (direction.Direction) { case 1: iNext = iLoc; jNext = jLoc + nSteps; break; case 2: iNext = iLoc - nSteps; jNext = jLoc + nSteps; break; case 3: iNext = iLoc - nSteps; jNext = jLoc; break; case 4: iNext = iLoc - nSteps; jNext = jLoc - nSteps; break; case 5: iNext = iLoc; jNext = jLoc - nSteps; break; case 6: iNext = iLoc + nSteps; jNext = jLoc - nSteps; break; case 7: iNext = iLoc + nSteps; jNext = jLoc; break; case 8: iNext = iLoc + nSteps; jNext = jLoc + nSteps; break; default: throw new Exception($"Unknown direction number {direction}"); } var newLocation = new LocationOnGrid(iNext, jNext); return(newLocation); }
public string GetWordOfLengthInDirectionAtLocation(int wordLength, DirectionInGrid direction, LocationOnGrid location) { string wordOut = ""; for (int i = 0; i < wordLength; i++) { var c = CharAtLocation(location); wordOut += c.ToString(); location = location.AfterSoManyStepsInDirection(1, direction); } return(wordOut); }
internal void PutWordAtLocationInDirection(string word, LocationOnGrid location, DirectionInGrid direction) { foreach (char c in word) { PutCharAtLocation(c, location); location = location.AfterSoManyStepsInDirection(1, direction); } }
public PuzzleSolutionWord(string word, DirectionInGrid direction, LocationOnGrid location) { Word = word; Direction = direction; Location = location; }
private bool IsSpaceEnough(DirectionInGrid direction, LocationOnGrid location, string word, Puzzle puzzle) { var finalLoc = location.AfterSoManyStepsInDirection(word.Length, direction); return(finalLoc.IsValidLocationInPuzzle(puzzle, finalLoc)); }