Facebook
From Coral Camel, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 71
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. namespace Hangmann
  10. {
  11.     class GameEngine
  12.     {
  13.         private string _sentence;
  14.         private string _sentenceToLowerSpace;
  15.         private int _live;
  16.         private int _operation;
  17.         private string path = @"plik.txt";
  18.  
  19.         public GameEngine()
  20.         {
  21.             _sentence = RandomWord(path);
  22.             _sentenceToLowerSpace = ToLowerSpace(_sentence);
  23.             _live = 5;
  24.         }
  25.  
  26.         public void Menu()
  27.         {
  28.             do
  29.             {
  30.                 Console.WriteLine("1)Play gamen2)Show list of passwordsn3)Add passwordn4)Delete passwordn5)ExitnChoose your operation: ");
  31.                 _operation = Convert.ToInt32(Console.ReadLine());
  32.                 if (_operation > 1 || _operation < 5)
  33.                 {
  34.                     switch (_operation)
  35.                     {
  36.                         case 1:
  37.                             var game = new GameEngine();
  38.                             game.PlayGame();
  39.                             Clear();
  40.                             break;
  41.                         case 2:
  42.                             ShowListOfPasswords(path);
  43.                             Console.WriteLine("Press any key to go back");
  44.                             Console.ReadLine();
  45.                             break;
  46.                         case 3:
  47.                             AddPassword(path);
  48.                             break;
  49.                         case 4:
  50.                             DeletePassword(path);
  51.                             break;
  52.                         case 5:
  53.                             Console.WriteLine("You exit");
  54.                             break;
  55.                     }
  56.                 }
  57.                 else
  58.                 {
  59.                     Clear();
  60.                     Console.WriteLine($"Wrong operation!");
  61.                     Wait(1500);
  62.                 }
  63.  
  64.                 Clear();
  65.             } while (_operation != 5);
  66.         }
  67.  
  68.         public void PlayGame()
  69.         {
  70.             char c;
  71.             Clear();
  72.             while (!IsSame())
  73.             {
  74.                 Console.WriteLine($"Sentence: {_sentenceToLowerSpace}nLive: {_live}");
  75.                 Console.Write("Put 1 letter: ");
  76.                 c = Convert.ToChar(Console.ReadLine());
  77.                 if(c==1)
  78.                 if (IsContain(c))
  79.                 {
  80.                     PutLetter(c);
  81.  
  82.                     if (_live == 0)
  83.                     {
  84.                         Clear();
  85.                         Console.WriteLine("Game over!");
  86.                         Wait(2000);
  87.                         break;
  88.                     }
  89.                     else if (IsSame())
  90.                     {
  91.                         Clear();
  92.                         Console.WriteLine("You Win!");
  93.                         Wait(1000);
  94.                     }
  95.                     Wait(200);
  96.                 }
  97.                 else
  98.                 {
  99.                     _live--;
  100.  
  101.                     if (_live == 0)
  102.                     {
  103.                         Clear();
  104.                         Console.WriteLine("Game over!");
  105.                         Wait(2000);
  106.                         break;
  107.                     }
  108.                 }
  109.                
  110.                 Clear();
  111.  
  112.             }
  113.         }
  114.  
  115.         public void PutLetter(char c)
  116.         {
  117.             char[] sentence = _sentence.ToCharArray();
  118.             char[] sentenceToLower = _sentenceToLowerSpace.ToCharArray();
  119.             _sentenceToLowerSpace = "";
  120.  
  121.             for(int i = 0; i < sentenceToLower.Length; i++)
  122.             {
  123.                 if (sentenceToLower[i] == c)
  124.                     _live--;
  125.  
  126.                 if (c == sentence[i])
  127.                 {
  128.                     sentenceToLower[i] = c;
  129.                 }
  130.             }
  131.            
  132.             for(int i = 0; i < sentenceToLower.Length; i++)
  133.             {
  134.                 _sentenceToLowerSpace += sentenceToLower[i];
  135.             }
  136.         }
  137.         public bool IsContain(char c)
  138.         {
  139.             char[] arrChar = _sentence.ToCharArray();
  140.  
  141.             for(int i = 0; i < arrChar.Length; i++)
  142.             {
  143.                 if (c == arrChar[i])
  144.                 {
  145.                     return true;
  146.                 }
  147.             }
  148.  
  149.             return false;
  150.         }
  151.         public string RandomWord(string path)
  152.         {
  153.             Random rnd = new Random();
  154.             var words = new List<string>(File.ReadAllLines(path));
  155.             int randomNumber = rnd.Next(words.Count);
  156.  
  157.             return words[randomNumber];
  158.         }
  159.         public string ToLowerSpace(string sentence)
  160.         {
  161.             string outPut="";
  162.  
  163.             for(int i = 0; i <sentence.Length; i++)
  164.             {
  165.                 outPut += "_";
  166.             }
  167.  
  168.             return outPut;
  169.         }
  170.  
  171.         private void ShowListOfPasswords(string path)
  172.         {
  173.             int i = 1;
  174.             List<string> pswds = new List <string>(File.ReadAllLines(path));
  175.  
  176.             Clear();
  177.             foreach(var pswd in pswds)
  178.             {
  179.                 Console.WriteLine($"{i++}) {pswd}");
  180.             }
  181.         }
  182.  
  183.        
  184.         private void AddPassword(string path)
  185.         {
  186.             List<string> pswds = new List<string>(File.ReadAllLines(path));
  187.             string pswd;
  188.  
  189.             ShowListOfPasswords(path);
  190.             Console.Write("Put your password u want to add: ");
  191.             pswd = Console.ReadLine();
  192.  
  193.             pswds.Add(pswd);
  194.  
  195.             File.WriteAllLines(path, pswds);
  196.  
  197.         }
  198.  
  199.         private void DeletePassword(string path)
  200.         {
  201.             List<string> pswds = new List<string>(File.ReadAllLines(path));
  202.             int choice;
  203.  
  204.             ShowListOfPasswords(path);
  205.             Console.Write("Press number of password u want to delete: ");
  206.             choice = Convert.ToInt32(Console.ReadLine());
  207.  
  208.             pswds.RemoveAt(choice - 1);
  209.             File.WriteAllLines(path, pswds);
  210.         }
  211.        
  212.         public bool IsSame()
  213.         {
  214.             return _sentence == _sentenceToLowerSpace;
  215.         }
  216.  
  217.         private void Clear()
  218.         {
  219.             Console.Clear();
  220.         }
  221.  
  222.         private void Wait(int ms)
  223.         {
  224.             Thread.Sleep(ms);
  225.         }
  226.         public override string ToString()
  227.         {
  228.             return $"Sentence: {_sentence}nSentence: {_sentenceToLowerSpace}nLives: {_live}";
  229.         }
  230.     }
  231. }
  232.