コード例 #1
0
        /// <summary>
        /// Searches East for a given word on a given board starting at a given origin point. If it finds the word,
        /// it returns a list of all of the coordinates that make up the word. Otherwise, it returns null
        /// </summary>
        /// <param name="board">WordSearchBoard to search on</param>
        /// <param name="origin">Coordinate point to begin searching</param>
        /// <param name="word">Word to search for</param>
        /// <returns>If it finds the word, returns a list of all of the coordinates that make up the word. Otherwise, it returns null</returns>
        public List <Coordinate> CheckEast(WordSearchBoard board, Coordinate origin, string word)
        {
            _output = new List <Coordinate>();

            Coordinate tempCoordinate = new Coordinate(origin.X, origin.Y);

            foreach (char letter in word)
            {
                try
                {
                    if (board.GetCharAt(tempCoordinate) == letter)
                    {
                        _output.Add(tempCoordinate);
                        tempCoordinate = new Coordinate(tempCoordinate.X + 1, tempCoordinate.Y);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (ArgumentException)
                {
                    //ArgumentException here means that the tempCoordinate is pointed to an invalid coordinate off the board
                    //Return null because we know words cannot wrap around to another part of the board
                    return(null);
                }
            }

            return(_output);
        }
コード例 #2
0
        /// <summary>
        /// Searches North-West for a given word on a given board starting at a given origin point. If it finds the word,
        /// it returns a list of all of the coordinates that make up the word. Otherwise, it returns null
        /// </summary>
        /// <param name="board">WordSearchBoard to search on</param>
        /// <param name="origin">Coordinate point to begin searching</param>
        /// <param name="word">Word to search for</param>
        /// <returns>If it finds the word, returns a list of all of the coordinates that make up the word. Otherwise, it returns null</returns>
        public List <Coordinate> CheckNorthWest(WordSearchBoard board, Coordinate origin, string word)
        {
            _output = new List <Coordinate>();

            Coordinate tempCoordinate = new Coordinate(origin.X, origin.Y);

            foreach (char letter in word)
            {
                try
                {
                    if (board.GetCharAt(tempCoordinate) == letter)
                    {
                        _output.Add(tempCoordinate);
                        tempCoordinate = new Coordinate(tempCoordinate.X - 1, tempCoordinate.Y - 1);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (ArgumentException)
                {
                    //ArgumentException here means that the tempCoordinate is pointed to an invalid coordinate off the board
                    return(null);
                }
            }

            return(_output);
        }
        public void TestWithInvalidY()
        {
            List <string> lines = new List <string>();

            lines.Add("A,B,C");
            lines.Add("D,E,F");
            lines.Add("G,H,I");

            _board = new WordSearchBoard(lines);
            _board.GetCharAt(0, 3);
        }
        public void TestWithNegativeX()
        {
            List <string> lines = new List <string>();

            lines.Add("A,B,C");
            lines.Add("D,E,F");
            lines.Add("G,H,I");

            _board = new WordSearchBoard(lines);
            _board.GetCharAt(-1, 0);
        }
コード例 #5
0
        public void TestWithNegativeY()
        {
            List <string> lines = new List <string>();

            lines.Add("A,B,C");
            lines.Add("D,E,F");
            lines.Add("G,H,I");

            _board = new WordSearchBoard(lines);
            _board.GetCharAt(new Coordinate(0, -1));
        }
        public void TestWithValidXY()
        {
            List <string> lines = new List <string>();

            lines.Add("A,B,C");
            lines.Add("D,E,F");
            lines.Add("G,H,I");

            _board = new WordSearchBoard(lines);

            Assert.AreEqual('A', _board.GetCharAt(0, 0));
            Assert.AreEqual('B', _board.GetCharAt(1, 0));
            Assert.AreEqual('C', _board.GetCharAt(2, 0));

            Assert.AreEqual('D', _board.GetCharAt(0, 1));
            Assert.AreEqual('E', _board.GetCharAt(1, 1));
            Assert.AreEqual('F', _board.GetCharAt(2, 1));

            Assert.AreEqual('G', _board.GetCharAt(0, 2));
            Assert.AreEqual('H', _board.GetCharAt(1, 2));
            Assert.AreEqual('I', _board.GetCharAt(2, 2));
        }
        /// <summary>
        /// Returns a list of coordinates that contain the character letter
        /// </summary>
        /// <param name="letter">The character that we are searching the board for</param>
        /// <returns>Returns a list of coordinates that contain the character letter</returns>
        private List<Coordinate> GetCoordinatesContainingChar(char letter)
        {
            List<Coordinate> output = new List<Coordinate>();

            for (int y = 0; y < _board.GetLength(); y++)
            {
                for (int x = 0; x < _board.GetLength(); x++)
                {
                    if (_board.GetCharAt(x, y) == letter)
                    {
                        output.Add(new Coordinate(x,y));
                    }
                }
            }

            return output;
        }