Skip to content

Commit

Permalink
Automatic coloring based on a given answer.
Browse files Browse the repository at this point in the history
  • Loading branch information
dclamage committed Nov 23, 2022
1 parent 48c5849 commit caf4274
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 178 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ <h3 class="mini-header">Choose Your Bot</h3>
<p class="extra-settings"></p>
</div>

<div id="known-answer-div">
<input type="text" id="word-known-answer">
</div>

<div id="guesses">
<button id="refresh" onclick="resetPage();"><i class='fa fa-refresh'></i></button>
<input type="text" id = "word-entered">
Expand Down
129 changes: 76 additions & 53 deletions js/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const WARMLE = 'Warmle';
class Bot {
constructor(type) {
this.type = type;
this.answer = '';
}

setAnswer(answer) {
this.answer = answer;
}

isFor(type) {
Expand Down Expand Up @@ -47,16 +52,16 @@ class Bot {
getDifference(word1, word2) {
if (this.type == WOODLE) {
return differencesWithoutPositions(word1, word2);
}
}

if (this.type == PEAKS) {
return getAlphabeticDifferences(word1, word2);
}
}

if (typeof word2 == 'object') {
return getDoubleDifference(word1, word2);
}
}

if (this.type == WARMLE) {
return getWarmleDifferences(word1, word2);
}
Expand Down Expand Up @@ -87,7 +92,7 @@ class Bot {
getBestLetters(list) {
if (this.type == PEAKS) {
return lettersClosestToCenter(list);
}
}

if (this.type == WARMLE) {
return bestWarmleLetters(list);
Expand All @@ -108,19 +113,19 @@ class Bot {
if (reduced_filter) {
return getAntiWordleDiffs(difference, guess);
}

if (this.type == XORDLE) {
return getXordleDiffs(difference, 0, [difference]);
}
}

if (this.type == FIBBLE) {
return getFibbleDiffs(difference);
}

if (this.type == HARDLE) {
return getHardleDiffs(difference);
}
}

if (this.getCount() > 1) {
return difference;
}
Expand All @@ -145,7 +150,7 @@ class Bot {

getAllPossibleAnswersFrom(list) {
list = filterList(list, 0, 0, bot.getCount() > 1);

if (bot.isFor(XORDLE)) {
list = xordleFilter(uniqueWordsFrom(list));
}
Expand All @@ -156,8 +161,8 @@ class Bot {
getAnswerListLength(answers) {
if (bot.getCount() > 1) {
return lengthOfAllLists(answers);
}
}

return answers.length;
}

Expand All @@ -182,13 +187,31 @@ function isLower(a, b) {
function tilesChangeColor(row) {
let tiles = row.getElementsByClassName('tile');

let colors = [];
if (bot.answer && bot.answer.length == tiles.length) {
let guess = '';
for (let tile of tiles) {
guess += tile.textContent;
}
colors = bot.getDifference(guess, bot.answer);
for (let i = 0; i < colors.length; i++) {
setTileColor(tiles[i], colors[i]);
}
}

Array.from(tiles).forEach(function(t) {
t.addEventListener('click', function() {
changeTileColor(t);
});
t.addEventListener('click', function() {
changeTileColor(t);
});
});
}

function setTileColor(tile, new_color) {
let old_color = getTileColor(tile);
if (old_color != new_color) {
tile.classList.replace(old_color, new_color);
}
}

function changeTileColor(tile) {
let old_color = getTileColor(tile);
Expand All @@ -208,14 +231,14 @@ function differencesWithPositions(word1, word2) {
if (pairings[word1]) {
if (pairings[word1][word2]) return pairings[word1][word2];
} else pairings[word1] = [];


let temp1 = word1;
let temp2 = word2;
let diff = EMPTY.repeat(word_length);
let pos = 0;

for (let j = 0; j < temp1.length; j++) {
for (let j = 0; j < temp1.length; j++) {
let word1_c = temp1.charAt(j);
let word2_c = temp2.charAt(j);

Expand Down Expand Up @@ -273,7 +296,7 @@ function getDoubleDifference(guess, answers) {
}

function dordleDifference(guess, answers) {
return [differencesWithPositions(guess, answers.word1),
return [differencesWithPositions(guess, answers.word1),
differencesWithPositions(guess, answers.word2)];
}

Expand Down Expand Up @@ -302,7 +325,7 @@ function rowDifferencesWithPairs(row_number) {

colors.push(coloring);
}

return colors;
}

Expand Down Expand Up @@ -394,14 +417,14 @@ function differencesWithoutPositions(word1, word2) {

for (let j = 0; j < temp1.length; j++) {
if (num_wrong == 0) break;

let word1_c = temp1.charAt(j);
let word2_c = temp2.charAt(j);

if (word1_c == word2_c) {
correct += CORRECT;
num_wrong--;

temp1 = temp1.slice(0, j) + temp1.slice(j+1);
temp2 = temp2.slice(0, j) + temp2.slice(j+1);
j--;
Expand Down Expand Up @@ -498,14 +521,14 @@ function bestWarmleLetters(list) {
new_letters[pos][j] = letters[pos][j];

let distance = document.getElementsByClassName('warmle-selector')[0].value;
for (let k = 1; k <= distance; k++) {
for (let k = 1; k <= distance; k++) {
let c = charToInt(pos)+k;

if (c <= 90) {
c = intToChar(c);
new_letters[pos][j] += letters[c][j];
}

c = charToInt(pos)-k;
if (c >= 65) {
c = intToChar(c);
Expand All @@ -523,15 +546,15 @@ function bestWarmleLetters(list) {

function makeAlphabetArray(size) {
let letters = [];

for (let i = 65; i <= 90; i++) {
let c = String.fromCharCode(i);

letters[c] = [];
for (let i = 0; i < size; i++) {
letters[c].push(0);
}
}
}

return letters;
}
Expand All @@ -547,18 +570,18 @@ function reducesListMost(answers, guesses, future_guess) {
let data_per_list = [];

for (let j = 0; j < answers.length; j++) {

min = answers[j].length;
data_per_list.push(calculateAverageBucketSize(guesses[i], answers[j], min, future_guess));
}

data = averageBucketSizeFromAllLists(data_per_list);
} else {
data = calculateAverageBucketSize(guesses[i], answers, min, future_guess);
}

if (!data) continue;

min = Math.min(min, data.adjusted);
best_words.push({word: guesses[i], average: data.adjusted, differences: data.differences, wrong: 0});

Expand Down Expand Up @@ -604,7 +627,7 @@ function reducesListLeast(answers, guesses) {
}

best_words = sortListByAverage(best_words);
return best_words;
return best_words;
}

function calculateAverageBucketSize(guess, answers, min, future_guess) {
Expand All @@ -614,7 +637,7 @@ function calculateAverageBucketSize(guess, answers, min, future_guess) {
let threes = 1;

for (let i = 0; i < list_size; i++) {
let diff = bot.getDifference(guess, answers[i]);
let diff = bot.getDifference(guess, answers[i]);

if (differences[diff] == null) {
differences[diff] = [];
Expand All @@ -625,7 +648,7 @@ function calculateAverageBucketSize(guess, answers, min, future_guess) {
}

let freq = differences[diff].length;

if (freq > 0) {
weighted += (freq/list_size)*freq - ((freq-1)/list_size)*(freq-1);
if (freq > 1) {
Expand All @@ -650,7 +673,7 @@ function getXordleDiffs(difference, index, diff_list) {

diff_list.push(alt);
getXordleDiffs(alt, index+1, diff_list);
}
}

return getXordleDiffs(difference, index+1, diff_list);
}
Expand Down Expand Up @@ -685,7 +708,7 @@ function getHardleDiffs(diff) {
if (diff == WRONG_SPOT.repeat(word_length)) {
return differences;
}

for (let i = 0; i < diff.length; i++) {
if (diff.charAt(i) == CORRECT) {
new_diff += WRONG_SPOT;
Expand All @@ -701,7 +724,7 @@ function getHardleDiffs(diff) {
function getAntiWordleDiffs(diff, guess) {
let wrong_letters = findWrongSpotLetters(diff, guess);
let differences = antiRecursion(guess, diff, wrong_letters, [], 0);

includesAllWrongSpots(differences, wrong_letters, guess);

return differences;
Expand All @@ -711,26 +734,26 @@ function includesAllWrongSpots(differences, wrong_letters, word) {
if (!wrong_letters.length) return differences;

outer:
for (let i = 0; i < differences.length; i++) {
let check_list = [];
for (let i = 0; i < differences.length; i++) {
let check_list = [];

for (let j = 0; j < word_length; j++) {
if (differences[i].charAt(j) != INCORRECT) {
let c = word.charAt(j);
for (let j = 0; j < word_length; j++) {
if (differences[i].charAt(j) != INCORRECT) {
let c = word.charAt(j);

if (!check_list.includes(c)) {
check_list.push(c);
if (!check_list.includes(c)) {
check_list.push(c);

if (check_list.length == wrong_letters.length) {
continue outer;
if (check_list.length == wrong_letters.length) {
continue outer;
}
}
}
}

differences.splice(i, 1);
i--;
}

differences.splice(i, 1);
i--;
}
}

function antiRecursion(word, difference, wrong_letters, diff_list, i) {
Expand All @@ -739,7 +762,7 @@ function antiRecursion(word, difference, wrong_letters, diff_list, i) {
if (i == word_length) {
return [...new Set(diff_list)];
}

if (wrong_letters.includes(word.charAt(i)) && difference.charAt(i) != CORRECT) {
antiRecursion(word, replaceAt(difference, CORRECT, i), wrong_letters, diff_list, i+1);

Expand Down
Loading

0 comments on commit caf4274

Please sign in to comment.