This commit is contained in:
Gwenhael Le Moine 2011-07-01 15:36:55 +02:00
parent c5e974a15d
commit c27494e73d

597
star.c
View file

@ -7,17 +7,18 @@
#define LEVEL_WIDTH 16 #define LEVEL_WIDTH 16
typedef enum { typedef enum {
WALL='#', WALL ='#',
BALL='@', BALL ='@',
CUBE='H', CUBE ='H',
VOID=' ', VOID =' ',
GIFT='*' GIFT ='*'
} cell; } cell;
typedef enum { typedef enum {
UP='u', UP ='u',
DOWN='d', DOWN ='d',
LEFT='l', LEFT ='l',
RIGHT='r' RIGHT ='r'
} direction; } direction;
struct state { struct state {
@ -30,9 +31,9 @@ int count_gifts( struct state *s )
{ {
int i, n = 0; int i, n = 0;
for( i = 0 ; i < LEVEL_HEIGHT * LEVEL_WIDTH ; i++ ) { for( i = 0 ; i < LEVEL_HEIGHT * LEVEL_WIDTH ; i++ ) {
if ( s->level[ i ] == GIFT ) { if ( s->level[ i ] == GIFT ) {
n++; n++;
} }
} }
return n; return n;
} }
@ -40,7 +41,7 @@ int count_gifts( struct state *s )
void get_pos( struct state *s, int* pos ) void get_pos( struct state *s, int* pos )
{ {
int p; int p;
p = (int)( strchr( s->level, s->moving ) - s->level ); p = (int)( strchr( s->level, s->moving ) - s->level );
pos[ 1 ] = p / LEVEL_WIDTH; pos[ 1 ] = p / LEVEL_WIDTH;
@ -81,46 +82,46 @@ void move( struct state *s, direction where )
get_pos( s, item_coord ); get_pos( s, item_coord );
tmpx = item_coord[ 0 ]; tmpx = item_coord[ 0 ];
tmpy = item_coord[ 1 ]; tmpy = item_coord[ 1 ];
switch( where ) { switch( where ) {
case UP: case UP:
dy--; dy--;
break; break;
case DOWN: case DOWN:
dy++; dy++;
break; break;
case LEFT: case LEFT:
dx--; dx--;
break; break;
case RIGHT: case RIGHT:
dx++; dx++;
break; break;
default: break; default: break;
} }
/* Calculating arrival coordinates */ /* Calculating arrival coordinates */
while ( /* Hairy conditions ahead */ while ( /* Hairy conditions ahead */
/* target cell is within level's boundaries */ /* target cell is within level's boundaries */
( ( tmpx + dx >= 0 ) && ( tmpx + dx < LEVEL_WIDTH ) ) && ( ( tmpx + dx >= 0 ) && ( tmpx + dx < LEVEL_WIDTH ) ) &&
( ( tmpy + dy >= 0 ) && ( tmpy + dy < LEVEL_HEIGHT ) ) && ( ( tmpy + dy >= 0 ) && ( tmpy + dy < LEVEL_HEIGHT ) ) &&
/* and target cell is empty */ /* and target cell is empty */
( get_cell( s, tmpx + dx, tmpy + dy ) == VOID ) ( get_cell( s, tmpx + dx, tmpy + dy ) == VOID )
/* or, in case the ball is moving, target cell can be a gift (which we'll eat) */ /* or, in case the ball is moving, target cell can be a gift (which we'll eat) */
|| ( s->moving == BALL && ( get_cell( s, tmpx + dx, tmpy + dy ) == GIFT ) ) || ( s->moving == BALL && ( get_cell( s, tmpx + dx, tmpy + dy ) == GIFT ) )
) )
{ {
tmpx += dx; tmpx += dx;
tmpy += dy; tmpy += dy;
if ( s->moving == BALL && get_cell( s, tmpx, tmpy ) == GIFT ) { if ( s->moving == BALL && get_cell( s, tmpx, tmpy ) == GIFT ) {
set_cell( s, tmpx, tmpy, VOID ); set_cell( s, tmpx, tmpy, VOID );
} }
} }
/* Moving to arrival coordinates */ /* Moving to arrival coordinates */
set_cell( s, item_coord[ 0 ], item_coord[ 1 ], VOID ); set_cell( s, item_coord[ 0 ], item_coord[ 1 ], VOID );
set_cell( s, tmpx, tmpy, s->moving ); set_cell( s, tmpx, tmpy, s->moving );
free( item_coord ); free( item_coord );
} }
@ -128,289 +129,289 @@ void move( struct state *s, direction where )
/* so if I declare level as cell* levels[] I get a warning :/ */ /* so if I declare level as cell* levels[] I get a warning :/ */
char *levels[] = { "################" char *levels[] = { "################"
"#@## *#H#" "#@## *#H#"
"# * ###" "# * ###"
"# ##* #" "# ##* #"
"# ## * ##" "# ## * ##"
"## * * * #" "## * * * #"
"# * *## * #" "# * *## * #"
"# ##* *#" "# ##* *#"
"################", "################",
" # # # # # ##" " # # # # # ##"
"# * @#" "# * @#"
" #* #* * " " #* #* * "
"# # * * # #" "# # * * # #"
" # * # " " # * # "
"# #H# * #" "# #H# * #"
" # # # #**#" " # # # #**#"
"# # " "# # "
" # # # ", " # # # ",
"################" "################"
"# *#@#" "# *#@#"
"# ## ##H#" "# ## ##H#"
"# #* * #" "# #* * #"
"# * *## *#" "# * *## *#"
"# #* * *# *##" "# #* * *# *##"
"# ##* #* * *###" "# ##* #* * *###"
"#* ##* #" "#* ##* #"
"################", "################",
"################" "################"
"# #H#" "# #H#"
"# # #" "# # #"
"##*#* *#*#*#*#*#" "##*#* *#*#*#*#*#"
"# # #* *# # # ##" "# # #* *# # # ##"
"##*#*#* *#*#*#*#" "##*#*#* *#*#*#*#"
"# # #" "# # #"
"# # #@ #" "# # #@ #"
"################", "################",
" ############## " " ############## "
"#@ # # # #" "#@ # # # #"
"# #* # * * # #" "# #* # * * # #"
"## # # #" "## # # #"
"#* #*# ##" "#* #*# ##"
"## # * # #" "## # * # #"
"#*# # # # #H#" "#*# # # # #H#"
"# # *# #*#" "# # *# #*#"
" ############## ", " ############## ",
" ############" " ############"
" # * #* *#" " # * #* *#"
" # * # ##" " # * # ##"
" # * #" " # * #"
"#@ * #" "#@ * #"
"## * # ##" "## * # ##"
"# * # #" "# * # #"
"#H # * ##* #" "#H # * ##* #"
"################", "################",
"################" "################"
"# #" "# #"
" ## ### #* ##*#" " ## ### #* ##*#"
" #* #* # # # # " " #* #* # # # # "
" # # ### ## " " # # ### ## "
" ## # #*# #*# " " ## # #*# #*# "
"# #" "# #"
"# @#* H #*#" "# @#* H #*#"
"################", "################",
"############### " "############### "
"# *## ##" "# *## ##"
"# #* ## * #" "# #* ## * #"
"# *## # #* #" "# *## # #* #"
"## ## #*# #" "## ## #*# #"
"## # *#* #" "## # *#* #"
"#*H*# * #@# #" "#*H*# * #@# #"
"## #" "## #"
" ###############", " ###############",
" # ########### " " # ########### "
" #*#* # @#" " #*#* # @#"
"#* *# * # " "#* *# * # "
" # # *## *# #" " # # *## *# #"
"# #* #*H* *#" "# #* #*H* *#"
"# *## # " "# *## # "
"#*#* # " "#*#* # "
"# # " "# # "
"############ ", "############ ",
" ########### " " ########### "
"#### * #" "#### * #"
"# H ###* *# *#" "# H ###* *# *#"
"# * #* #* # " "# * #* #* # "
"# # * # *#" "# # * # *#"
"#*#* # *# #@# " "#*#* # *# #@# "
" #* ### ### " " #* ### ### "
"# # # # " "# # # # "
" ######### # #", " ######### # #",
"################" "################"
"# # @#" "# # @#"
"# #** ** ##" "# #** ** ##"
"## * ## *#" "## * ## *#"
"#* #*#** ###" "#* #*#** ###"
"## ## ## #" "## ## ## #"
"#* *# * H *#" "#* *# * H *#"
"##*### # ##" "##*### # ##"
" ## ########### ", " ## ########### ",
"## ## #### " "## ## #### "
"#@#####* ### *##" "#@#####* ### *##"
"# ** * #" "# ** * #"
"# ## ##* #*# #" "# ## ##* #*# #"
"# # * ###* ## #" "# # * ###* ## #"
"# ## ## #H# #" "# ## ## #H# #"
"# * #" "# * #"
"# * #" "# * #"
"################", "################",
" ############## " " ############## "
"# @# * ##" "# @# * ##"
"# # #* *## #" "# # #* *## #"
"# * # #" "# * # #"
"# * #*#" "# * #*#"
"# # * #" "# # * #"
"## * * #*#" "## * * #*#"
"#H # * # # #" "#H # * # # #"
" ############## ", " ############## ",
"################" "################"
"#*#* *#*#" "#*#* *#*#"
"# *#@ ## #" "# *#@ ## #"
"# H * #" "# H * #"
"# *# #" "# *# #"
"# * #" "# * #"
"# *# # #" "# *# # #"
"#*#* *#*#" "#*#* *#*#"
"################", "################",
" ###### ####### " " ###### ####### "
"# *# * #" "# *# * #"
"# # * # # * #" "# # * # # * #"
"# @# #** #* #" "# @# #** #* #"
" # # # * H# #" " # # # * H# #"
"#* # #* #" "#* # #* #"
" # * # " " # * # "
"#* *#" "#* *#"
" ############## ", " ############## ",
"################" "################"
"## H#* * *#" "## H#* * *#"
"#* @*#* ##" "#* @*#* ##"
"## ### * ##" "## ### * ##"
"## *#*# #" "## *#*# #"
"#** *#* #" "#** *#* #"
"## * ####* #" "## * ####* #"
"##*# # #" "##*# # #"
"################", "################",
"################" "################"
"# *# #@ #" "# *# #@ #"
"# # *#**#* # #" "# # *#**#* # #"
"# #*##*# * #" "# #*##*# * #"
"# *# *# #" "# *# *# #"
"# *#* *# #" "# *#* *# #"
"# # # ##*# # #" "# # # ##*# # #"
"# * #* H #" "# * #* H #"
"################", "################",
"################" "################"
"# * * H# #" "# * * H# #"
"# #*#* #* #" "# #*#* #* #"
"# #*# #* #" "# #*# #* #"
"# * # *#* #" "# * # *#* #"
"# #*# # *# #" "# #*# # *# #"
"# *#* # * # #" "# *#* # * # #"
"#*#@ # # #" "#*#@ # # #"
"################", "################",
"################" "################"
"#* ## ##*#" "#* ## ##*#"
"# # # #* #" "# # # #* #"
"# *# *## * #" "# *# *## * #"
"# # #* #" "# # #* #"
"# # *# #" "# # *# #"
"# ## *# ##* #H#" "# ## *# ##* #H#"
"# *# #* ##@#" "# *# #* ##@#"
"################", "################",
"################" "################"
"# *#* #" "# *#* #"
"##* *# ##* ##" "##* *# ##* ##"
"# # # * # # #" "# # # * # # #"
"# H # ## # @*#" "# H # ## # @*#"
"# # # * # # #" "# # # * # # #"
"## *## #* *##" "## *## #* *##"
"# *#* #" "# *#* #"
"################", "################",
"################" "################"
"# ### * ##" "# ### * ##"
"# # # ##" "# # # ##"
"# ##* * #" "# ##* * #"
"# * * * ##" "# * * * ##"
"# # ###* #" "# # ###* #"
"# * * @ H * **#" "# * * @ H * **#"
"################" "################"
" ", " ",
"################" "################"
"#*# #*# #* # #" "#*# #*# #* # #"
"# # #" "# # #"
"#* # #* * #" "#* # #* * #"
"## #* * ###" "## #* * ###"
"# * # ###* #" "# * # ###* #"
"# #@#H * #" "# #@#H * #"
"################" "################"
" ", " ",
" ############## " " ############## "
"# # #*# #* # #" "# # #*# #* # #"
"# * # #" "# * # #"
"## # * #* #" "## # * #* #"
"# #* # ** * #" "# #* # ** * #"
"##* # ## * #" "##* # ## * #"
"# #@#H * #" "# #@#H * #"
" ############## " " ############## "
" ", " ",
"################" "################"
"# # ##" "# # ##"
"# ##* * ##*##" "# ##* * ##*##"
"# #* *# ###" "# #* *# ###"
"# ** *# ## #" "# ** *# ## #"
"# #* * # ## #" "# #* * # ## #"
"# ## @#H###**#" "# ## @#H###**#"
"################" "################"
" ", " ",
"################" "################"
"# # #" "# # #"
"# * ##* * #" "# * ##* * #"
"# #* * ## #" "# #* * ## #"
"# * ## #* #" "# * ## #* #"
"# #* *# * #" "# #* *# * #"
"# ##* #@ H #" "# ##* #@ H #"
"################" "################"
" " }; " " };
void display_level( struct state *s ) void display_level( struct state *s )
{ {
int i, j, *ball, *cube; int i, j, *ball, *cube;
printf( "%i gifts left", count_gifts( s ) ); printf( "%i gifts left", count_gifts( s ) );
if ( won_or_not( s ) ) { if ( won_or_not( s ) ) {
printf( ", You WON !\n"); printf( ", You WON !\n");
} }
else { else {
printf( ", go on.\n"); printf( ", go on.\n");
} }
for( i = 0 ; i < LEVEL_HEIGHT ; i++ ) { for( i = 0 ; i < LEVEL_HEIGHT ; i++ ) {
for( j = 0 ; j < LEVEL_WIDTH ; j++ ) { for( j = 0 ; j < LEVEL_WIDTH ; j++ ) {
switch( get_cell( s, j, i ) ) { switch( get_cell( s, j, i ) ) {
case WALL: case WALL:
printf( "##" ); printf( "##" );
break; break;
case VOID: case VOID:
printf( " " ); printf( " " );
break; break;
case BALL: case BALL:
printf( "()" ); printf( "()" );
break; break;
case CUBE: case CUBE:
printf( "[]" ); printf( "[]" );
break; break;
case GIFT: case GIFT:
printf( "<>" ); printf( "<>" );
break; break;
default: break; /* ignore newlines */ default: break; /* ignore newlines */
} }
} }
printf( "\n" ); printf( "\n" );
} }
} }
@ -418,17 +419,17 @@ int main( int argc, char* argv[] )
{ {
int i = 0; int i = 0;
struct state *s = malloc( sizeof( struct state ) ); struct state *s = malloc( sizeof( struct state ) );
load_level( s, levels[ 0 ] ); load_level( s, levels[ 0 ] );
display_level( s ); display_level( s );
char* moves = "drdluruldrdlrulurudlurdul"; char* moves = "drdluruldrdlrulurudlurdul";
do { do {
fprintf( stderr, "move %c\n", moves[ i ]); fprintf( stderr, "move %c\n", moves[ i ]);
move( s, moves[ i ] ); move( s, moves[ i ] );
display_level( s ); display_level( s );
i++; i++;
} while( ( ! won_or_not( s ) ) && ( moves[ i ] != '\0' ) ); } while( ( ! won_or_not( s ) ) && ( moves[ i ] != '\0' ) );
display_level( s ); display_level( s );