refoctoring to have state always as first argument ; fix end of game test
This commit is contained in:
parent
92bfa8e599
commit
1c7ce20bcb
2 changed files with 12 additions and 10 deletions
|
@ -49,8 +49,8 @@
|
|||
<script src="js/star.js"></script>
|
||||
<script>
|
||||
state = load_level( levels, 0 );
|
||||
start_loop( "#blackboard", state );
|
||||
$("#blackboard").focus();
|
||||
start_loop( state, "#blackboard" );
|
||||
$( document ).focus();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
18
js/star.js
18
js/star.js
|
@ -104,7 +104,7 @@ function load_level( levelset, nb ) {
|
|||
return state;
|
||||
}
|
||||
|
||||
function display_level( elt, state ) {
|
||||
function display_level( state, elt ) {
|
||||
$( elt ).html( format_level( state ) );
|
||||
}
|
||||
|
||||
|
@ -153,8 +153,8 @@ function make_a_move( state, where ) {
|
|||
return state;
|
||||
}
|
||||
|
||||
function start_loop( elt, state ) {
|
||||
display_level( elt, state );
|
||||
function start_loop( state, elt ) {
|
||||
display_level( state, elt );
|
||||
|
||||
$( document ).keydown( function( e ) {
|
||||
switch( e.keyCode ) {
|
||||
|
@ -187,13 +187,15 @@ function start_loop( elt, state ) {
|
|||
break;
|
||||
}
|
||||
|
||||
if ( won_or_not( state ) && state.level < levels.length - 1 ) {
|
||||
if ( won_or_not( state ) ) {
|
||||
if ( state.level < levels.length - 1 ) {
|
||||
state = load_level( levels, state.level + 1 )
|
||||
}
|
||||
else {
|
||||
alert( "You won!" );
|
||||
}
|
||||
else {
|
||||
alert( "You won!" );
|
||||
}
|
||||
}
|
||||
|
||||
display_level( elt, state );
|
||||
display_level( state, elt );
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue