Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace Oktatas
- {
- // https://github.com/laszlonemeth/magyarispell/tree/master/szotar
- public class Akasztofa
- {
- static string[] dictFiles = new string[]
- {
- "resources/alap/fonev.1"
- };
- static Regex rx = new Regex(@"^(\w*)",
- RegexOptions.Compiled | RegexOptions.IgnoreCase);
- List<string> words = new List<string>();
- List<char> guesses = new List<char>();
- Random rnd = new Random();
- int round;
- public Akasztofa()
- {
- readDict();
- }
- private void readDict()
- {
- foreach (string dicFile in dictFiles)
- {
- System.IO.StreamReader file = new System.IO.StreamReader(dicFile);
- string line;
- while ((line = file.ReadLine()) != null)
- {
- MatchCollection matches = rx.Matches(line);
- Match match = rx.Match(line);
- if (match.Success)
- {
- // System.Console.Write(line + " -> '");
- // System.Console.WriteLine(match.Value + "'");
- if (match.Value.Length > 0)
- {
- words.Add(match.Value);
- }
- }
- }
- file.Close();
- }
- }
- public void run()
- {
- string word = pickWord();
- round = 1;
- int errorCount = 0;
- while (true)
- {
- bool allFound = displayMaskedWord(word);
- if (allFound)
- {
- Console.WriteLine("Done with " + errorCount + " errors.");
- break;
- }
- char c = askForChar();
- guesses.Add(c);
- if (!word.Contains(c))
- {
- errorCount += 1;
- }
- round += 1;
- }
- }
- private string pickWord()
- {
- int index = rnd.Next(words.Count);
- return words[index];
- }
- private bool displayMaskedWord(string word)
- {
- Console.Write(" ");
- bool allFound = true;
- for(int i=0; i<word.Length; i++)
- {
- if (guesses.Contains(word[i]))
- {
- Console.Write(word[i]);
- }
- else
- {
- Console.Write(".");
- allFound = false;
- }
- }
- Console.Write(" [");
- Console.Write(String.Join(", ", guesses.ToArray()));
- Console.WriteLine("]");
- return allFound;
- }
- private char askForChar()
- {
- while (true)
- {
- Console.Write(round + ". ? ");
- string line = Console.ReadLine();
- if (line.Length > 0)
- {
- return line[0];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement