Advertisement
prampec

C# Akasztofa

Jun 21st, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Oktatas
  7. {
  8.     // https://github.com/laszlonemeth/magyarispell/tree/master/szotar
  9.     public class Akasztofa
  10.     {
  11.         static string[] dictFiles = new string[]
  12.         {
  13.             "resources/alap/fonev.1"
  14.         };
  15.         static Regex rx = new Regex(@"^(\w*)",
  16.           RegexOptions.Compiled | RegexOptions.IgnoreCase);
  17.         List<string> words = new List<string>();
  18.         List<char> guesses = new List<char>();
  19.         Random rnd = new Random();
  20.         int round;
  21.  
  22.         public Akasztofa()
  23.         {
  24.             readDict();
  25.         }
  26.  
  27.  
  28.         private void readDict()
  29.         {
  30.             foreach (string dicFile in dictFiles)
  31.             {
  32.                 System.IO.StreamReader file = new System.IO.StreamReader(dicFile);
  33.                 string line;
  34.                 while ((line = file.ReadLine()) != null)
  35.                 {
  36.                     MatchCollection matches = rx.Matches(line);
  37.                     Match match = rx.Match(line);
  38.                     if (match.Success)
  39.                     {
  40.                         // System.Console.Write(line + " -> '");
  41.                         // System.Console.WriteLine(match.Value + "'");
  42.                         if (match.Value.Length > 0)
  43.                         {
  44.                             words.Add(match.Value);
  45.                         }
  46.                     }
  47.                 }
  48.  
  49.                 file.Close();
  50.             }
  51.         }
  52.  
  53.         public void run()
  54.         {
  55.             string word = pickWord();
  56.             round = 1;
  57.             int errorCount = 0;
  58.             while (true)
  59.             {
  60.                 bool allFound = displayMaskedWord(word);
  61.                 if (allFound)
  62.                 {
  63.                     Console.WriteLine("Done with " + errorCount + " errors.");
  64.                     break;
  65.                 }
  66.                 char c = askForChar();
  67.                 guesses.Add(c);
  68.                 if (!word.Contains(c))
  69.                 {
  70.                     errorCount += 1;
  71.                 }
  72.                 round += 1;
  73.             }
  74.         }
  75.  
  76.  
  77.         private string pickWord()
  78.         {
  79.             int index = rnd.Next(words.Count);
  80.             return words[index];
  81.         }
  82.  
  83.         private bool displayMaskedWord(string word)
  84.         {
  85.             Console.Write("      ");
  86.             bool allFound = true;
  87.             for(int i=0; i<word.Length; i++)
  88.             {
  89.                 if (guesses.Contains(word[i]))
  90.                 {
  91.                     Console.Write(word[i]);
  92.                 }
  93.                 else
  94.                 {
  95.                     Console.Write(".");
  96.                     allFound = false;
  97.                 }
  98.             }
  99.             Console.Write("  [");
  100.             Console.Write(String.Join(", ", guesses.ToArray()));
  101.             Console.WriteLine("]");
  102.             return allFound;
  103.         }
  104.  
  105.         private char askForChar()
  106.         {
  107.             while (true)
  108.             {
  109.                 Console.Write(round + ". ? ");
  110.                 string line = Console.ReadLine();
  111.  
  112.                 if (line.Length > 0)
  113.                 {
  114.                     return line[0];
  115.                 }
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement