This commit is contained in:
Gwenhael Le Moine 2011-06-30 16:15:03 +02:00
parent e73d385170
commit 538de01186

105
star.c
View file

@ -1,40 +1,97 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEVEL_HEIGHT 9 #define LEVEL_HEIGHT 9
#define LEVEL_WIDTH 16 #define LEVEL_WIDTH 16
#define WALL 'W'
#define BALL 'B'
#define CUBE 'C'
#define VOID 'V'
#define GIFT 'G'
char* levels[] = { "WWWWWWWWWWWWWWWWWBWWVVVVVVVVGWCWWVVVVVVVVVVGVWWWWVVVVVVVWWGVVVVWWVVVWWVGVVVVVVWWWWVGVVGVVVVVGVVWWVGVVVVVVGWWVGVWWVVVVVWWGVVVVVGWWWWWWWWWWWWWWWWW" }; char* levels[] = { "WWWWWWWWWWWWWWWWWBWWVVVVVVVVGWCWWVVVVVVVVVVGVWWWWVVVVVVVWWGVVVVWWVVVWWVGVVVVVVWWWWVGVVGVVVVVGVVWWVGVVVVVVGWWVGVWWVVVVVWWGVVVVVGWWWWWWWWWWWWWWWWW" };
struct {
char level[ LEVEL_HEIGHT * LEVEL_WIDTH ];
} state;
int count_gifts( char* lvl )
{
int i, n = 0;
for( i = 0 ; i < LEVEL_HEIGHT * LEVEL_WIDTH ; i++ ) {
if ( lvl[ i ] == GIFT ) {
n++;
}
}
return n;
}
void get_pos( char* lvl, char item, int* pos )
{
int p;
p = (int)( strchr( lvl, item ) - lvl );
fprintf( stderr, "### %i\n", p );
pos[ 1 ] = p / LEVEL_WIDTH;
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH );
}
char get_cell( char* lvl, int x, int y )
{
return lvl[ y * LEVEL_WIDTH + x ];
}
void load_level( char* lvl )
{
strncpy( state.level, lvl, LEVEL_HEIGHT * LEVEL_WIDTH );
}
void display_level( char* lvl ) void display_level( char* lvl )
{ {
int i; int i, j, *ball, *cube;
for( i = 0 ; i < LEVEL_HEIGHT * LEVEL_WIDTH ; i++ ) { ball = malloc( sizeof( int ) * 2 );
if ( ( i > 0 ) && ( i % LEVEL_WIDTH == 0 ) ) { cube = malloc( sizeof( int ) * 2 );
printf( "\n" );
} printf( "%i gifts\n", count_gifts( lvl ) );
switch( lvl[ i ] ) { get_pos( lvl, BALL, ball );
case 'W': get_pos( lvl, CUBE, cube );
printf( "##" ); printf( "Ball: ( %i, %i) ; Cube: ( %i, %i)\n", ball[ 0 ], ball[ 1 ], cube[ 0 ], cube[ 1 ] );
break;
case 'V': for( i = 0 ; i < LEVEL_HEIGHT ; i++ ) {
printf( " " ); for( j = 0 ; j < LEVEL_WIDTH ; j++ ) {
break; switch( get_cell( lvl, j, i ) ) {
case 'B': case WALL:
printf( "()" ); printf( "##" );
break; break;
case 'C': case VOID:
printf( "[]" ); printf( " " );
break; break;
case 'G': case BALL:
printf( "<>" ); printf( "()" );
break; break;
default: break; case CUBE:
printf( "[]" );
break;
case GIFT:
printf( "<>" );
break;
default: break;
}
} }
printf( "\n" );
} }
printf( "\n" );
free( ball );
free( cube );
} }
int main( int argc, char* argv[] ) int main( int argc, char* argv[] )
{ {
display_level( levels[ 0 ] ); load_level( levels[ 0 ] );
display_level( state.level );
return 0; return 0;
} }