1
0
Fork 0

Initial support for generating random puzzles, without any difficulty support.

feature/save-game
Daniel Perelman 5 years ago
parent 0855f7d0bc
commit 2d787ac74a

File diff suppressed because it is too large Load Diff

@ -182,7 +182,12 @@
}
function loaded() {
new MastermindUI(new Mastermind('hello', 'heloakcmtq'.split('')));
dictionaryPromise.then(_ =>
topWordsPromise
.then(_ => new MastermindUI(
//new Mastermind('hello', 'heloakcmtq'.split(''))
Mastermind.generateRandom()
)));
}
</script>
<template id="letter_entry">

@ -1,5 +1,12 @@
'use strict';
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function getRandomLetter() {
return (10 + getRandomInt(26)).toString(36);
}
const LetterGuessResult = Object.freeze({
MATCH: Symbol("match"),
UNUSED: Symbol("unused"),
@ -16,10 +23,36 @@ class Mastermind {
constructor(word, availableLetters) {
this.word = word;
this.numLetters = word.length;
this.availableLetters = availableLetters.sort();
this.availableLetters = word.split('');
for (var i = 0; i < availableLetters.length; i++) {
this.availableLetters = this.availableLetters
.concat(availableLetters[i].split(''));
}
this.availableLetters = [...new Set(this.availableLetters)].sort();
this.guesses = new Array();
}
static generateRandom() {
var regExp = new RegExp('^[a-z]{4,7}$');
var word;
do {
word = topWords[1000+getRandomInt(30000)].word;
} while (!regExp.test(word) || !isValidWord(word));
var availableLetters = new Set(word.split(''));
for (var i = 0; i < 10; i++) {
var newLetter;
do {
newLetter = getRandomLetter();
} while (availableLetters.has(newLetter));
availableLetters.add(newLetter);
}
return new Mastermind(word, [...availableLetters]);
}
isValidGuess(guess) {
var availableLetters = this.availableLetters;
return guess.length == this.numLetters

@ -0,0 +1 @@
../../../Typo.js/typo/dictionaries/en_US/en_US.aff

@ -0,0 +1 @@
../../../Typo.js/typo/dictionaries/en_US/en_US.dic

@ -0,0 +1 @@
../Typo.js/typo/typo.js

@ -1,12 +1,24 @@
var dictionary;
var topWords;
fetch('typo/dictionaries/en_US/en_US.aff')
const dictionaryPromise = fetch('typo/dictionaries/en_US/en_US.aff')
.then(response => response.text())
.then(aff => fetch('typo/dictionaries/en_US/en_US.dic')
.then(response => response.text())
.then(dic => {
dictionary = new Typo('en_US', aff, dic);
}));
}))
.catch(error => console.log(error));
const topWordsPromise = fetch('en_50k.txt')
.then(response => response.text())
.then(text => {
topWords = text.split('\n').map(line => {
var arr = line.split(' ');
return {'word': arr[0], 'frequency': Number(arr[1])};
});
})
.catch(error => console.log(error));
function isValidWord(str) {
return dictionary.check(str);

Loading…
Cancel
Save