2011-07-04 16:11:42 +02:00
|
|
|
LEVEL_HEIGHT = 9;
|
|
|
|
LEVEL_WIDTH = 16;
|
|
|
|
|
|
|
|
cell= {
|
|
|
|
WALL: '#',
|
|
|
|
BALL: '@',
|
|
|
|
CUBE: 'H',
|
|
|
|
VOID: ' ',
|
2011-07-04 21:10:44 +02:00
|
|
|
GIFT: 'x'
|
|
|
|
};
|
2011-07-04 16:11:42 +02:00
|
|
|
direction = {
|
|
|
|
UP: 'u',
|
|
|
|
DOWN: 'd',
|
|
|
|
LEFT: 'l',
|
2011-07-04 21:10:44 +02:00
|
|
|
RIGHT: 'r'
|
|
|
|
};
|
2011-07-04 16:11:42 +02:00
|
|
|
options = {
|
2011-07-04 21:10:44 +02:00
|
|
|
starting_level: 0
|
|
|
|
};
|
2011-07-04 16:11:42 +02:00
|
|
|
state = {
|
|
|
|
moving: cell.BALL,
|
|
|
|
moves: 0,
|
2011-07-04 16:40:15 +02:00
|
|
|
level: 0,
|
|
|
|
board: ""
|
2011-07-04 21:10:44 +02:00
|
|
|
};
|
2011-07-04 16:11:42 +02:00
|
|
|
|
|
|
|
levels = [ "#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################",
|
|
|
|
" # # # # # ### x @# #x #x x # # x x # # # x # # #H# x # # # # #xx## # # # # ",
|
|
|
|
"################# x#@## ## ##H## #x x ## x x## x## #x x x# x### ##x #x x x####x ##x #################",
|
|
|
|
"################# #H## # ###x#x x#x#x#x#x## # #x x# # # ####x#x#x x#x#x#x## # ## # #@ #################",
|
|
|
|
" ############## #@ # # # ## #x # x x # ### # # ##x #x# #### # x # ##x# # # # #H## # x# #x# ############## ",
|
|
|
|
" ############ # x #x x# # x # ## # x ##@ x ### x # ### x # ##H # x ##x #################",
|
|
|
|
"################# # ## ### #x ##x# #x #x # # # # # # ### ## ## # #x# #x# # ## @#x H #x#################",
|
|
|
|
"############### # x## ### #x ## x ## x## # #x ### ## #x# ### # x#x ##xHx# x #@# ### # ###############",
|
|
|
|
" # ########### #x#x # @##x x# x # # # x## x# ## #x #xHx x## x## # #x#x # # # ############ ",
|
|
|
|
" ########### #### x ## H ###x x# x## x #x #x # # # x # x##x#x # x# #@# #x ### ### # # # # ######### # #",
|
|
|
|
"################# # @## #xx xx #### x ## x##x #x#xx ##### ## ## ##x x# x H x###x### # ## ## ########### ",
|
|
|
|
"## ## #### #@#####x ### x### xx x ## ## ##x #x# ## # x ###x ## ## ## ## #H# ## x ## x #################",
|
|
|
|
" ############## # @# x ### # #x x## ## x # ## x #x## # x ### x x #x##H # x # # # ############## ",
|
|
|
|
"#################x#x x#x## x#@ ## ## H x ## x# ## x ## x# # ##x#x x#x#################",
|
|
|
|
" ###### ####### # x# x ## # x # # x ## @# #xx #x # # # # x H# ##x # #x # # x # #x x# ############## ",
|
|
|
|
"################## H#x x x##x @x#x #### ### x #### x#x# ##xx x#x ### x ####x ###x# # #################",
|
|
|
|
"################# x# #@ ## # x#xx#x # ## #x##x# x ## x# x# ## x#x x# ## # # ##x# # ## x #x H #################",
|
|
|
|
"################# x x H# ## #x#x #x ## #x# #x ## x # x#x ## #x# # x# ## x#x # x # ##x#@ # # #################",
|
|
|
|
"#################x ## ##x## # # #x ## x# x## x ## # #x ## # x# ## ## x# ##x #H## x# #x ##@#################",
|
|
|
|
"################# x#x ###x x# ##x ### # # x # # ## H # ## # @x## # # x # # ### x## #x x### x#x #################",
|
|
|
|
"################# ### x ### # # ### ##x x ## x x x ### # ###x ## x x @ H x xx################# ",
|
|
|
|
"#################x# #x# #x # ## # ##x # #x x ### #x x #### x # ###x ## #@#H x ################# ",
|
|
|
|
" ############## # # #x# #x # ## x # ### # x #x ## #x # xx x ###x # ## x ## #@#H x # ############## ",
|
|
|
|
"################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ",
|
|
|
|
"################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# " ];
|
|
|
|
|
|
|
|
function count_gifts( state ) {
|
2011-07-04 21:10:44 +02:00
|
|
|
n = 0;
|
2011-07-04 16:11:42 +02:00
|
|
|
for ( i = 0 ; i < LEVEL_HEIGHT * LEVEL_WIDTH ; i++ ) {
|
2011-07-04 16:40:15 +02:00
|
|
|
if ( state.board.charAt( i ) == cell.GIFT ) {
|
2011-07-04 16:11:42 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function get_pos( state, actor ) {
|
2011-07-04 16:40:15 +02:00
|
|
|
p = state.board.indexOf( actor, state.board );
|
2011-07-04 21:10:44 +02:00
|
|
|
pos = {};
|
2011-07-04 16:11:42 +02:00
|
|
|
pos[ 1 ] = Math.floor( p / LEVEL_WIDTH ); /* y */
|
|
|
|
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH ); /* x */
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function get_cell( state, x, y ) {
|
2011-07-04 16:40:15 +02:00
|
|
|
return( state.board.charAt( x + ( y * LEVEL_WIDTH ) ) );
|
2011-07-04 16:11:42 +02:00
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function set_cell( state, x, y, value ) {
|
|
|
|
p = x + ( y * LEVEL_WIDTH );
|
2011-07-04 16:40:15 +02:00
|
|
|
state.board = state.board.substring( 0, p ) + value + state.board.substring( p+1, state.board.length );
|
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:40:15 +02:00
|
|
|
function switch_actor( state ) {
|
|
|
|
state.moving = ( state.moving == cell.BALL ) ? cell.CUBE : cell.BALL;
|
2011-07-04 16:11:42 +02:00
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function won_or_not( state ) {
|
|
|
|
return count_gifts( state ) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function format_level( state ) {
|
|
|
|
dl = "";
|
|
|
|
for ( i = 0 ; i < LEVEL_HEIGHT ; i++ ) {
|
2011-07-04 17:53:00 +02:00
|
|
|
for ( j = 0 ; j < LEVEL_WIDTH ; j++ ) {
|
|
|
|
c = get_cell( state, j, i );
|
|
|
|
classes = "starcell";
|
|
|
|
if ( state.moving == c) {
|
|
|
|
classes = classes + " selected";
|
|
|
|
}
|
|
|
|
switch( c ) {
|
|
|
|
case cell.BALL:
|
|
|
|
classes = classes + " ball";
|
|
|
|
break;
|
|
|
|
case cell.CUBE:
|
|
|
|
classes = classes + " cube";
|
|
|
|
break;
|
|
|
|
case cell.WALL:
|
|
|
|
classes = classes + " wall";
|
|
|
|
break;
|
|
|
|
case cell.VOID:
|
|
|
|
classes = classes + " void";
|
|
|
|
break;
|
|
|
|
case cell.GIFT:
|
|
|
|
classes = classes + " gift";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-04 21:10:44 +02:00
|
|
|
dl = dl + "<span class=\"" + classes + "\">" + c + "</span>";
|
2011-07-04 17:53:00 +02:00
|
|
|
}
|
2011-07-04 21:10:44 +02:00
|
|
|
dl = dl + "<br />";
|
2011-07-04 16:11:42 +02:00
|
|
|
}
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function load_level( levelset, nb ) {
|
2011-07-04 21:10:44 +02:00
|
|
|
state.level = nb;
|
2011-07-04 16:40:15 +02:00
|
|
|
state.board = levelset[ state.level ];
|
2011-07-04 16:11:42 +02:00
|
|
|
state.moves = 0;
|
|
|
|
state.moving = cell.BALL;
|
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 17:23:22 +02:00
|
|
|
function display_level( state, elt ) {
|
2011-07-04 16:11:42 +02:00
|
|
|
$( elt ).html( format_level( state ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function make_a_move( state, where ) {
|
|
|
|
motion = [ 0, 0 ];
|
|
|
|
item_coord = get_pos( state, state.moving );
|
|
|
|
|
2011-07-04 17:19:39 +02:00
|
|
|
/* Setup the motion vector according to direction.*/
|
|
|
|
switch( where ) {
|
|
|
|
case direction.UP:
|
|
|
|
motion[ 1 ]--;
|
|
|
|
break;
|
|
|
|
case direction.DOWN:
|
|
|
|
motion[ 1 ]++;
|
|
|
|
break;
|
|
|
|
case direction.LEFT:
|
|
|
|
motion[ 0 ]--;
|
|
|
|
break;
|
|
|
|
case direction.RIGHT:
|
|
|
|
motion[ 0 ]++;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculating arrival coordinates */
|
|
|
|
while ( /* Hairy conditions ahead */
|
|
|
|
/* target cell is within level's boundaries */
|
|
|
|
( ( item_coord[ 0 ] + motion[ 0 ] >= 0 ) && ( item_coord[ 0 ] + motion[ 0 ] < LEVEL_WIDTH ) ) &&
|
|
|
|
( ( item_coord[ 1 ] + motion[ 1 ] >= 0 ) && ( item_coord[ 1 ] + motion[ 1 ] < LEVEL_HEIGHT ) ) &&
|
|
|
|
/* and target cell is empty */
|
|
|
|
( get_cell( state, item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == cell.VOID )
|
|
|
|
/* or, the ball will eat gifts so we can move it on one */
|
|
|
|
|| ( state.moving == cell.BALL && ( get_cell( state, item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == cell.GIFT ) )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
state = set_cell( state, item_coord[ 0 ], item_coord[ 1 ], cell.VOID ); /* void the origin cell */
|
|
|
|
|
|
|
|
item_coord[ 0 ] += motion[ 0 ]; /* move coordinate */
|
|
|
|
item_coord[ 1 ] += motion[ 1 ]; /* to those of target cells */
|
|
|
|
|
|
|
|
state = set_cell( state, item_coord[ 0 ], item_coord[ 1 ], state.moving ); /* move actor into target cell */
|
|
|
|
}
|
|
|
|
|
|
|
|
state.moves++; /* increment moves' counter */
|
2011-07-04 16:11:42 +02:00
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:40:15 +02:00
|
|
|
|
2011-07-04 17:23:22 +02:00
|
|
|
function start_loop( state, elt ) {
|
|
|
|
display_level( state, elt );
|
2011-07-04 16:40:15 +02:00
|
|
|
|
|
|
|
$( document ).keydown( function( e ) {
|
|
|
|
switch( e.keyCode ) {
|
|
|
|
case 38: // UP
|
|
|
|
state = make_a_move( state, direction.UP );
|
|
|
|
break;
|
|
|
|
case 40: // DOWN
|
|
|
|
state = make_a_move( state, direction.DOWN );
|
|
|
|
break;
|
|
|
|
case 37: // LEFT
|
|
|
|
state = make_a_move( state, direction.LEFT );
|
|
|
|
break;
|
|
|
|
case 39: // RIGHT
|
|
|
|
state = make_a_move( state, direction.RIGHT );
|
|
|
|
break;
|
2011-07-04 16:51:03 +02:00
|
|
|
case 32: // SPACE
|
2011-07-04 16:40:15 +02:00
|
|
|
state = switch_actor( state );
|
|
|
|
break;
|
2011-07-04 16:51:03 +02:00
|
|
|
case 78: // n
|
|
|
|
if ( state.level < levels.length - 1 ) {
|
2011-07-04 21:10:44 +02:00
|
|
|
state = load_level( levels, state.level + 1 );
|
2011-07-04 16:51:03 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 80: // p
|
|
|
|
if ( state.level > 0 ) {
|
2011-07-04 21:10:44 +02:00
|
|
|
state = load_level( levels, state.level - 1 );
|
2011-07-04 16:51:03 +02:00
|
|
|
}
|
|
|
|
break;
|
2011-07-04 16:40:15 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-04 17:23:22 +02:00
|
|
|
if ( won_or_not( state ) ) {
|
|
|
|
if ( state.level < levels.length - 1 ) {
|
2011-07-04 21:10:44 +02:00
|
|
|
state = load_level( levels, state.level + 1 );
|
2011-07-04 17:23:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert( "You won!" );
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
}
|
2011-07-04 16:40:15 +02:00
|
|
|
|
2011-07-04 17:23:22 +02:00
|
|
|
display_level( state, elt );
|
2011-07-04 16:40:15 +02:00
|
|
|
});
|
|
|
|
}
|