public
Created

  • Download Gist
gistfile1.dart
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 
#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');
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.