|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Net; |
| 5 | + |
| 6 | +class Haiku{ |
| 7 | + static void Main(string[] args){ |
| 8 | + Console.ForegroundColor = ConsoleColor.Green; |
| 9 | + Console.WriteLine("<-----------Haiku Genrator------------>"); |
| 10 | + Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)"); |
| 11 | + Console.WriteLine("It may take a while. Don't worry, be happy."); |
| 12 | + Generate(); |
| 13 | + Console.ReadKey(); |
| 14 | + } |
| 15 | + |
| 16 | + static void Generate(){ |
| 17 | + //Template for Haiku |
| 18 | + //From 4-syllabes |
| 19 | + //To 6-syllabes |
| 20 | + //5-syllabes |
| 21 | + string haiku = ""; |
| 22 | + string[] words = GetWords(); |
| 23 | + haiku += "From " + words[0] + "\n"; |
| 24 | + haiku += "to " + words[1] + " " + words[2] + "\n"; |
| 25 | + haiku += words[3] + " " + words[4] + "."; |
| 26 | + Console.WriteLine(haiku); |
| 27 | + } |
| 28 | + |
| 29 | + static string[] GetWords(){ |
| 30 | + List<string> words = new List<string>() {"", "", "", "", ""}; |
| 31 | + while(words.Contains("")){ |
| 32 | + string word = GetWord(); |
| 33 | + int syllabes = CountSyllabes(word); |
| 34 | + if(words[0] == "" && syllabes == 4) words[0] = word; |
| 35 | + else if(words[1] == "" && syllabes == 2) words[1] = word; |
| 36 | + else if(syllabes == 4) words[2] = word; |
| 37 | + else if(syllabes == 2) words[3] = word; |
| 38 | + else if(syllabes == 3) words[4] = word; |
| 39 | + } |
| 40 | + |
| 41 | + return words.ToArray(); |
| 42 | + } |
| 43 | + |
| 44 | + static int CountSyllabes(string word){ |
| 45 | + //Count syllabes, simple as that. It works, but have a few exception like "pure" or "Language". Damn you English. |
| 46 | + string wordLow = word.ToLower(); |
| 47 | + int syllabes = 0; |
| 48 | + // string consonants = "bcdfghjklmnqprstvwxz"; |
| 49 | + string vovels = "aeiouy"; |
| 50 | + |
| 51 | + for (int i = 0; i < wordLow.Length; i++){ |
| 52 | + if (i < wordLow.Length - 1){ |
| 53 | + if (vovels.Contains(wordLow[i].ToString()) && !vovels.Contains(wordLow[i+1].ToString())) |
| 54 | + syllabes++; |
| 55 | + } else if (i == wordLow.Length - 1){ |
| 56 | + if (vovels.Contains(wordLow[i].ToString())) |
| 57 | + syllabes++; |
| 58 | + } |
| 59 | + } |
| 60 | + return syllabes; |
| 61 | + } |
| 62 | + |
| 63 | + static string GetWord(){ |
| 64 | + //Copied from 08_Hangman |
| 65 | + //Get a word from https://fakena.me/random-english-words/one/ |
| 66 | + WebClient client = new WebClient(); |
| 67 | + string url = "https://fakena.me/random-english-words/one/"; |
| 68 | + string line = ""; |
| 69 | + //Check internet connection |
| 70 | + try{ |
| 71 | + line = client.DownloadString(url).Split('\n')[27]; |
| 72 | + } catch (System.Net.WebException) { |
| 73 | + Console.WriteLine("Network connection problem :("); |
| 74 | + System.Environment.Exit(1); |
| 75 | + } |
| 76 | + string word = line.Substring(39).Split('<')[0]; |
| 77 | + return word; |
| 78 | + } |
| 79 | +} |
0 commit comments