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); }
internal void PutWordAtLocationInDirection(string word, LocationOnGrid location, DirectionInGrid direction) { foreach (char c in word) { PutCharAtLocation(c, location); location = location.AfterSoManyStepsInDirection(1, direction); } }
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 LocationOnGrid GetNextLocationInDirection(LocationOnGrid location) { int iLoc = location.i; int jLoc = location.j; int iNext; int jNext; switch (Direction) { case 1: iNext = iLoc; jNext = jLoc++; break; case 2: iNext = iLoc--; jNext = jLoc++; break; case 3: iNext = iLoc--; jNext = jLoc; break; case 4: iNext = iLoc--; jNext = jLoc--; break; case 5: iNext = iLoc; jNext = jLoc--; break; case 6: iNext = iLoc++; jNext = jLoc--; break; case 7: iNext = iLoc++; jNext = jLoc; break; case 8: iNext = iLoc++; jNext = jLoc++; break; default: throw new Exception($"Unknown direction number {Direction}"); } return(new LocationOnGrid(iNext, jNext)); }
public void PutCharAtLocation(char c, LocationOnGrid location) { var existingChar = CharAtLocation(location); if (existingChar == c) { return; } if (existingChar != ' ') { throw new Exception($"Can not add char {c} at location ({location.i},{location.j})"); } PuzzleGrid[location.i, location.j] = c; }
public bool IsValidLocationInPuzzle(Puzzle puzzle, LocationOnGrid location) { int iMax = puzzle.iMax; int jMax = puzzle.jMax; if ((location.i < 0) || (location.j < 0)) { return(false); } if ((location.i > iMax) || (location.j > jMax)) { return(false); } return(true); }
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); }
public char CharAtLocation(LocationOnGrid location) { return(PuzzleGrid[location.i, location.j]); }
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)); }