mirror of
https://github.com/gwenhael-le-moine/c-urs_-toil-s.git
synced 2024-12-27 09:58:44 +01:00
document
This commit is contained in:
parent
0e22f03d41
commit
6e21e84181
1 changed files with 40 additions and 8 deletions
48
star.c
48
star.c
|
@ -261,7 +261,7 @@ char *levels[] = { "################" /* 0 */
|
||||||
#define LEVEL_HEIGHT 9
|
#define LEVEL_HEIGHT 9
|
||||||
#define LEVEL_WIDTH 16
|
#define LEVEL_WIDTH 16
|
||||||
|
|
||||||
enum {
|
enum { /* color names for ncurses */
|
||||||
color_BALL,
|
color_BALL,
|
||||||
color_CUBE,
|
color_CUBE,
|
||||||
color_VOID,
|
color_VOID,
|
||||||
|
@ -271,7 +271,7 @@ enum {
|
||||||
color_CUBE_SELECTED,
|
color_CUBE_SELECTED,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum {
|
typedef enum { /* characters used to design levels */
|
||||||
WALL = '#',
|
WALL = '#',
|
||||||
BALL = '@',
|
BALL = '@',
|
||||||
CUBE = 'H',
|
CUBE = 'H',
|
||||||
|
@ -279,19 +279,19 @@ typedef enum {
|
||||||
GIFT = 'x'
|
GIFT = 'x'
|
||||||
} cell;
|
} cell;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum { /* values aren't strictly necessary but useful for unit-testing */
|
||||||
UP = 'u',
|
UP = 'u',
|
||||||
DOWN = 'd',
|
DOWN = 'd',
|
||||||
LEFT = 'l',
|
LEFT = 'l',
|
||||||
RIGHT = 'r'
|
RIGHT = 'r'
|
||||||
} direction;
|
} direction;
|
||||||
|
|
||||||
struct options {
|
struct options { /* store how the game is started (controlled by --arguments) */
|
||||||
int black_and_white;
|
int black_and_white;
|
||||||
int starting_level;
|
int starting_level;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct state {
|
struct state { /* current state of the game at an instant T */
|
||||||
char board[ LEVEL_HEIGHT * LEVEL_WIDTH ];
|
char board[ LEVEL_HEIGHT * LEVEL_WIDTH ];
|
||||||
char moving;
|
char moving;
|
||||||
int moves;
|
int moves;
|
||||||
|
@ -299,6 +299,8 @@ struct state {
|
||||||
int nb_levels;
|
int nb_levels;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Count how many gifts are present in the current level
|
||||||
|
*/
|
||||||
int count_gifts( struct state *s )
|
int count_gifts( struct state *s )
|
||||||
{
|
{
|
||||||
int i, n = 0;
|
int i, n = 0;
|
||||||
|
@ -310,6 +312,8 @@ int count_gifts( struct state *s )
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Write the position of the currently moving actor into pos
|
||||||
|
*/
|
||||||
void get_pos( struct state *s, int* pos )
|
void get_pos( struct state *s, int* pos )
|
||||||
{
|
{
|
||||||
int p;
|
int p;
|
||||||
|
@ -320,34 +324,49 @@ void get_pos( struct state *s, int* pos )
|
||||||
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH );
|
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns the content of cell( x, y ) of the current level
|
||||||
|
*/
|
||||||
char get_cell( struct state *s, int x, int y )
|
char get_cell( struct state *s, int x, int y )
|
||||||
{
|
{
|
||||||
return s->board[ y * LEVEL_WIDTH + x ];
|
return s->board[ y * LEVEL_WIDTH + x ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set cell( x, y ) of the current level to value
|
||||||
|
*/
|
||||||
void set_cell( struct state *s, int x, int y, cell value )
|
void set_cell( struct state *s, int x, int y, cell value )
|
||||||
{
|
{
|
||||||
s->board[ y * LEVEL_WIDTH + x ] = value;
|
s->board[ y * LEVEL_WIDTH + x ] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_level( struct state *s, char* lvl[], int nb )
|
/* Load level nb from levels as the current level
|
||||||
|
*/
|
||||||
|
void load_level( struct state *s, char* levels[], int nb )
|
||||||
{
|
{
|
||||||
strncpy( s->board, lvl[ nb ], LEVEL_HEIGHT * LEVEL_WIDTH );
|
strncpy( s->board, levels[ nb ], LEVEL_HEIGHT * LEVEL_WIDTH );
|
||||||
s->level = nb;
|
s->level = nb;
|
||||||
s->moving = BALL;
|
s->moving = BALL;
|
||||||
s->moves = 0;
|
s->moves = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Swicth the currently moving actor between BALL and CUBE
|
||||||
|
*/
|
||||||
void switch_actor( struct state *s )
|
void switch_actor( struct state *s )
|
||||||
{
|
{
|
||||||
s->moving = (s->moving == BALL) ? CUBE : BALL;
|
s->moving = (s->moving == BALL) ? CUBE : BALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns a 'boolean' indicating if the current level is finished (no gifts left)
|
||||||
|
*/
|
||||||
int won_or_not( struct state *s )
|
int won_or_not( struct state *s )
|
||||||
{
|
{
|
||||||
return( count_gifts( s ) == 0 );
|
return( count_gifts( s ) == 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Move the current actor towards direction in the current level,
|
||||||
|
eating gifts on the way if the moving actor is the ball
|
||||||
|
|
||||||
|
FIXME: more documentation
|
||||||
|
*/
|
||||||
void make_a_move( struct state *s, direction where )
|
void make_a_move( struct state *s, direction where )
|
||||||
{
|
{
|
||||||
int dx = 0, dy = 0, tmpx, tmpy, *item_coord;
|
int dx = 0, dy = 0, tmpx, tmpy, *item_coord;
|
||||||
|
@ -400,6 +419,8 @@ void make_a_move( struct state *s, direction where )
|
||||||
free( item_coord );
|
free( item_coord );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* display the current level (with Ncurses)
|
||||||
|
*/
|
||||||
void display_level( struct state *s )
|
void display_level( struct state *s )
|
||||||
{
|
{
|
||||||
int i, j, *ball, *cube;
|
int i, j, *ball, *cube;
|
||||||
|
@ -464,6 +485,8 @@ void display_level( struct state *s )
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Parse the --arguments, if any, to populate options
|
||||||
|
*/
|
||||||
int parse_args( int argc, char* argv[], struct options *o, struct state *s )
|
int parse_args( int argc, char* argv[], struct options *o, struct state *s )
|
||||||
{
|
{
|
||||||
int option_index;
|
int option_index;
|
||||||
|
@ -521,6 +544,13 @@ int parse_args( int argc, char* argv[], struct options *o, struct state *s )
|
||||||
return optind;
|
return optind;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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.
|
||||||
|
*/
|
||||||
int main( int argc, char* argv[] )
|
int main( int argc, char* argv[] )
|
||||||
{
|
{
|
||||||
int key;
|
int key;
|
||||||
|
@ -566,7 +596,7 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
display_level( s );
|
display_level( s );
|
||||||
key = getch();
|
key = getch();
|
||||||
switch( key ) {
|
switch( key ) {
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
|
@ -605,8 +635,10 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
free( s );
|
free( s );
|
||||||
|
|
||||||
|
/* Clean ncurses mess */
|
||||||
echo();
|
echo();
|
||||||
nocbreak();
|
nocbreak();
|
||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue