Extended Example
Example: Two-Player Board Game
•
At the start we create a representation of the board
functi on init ( table ) {
for ( j =0; j < board . le ngth ; j ++) {
var tr = do cument . cre a teE leme nt (" tr " );
table . app endC h ild ( tr );
for ( i =0; i < board [j ]. length ; x ++) {
var td = documen t . cre ateE lem ent (" td " );
var txt = docume nt . cr eat eTe xtNo de (
numT oLet ter ( board [j ][ i ]);
td . app endC hild ( txt );
td . ad dEv ent List ene r (" c lick " , play . bind ( null ,i ,j ));
tr . app endC hild ( td );
}
}
}
table = do cument . get Elem ent ByI d ( ' t1 ' );
init ( table );
•
play.bind makes sure that parameters x and y of play are bound to the
current values of i and j
COMP284 Scripting Languages Lecture 14 Slide L14 – 16