add n and p to move among levels ; end of game alert

This commit is contained in:
Gwenhael Le Moine 2011-07-04 16:51:03 +02:00
parent dfba38b71b
commit b8af3517d8

View file

@ -59,6 +59,7 @@ function count_gifts( state ) {
} }
return n; return n;
} }
function get_pos( state, actor ) { function get_pos( state, actor ) {
p = state.board.indexOf( actor, state.board ); p = state.board.indexOf( actor, state.board );
pos = {} pos = {}
@ -67,18 +68,22 @@ function get_pos( state, actor ) {
return pos; return pos;
} }
function get_cell( state, x, y ) { function get_cell( state, x, y ) {
return( state.board.charAt( x + ( y * LEVEL_WIDTH ) ) ); return( state.board.charAt( x + ( y * LEVEL_WIDTH ) ) );
} }
function set_cell( state, x, y, value ) { function set_cell( state, x, y, value ) {
p = x + ( y * LEVEL_WIDTH ); p = x + ( y * LEVEL_WIDTH );
state.board = state.board.substring( 0, p ) + value + state.board.substring( p+1, state.board.length ); state.board = state.board.substring( 0, p ) + value + state.board.substring( p+1, state.board.length );
return state; return state;
} }
function switch_actor( state ) { function switch_actor( state ) {
state.moving = ( state.moving == cell.BALL ) ? cell.CUBE : cell.BALL; state.moving = ( state.moving == cell.BALL ) ? cell.CUBE : cell.BALL;
return state; return state;
} }
function won_or_not( state ) { function won_or_not( state ) {
return count_gifts( state ) == 0; return count_gifts( state ) == 0;
} }
@ -98,6 +103,7 @@ function load_level( levelset, nb ) {
state.moving = cell.BALL; state.moving = cell.BALL;
return state; return state;
} }
function display_level( elt, state ) { function display_level( elt, state ) {
$( elt ).html( format_level( state ) ); $( elt ).html( format_level( state ) );
} }
@ -171,16 +177,29 @@ function start_loop( elt, state ) {
case 39: // RIGHT case 39: // RIGHT
state = make_a_move( state, direction.RIGHT ); state = make_a_move( state, direction.RIGHT );
break; break;
case 32: //SPACE case 32: // SPACE
state = switch_actor( state ); state = switch_actor( state );
break; break;
case 78: // n
if ( state.level < levels.length - 1 ) {
state = load_level( levels, state.level + 1 )
}
break;
case 80: // p
if ( state.level > 0 ) {
state = load_level( levels, state.level - 1 )
}
break;
default: default:
break; break;
} }
if ( won_or_not( state ) ) { if ( won_or_not( state ) && state.level < levels.length - 1 ) {
state = load_level( levels, state.level + 1 ) state = load_level( levels, state.level + 1 )
} }
else {
alert( "You won!" );
}
display_level( elt, state ); display_level( elt, state );
}); });