Example #1
0
        private string makeLine()
        {
            StringBuilder builder        = new StringBuilder();
            int           syllabsPerLine = 13;
            Stack         path           = new Stack();
            List <int>    usedVertex     = new List <int>();
            int           syllable       = 0;
            int           counter        = 0;

            while (syllable != syllabsPerLine /*&& counter < 200*/)
            {
                counter++;
                if (syllable == 0)
                {
                    int vertex = 0;
                    int trying = 0;
                    do
                    {
                        trying++;
                        vertex = graph.randomFirstLevelVertex();
                    } while (!validVertex(usedVertex, vertex) && trying < graph.NodesName.Count);

                    if (validVertex(usedVertex, vertex))
                    {
                        path.Push(vertex);
                        usedVertex.Add(vertex);
                        syllable += countSyllabs(graph.getNameOfVertex(vertex));
                    }
                    else
                    {
                        throw new NotEnoughtVertexToGenerateLine();
                    }
                }
                else if (path.Count > 0 && syllable < syllabsPerLine)
                {
                    int vertex     = 0;
                    int trying     = 0;
                    int lastVertex = getLsatVertex(path);
                    do
                    {
                        trying++;
                        vertex = graph.randomNeighbourVertex(lastVertex);
                    } while (!validVertex(usedVertex, vertex) && trying < graph.countNeihbours(vertex));

                    if (validVertex(usedVertex, vertex))
                    {
                        path.Push(vertex);
                        usedVertex.Add(vertex);
                        syllable += countSyllabs(graph.getNameOfVertex(vertex));
                    }
                    else
                    {
                        lastVertex = (int)path.Pop();
                        syllable  -= countSyllabs(graph.getNameOfVertex(lastVertex));
                    }
                }
                else
                {
                    if (path.Count > 0)
                    {
                        int lastVertex = (int)path.Pop();
                        syllable -= countSyllabs(graph.getNameOfVertex(lastVertex));
                    }
                    else
                    {
                        throw new NotEnoughtVertexToGenerateLine();
                    }
                }
            }

            if (syllable != syllabsPerLine)
            {
                throw new NotEnoughtVertexToGenerateLine();
            }

            return(buildLineFromPath(path));
        }