Advertisement
ArCiGo

WordGame_A

Jan 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.17 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 WordGame
  8. {
  9.     class MainClass
  10.     {
  11.         public static string VOWELS = "aeiou";
  12.         public static string CONSONANTS = "bcdfghjklmnpqrstvwxyz";
  13.         public static int HANDSIZE = 7;
  14.         public static Dictionary<char, int> SCRABBLE_LETTER_VALUES = new Dictionary<char, int>()
  15.         {
  16.             { 'a', 1 }, { 'b', 3 }, { 'c', 3 }, { 'd', 2 }, { 'e', 1 },
  17.             { 'f', 4 }, { 'g', 2 }, { 'h', 4 }, { 'i', 1 }, { 'j', 8 },
  18.             { 'k', 5 }, { 'l', 1 }, { 'm', 3 }, { 'n', 1 }, { 'o', 1 },
  19.             { 'p', 3 }, { 'q', 10 }, { 'r', 1 }, { 's', 1 }, { 't', 1 },
  20.             { 'u', 1 }, { 'v', 4 }, { 'w', 4 }, { 'x', 8 }, { 'y', 4 },
  21.             { 'z', 10 }
  22.         };
  23.  
  24.         public static Dictionary<char, int> aux = new Dictionary<char, int>()
  25.         {
  26.             {'a',1 }, {'q',1 }, {'l',2 }, {'m',1 }, {'u',1 }, {'i',1}
  27.         };
  28.  
  29.         public static void Main(string[] args)
  30.         {
  31.             List<string> wordList = LoadWords();
  32.             Dictionary<char, int> handOne = new Dictionary<char, int>()
  33.             {
  34.                 { 'h',1}, {'i',1}, {'c',1}, {'z',1}, {'m',2}, {'a',1}
  35.             };
  36.             Dictionary<char, int> handTwo = new Dictionary<char, int>()
  37.             {
  38.                 {'w',1}, { 's',1}, {'t',2 }, { 'a',1}, { 'o',1}, {'f',1}
  39.             };
  40.             Dictionary<char, int> handThree = new Dictionary<char, int>()
  41.             {
  42.                 { 'n',1}, { 'e',1}, { 't',1}, { 'a',1}, {'r',1}, {'i',2}
  43.             };
  44.  
  45.             // PlayHand(handOne, wordList, 7);
  46.             // PlayHand(handTwo, wordList, 7);
  47.             // PlayHand(handThree, wordList, 7);
  48.             PlayGame(wordList);
  49.  
  50.             //Console.ReadKey();
  51.         }
  52.  
  53.         public static List<string> LoadWords()
  54.         {
  55.             Console.WriteLine("Loading word list from file...");
  56.  
  57.             string[] lines = System.IO.File.ReadAllLines(@"/Users/arcigo/Documents/Traning/words.txt");
  58.             List<string> wordList = new List<string>();
  59.  
  60.             foreach (string line in lines)
  61.             {
  62.                 if (!string.IsNullOrEmpty(line) || !string.IsNullOrWhiteSpace(line))
  63.                 {
  64.                     wordList.Add(line.Trim().ToLower());
  65.                 }
  66.             }
  67.  
  68.             Console.WriteLine(" " + wordList.Count + "words loaded");
  69.  
  70.             return wordList;
  71.         }
  72.  
  73.         public static Dictionary<char, int> GetFequencyDict(string sequence)
  74.         {
  75.             Dictionary<char, int> freq = new Dictionary<char, int>();
  76.  
  77.             foreach (var i in sequence)
  78.             {
  79.                 freq[i] = freq.Where(x => x.Key == i).SingleOrDefault().Value + 1;
  80.             }
  81.  
  82.             return freq;
  83.         }
  84.  
  85.         public static int GetWordScore(string word, int n)
  86.         {
  87.             int score = 0;
  88.  
  89.             foreach (var i in word)
  90.             {
  91.                 score += SCRABBLE_LETTER_VALUES[i];
  92.             }
  93.  
  94.             score *= word.Length;
  95.  
  96.             if (word.Length == n)
  97.             {
  98.                 score += 50;
  99.             }
  100.  
  101.             return score;
  102.         }
  103.  
  104.         public static void DisplayHand(Dictionary<char, int> hand)
  105.         {
  106.             foreach (var letter in hand.Keys)
  107.             {
  108.                 foreach (var j in RangePython(hand[letter]))
  109.                 {
  110.                     Console.Write(letter + "");
  111.                 }
  112.             }
  113.             Console.WriteLine("");
  114.         }
  115.  
  116.         public static Dictionary<char, int> DealHand(int n)
  117.         {
  118.             Random rand = new Random();
  119.             Dictionary<char, int> hand = new Dictionary<char, int>();
  120.  
  121.             int numVowels = n / 3;
  122.             char x;
  123.  
  124.             foreach (var i in RangePython(numVowels))
  125.             {
  126.                 x = VOWELS[rand.Next(0, VOWELS.Length)];
  127.                 hand[x] = hand.Where(y => y.Key == x).SingleOrDefault().Value + 1;
  128.             }
  129.  
  130.             foreach (var i in RangePython(numVowels, n))
  131.             {
  132.                 x = CONSONANTS[rand.Next(0, CONSONANTS.Length)];
  133.                 hand[x] = hand.Where(y => y.Key == x).SingleOrDefault().Value + 1;
  134.             }
  135.  
  136.             return hand;
  137.         }
  138.  
  139.         public static Dictionary<char, int> UpdateHand(Dictionary<char, int> hand, string word)
  140.         {
  141.             Dictionary<char, int> copyHand = new Dictionary<char, int>(hand);
  142.  
  143.             foreach (var i in word)
  144.             {
  145.                 copyHand[i] -= 1;
  146.             }
  147.  
  148.             return copyHand;
  149.         }
  150.  
  151.         public static bool IsValidWord(string word, Dictionary<char, int> hand, List<string> wordList)
  152.         {
  153.             Dictionary<char, int> copyHand = new Dictionary<char, int>(hand);
  154.             string auxWord = "";
  155.  
  156.             foreach (var i in word)
  157.             {
  158.                 if (copyHand.Keys.Contains(i) && copyHand[i] > 0)
  159.                 {
  160.                     auxWord += i;
  161.                     copyHand[i] -= 1;
  162.                 }
  163.             }
  164.  
  165.             foreach (var item in wordList)
  166.             {
  167.                 if (item.Contains(auxWord.ToLower()))
  168.                 {
  169.                     return true;
  170.                 }
  171.             }
  172.  
  173.             return false;
  174.         }
  175.  
  176.         public static int CalculateHandLen(Dictionary<char, int> hand)
  177.         {
  178.             int count = 0;
  179.  
  180.             foreach (var item in hand.Keys)
  181.             {
  182.                 count += hand[item];
  183.             }
  184.  
  185.             return count;
  186.         }
  187.  
  188.         public static void PlayHand(Dictionary<char, int> hand, List<string> wordList, int n)
  189.         {
  190.             int totalScore = 0;
  191.             string userInput = "";
  192.  
  193.             while (CalculateHandLen(hand) >= 0)
  194.             {
  195.                 Console.Write("Current hand: ");
  196.                 DisplayHand(hand);
  197.  
  198.                 Console.Write("Enter word, or a \".\" to indicate that you are finished: ");
  199.                 userInput = Console.ReadLine();
  200.  
  201.                 if (userInput == ".")
  202.                 {
  203.                     Console.WriteLine("Goodbye! Total score: " + totalScore + " points.");
  204.                     break;
  205.                 }
  206.                 else if (!IsValidWord(userInput, hand, wordList))
  207.                 {
  208.                     Console.WriteLine("Invalid word, please try again.");
  209.                     Console.WriteLine("");
  210.                 }
  211.                 else
  212.                 {
  213.                     totalScore += GetWordScore(userInput, n);
  214.                     Console.WriteLine(userInput + " earned: " + GetWordScore(userInput, n) + ". Total: " + totalScore + " points.");
  215.                     Console.WriteLine("");
  216.  
  217.                     hand = UpdateHand(hand, userInput);
  218.                 }
  219.  
  220.                 if (CalculateHandLen(hand) == 0)
  221.                 {
  222.                     Console.WriteLine("Run out of letters. Total score: " + totalScore + " points.");
  223.                     break;
  224.                 }
  225.             }
  226.         }
  227.  
  228.         public static void PlayGame(List<string> wordList)
  229.         {
  230.             Dictionary<char, int> hand = new Dictionary<char, int>();
  231.             string userInput = "";
  232.             int n;
  233.  
  234.             Console.Write("Enter \"n\" to deal a new hand, \"r\" to replay the last hand, or \"e\" to end game: ");
  235.             userInput = "";
  236.  
  237.             while (true)
  238.             {
  239.                 if (userInput == "n")
  240.                 {
  241.                     n = HANDSIZE;
  242.                     hand = DealHand(n);
  243.  
  244.                     PlayHand(hand, wordList, n);
  245.                 }
  246.                 else if (userInput == "r")
  247.                 {
  248.                     try
  249.                     {
  250.                         n = HANDSIZE;
  251.  
  252.                         PlayHand(hand, wordList, n);
  253.                     }
  254.                     catch (Exception)
  255.                     {
  256.                         Console.WriteLine("You have not played a hand yet. Please play a new hand first.");
  257.                     }
  258.                 }
  259.                 else if (userInput == "e")
  260.                 {
  261.                     break;
  262.                 }
  263.                 else
  264.                 {
  265.                     Console.Write("Invalid command!");
  266.                 }
  267.             }
  268.         }
  269.  
  270.         /*
  271.          * Custom Range()
  272.          * provided on Stack Overflow
  273.          */
  274.  
  275.         public static IEnumerable<int> RangePython(int start, int stop, int step = 1)
  276.         {
  277.             if (step == 0)
  278.                 throw new ArgumentException("Parameter step cannot equal zero.");
  279.  
  280.             if (start < stop && step > 0)
  281.             {
  282.                 for (var i = start; i < stop; i += step)
  283.                 {
  284.                     yield return i;
  285.                 }
  286.             }
  287.             else if (start > stop && step < 0)
  288.             {
  289.                 for (var i = start; i > stop; i += step)
  290.                 {
  291.                     yield return i;
  292.                 }
  293.             }
  294.         }
  295.  
  296.         public static IEnumerable<int> RangePython(int stop)
  297.         {
  298.             return RangePython(0, stop);
  299.         }
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement