teacherGames/mj/main.js

91 lines
2.0 KiB
JavaScript
Raw Normal View History

import Card from './card.js'
document.body.innerText = "";
const vocabulary = [
["drive", "drove"],
["send", "sent"],
["understand", "understood"],
["find", "found"],
["write", "wrote"],
["feel", "felt"],
["fall", "fell"],
["run", "ran"],
["do", "did"],
["speak", "spoke"],
["awake", "awoke"],
["bear", "bore"],
["bend", "bent"],
["bite", "bit"],
["cling", "clung"],
["breed", "bred"],
["feed", "fed"]
];
let deck = [];
let selected = null;
Card.setBounds(0, 0, window.innerWidth, window.innerHeight)
for (let i = 0; i < 50; ++i) {
let id = Math.floor(Math.random() * vocabulary.length);
let set = vocabulary[id];
for (let s = 0; s < set.length; ++s) {
let card = new Card(set[s], onCardClick, id, s);
card.disrotate();
card.displace();
deck.push(card);
}
}
deck = shuffle(deck);
for (let i = 0; i < deck.length; ++i) {
document.body.appendChild(deck[i].element);
}
function onCardClick(card) {
if (selected != null) {
if (card === selected) {
return;
}
if (selected.setId === card.setId && selected.cardId !== card.cardId) {
score(selected, card);
selected = null;
return;
}
selected.unselect();
}
selected = card;
card.select();
}
function score(c1, c2) {
deck.splice(deck.indexOf(c1), 1)
deck.splice(deck.indexOf(c2), 1);
let x = window.innerWidth / 2
let y = -500;
c1.position(x, y);
c2.position(x, y);
c1.rotate(0);
c2.rotate(0);
c1.element.style.zIndex = "5";
c2.element.style.zIndex = "5";
setTimeout(fade.bind(null, c1, c2), 200);
}
function fade(c1, c2) {
c1.destructor();
c2.destructor();
}
function shuffle(deck) {
let spare = deck.slice();
let result = [];
while (spare.length > 0) {
let index = Math.floor(Math.random() * spare.length);
result.push(spare.splice(index, 1)[0]);
}
return result;
}