c-urs_-toil-s/star.c

658 lines
22 KiB
C
Raw Normal View History

//&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";cc -lncurses -o "$x" "$0")&&"$x" $*;exit
2011-07-03 23:06:22 +02:00
// ^^ this up there makes the file executable ^^
2011-06-30 15:17:21 +02:00
#include <stdio.h>
2011-06-30 16:15:03 +02:00
#include <string.h>
#include <stdlib.h>
2011-06-30 15:17:21 +02:00
#include <ncurses.h>
#include <getopt.h>
2011-07-04 08:57:56 +02:00
/* levels have fixed, hardcoded dimensions */
#define LEVEL_HEIGHT 9
#define LEVEL_WIDTH 16
char *levels[] = { "################" /* 0 */
"#@## x#H#"
"# x ###"
"# ##x #"
"# ## x ##"
"## x x x #"
"# x x## x #"
"# ##x x#"
2011-07-01 15:36:55 +02:00
"################",
" # # # # # ##" /* 1 */
"# x @#"
" #x #x x "
"# # x x # #"
" # x # "
"# #H# x #"
" # # # #xx#"
2011-07-01 15:36:55 +02:00
"# # "
" # # # ",
"################" /* 2 */
"# x#@#"
2011-07-01 15:36:55 +02:00
"# ## ##H#"
"# #x x #"
"# x x## x#"
"# #x x x# x##"
"# ##x #x x x###"
"#x ##x #"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 3 */
2011-07-01 15:36:55 +02:00
"# #H#"
"# # #"
"##x#x x#x#x#x#x#"
"# # #x x# # # ##"
"##x#x#x x#x#x#x#"
2011-07-01 15:36:55 +02:00
"# # #"
"# # #@ #"
"################",
" ############## " /* 4 */
2011-07-01 15:36:55 +02:00
"#@ # # # #"
"# #x # x x # #"
2011-07-01 15:36:55 +02:00
"## # # #"
"#x #x# ##"
"## # x # #"
"#x# # # # #H#"
"# # x# #x#"
2011-07-01 15:36:55 +02:00
" ############## ",
" ############" /* 5 */
" # x #x x#"
" # x # ##"
" # x #"
"#@ x #"
"## x # ##"
"# x # #"
"#H # x ##x #"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 6 */
2011-07-01 15:36:55 +02:00
"# #"
" ## ### #x ##x#"
" #x #x # # # # "
2011-07-01 15:36:55 +02:00
" # # ### ## "
" ## # #x# #x# "
2011-07-01 15:36:55 +02:00
"# #"
"# @#x H #x#"
2011-07-01 15:36:55 +02:00
"################",
"############### " /* 7 */
"# x## ##"
"# #x ## x #"
"# x## # #x #"
"## ## #x# #"
"## # x#x #"
"#xHx# x #@# #"
2011-07-01 15:36:55 +02:00
"## #"
" ###############",
" # ########### " /* 8 */
" #x#x # @#"
"#x x# x # "
" # # x## x# #"
"# #x #xHx x#"
"# x## # "
"#x#x # "
2011-07-01 15:36:55 +02:00
"# # "
"############ ",
" ########### " /* 9 */
"#### x #"
"# H ###x x# x#"
"# x #x #x # "
"# # x # x#"
"#x#x # x# #@# "
" #x ### ### "
2011-07-01 15:36:55 +02:00
"# # # # "
" ######### # #",
"################" /* 10 */
2011-07-01 15:36:55 +02:00
"# # @#"
"# #xx xx ##"
"## x ## x#"
"#x #x#xx ###"
2011-07-01 15:36:55 +02:00
"## ## ## #"
"#x x# x H x#"
"##x### # ##"
2011-07-01 15:36:55 +02:00
" ## ########### ",
"## ## #### " /* 11 */
"#@#####x ### x##"
"# xx x #"
"# ## ##x #x# #"
"# # x ###x ## #"
2011-07-01 15:36:55 +02:00
"# ## ## #H# #"
"# x #"
"# x #"
2011-07-01 15:36:55 +02:00
"################",
" ############## " /* 12 */
"# @# x ##"
"# # #x x## #"
"# x # #"
"# x #x#"
"# # x #"
"## x x #x#"
"#H # x # # #"
2011-07-01 15:36:55 +02:00
" ############## ",
"################" /* 13 */
"#x#x x#x#"
"# x#@ ## #"
"# H x #"
"# x# #"
"# x #"
"# x# # #"
"#x#x x#x#"
2011-07-01 15:36:55 +02:00
"################",
" ###### ####### " /* 14 */
"# x# x #"
"# # x # # x #"
"# @# #xx #x #"
" # # # x H# #"
"#x # #x #"
" # x # "
"#x x#"
2011-07-01 15:36:55 +02:00
" ############## ",
"################" /* 15 */
"## H#x x x#"
"#x @x#x ##"
"## ### x ##"
"## x#x# #"
"#xx x#x #"
"## x ####x #"
"##x# # #"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 16 */
"# x# #@ #"
"# # x#xx#x # #"
"# #x##x# x #"
"# x# x# #"
"# x#x x# #"
"# # # ##x# # #"
"# x #x H #"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 17 */
"# x x H# #"
"# #x#x #x #"
"# #x# #x #"
"# x # x#x #"
"# #x# # x# #"
"# x#x # x # #"
"#x#@ # # #"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 18 */
"#x ## ##x#"
"# # # #x #"
"# x# x## x #"
"# # #x #"
"# # x# #"
"# ## x# ##x #H#"
"# x# #x ##@#"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 19 */
"# x#x #"
"##x x# ##x ##"
"# # # x # # #"
"# H # ## # @x#"
"# # # x # # #"
"## x## #x x##"
"# x#x #"
2011-07-01 15:36:55 +02:00
"################",
"################" /* 20 */
"# ### x ##"
2011-07-01 15:36:55 +02:00
"# # # ##"
"# ##x x #"
"# x x x ##"
"# # ###x #"
"# x x @ H x xx#"
2011-07-01 15:36:55 +02:00
"################"
" ",
"################" /* 21 */
"#x# #x# #x # #"
2011-07-01 15:36:55 +02:00
"# # #"
"#x # #x x #"
"## #x x ###"
"# x # ###x #"
"# #@#H x #"
2011-07-01 15:36:55 +02:00
"################"
" ",
" ############## " /* 22 */
"# # #x# #x # #"
"# x # #"
"## # x #x #"
"# #x # xx x #"
"##x # ## x #"
"# #@#H x #"
2011-07-01 15:36:55 +02:00
" ############## "
" ",
"################" /* 23 */
2011-07-01 15:36:55 +02:00
"# # ##"
"# ##x x ##x##"
"# #x x# ###"
"# xx x# ## #"
"# #x x # ## #"
"# ## @#H###xx#"
2011-07-01 15:36:55 +02:00
"################"
" ",
"################" /* 24 */
2011-07-01 15:36:55 +02:00
"# # #"
"# x ##x x #"
"# #x x ## #"
"# x ## #x #"
"# #x x# x #"
"# ##x #@ H #"
2011-07-01 15:36:55 +02:00
"################"
" " };
2011-06-30 15:17:21 +02:00
2011-07-03 22:38:00 +02:00
enum { /* color names for ncurses */
2011-07-03 22:39:58 +02:00
color_BALL,
color_CUBE,
color_VOID,
color_GIFT,
color_WALL,
color_BALL_SELECTED,
color_CUBE_SELECTED,
2011-07-01 17:31:01 +02:00
};
2011-07-03 22:38:00 +02:00
typedef enum { /* characters used to design levels */
2011-07-03 22:39:58 +02:00
WALL = '#',
BALL = '@',
CUBE = 'H',
VOID = ' ',
GIFT = 'x'
2011-07-01 17:31:01 +02:00
} cell;
typedef enum { /* and now a nasty trick to slightly simplify key handling */
UP = KEY_UP, /* we'll use ncurses codes to define directions */
DOWN = KEY_DOWN,
LEFT = KEY_LEFT,
RIGHT = KEY_RIGHT
2011-07-01 17:31:01 +02:00
} direction;
2011-07-03 22:38:00 +02:00
struct options { /* store how the game is started (controlled by --arguments) */
int black_and_white;
int starting_level;
};
2011-07-03 22:38:00 +02:00
struct state { /* current state of the game at an instant T */
2011-07-03 22:39:58 +02:00
char board[ LEVEL_HEIGHT * LEVEL_WIDTH ];
char moving;
int distance_travelled;
2011-07-03 22:39:58 +02:00
int level;
int nb_levels;
2011-07-01 17:31:01 +02:00
};
2011-07-03 22:38:00 +02:00
/* Count how many gifts are present in the current level
*/
2011-07-01 17:31:01 +02:00
int count_gifts( struct state *s )
2011-06-30 16:15:03 +02:00
{
2011-07-03 22:39:58 +02:00
int i, n = 0;
for( i = 0 ; i < LEVEL_HEIGHT * LEVEL_WIDTH ; i++ ) {
if ( s->board[ i ] == GIFT ) {
n++;
}
}
return n;
2011-07-01 17:31:01 +02:00
}
2011-07-04 09:12:58 +02:00
/* Write the position of actor into pos
2011-07-03 22:38:00 +02:00
*/
void get_pos( struct state *s, cell actor, int* pos )
2011-07-01 17:31:01 +02:00
{
2011-07-03 22:39:58 +02:00
int p;
2011-07-01 17:31:01 +02:00
2011-07-04 09:12:58 +02:00
p = (int)( strchr( s->board, actor ) - s->board );
2011-07-01 17:31:01 +02:00
2011-07-04 09:12:58 +02:00
pos[ 1 ] = p / LEVEL_WIDTH; /* y */
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH ); /* x */
2011-07-01 17:31:01 +02:00
}
2011-07-01 15:36:55 +02:00
2011-07-03 22:38:00 +02:00
/* Returns the content of cell( x, y ) of the current level
*/
2011-07-01 17:31:01 +02:00
char get_cell( struct state *s, int x, int y )
{
2011-07-03 22:39:58 +02:00
return s->board[ y * LEVEL_WIDTH + x ];
2011-07-01 17:31:01 +02:00
}
2011-07-03 22:38:00 +02:00
/* Set cell( x, y ) of the current level to value
*/
2011-07-01 17:31:01 +02:00
void set_cell( struct state *s, int x, int y, cell value )
{
2011-07-03 22:39:58 +02:00
s->board[ y * LEVEL_WIDTH + x ] = value;
2011-07-01 17:31:01 +02:00
}
2011-07-03 22:38:00 +02:00
/* Load level nb from levels as the current level
*/
void load_level( struct state *s, char* levels[], int nb )
2011-07-01 17:31:01 +02:00
{
2011-07-03 22:39:58 +02:00
strncpy( s->board, levels[ nb ], LEVEL_HEIGHT * LEVEL_WIDTH );
s->level = nb;
s->moving = BALL;
s->distance_travelled = 0;
2011-07-01 17:31:01 +02:00
}
2011-07-03 22:38:00 +02:00
/* Swicth the currently moving actor between BALL and CUBE
*/
2011-07-01 17:31:01 +02:00
void switch_actor( struct state *s )
{
2011-07-03 22:39:58 +02:00
s->moving = (s->moving == BALL) ? CUBE : BALL;
2011-07-01 17:31:01 +02:00
}
2011-07-03 22:38:00 +02:00
/* Returns a 'boolean' indicating if the current level is finished (no gifts left)
*/
2011-07-01 17:31:01 +02:00
int won_or_not( struct state *s )
{
2011-07-03 22:39:58 +02:00
return( count_gifts( s ) == 0 );
2011-07-01 17:31:01 +02:00
}
2011-07-03 22:38:00 +02:00
/* display the current level (with Ncurses)
*/
2011-07-01 17:31:01 +02:00
void display_level( struct state *s )
{
2011-07-04 10:28:53 +02:00
int i, j;
2011-07-03 22:39:58 +02:00
for( i = 0 ; i < LEVEL_HEIGHT ; i++ ) {
for( j = 0 ; j < LEVEL_WIDTH ; j++ ) {
switch( get_cell( s, j, i ) ) {
case WALL:
attron( COLOR_PAIR( color_WALL ));
mvprintw( i+1, j*2, "##" );
attroff( COLOR_PAIR( color_WALL ));
break;
case VOID:
attron( COLOR_PAIR( color_VOID ));
mvprintw( i+1, j*2, " " );
attroff( COLOR_PAIR( color_VOID ));
break;
case BALL:
if ( s->moving == BALL ) {
attron( A_BOLD );
attron( COLOR_PAIR( color_BALL_SELECTED ));
}
else {
attron( COLOR_PAIR( color_BALL ));
}
mvprintw( i+1, j*2, "()" );
if ( s->moving == BALL ) {
attroff( A_BOLD );
}
break;
case CUBE:
if ( s->moving == CUBE ) {
attron( A_BOLD );
attron( COLOR_PAIR( color_BALL_SELECTED ));
}
else {
attron( COLOR_PAIR( color_BALL ));
}
mvprintw( i+1, j*2, "[]" );
if ( s->moving == CUBE ) {
attroff( A_BOLD );
attroff( COLOR_PAIR( color_CUBE_SELECTED ));
}
else {
attroff( COLOR_PAIR( color_CUBE ));
}
break;
case GIFT:
attron( COLOR_PAIR( color_GIFT ));
mvprintw( i+1, j*2, "<>" );
attroff( COLOR_PAIR( color_GIFT ));
break;
default: break; /* ignore newlines */
}
}
}
2011-07-01 17:31:01 +02:00
2011-07-03 22:39:58 +02:00
mvprintw( 1, 35, "level %i of %i", s->level + 1, s->nb_levels );
mvprintw( 2, 35, "%i gifts left", count_gifts( s ) );
mvprintw( 3, 35, "%i meter%s traveled", s->distance_travelled, ( s->distance_travelled > 1 ) ? "s" : "" );
2011-07-01 17:31:01 +02:00
2011-07-03 22:39:58 +02:00
refresh();
2011-06-30 15:17:21 +02:00
}
/* Move the current actor towards direction in the current level,
eating gifts on the way if the moving actor is the ball
*/
void make_a_move( struct state *s, direction where )
{
int motion[ 2 ] = { 0, 0 };
2011-07-04 10:33:46 +02:00
int item_coord[ 2 ];
get_pos( s, s->moving, item_coord ); /* get the coordinates of the moving actor */
/* Setup the motion vector according to direction.*/
switch( where ) {
case UP:
motion[ 1 ]--;
break;
case DOWN:
motion[ 1 ]++;
break;
case LEFT:
motion[ 0 ]--;
break;
case 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( s, item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == VOID )
/* or, the ball will eat gifts so we can move it on one */
|| ( s->moving == BALL && ( get_cell( s, item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == GIFT ) )
)
{
set_cell( s, item_coord[ 0 ], item_coord[ 1 ], VOID ); /* void the origin cell */
item_coord[ 0 ] += motion[ 0 ]; /* move coordinate */
item_coord[ 1 ] += motion[ 1 ]; /* to those of target cells */
set_cell( s, item_coord[ 0 ], item_coord[ 1 ], s->moving ); /* move actor into target cell */
/*
We could update the display here, but in practice it all happen so fast
that the move isn't noticeable. So it's commented.
(maybe on a very slow machine it adds beauty?...)
*/
/* display_level( s ); */
2011-07-06 13:46:27 +02:00
s->distance_travelled++; /* increment distance_travelled */
}
}
2011-07-03 22:38:00 +02:00
/* Parse the --arguments, if any, to populate options
*/
int parse_args( int argc, char* argv[], struct options *o, struct state *s )
{
int option_index;
char c = '?';
char* optstring = "l:bhv";
static struct option long_options[] = {
{"black-and-white" , no_argument, NULL, 'b'},
{"level" , required_argument, NULL, 'l'},
{"help" , no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
2011-07-04 08:57:56 +02:00
char* help_text = "star [options]\n"
"\t-h --help :\n\t\t what you are reading\n"
"\t-b --black-and-white :\n\t\t don't use colors (ncurses)\n"
"\t-l<n> --level=<n> :\n\t\t wrap to level n\n";
while(c != EOF) {
c = getopt_long(argc, argv, optstring, long_options, &option_index);
switch(c) {
case 'h' :
printf( "%s\n", help_text );
2011-07-04 10:39:00 +02:00
exit( EXIT_SUCCESS );
case 'b' :
o->black_and_white = 1;
break;
case 'l' :
o->starting_level = atoi( optarg ) - 1;
if ( o->starting_level > s->nb_levels ) {
o->starting_level = s->nb_levels - 1;
2011-07-03 22:24:05 +02:00
} else {
if ( o->starting_level < 0 ) {
o->starting_level = 0;
}
}
break;
case '?' :
case ':' :
2011-07-04 10:39:00 +02:00
exit( EXIT_SUCCESS );
default : break;
}
}
if (optind < argc) {
fprintf( stderr, "Invalid arguments :\n" );
2011-07-04 10:30:56 +02:00
while (optind < argc) {
fprintf( stderr, "%s\n", argv[optind++] );
2011-07-04 10:30:56 +02:00
}
}
return optind;
}
2011-07-03 22:38:00 +02:00
/* Entry point of the program.
Read --arguments.
Initialize Ncurses.
Load the first level.
Enter the loop where it read the keys to play or 'q' or 'Q' to quit.
2011-07-03 22:39:58 +02:00
*/
2011-06-30 15:17:21 +02:00
int main( int argc, char* argv[] )
{
2011-07-03 22:56:00 +02:00
int key, over = 0;
2011-07-03 22:39:58 +02:00
struct state *s = malloc( sizeof( struct state ) );
struct options *o = malloc( sizeof( struct options ) );
2011-07-01 15:36:55 +02:00
if ( s == NULL || o == NULL ) {
2011-07-04 10:39:00 +02:00
exit( EXIT_FAILURE ); /* Guys, we're out of memory */
}
2011-07-03 22:39:58 +02:00
/* trick to count how many levels we have */
s->nb_levels = sizeof( levels ) / sizeof( levels[ 0 ] );
2011-07-03 22:39:58 +02:00
o->starting_level = 0;
o->black_and_white = 0;
2011-07-03 22:39:58 +02:00
parse_args( argc, argv, o, s ); /* not caring about return value here, it's treated inside the function */
2011-07-03 22:39:58 +02:00
/* ncurses */
WINDOW *w_main = initscr( ); /* why can't stdscr be used transparently? */
cbreak();
noecho();
nonl();
intrflush( w_main, FALSE );
keypad( w_main, TRUE );
if ( ( ! o->black_and_white ) && has_colors( ) == TRUE ) {
start_color( );
init_pair( color_CUBE, COLOR_RED, COLOR_BLACK );
init_pair( color_BALL, COLOR_BLUE, COLOR_BLACK );
init_pair( color_GIFT, COLOR_YELLOW, COLOR_BLACK );
init_pair( color_WALL, COLOR_WHITE, COLOR_WHITE );
init_pair( color_VOID, COLOR_BLACK, COLOR_BLACK );
init_pair( color_CUBE_SELECTED, COLOR_RED, COLOR_YELLOW );
init_pair( color_BALL_SELECTED, COLOR_BLUE, COLOR_YELLOW );
}
/* load the first level to start the loop in a correct state */
load_level( s, levels, o->starting_level );
2011-07-03 22:39:58 +02:00
do {
if ( won_or_not( s ) ) {
if ( s->level < s->nb_levels - 1 ) {
load_level( s, levels, s->level + 1 );
}
else {
2011-07-03 22:56:00 +02:00
over = 1;
2011-07-03 22:39:58 +02:00
}
}
2011-07-03 22:56:00 +02:00
if ( over == 0 ) {
display_level( s );
key = getch();
switch( key ) {
case KEY_UP:
case KEY_DOWN:
case KEY_LEFT:
case KEY_RIGHT:
2011-07-03 23:01:45 +02:00
make_a_move( s, key ); /* see definition of direction up above */
2011-07-03 22:56:00 +02:00
break;
case ' ':
switch_actor( s );
break;
case 'n':
if ( s->level < s->nb_levels - 1 ) {
load_level( s, levels, s->level + 1 );
}
break;
case 'p':
if ( s->level > 0 ) {
load_level( s, levels, s->level - 1 );
}
break;
case 'r':
load_level( s, levels, s->level );
break;
default:
break;
}
}
else {
key = 'q';
2011-07-03 22:39:58 +02:00
}
} while( ( s->level < s->nb_levels ) && (( key != 'q' ) && ( key != 'Q' )) );
free( s );
free( o );
2011-07-04 09:56:46 +02:00
/* Clean ncurses' mess */
2011-07-03 22:39:58 +02:00
echo();
nocbreak();
endwin();
2011-07-03 23:04:12 +02:00
nl();
2011-07-03 22:39:58 +02:00
2011-07-04 08:36:14 +02:00
if ( over == 1 ) {
2011-07-03 22:56:00 +02:00
printf( "################################\n" );
printf( "## ##\n" );
printf( "## You've finished the whole ##\n" );
printf( "## game! ##\n" );
printf( "## ##\n" );
printf( "## Now it's your turn to ##\n" );
printf( "## contribute new levels ;) ##\n" );
printf( "## ##\n" );
printf( "################################\n" );
}
2011-07-03 22:39:58 +02:00
return 0;
2011-06-30 15:17:21 +02:00
}