Created
January 26, 2015 20:56
-
-
Save jinwood/b8ad5cf90447f722b2d4 to your computer and use it in GitHub Desktop.
Words With Enemies Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace WordsWithEnemies | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Welcome to words with enemies\n\n"); | |
Console.WriteLine("There are two cannons facing each other over a valley. The cannons fire words at each other. Similar letters cancel each other out at a 1:1 ratio. The remaining letters return to their respective cannon.\n"); | |
Console.WriteLine("The first word:"); | |
string firstWord = Console.ReadLine(); | |
Console.Clear(); | |
Console.WriteLine("The second word:"); | |
string secondWord = Console.ReadLine(); | |
Console.Clear(); | |
//convert our strings to char lists | |
List<char> First = new List<char>(firstWord.ToCharArray()); | |
List<char> Second = new List<char>(secondWord.ToCharArray()); | |
//duplicate list so we can modify its contents without loosing reference | |
List<char> modFirst = new List<char>(firstWord.ToCharArray()); ; | |
List<char> modSecond = new List<char>(secondWord.ToCharArray()); | |
Console.WriteLine("\nLets fire the words at each other and see what happens!\n"); | |
for (int i = 0; i < First.Count; i++) | |
{ | |
for (int k = 0; k < Second.Count; k++) | |
{ | |
if (First[i] == Second[k]) | |
{ | |
//we have a char match | |
//create a temp char that we can remove from the other list | |
char temp = First[i]; | |
while (modFirst.Contains(temp) || modSecond.Contains(temp)) | |
{ | |
//remove as necessary | |
if (modFirst.Contains(temp)) | |
{ | |
modFirst.Remove(temp); | |
} | |
if (modSecond.Contains(temp)) | |
{ | |
modSecond.Remove(temp); | |
} | |
} | |
} | |
} | |
} | |
//recombine as strings | |
string result1 = string.Join("", modFirst); | |
string result2 = string.Join("", modSecond); | |
if (modFirst.Count > modSecond.Count) | |
{ | |
Console.WriteLine("The first word wins!\nWord 1:{0}\tWord 2{1}", result1, result2); | |
} | |
if (modFirst.Count < modSecond.Count) | |
{ | |
Console.WriteLine("The secondWord word wins!\nWord 1:{0}\tWord 2:{1}", result1, result2); | |
} | |
if (modFirst.Count == modSecond.Count) | |
{ | |
Console.WriteLine("Its a draw!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment