xxxxxxxxxx
let wordList;
let guesses = 6;
let curr = "";
let gameState = 0;
let button;
let word;
function preload() {
wordList = loadStrings('words.txt');
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(230);
button = createButton('reset');
button.position(width / 2, height / 2 + 70);
button.mousePressed(reset);
reset();
}
function draw() {
}
function keyPressed() {
if (gameState >= 0) {
if (keyCode >= 65 && keyCode <= 90) {
if (curr.length < 5) curr = join([curr, char(keyCode)], "");
else alert("That's more than enough letters fool!");
} else if (keyCode == 8) { //Backspace
if (curr.length > 0) curr = curr.slice(0, -1);
else alert("You can't turn back time, you fool!");
} else if (keyCode == 13) { //Enter
if (curr.length == 5) {
compare(curr);
if (curr == word.toUpperCase()) {
gameOver(true);
return;
}
curr = "";
guesses--;
} else {
alert("Do you not know any 5 letter words?");
}
if (guesses == 0) {
gameOver(false);
}
}
}
}
function compare(curr) {
var out = [-1,-1,-1,-1,-1];
for (var i = 0; i < curr.length; i++) {
if (word[i] == curr[i]) {
out[i] = 1;
} else if (wc.includes(curr[i])) {
var l = wc.indexOf(curr[i]);
wc = wc.replace(curr[i], "");
console.log(wc);
out.push(0);
} else out.push(-1);
}
console.log(out);
}
function gameOver(win) {
background(230);
textSize(60);
if (win) {
text('YOU WIN!', width / 2.5, height / 2);
} else {
text('GAME OVER!', width / 2.5, height / 2);
}
gameState = -1;
button.show();
}
function reset() {
guesses = 6;
curr = "";
gameState = 0;
background(230);
button.hide();
word = random(wordList);
word = word.toUpperCase();
console.log(word);
}