Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace Rack_Management_1
  7. {
  8. class Program
  9. {
  10. static Tuple<string, string>[] scrabbleLetters =
  11. {
  12. Tuple.Create("ladilmy", "daily"),
  13. Tuple.Create("eerriin", "eerie"),
  14. Tuple.Create("orrpgma", "program"),
  15. Tuple.Create("orppgma", "program"),
  16.  
  17. Tuple.Create("pizza??", "pizzazz"),
  18. Tuple.Create("piizza?", "pizzazz"),
  19. Tuple.Create("a??????", "program"),
  20. Tuple.Create("b??????", "program")
  21. };
  22.  
  23. static string[] longestLetters =
  24. {
  25. "dcthoyueorza",
  26. "uruqrnytrois",
  27. "rryqeiaegicgeo??",
  28. "udosjanyuiuebr??",
  29. "vaakojeaietg????????"
  30. };
  31.  
  32. static string[] highestPointLetters =
  33. {
  34. "dcthoyueorza",
  35. "uruqrnytrois",
  36. "rryqeiaegicgeo??",
  37. "udosjanyuiuebr??",
  38. "vaakojeaietg????????"
  39. };
  40.  
  41. static List<string> dictionary;
  42.  
  43. static void Main(string[] args)
  44. {
  45. dictionary = new List<string>();
  46.  
  47. using (StreamReader file = new StreamReader("Dictionary.txt"))
  48. {
  49. string line;
  50.  
  51. //Each line of this file is a new word
  52. while ((line = file.ReadLine()) != null)
  53. dictionary.Add(line.Trim());
  54. }
  55.  
  56. //Scrabble
  57. foreach (Tuple<string, string> row in scrabbleLetters)
  58. Console.WriteLine(row.Item1 + ", " + row.Item2 + " -> " + Scrabble(row.Item1, row.Item2));
  59.  
  60. Console.ReadKey();
  61. Console.Clear();
  62.  
  63.  
  64. //Longest
  65. foreach (string letterList in longestLetters)
  66. Console.WriteLine(letterList + " -> " + Longest(letterList));
  67.  
  68. Console.ReadKey();
  69. Console.Clear();
  70.  
  71. //Highest Point Value
  72. foreach (string letterList in highestPointLetters) {
  73. Tuple<string, int> highest = HighestValue(letterList);
  74. Console.WriteLine(letterList + " -> " + highest.Item1 + " at " + highest.Item2);
  75. }
  76.  
  77. Console.ReadKey();
  78. Console.Clear();
  79. }
  80.  
  81. static string usedLetters;
  82.  
  83. /// <summary>
  84. /// Determines if you can make the word given the letters
  85. /// </summary>
  86. /// <param name="letters">
  87. /// A string of every letter you have,
  88. /// ? is a wild tile
  89. /// </param>
  90. /// <param name="word">
  91. /// Word you wish to create
  92. /// </param>
  93. static bool Scrabble(string letters, string word)
  94. {
  95. usedLetters = "";
  96.  
  97. foreach (char c in word)
  98. if (letters.Contains(c))
  99. {
  100. letters = letters.Remove(letters.IndexOf(c), 1);
  101. usedLetters += c;
  102. }
  103. else if (letters.Contains('?'))
  104. {
  105. letters = letters.Remove(letters.IndexOf('?'), 1);
  106. usedLetters += '?';
  107. }
  108. else
  109. return false;
  110.  
  111. return true;
  112. }
  113.  
  114. /// <summary>
  115. /// Returns the longest possible word you can make using the passed in letters
  116. /// </summary>
  117. /// <param name="letters">
  118. /// Letters you have on your rack,
  119. /// ? is a wild tile
  120. /// </param>
  121. static string Longest(string letters)
  122. {
  123. foreach(string word in dictionary.OrderByDescending(x => x.Length))
  124. if (Scrabble(letters, word))
  125. return word;
  126.  
  127. return null;
  128. }
  129.  
  130. static Tuple<string, int> HighestValue(string letters)
  131. {
  132. Tuple<string, int> best = new Tuple<string, int>("", 0);
  133.  
  134. foreach (string word in dictionary.Where(x => x.GetValue() > best.Item2).OrderByDescending(x => x.GetValue()))
  135. if (Scrabble(letters, word) && usedLetters.GetValue() > best.Item2)
  136. best = new Tuple<string, int>(usedLetters, usedLetters.GetValue());
  137.  
  138. return best;
  139. }
  140. }
  141.  
  142. public static class ExtensionMethods
  143. {
  144. static Dictionary<char, int> pointValues = new Dictionary<char, int>
  145. {
  146. { 'a', 1 },
  147. { 'b', 3 },
  148. { 'c', 3 },
  149. { 'd', 2 },
  150. { 'e', 1 },
  151. { 'f', 4 },
  152. { 'g', 2 },
  153. { 'h', 4 },
  154. { 'i', 1 },
  155. { 'j', 8 },
  156. { 'k', 5 },
  157. { 'l', 1 },
  158. { 'm', 3 },
  159. { 'n', 1 },
  160. { 'o', 1 },
  161. { 'p', 3 },
  162. { 'q', 10 },
  163. { 'r', 1 },
  164. { 's', 1 },
  165. { 't', 1 },
  166. { 'u', 1 },
  167. { 'v', 4 },
  168. { 'w', 4 },
  169. { 'x', 8 },
  170. { 'y', 4 },
  171. { 'z', 10 },
  172. { '?', 0 }
  173. };
  174.  
  175. public static int GetValue(this string word)
  176. {
  177. int value = 0;
  178.  
  179. foreach (char letter in word)
  180. value += pointValues[letter];
  181.  
  182. return value;
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement