Last active
May 10, 2016 10:28
-
-
Save quangnle/bc82d7c54bf94fb97d89b0718d4e7e91 to your computer and use it in GitHub Desktop.
This file contains 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.IO; | |
using System.Linq; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace Tuenti5 | |
{ | |
// | |
class Program | |
{ | |
static Dictionary<int, List<string>> _dict = new Dictionary<int, List<string>>(); | |
static void BuildDictionary() | |
{ | |
var words = File.ReadAllLines("words.txt"); | |
foreach (var word in words) | |
{ | |
var len = word.Length; | |
if (!_dict.ContainsKey(len)) | |
_dict.Add(len, new List<string>()); | |
_dict[len].Add(word); | |
} | |
} | |
static string ExtractPattern(string st) | |
{ | |
MatchCollection mc = Regex.Matches(st, "\n\n(.*)\n\n"); | |
var r = mc[0].Groups[1].Value.Replace(" ", ""); | |
return r; | |
} | |
static bool IsMatch(string input, string word) | |
{ | |
for (int i = 0; i < word.Length; i++) | |
{ | |
if (input[i] != '_') | |
{ | |
if (input[i].ToString().ToUpper() != word[i].ToString().ToUpper()) return false; | |
} | |
else | |
{ | |
if (input.ToUpper().IndexOf(word[i].ToString().ToUpper()) > 0) return false; | |
} | |
} | |
return true; | |
} | |
static List<string> MatchList(string pattern, List<string> lst) | |
{ | |
var l = lst.Where(w => IsMatch(pattern, w)).ToList(); | |
return l; | |
} | |
static List<string> FilterList(string c, List<string> lst) | |
{ | |
var l = lst.Where(w => w.ToUpper().IndexOf(c.ToUpper()) < 0).ToList(); | |
return l; | |
} | |
static List<KeyValuePair<char, int>> AppearanceCount(List<string> lst) | |
{ | |
Dictionary<char, int> app = new Dictionary<char, int>(); | |
foreach (var item in lst) | |
{ | |
for (int i = 0; i < item.Length; i++) | |
{ | |
var ch = item[i]; | |
if (!app.ContainsKey(ch)) | |
app.Add(ch, 0); | |
app[ch] += 1; | |
} | |
} | |
var result = app.ToList().OrderByDescending(i => i.Value).ToList(); | |
return result; | |
} | |
public static void Main() | |
{ | |
BuildDictionary(); | |
//var cc = _dict[4]; | |
//var rrr = MatchList("_EEP", cc); | |
////rrr = FilterList("S", rrr); | |
////var rrrr = AppearanceCount(rrr); | |
byte[] data = new byte[1024]; | |
TcpClient server; | |
try | |
{ | |
server = new TcpClient("52.49.91.111", 9988); | |
} | |
catch (SocketException) | |
{ | |
Console.WriteLine("Unable to connect to server"); | |
return; | |
} | |
NetworkStream ns = server.GetStream(); | |
int recv = ns.Read(data, 0, data.Length); | |
var stringData = Encoding.ASCII.GetString(data, 0, recv); | |
Console.WriteLine(stringData); | |
List<string> cat = null; | |
List<KeyValuePair<char,int>> selectionList = null; | |
var pattern = ""; | |
var historyInput = ""; | |
var input = ""; | |
while (true) | |
{ | |
if (pattern == "") | |
input = "\n"; | |
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length); | |
ns.Flush(); | |
data = new byte[1024]; | |
recv = ns.Read(data, 0, data.Length); | |
stringData = Encoding.ASCII.GetString(data, 0, recv); | |
if (stringData.IndexOf("GAME OVER") >= 0) | |
break; | |
var newPattern = ExtractPattern(stringData); | |
if (newPattern == "Welldone!") | |
{ | |
pattern = ""; | |
historyInput = ""; | |
cat = null; | |
continue; | |
} | |
if (cat == null) | |
{ | |
cat = _dict[newPattern.Length]; | |
} | |
else | |
{ | |
if (newPattern != pattern) | |
cat = MatchList(newPattern, cat); | |
else | |
cat = FilterList(input, cat); | |
} | |
selectionList = AppearanceCount(cat); | |
pattern = newPattern; | |
if (cat != null && cat.Count > 0) | |
{ | |
var idx = 0; | |
do | |
{ | |
if (historyInput.IndexOf(selectionList[idx].Key) < 0) break; | |
idx++; | |
} while (idx < selectionList.Count); | |
input = selectionList[idx].Key.ToString(); | |
historyInput += input; | |
} | |
Console.WriteLine(pattern); | |
Console.WriteLine(historyInput); | |
} | |
Console.Read(); | |
ns.Close(); | |
server.Close(); | |
} | |
private static List<string> RemoveWords(string input, List<string> cat) | |
{ | |
cat = cat.Where(w => w.IndexOf(input.ToUpper()) < 0).ToList(); | |
return cat; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment