示例#1
0
        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);
        }
示例#2
0
 internal void PutWordAtLocationInDirection(string word, LocationOnGrid location, DirectionInGrid direction)
 {
     foreach (char c in word)
     {
         PutCharAtLocation(c, location);
         location = location.AfterSoManyStepsInDirection(1, direction);
     }
 }
示例#3
0
        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);
        }
示例#4
0
        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));
        }
示例#5
0
        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;
        }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
 public char CharAtLocation(LocationOnGrid location)
 {
     return(PuzzleGrid[location.i, location.j]);
 }
示例#9
0
 public PuzzleSolutionWord(string word, DirectionInGrid direction, LocationOnGrid location)
 {
     Word      = word;
     Direction = direction;
     Location  = location;
 }
示例#10
0
        private bool IsSpaceEnough(DirectionInGrid direction, LocationOnGrid location, string word, Puzzle puzzle)
        {
            var finalLoc = location.AfterSoManyStepsInDirection(word.Length, direction);

            return(finalLoc.IsValidLocationInPuzzle(puzzle, finalLoc));
        }