Advertisement
Guest User

[4/5/2012] Challenge #36 [difficult]

a guest
Apr 6th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Lingo
  8. {
  9.     class Program
  10.     {
  11.         private static readonly List<string> Words = new List<string>();
  12.  
  13.         static void Main()
  14.         {
  15.             var rand = new Random();
  16.             var bNewWord = true;
  17.             var wordToGuess = new StringBuilder();
  18.             var initOutput = new StringBuilder();
  19.  
  20.             InitializeDictionary();
  21.  
  22.             while (true)
  23.             {
  24.                 if (bNewWord)
  25.                 {
  26.                     var index = rand.Next(0, Words.Count);
  27.                     wordToGuess = new StringBuilder();
  28.                     wordToGuess.Append(Words[index]);
  29.  
  30.                     initOutput = new StringBuilder(wordToGuess[0] + "....");
  31.                     Console.WriteLine(initOutput);
  32.  
  33.                     bNewWord = false;
  34.                 }
  35.  
  36.                 Console.Write("Guess? ");
  37.                 string strGuess = Console.ReadLine();
  38.  
  39.                 if (String.IsNullOrEmpty(strGuess) || strGuess.Length != 5)
  40.                     continue;
  41.  
  42.                 if (strGuess.ToUpper().Equals(wordToGuess.ToString()))
  43.                 {
  44.                     bNewWord = true;
  45.                     Console.WriteLine("Correct!");
  46.                     continue;
  47.                 }
  48.  
  49.                 var output = CheckMatches(wordToGuess.ToString(), strGuess.ToUpper(), ref initOutput);
  50.                 Console.WriteLine(output);
  51.                 Console.WriteLine(initOutput);
  52.             }
  53.         }
  54.  
  55.         private static StringBuilder CheckMatches(string strWordToFind, string strUserInput, ref StringBuilder initOutput)
  56.         {
  57.             var sbOutput = new StringBuilder();
  58.             var sbLettersToFind = new StringBuilder();
  59.  
  60.             for (int i = 1; i < sbLettersToFind.ToString().Length; i++ )
  61.             {
  62.                 if (sbLettersToFind[i] != '.')
  63.                 {
  64.                     sbLettersToFind.Append(sbLettersToFind[i]);
  65.                 }
  66.             }
  67.  
  68.             for (var i = 0; i < 5; i++)
  69.             {
  70.                 if (strUserInput[i].Equals(strWordToFind[i]))
  71.                 {
  72.                     sbOutput.Append("[" + strUserInput[i] + "]");
  73.                     initOutput[i] = strUserInput[i];
  74.                 }
  75.                 else if (sbLettersToFind.ToString().Contains(strUserInput[i]))
  76.                 {
  77.                     sbOutput.Append("(" + strUserInput[i] + ")");
  78.                 }
  79.                 else
  80.                 {
  81.                     sbOutput.Append(strUserInput[i]);
  82.                 }
  83.             }
  84.  
  85.             return sbOutput;
  86.         }
  87.  
  88.         private static void InitializeDictionary()
  89.         {
  90.             // Read the dictionary file and load a random word into memory
  91.             using (TextReader tr = new StreamReader("Database.txt"))
  92.             {
  93.                 var readLine = tr.ReadLine();
  94.                 if (readLine != null)
  95.                     foreach (var s in readLine.Split(' '))
  96.                     {
  97.                         Words.Add(s);
  98.                     }
  99.             }
  100.  
  101.             return;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement