Пример #1
0
        /// <summary>
        /// Finds an sets a vaild location for the word to be placed
        /// </summary>
        /// <param name="xRange"></param>
        /// <param name="yRange"></param>
        /// <param name="word"></param>
        /// <param name="grid"></param>
        /// <returns></returns>
        protected PlacedWord FindWordLocation(IEnumerable<int> xRange, IEnumerable<int> yRange, string word, WordSearch grid)
        {
            xRange = xRange.Shuffle();
            yRange = yRange.Shuffle();

            var placeFound=false;
            GridCell startPos = new GridCell();

            //Find a Valid location
            foreach (var currentX in xRange) {
                foreach (var currentY in yRange)
                {
                    placeFound = this.TryPlaceWordLetters(word, new GridCell(currentX, currentY), grid);
                    if (placeFound)
                    {
                        startPos = new GridCell(currentX,currentY);
                        break;
                    }
                }
                if(placeFound) break;
            }

            //Set Word
            if(placeFound)
            {
                for (var i = 0; i < word.Length; i++)
                {
                    this.SetCellValue(startPos, i, grid, word[i]);
                }
                return new PlacedWord(word, this.GetDisplayDirection(), startPos);
            }else{
                return null;
            }
        }
Пример #2
0
        public override PlacedWord TryPlaceWord(string word, WordSearch grid)
        {
            if (word.Length > grid.Width)
                   return null;

            var xRange = Enumerable.Range(0, (grid.Width  - word.Length) - 1).ToArray();
            var yRange = Enumerable.Range(0, grid.Width - 1).ToArray();

            return FindWordLocation(xRange, yRange, word, grid);
        }
Пример #3
0
 /// <summary>
 /// Tests if a word can be placed at a givern location
 /// </summary>
 /// <param name="wordLetters"></param>
 /// <param name="startX"></param>
 /// <param name="startY"></param>
 /// <param name="grid"></param>
 /// <returns></returns>
 protected bool TryPlaceWordLetters(string wordLetters, GridCell start, WordSearch grid)
 {
     for (var i = 0; i < wordLetters.Length; i++)
     {
         var cell = this.GetCellValue(start, i, grid);
         if (cell != NullChar && cell != wordLetters[i])
             return false;
     }
     return true;
 }
Пример #4
0
 public abstract void SetCellValue(GridCell cell, int offset, WordSearch grid, char value);
Пример #5
0
 public abstract char? GetCellValue(GridCell cell, int offset, WordSearch grid);
Пример #6
0
 void Awake()
 {
     instance = this;
 }
Пример #7
0
        private void UpdateSearchResults(WordSearch search)
        {
            LastResults = search.Results;

            IAsyncAction action = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, UpdateSearchResultsUI);
        }
Пример #8
0
        void InternalTest(char[][] board, string word, bool expected)
        {
            bool actual = WordSearch.Exist(board, word);

            Assert.Equal <bool>(expected, actual);
        }
Пример #9
0
 private void OnResuming(object sender, object e)
 {
     Debug.WriteLine("Resuming...");
     WordSearch.StartSearchService();
 }
Пример #10
0
 public override void SetCellValue(GridCell cell, int offset, WordSearch grid, char value)
 {
     throw new NotImplementedException();
 }
Пример #11
0
        public void Height_WithValidCharacterGrid_ReturnsHeight()
        {
            var w = new WordSearch(ThreeByFourGrid());

            Assert.Equal(4, w.Height);
        }
Пример #12
0
        public void Parse_WithNoLines_ThrowsAppropriateException()
        {
            var e = Assert.Throws <ArgumentException>(() => WordSearch.Parse(new List <string>()));

            Assert.Equal($"{WordSearch.EmptyGridError} (Parameter 'lines')", e.Message);
        }
Пример #13
0
        public void Width_WithValidCharacterGrid_ReturnsWidth()
        {
            var w = new WordSearch(ThreeByFourGrid());

            Assert.Equal(3, w.Width);
        }
Пример #14
0
            public static WordSearch CreateNew(int width, int height, string[] words)
            {
                var ws = new WordSearch {
                    Width = width, Height = height, WordSearchLetters = new char[width, height]
                };
                var maxAttempts = ws.Width * ws.Height * 10;

                foreach (var word in words)
                {
                    var hiddenWord = new HiddenWord
                    {
                        Word = word.ToUpper()
                    };

                    int attempts = 0;
                    do
                    {
                        hiddenWord.Direction = (WordDirection)_random.Next(3);

                        switch (hiddenWord.Direction)
                        {
                        case WordDirection.HORIZONTAL:
                            hiddenWord.X = _random.Next(ws.Width - word.Length);
                            hiddenWord.Y = _random.Next(ws.Height);
                            break;

                        case WordDirection.VERTICAL:
                            hiddenWord.X = _random.Next(ws.Width);
                            hiddenWord.Y = _random.Next(ws.Height - word.Length);
                            break;

                        case WordDirection.DIAGONAL:
                            hiddenWord.X = _random.Next(ws.Width - word.Length);
                            hiddenWord.Y = _random.Next(ws.Height - word.Length);
                            break;
                        }
                        attempts++;
                    } while (!hiddenWord.FitsInWordSearch(ws) && attempts < maxAttempts);

                    if (attempts >= maxAttempts)
                    {
                        throw new Exception("SORRY! This ain't going to work.");
                    }
                    else
                    {
                        ws.AddWord(hiddenWord);
                    }
                }

                for (var y = 0; y < ws.WordSearchLetters.GetLength(1); y++)
                {
                    for (var x = 0; x < ws.WordSearchLetters.GetLength(0); x++)
                    {
                        if (ws.WordSearchLetters[x, y] == '\0')
                        {
                            ws.WordSearchLetters[x, y] = (char)('A' + _random.Next(0, 26));
                        }
                    }
                }

                return(ws);
            }
Пример #15
0
            static void Main(string[] args)
            {
                var showCheats = false;

                Console.Write("Show cheat colors? (Y/N)");
                ConsoleKeyInfo key;

                do
                {
                    key = Console.ReadKey(true);
                    if (key.Key == ConsoleKey.Y)
                    {
                        showCheats = true;
                        break;
                    }
                    else if (key.Key == ConsoleKey.N)
                    {
                        break;
                    }
                    else
                    {
                        Console.Beep();
                    }
                } while (true);
                Console.Clear();

                var wordList = new string[] {
                    "Justin", "kelly", "Whitney", "spencer", "andromeda", "aurora", "perry", "greenwood",
                    "josephine", "abraham", "eugene", "renee", "foster", "elizabeth"
                };

                var wordList2 = new string[] {
                    "Evy", "Julia", "Sylvia", "Treehouse", "Mailbox", "Lowe", "clubhouse", "Wakeup", "Eat", "Schoolwork", "Sleep"
                };

                var wordList3 = new string[] {
                    "Apples", "AppleJuice", "AppleCider", "SoynutButter", "SparklingGrapeJuice", "PaperTowels", "AppleButter", "Yogert", "Raspberries", "Blackberries", "Blueberries", "Strawberries", "Pants", "Earings", "Scarf", "Snowboots", "Dress", "Rubberbands", "Meltingbeads", "Fabrics", "Thread", "Needles", "OragomyPaper"
                };

                Console.Write("which option(a1,s2,d3)");
                ConsoleKeyInfo num;

                do
                {
                    num = Console.ReadKey(true);
                    if (num.Key == ConsoleKey.A)
                    {
                        Console.Clear();
                        var ws = WordSearch.CreateNew(18, 18, wordList);
                        for (var y = 0; y < ws.WordSearchLetters.GetLength(1); y++)
                        {
                            for (var x = 0; x < ws.WordSearchLetters.GetLength(0); x++)
                            {
                                if (showCheats && ws.HasHiddenWord(x, y))
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.White;
                                }
                                Console.Write($"{ws.WordSearchLetters[x, y]} ");
                            }
                            Console.WriteLine();
                        }
                        Console.ReadKey();
                        break;
                    }
                    else if (num.Key == ConsoleKey.S)
                    {
                        Console.Clear();
                        var ws = WordSearch.CreateNew(15, 15, wordList2);
                        for (var y = 0; y < ws.WordSearchLetters.GetLength(1); y++)
                        {
                            for (var x = 0; x < ws.WordSearchLetters.GetLength(0); x++)
                            {
                                if (showCheats && ws.HasHiddenWord(x, y))
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.White;
                                }
                                Console.Write($"{ws.WordSearchLetters[x, y]} ");
                            }
                            Console.WriteLine();
                        }
                        Console.ReadKey();
                        break;
                    }
                    else if (num.Key == ConsoleKey.D)
                    {
                        Console.Clear();
                        var ws = WordSearch.CreateNew(25, 25, wordList3);

                        for (var y = 0; y < ws.WordSearchLetters.GetLength(1); y++)
                        {
                            for (var x = 0; x < ws.WordSearchLetters.GetLength(0); x++)
                            {
                                if (showCheats && ws.HasHiddenWord(x, y))
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.White;
                                }
                                Console.Write($"{ws.WordSearchLetters[x, y]} ");
                            }
                            Console.WriteLine();
                        }
                        Console.ReadKey();
                        break;
                    }
                    else
                    {
                        Console.Beep();
                    }
                } while (true);
            }
Пример #16
0
 public override PlacedWord TryPlaceWord(string word, WordSearch grid)
 {
     throw new NotImplementedException();
 }
Пример #17
0
 public abstract PlacedWord TryPlaceWord(string word, WordSearch grid);
Пример #18
0
 public override void SetCellValue(GridCell cell, int offset, WordSearch ws, char value)
 {
     ws.Grid[cell.Y, cell.X + offset] = value;
 }
Пример #19
0
 private static WordSearch ReadSample(string name)
 {
     return(WordSearch.Parse(File.ReadAllLines($"../../../../WordSearchSolverTests/TestWordSearches/{name}.txt")));
 }
Пример #20
0
        //Method for the button "Save". Method is invoked when clicked
        private void button1_Click(object sender, EventArgs e)
        {
            //To make sure that the instructions don't get inserted into dictionary
            if (!clicked[0] || !clicked[1] || !clicked[2])
            {
                MessageBox.Show("You must have a complete entry to insert into the dictionary!");
                return;
            }

            string check = textBox1.Text;//Assign to text to a string object

            if (check == string.Empty)
            {
                MessageBox.Show("Empty Field!");
                return;
            }
            else
            {
                Regex expr = new Regex("[a-zA-Z]");//use regular expressions to check for input validity
                if (!expr.IsMatch(check) || !expr.IsMatch(textBox3.Text) || !expr.IsMatch(textBox4.Text))
                {
                    MessageBox.Show("Invalid character(s) in one or more fields. Please use alphabetic characters only and leave no field empty.");
                    return;
                }
            }

            if (WordSearch.search(check.ToLower()))                   //Use method in the class library to look up word in the file.
            {
                MessageBox.Show("That word is already in the list!"); //If word exists display message box saying so
            }
            else//If the word was not found then
            {
                //Clear all the items in the list box
                //And prepare to update the list box
                StringBuilder entry = new StringBuilder();
                listBox1.Items.Clear();
                listBox1.BeginUpdate();
                //Open file
                writer = File.AppendText(path.ToString());
                //Take content into buffer
                entry.Append(check.ToLower()).Append('*').Append(textBox3.Text).Append('*').Append(textBox4.Text);
                writer.WriteLine(entry.ToString());
                //Then write to file and close the file
                writer.Flush();

                writer.Close();

                //Open the file for reading
                reader = File.OpenText(path.ToString());
                //Read till end of file and add to list
                while (!reader.EndOfStream)
                {
                    listBox1.Items.Add(reader.ReadLine());
                }
                //End list box update close file and clear text box
                listBox1.EndUpdate();
                reader.Close();
            }
            //clear all three textboxes
            textBox1.Clear();
            textBox4.Clear();
            textBox3.Clear();
        }
Пример #21
0
 public override char? GetCellValue(GridCell cell, int offset, WordSearch ws)
 {
     return ws.Grid[cell.Y,cell.X+offset];
 }
Пример #22
0
 void Start()
 {
     //find the main word search script
     wordSearch = FindObjectOfType <WordSearch>();
 }
Пример #23
0
        public void Test001_GivenWordSearch_GetInputReturnsTheStringOfTextFromInputTextFile()
        {
            WordSearch wordSearch = new WordSearch();

            Assert.AreEqual("SEEK,TEST,WORD B,V,L,T,W X,V,S,O,O S,E,E,K,R T,P,W,X,D K,T,E,S,D", wordSearch.GetInput("TestDocument001.txt"));
        }
Пример #24
0
 public void Setup()
 {
     solution = new WordSearch();
 }