Skip to content

Instantly share code, notes, and snippets.

@kevinwestern
Created August 1, 2012 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinwestern/3229945 to your computer and use it in GitHub Desktop.
Save kevinwestern/3229945 to your computer and use it in GitHub Desktop.
#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