Skip to content

Instantly share code, notes, and snippets.

@lunasorcery
Last active December 12, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunasorcery/4733779 to your computer and use it in GitHub Desktop.
Save lunasorcery/4733779 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace LetterBloxBot {
class Program {
// WinAPI:
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
const uint KEYEVENTF_KEYUP = 0x0002;
// Timings (tune this to change difficulty)
const int TIME_DOWN = 1;
const int TIME_UP = 1;
const bool RANDOM = false; // make it try words in a random order, or not.
static void Main(string[] args) {
Console.Write("What are the 6 letters? ");
string letterPool = Console.ReadLine().Substring(0, 6).ToLower();
Console.WriteLine("Go click on the game window in the next 2 seconds.");
Thread.Sleep(2000);
List<string> strings = new List<string>();
for (int i = 6; i >= 3; i--) // iterate backwards so we get the high-scoring words first
strings.AddRange(Iterate(letterPool, i));
if (RANDOM) {
Random rand = new Random();
while (strings.Count > 0) {
int i = rand.Next(strings.Count);
HackLine(strings[i]);
strings.RemoveAt(i);
}
}
else {
for (int i = 0; i < strings.Count; i++) {
HackLine(strings[i]);
}
}
}
static string[] Iterate(string letterPool, int depth) {
List<string> words = new List<string>();
int perm;
int[] indices = new int[depth];
for (perm = 0; perm < Math.Pow(letterPool.Length, depth); perm++) {
// build the indices array
int temp = perm;
for (int i = 0; i < depth; i++) {
indices[i] = temp % letterPool.Length;
temp /= letterPool.Length;
}
// check if there are duplicate letters
bool skip = false;
for (int i = 0; i < depth; i++) {
for (int j = i + 1; j < depth; j++) {
if (indices[i] == indices[j])
skip = true;
}
}
if (skip)
continue;
// construct the string
string s = "";
for (int i = 0; i < depth; i++)
s += letterPool[indices[depth - 1 - i]];
// check to avoid redundancy, and add to the list
if (!words.Contains(s))
words.Add(s);
}
return words.ToArray();
}
// print to console and use WinApi to convey it
static void HackLine(string message) {
Console.WriteLine(message);
message += "\n";
for (int i = 0; i < message.Length; i++) {
int keycode = -1;
if (message[i] >= 'a' && message[i] <= 'z')
keycode = (byte)(0x41 + message[i] - 'a');
else if (message[i] == '\n')
keycode = 0x0D;
if (keycode >= 0) {
keybd_event((byte)keycode, 0, 0, UIntPtr.Zero); // KEY_DOWN
Thread.Sleep(TIME_DOWN);
keybd_event((byte)keycode, 0, KEYEVENTF_KEYUP, UIntPtr.Zero); // KEY_UP
Thread.Sleep(TIME_UP);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment