Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace Lingo
- {
- class Program
- {
- private static readonly List<string> Words = new List<string>();
- static void Main()
- {
- var rand = new Random();
- var bNewWord = true;
- var wordToGuess = new StringBuilder();
- var initOutput = new StringBuilder();
- InitializeDictionary();
- while (true)
- {
- if (bNewWord)
- {
- var index = rand.Next(0, Words.Count);
- wordToGuess = new StringBuilder();
- wordToGuess.Append(Words[index]);
- initOutput = new StringBuilder(wordToGuess[0] + "....");
- Console.WriteLine(initOutput);
- bNewWord = false;
- }
- Console.Write("Guess? ");
- string strGuess = Console.ReadLine();
- if (String.IsNullOrEmpty(strGuess) || strGuess.Length != 5)
- continue;
- if (strGuess.ToUpper().Equals(wordToGuess.ToString()))
- {
- bNewWord = true;
- Console.WriteLine("Correct!");
- continue;
- }
- var output = CheckMatches(wordToGuess.ToString(), strGuess.ToUpper(), ref initOutput);
- Console.WriteLine(output);
- Console.WriteLine(initOutput);
- }
- }
- private static StringBuilder CheckMatches(string strWordToFind, string strUserInput, ref StringBuilder initOutput)
- {
- var sbOutput = new StringBuilder();
- var sbLettersToFind = new StringBuilder();
- for (int i = 1; i < sbLettersToFind.ToString().Length; i++ )
- {
- if (sbLettersToFind[i] != '.')
- {
- sbLettersToFind.Append(sbLettersToFind[i]);
- }
- }
- for (var i = 0; i < 5; i++)
- {
- if (strUserInput[i].Equals(strWordToFind[i]))
- {
- sbOutput.Append("[" + strUserInput[i] + "]");
- initOutput[i] = strUserInput[i];
- }
- else if (sbLettersToFind.ToString().Contains(strUserInput[i]))
- {
- sbOutput.Append("(" + strUserInput[i] + ")");
- }
- else
- {
- sbOutput.Append(strUserInput[i]);
- }
- }
- return sbOutput;
- }
- private static void InitializeDictionary()
- {
- // Read the dictionary file and load a random word into memory
- using (TextReader tr = new StreamReader("Database.txt"))
- {
- var readLine = tr.ReadLine();
- if (readLine != null)
- foreach (var s in readLine.Split(' '))
- {
- Words.Add(s);
- }
- }
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement