Skip to content

Instantly share code, notes, and snippets.

@ArCiGo
Last active January 12, 2019 19:56
Show Gist options
  • Save ArCiGo/2e5f3405050c7d91d9f0f6706e55c0b4 to your computer and use it in GitHub Desktop.
Save ArCiGo/2e5f3405050c7d91d9f0f6706e55c0b4 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace WordGame
{
class MainClass
{
public static string VOWELS = "aeiou";
public static string CONSONANTS = "bcdfghjklmnpqrstvwxyz";
public static int HANDSIZE = 7;
public static Dictionary<char, int> SCRABBLE_LETTER_VALUES = new Dictionary<char, int>()
{
{ 'a', 1 }, { 'b', 3 }, { 'c', 3 }, { 'd', 2 }, { 'e', 1 },
{ 'f', 4 }, { 'g', 2 }, { 'h', 4 }, { 'i', 1 }, { 'j', 8 },
{ 'k', 5 }, { 'l', 1 }, { 'm', 3 }, { 'n', 1 }, { 'o', 1 },
{ 'p', 3 }, { 'q', 10 }, { 'r', 1 }, { 's', 1 }, { 't', 1 },
{ 'u', 1 }, { 'v', 4 }, { 'w', 4 }, { 'x', 8 }, { 'y', 4 },
{ 'z', 10 }
};
public static void Main(string[] args)
{
List<string> wordList = LoadWords();
Dictionary<char, int> handOne = new Dictionary<char, int>()
{
{ 'h',1}, {'i',1}, {'c',1}, {'z',1}, {'m',2}, {'a',1}
};
Dictionary<char, int> handTwo = new Dictionary<char, int>()
{
{'w',1}, { 's',1}, {'t',2 }, { 'a',1}, { 'o',1}, {'f',1}
};
Dictionary<char, int> handThree = new Dictionary<char, int>()
{
{ 'n',1}, { 'e',1}, { 't',1}, { 'a',1}, {'r',1}, {'i',2}
};
// PlayHand(handOne, wordList, 7);
// PlayHand(handTwo, wordList, 7);
// PlayHand(handThree, wordList, 7);
PlayGame(wordList);
Console.ReadKey();
}
public static List<string> LoadWords()
{
Console.WriteLine("Loading word list from file...");
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Armando\Documents\Online Courses\MITx 6.00.1x Introduction to Computer Science and Programming Using Python\Week 4\words.txt");
List<string> wordList = new List<string>();
foreach (string line in lines)
{
if (!string.IsNullOrEmpty(line) || !string.IsNullOrWhiteSpace(line))
{
wordList.Add(line.Trim().ToLower());
}
}
Console.WriteLine(" " + wordList.Count + "words loaded");
return wordList;
}
public static Dictionary<char, int> GetFequencyDict(string sequence)
{
Dictionary<char, int> freq = new Dictionary<char, int>();
foreach (var i in sequence)
{
freq[i] = freq.Where(x => x.Key == i).SingleOrDefault().Value + 1;
}
return freq;
}
public static int GetWordScore(string word, int n)
{
int score = 0;
foreach (var i in word)
{
score += SCRABBLE_LETTER_VALUES[i];
}
score *= word.Length;
if (word.Length == n)
{
score += 50;
}
return score;
}
public static void DisplayHand(Dictionary<char, int> hand)
{
foreach (var letter in hand.Keys)
{
foreach (var j in RangePython(hand[letter]))
{
Console.Write(letter + "");
}
}
Console.WriteLine("");
}
public static Dictionary<char, int> DealHand(int n)
{
Random rand = new Random();
Dictionary<char, int> hand = new Dictionary<char, int>();
int numVowels = n / 3;
char x;
foreach (var i in RangePython(numVowels))
{
x = VOWELS[rand.Next(0, VOWELS.Length)];
hand[x] = hand.Where(y => y.Key == x).SingleOrDefault().Value + 1;
}
foreach (var i in RangePython(numVowels, n))
{
x = CONSONANTS[rand.Next(0, CONSONANTS.Length)];
hand[x] = hand.Where(y => y.Key == x).SingleOrDefault().Value + 1;
}
return hand;
}
public static Dictionary<char, int> UpdateHand(Dictionary<char, int> hand, string word)
{
Dictionary<char, int> copyHand = new Dictionary<char, int>(hand);
foreach (var i in word)
{
copyHand[i] -= 1;
}
return copyHand;
}
public static bool IsValidWord(string word, Dictionary<char, int> hand, List<string> wordList)
{
Dictionary<char, int> copyHand = new Dictionary<char, int>(hand);
string auxWord = "";
foreach (var i in word)
{
if (copyHand.Keys.Contains(i) && copyHand[i] > 0)
{
auxWord += i;
copyHand[i] -= 1;
}
}
foreach (var item in wordList)
{
if (item.Equals(auxWord.ToLower()))
{
return true;
}
}
return false;
}
public static int CalculateHandLen(Dictionary<char, int> hand)
{
int count = 0;
foreach (var item in hand.Keys)
{
count += hand[item];
}
return count;
}
public static void PlayHand(Dictionary<char, int> hand, List<string> wordList, int n)
{
int totalScore = 0;
string userInput = "";
while (CalculateHandLen(hand) >= 0)
{
Console.Write("Current hand: ");
DisplayHand(hand);
Console.Write("Enter word, or a \".\" to indicate that you are finished: ");
userInput = Console.ReadLine();
if (userInput == ".")
{
Console.WriteLine("Goodbye! Total score: " + totalScore + " points.");
break;
}
else if (!IsValidWord(userInput, hand, wordList))
{
Console.WriteLine("Invalid word, please try again.");
Console.WriteLine("");
}
else
{
totalScore += GetWordScore(userInput, n);
Console.WriteLine(userInput + " earned: " + GetWordScore(userInput, n) + ". Total: " + totalScore + " points.");
Console.WriteLine("");
hand = UpdateHand(hand, userInput);
}
if (CalculateHandLen(hand) == 0)
{
Console.WriteLine("Run out of letters. Total score: " + totalScore + " points.");
break;
}
}
}
public static void PlayGame(List<string> wordList)
{
int n = HANDSIZE;
Dictionary<char, int> hand = DealHand(n);
string userInput;
int counter = 0;
ReturnHere:
Console.Write("Enter \"n\" to deal a new hand, \"r\" to replay the last hand, or \"e\" to end game: ");
userInput = Console.ReadLine();
switch (userInput)
{
case "n":
PlayHand(hand, wordList, n);
counter += 1;
break;
case "r":
if(counter > 0)
{
PlayHand(hand, wordList, n);
counter += 1;
}
else
{
Console.WriteLine("You have not played a hand yet. Please play a new hand first.");
goto ReturnHere;
}
break;
case "e":
break;
default:
Console.WriteLine("Invalid command!");
goto ReturnHere;
}
}
/*
* Custom Range()
* provided on Stack Overflow
*/
public static IEnumerable<int> RangePython(int start, int stop, int step = 1)
{
if (step == 0)
throw new ArgumentException("Parameter step cannot equal zero.");
if (start < stop && step > 0)
{
for (var i = start; i < stop; i += step)
{
yield return i;
}
}
else if (start > stop && step < 0)
{
for (var i = start; i > stop; i += step)
{
yield return i;
}
}
}
public static IEnumerable<int> RangePython(int stop)
{
return RangePython(0, stop);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment