Created
August 1, 2012 19:27
-
-
Save kevinwestern/3229945 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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