Skip to content

@kevinwestern /gist:3229945
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#import('dart:html');
List<Element> remaining;
int gameInterval = null, position = 0, points = 0;
Element selectedEl = null, pointsTally = null;
void main() {
remaining = new List<Element>();
TableElement table = createTable(3, 4);
pointsTally = query("#points-tally");
document.body.insertAdjacentElement('afterbegin', table);
table.on.click.add(cellClick);
gameInterval = window.setInterval(play, 1000);
}
TableElement createTable(int rows, int cols) {
TableElement table = new TableElement();
TableRowElement row;
TableCellElement col;
for (int i = 0; i < rows; i++) {
row = new TableRowElement();
for (int j = 0; j < cols; j++) {
col = new TableCellElement();
row.insertAdjacentElement('beforeend', col);
remaining.add(col);
}
table.insertAdjacentElement('beforeend', row);
}
return table;
}
void cellClick (Event evt) {
Element target = evt.srcElement;
if (!target.classes.contains('highlight')) return;
selectedEl.classes.add('whacked');
remaining.removeRange(position, 1);
if (remaining.length === 0) window.clearInterval(gameInterval);
pointsTally.text = (points += 10).toString();
}
void play () {
position = (Math.random() * remaining.length).toInt();
if (selectedEl !== null) selectedEl.classes.remove('highlight');
selectedEl = remaining[position];
selectedEl.classes.add('highlight');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.