using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading; namespace Hangmann { class GameEngine { private string _sentence; private string _sentenceToLowerSpace; private int _live; private int _operation; private string path = @"plik.txt"; public GameEngine() { _sentence = RandomWord(path); _sentenceToLowerSpace = ToLowerSpace(_sentence); _live = 5; } public void Menu() { do { Console.WriteLine("1)Play gamen2)Show list of passwordsn3)Add passwordn4)Delete passwordn5)ExitnChoose your operation: "); _operation = Convert.ToInt32(Console.ReadLine()); if (_operation > 1 || _operation < 5) { switch (_operation) { case 1: var game = new GameEngine(); game.PlayGame(); Clear(); break; case 2: ShowListOfPasswords(path); Console.WriteLine("Press any key to go back"); Console.ReadLine(); break; case 3: AddPassword(path); break; case 4: DeletePassword(path); break; case 5: Console.WriteLine("You exit"); break; } } else { Clear(); Console.WriteLine($"Wrong operation!"); Wait(1500); } Clear(); } while (_operation != 5); } public void PlayGame() { char c; Clear(); while (!IsSame()) { Console.WriteLine($"Sentence: {_sentenceToLowerSpace}nLive: {_live}"); Console.Write("Put 1 letter: "); c = Convert.ToChar(Console.ReadLine()); if(c==1) if (IsContain(c)) { PutLetter(c); if (_live == 0) { Clear(); Console.WriteLine("Game over!"); Wait(2000); break; } else if (IsSame()) { Clear(); Console.WriteLine("You Win!"); Wait(1000); } Wait(200); } else { _live--; if (_live == 0) { Clear(); Console.WriteLine("Game over!"); Wait(2000); break; } } Clear(); } } public void PutLetter(char c) { char[] sentence = _sentence.ToCharArray(); char[] sentenceToLower = _sentenceToLowerSpace.ToCharArray(); _sentenceToLowerSpace = ""; for(int i = 0; i < sentenceToLower.Length; i++) { if (sentenceToLower[i] == c) _live--; if (c == sentence[i]) { sentenceToLower[i] = c; } } for(int i = 0; i < sentenceToLower.Length; i++) { _sentenceToLowerSpace += sentenceToLower[i]; } } public bool IsContain(char c) { char[] arrChar = _sentence.ToCharArray(); for(int i = 0; i < arrChar.Length; i++) { if (c == arrChar[i]) { return true; } } return false; } public string RandomWord(string path) { Random rnd = new Random(); var words = new List(File.ReadAllLines(path)); int randomNumber = rnd.Next(words.Count); return words[randomNumber]; } public string ToLowerSpace(string sentence) { string outPut=""; for(int i = 0; i pswds = new List (File.ReadAllLines(path)); Clear(); foreach(var pswd in pswds) { Console.WriteLine($"{i++}) {pswd}"); } } private void AddPassword(string path) { List pswds = new List(File.ReadAllLines(path)); string pswd; ShowListOfPasswords(path); Console.Write("Put your password u want to add: "); pswd = Console.ReadLine(); pswds.Add(pswd); File.WriteAllLines(path, pswds); } private void DeletePassword(string path) { List pswds = new List(File.ReadAllLines(path)); int choice; ShowListOfPasswords(path); Console.Write("Press number of password u want to delete: "); choice = Convert.ToInt32(Console.ReadLine()); pswds.RemoveAt(choice - 1); File.WriteAllLines(path, pswds); } public bool IsSame() { return _sentence == _sentenceToLowerSpace; } private void Clear() { Console.Clear(); } private void Wait(int ms) { Thread.Sleep(ms); } public override string ToString() { return $"Sentence: {_sentence}nSentence: {_sentenceToLowerSpace}nLives: {_live}"; } } }