change demo url
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
Similar Words Game.
|
Similar Words Game.
|
||||||
============
|
============
|
||||||
A demo is on http://w.r0xy.it/
|
A demo is on https://similarwords.31337.it/
|
||||||
|
|
||||||
|
|
||||||
To create your own wordlist just edit ```createwordlist.js``` and run it with ```node```.
|
To create your own wordlist just edit ```createwordlist.js``` and run it with ```node```.
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
if (typeof(sw) == 'undefined') {
|
if (typeof (sw) == 'undefined') {
|
||||||
var sw = {};
|
var sw = {};
|
||||||
sw.apiPrefix = '/';
|
sw.apiPrefix = '/';
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ if (typeof(sw) == 'undefined') {
|
|||||||
(function($) {
|
(function($) {
|
||||||
|
|
||||||
|
|
||||||
//helper functions
|
// helper functions
|
||||||
var shuffle = function(myArray) {
|
var shuffle = function(myArray) {
|
||||||
var i = myArray.length, j, tempi, tempj;
|
var i = myArray.length, j, tempi, tempj;
|
||||||
if (i === 0)
|
if (i === 0)
|
||||||
@@ -25,12 +25,13 @@ if (typeof(sw) == 'undefined') {
|
|||||||
|
|
||||||
var getKey = function(obj) {
|
var getKey = function(obj) {
|
||||||
var word = [];
|
var word = [];
|
||||||
for(var k in obj) word.push(k);
|
for ( var k in obj)
|
||||||
|
word.push(k);
|
||||||
return word[0];
|
return word[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
var selectWordsForGame = function() {
|
var selectWordsForGame = function() {
|
||||||
//select a random word from the list
|
// select a random word from the list
|
||||||
var words = sw.words[Math.round(Math.random() * sw.words.length)];
|
var words = sw.words[Math.round(Math.random() * sw.words.length)];
|
||||||
|
|
||||||
var word = getKey(words);
|
var word = getKey(words);
|
||||||
@@ -40,17 +41,20 @@ if (typeof(sw) == 'undefined') {
|
|||||||
similar.push(word);
|
similar.push(word);
|
||||||
similar = shuffle(similar);
|
similar = shuffle(similar);
|
||||||
|
|
||||||
return {word: word, similar: similar};
|
return {
|
||||||
|
word: word,
|
||||||
|
similar: similar
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
var startGame = function(word) {
|
var startGame = function(word) {
|
||||||
if(typeof(word) == 'undefined') {
|
if (typeof (word) == 'undefined') {
|
||||||
word = selectWordsForGame();
|
word = selectWordsForGame();
|
||||||
}
|
}
|
||||||
sw.currentWord = word;
|
sw.currentWord = word;
|
||||||
$("#main").empty().html('views/index.ejs', word);
|
$("#main").empty().html('views/index.ejs', word);
|
||||||
$('.currentword').animate({
|
$('.currentword').animate({
|
||||||
opacity : 0
|
opacity: 0
|
||||||
}, 1000, 'linear');
|
}, 1000, 'linear');
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -59,15 +63,20 @@ if (typeof(sw) == 'undefined') {
|
|||||||
|
|
||||||
if (sw.finaltime > 33) {
|
if (sw.finaltime > 33) {
|
||||||
result = 'You can do better!';
|
result = 'You can do better!';
|
||||||
};
|
}
|
||||||
if (sw.finaltime > 36) {
|
if (sw.finaltime > 36) {
|
||||||
result = 'meh...';
|
result = 'meh...';
|
||||||
};
|
}
|
||||||
if (sw.wrongWords > 0 && sw.finaltime <= 32) {
|
if (sw.wrongWords > 0 && sw.finaltime <= 32) {
|
||||||
result = 'You are fast, but you made errors';
|
result = 'You are fast, but you made errors';
|
||||||
};
|
}
|
||||||
|
|
||||||
$("#main").empty().html('views/gameover.ejs', {result: result, time: sw.finaltime, wrong: sw.wrongWords, correct: sw.correctWords});
|
$("#main").empty().html('views/gameover.ejs', {
|
||||||
|
result: result,
|
||||||
|
time: sw.finaltime,
|
||||||
|
wrong: sw.wrongWords,
|
||||||
|
correct: sw.correctWords
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var startNewGame = function() {
|
var startNewGame = function() {
|
||||||
@@ -83,7 +92,7 @@ if (typeof(sw) == 'undefined') {
|
|||||||
$('.num-of-words').html(sw.correctWords + (sw.correctWords == 1 ? ' word' : ' words'));
|
$('.num-of-words').html(sw.correctWords + (sw.correctWords == 1 ? ' word' : ' words'));
|
||||||
$('.time-words').html(Math.round((new Date()).getTime() / 1000) - sw.time);
|
$('.time-words').html(Math.round((new Date()).getTime() / 1000) - sw.time);
|
||||||
|
|
||||||
if(playforrounds >= sw.rounds) {
|
if (playforrounds >= sw.rounds) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
updateBadges();
|
updateBadges();
|
||||||
}, 100);
|
}, 100);
|
||||||
@@ -109,7 +118,7 @@ if (typeof(sw) == 'undefined') {
|
|||||||
start.removeClass('disabled');
|
start.removeClass('disabled');
|
||||||
start.html(' Start ');
|
start.html(' Start ');
|
||||||
start.animate({
|
start.animate({
|
||||||
opacity : 1
|
opacity: 1
|
||||||
}, 1000, 'linear');
|
}, 1000, 'linear');
|
||||||
|
|
||||||
sw.words = words.words;
|
sw.words = words.words;
|
||||||
@@ -122,7 +131,7 @@ if (typeof(sw) == 'undefined') {
|
|||||||
return w;
|
return w;
|
||||||
};
|
};
|
||||||
|
|
||||||
//events
|
// events
|
||||||
$(document).on('click', '.dict', function(ev) {
|
$(document).on('click', '.dict', function(ev) {
|
||||||
var el = $(ev.currentTarget);
|
var el = $(ev.currentTarget);
|
||||||
var dict = el.attr('data-dict');
|
var dict = el.attr('data-dict');
|
||||||
@@ -142,8 +151,8 @@ if (typeof(sw) == 'undefined') {
|
|||||||
}
|
}
|
||||||
$('.word-button').addClass('disabled');
|
$('.word-button').addClass('disabled');
|
||||||
|
|
||||||
var wrongWord = undefined;
|
var wrongWord;
|
||||||
if(sw.currentWord.word == el.html()) {
|
if (sw.currentWord.word == el.html()) {
|
||||||
sw.correctWords = sw.correctWords + 1;
|
sw.correctWords = sw.correctWords + 1;
|
||||||
sw.rounds += 1;
|
sw.rounds += 1;
|
||||||
el.addClass('btn-success');
|
el.addClass('btn-success');
|
||||||
@@ -154,7 +163,7 @@ if (typeof(sw) == 'undefined') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
if(sw.gameover) {
|
if (sw.gameover) {
|
||||||
gameOver();
|
gameOver();
|
||||||
} else {
|
} else {
|
||||||
startGame(wrongWord);
|
startGame(wrongWord);
|
||||||
@@ -163,7 +172,7 @@ if (typeof(sw) == 'undefined') {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
//globals
|
// globals
|
||||||
sw.words = [];
|
sw.words = [];
|
||||||
sw.correctWords = 0;
|
sw.correctWords = 0;
|
||||||
sw.wrongWords = 0;
|
sw.wrongWords = 0;
|
||||||
@@ -171,18 +180,18 @@ if (typeof(sw) == 'undefined') {
|
|||||||
sw.time = 0;
|
sw.time = 0;
|
||||||
sw.finaltime = 0;
|
sw.finaltime = 0;
|
||||||
sw.gameover = false;
|
sw.gameover = false;
|
||||||
sw.currentWord;
|
sw.currentWord = '';
|
||||||
})(jQuery);
|
})(jQuery);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//utils
|
// utils
|
||||||
(function() {
|
(function() {
|
||||||
window.Utils = {};
|
window.Utils = {};
|
||||||
|
|
||||||
Utils.ellipsis = function(text, maxLength){
|
Utils.ellipsis = function(text, maxLength) {
|
||||||
if(typeof maxLength === 'undefined'){
|
if (typeof maxLength === 'undefined') {
|
||||||
maxLength = 9000; //a large number
|
maxLength = 9000; // a large number
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.length <= maxLength)
|
if (text.length <= maxLength)
|
||||||
@@ -197,4 +206,3 @@ if (typeof(sw) == 'undefined') {
|
|||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
fs = require('fs');
|
fs = require('fs');
|
||||||
|
|
||||||
//var dict = '/usr/share/dict/american-english';
|
// var dict = '/usr/share/dict/american-english';
|
||||||
var dict = '/usr/share/dict/italian';
|
var dict = '/usr/share/dict/italian';
|
||||||
//var dict = '/usr/share/dict/ngerman';
|
// var dict = '/usr/share/dict/ngerman';
|
||||||
|
|
||||||
fs.readFile(dict, 'utf8', function(err, data) {
|
fs.readFile(dict, 'utf8', function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -14,12 +14,13 @@ fs.readFile(dict, 'utf8', function(err, data) {
|
|||||||
|
|
||||||
var word = '';
|
var word = '';
|
||||||
var words = [];
|
var words = [];
|
||||||
for(var i=0; i<lines.length; i++) {
|
for ( var i = 0; i < lines.length; i++) {
|
||||||
word = lines[i];
|
word = lines[i];
|
||||||
if (word.length > 6 && word.length < 10) {
|
if (word.length > 6 && word.length < 10) {
|
||||||
//console.log('using ' + word);
|
// console.log('using ' + word);
|
||||||
words.push(word);
|
words.push(word);
|
||||||
};
|
}
|
||||||
|
;
|
||||||
}
|
}
|
||||||
console.log('using ' + words.length + ' words out of the dictionary');
|
console.log('using ' + words.length + ' words out of the dictionary');
|
||||||
|
|
||||||
@@ -27,39 +28,39 @@ fs.readFile(dict, 'utf8', function(err, data) {
|
|||||||
|
|
||||||
var wildcardWord = function(word) {
|
var wildcardWord = function(word) {
|
||||||
var words = [];
|
var words = [];
|
||||||
for(var i=1; i<word.length; i++) {
|
for ( var i = 1; i < word.length; i++) {
|
||||||
words.push(word.substr(0, i) + '.' + word.substr(i + 1));
|
words.push(word.substr(0, i) + '.' + word.substr(i + 1));
|
||||||
}
|
}
|
||||||
return words;
|
return words;
|
||||||
};
|
};
|
||||||
|
|
||||||
var matchingWordlist = [];
|
var matchingWordlist = [];
|
||||||
for(var w=0; w<words.length; w++) {
|
for ( var w = 0; w < words.length; w++) {
|
||||||
var word = words[w];
|
var word = words[w];
|
||||||
var wildcardedWords = wildcardWord(word);
|
var wildcardedWords = wildcardWord(word);
|
||||||
var matches = [];
|
var matches = [];
|
||||||
|
|
||||||
for(var i=0; i<wildcardedWords.length; i++) {
|
for ( var i = 0; i < wildcardedWords.length; i++) {
|
||||||
wildcardedWord = wildcardedWords[i];
|
wildcardedWord = wildcardedWords[i];
|
||||||
for(var j=0; j<words.length; j++) {
|
for ( var j = 0; j < words.length; j++) {
|
||||||
//console.log('match ' + words[j] + ' with ' + wildcardedWord);
|
// console.log('match ' + words[j] + ' with ' + wildcardedWord);
|
||||||
|
|
||||||
if(word.length == words[j].length && words[j].match(wildcardedWord) && word != words[j]) {
|
if (word.length == words[j].length && words[j].match(wildcardedWord) && word != words[j]) {
|
||||||
matches.push(words[j]);
|
matches.push(words[j]);
|
||||||
//console.log(word + ' -> ' + words[j]);
|
// console.log(word + ' -> ' + words[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//add to dictionary?
|
// add to dictionary?
|
||||||
if(matches.length >= 4) {
|
if (matches.length >= 6) {
|
||||||
console.log('using ' + word);
|
console.log('using ' + word);
|
||||||
var mw = {};
|
var mw = {};
|
||||||
mw[word] = matches;
|
mw[word] = matches;
|
||||||
matchingWordlist.push(mw);
|
matchingWordlist.push(mw);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(w % 5000 == 0) {
|
if (w % 5000 == 0) {
|
||||||
console.log('intermediary results...');
|
console.log('intermediary results...');
|
||||||
console.log(matchingWordlist);
|
console.log(matchingWordlist);
|
||||||
}
|
}
|
||||||
@@ -67,6 +68,8 @@ fs.readFile(dict, 'utf8', function(err, data) {
|
|||||||
|
|
||||||
|
|
||||||
console.log('final result');
|
console.log('final result');
|
||||||
console.log(JSON.stringify(matchingWordlist));
|
console.log(JSON.stringify({
|
||||||
|
words: matchingWordlist
|
||||||
|
}));
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user