indenting

Signed-off-by: Gwenhael Le Moine <gwenhael.le.moine@gmail.com>
This commit is contained in:
Gwenhael Le Moine 2012-04-10 16:10:59 +02:00
parent 371f2001cf
commit a9b6717314

View file

@ -28,380 +28,380 @@ using namespace std;
namespace ngstar2 { namespace ngstar2 {
// NGSTAR2 class implementation // NGSTAR2 class implementation
// in order of declaration // in order of declaration
// private: // private:
// //
// Set the cell(x, y) with the given value (if valid) // Set the cell(x, y) with the given value (if valid)
// //
void NGStar2::set_cell( int x, int y , char value ) void NGStar2::set_cell( int x, int y , char value )
{ {
if ( ( x <= NGSTAR_LEVEL_WIDTH ) && ( y <= NGSTAR_LEVEL_HEIGHT ) ) { if ( ( x <= NGSTAR_LEVEL_WIDTH ) && ( y <= NGSTAR_LEVEL_HEIGHT ) ) {
switch ( value ) { switch ( value ) {
case WALL : // char if for ngs levels case WALL : // char if for ngs levels
case 3 : // int is for gsl levels case 3 : // int is for gsl levels
this->board[y*NGSTAR_LEVEL_WIDTH+x] = wall; this->board[y*NGSTAR_LEVEL_WIDTH+x] = wall;
break; break;
case EMPTY : case EMPTY :
case 2 : case 2 :
this->board[y*NGSTAR_LEVEL_WIDTH+x] = empty; this->board[y*NGSTAR_LEVEL_WIDTH+x] = empty;
break; break;
case GIFT : case GIFT :
case 4 : case 4 :
this->board[y*NGSTAR_LEVEL_WIDTH+x] = gift; this->board[y*NGSTAR_LEVEL_WIDTH+x] = gift;
this->nb_gifts++; this->nb_gifts++;
break; break;
case BALL : case BALL :
case 0 : case 0 :
this->board[y*NGSTAR_LEVEL_WIDTH+x] = ball; this->board[y*NGSTAR_LEVEL_WIDTH+x] = ball;
this->ball_X = x; this->ball_X = x;
this->ball_Y = y; this->ball_Y = y;
break; break;
case CUBE : case CUBE :
case 1 : case 1 :
this->board[y*NGSTAR_LEVEL_WIDTH+x] = cube; this->board[y*NGSTAR_LEVEL_WIDTH+x] = cube;
this->cube_X = x; this->cube_X = x;
this->cube_Y = y; this->cube_Y = y;
break; break;
case '\n' : case '\n' :
break; break;
default : default :
throw InvalidCellValue( value ); throw InvalidCellValue( value );
break; break;
}
} else {
throw InvalidCellCoordinates( x, y );
} }
} else { }
throw InvalidCellCoordinates( x, y );
}
}
// //
// Load a level from file filename in NGStar format // Load a level from file filename in NGStar format
// //
inline void NGStar2::read_ngs_level( const string *filename ) inline void NGStar2::read_ngs_level( const string *filename )
{ {
int i; int i;
int j; int j;
char current_cell; char current_cell;
bool ball_set=false; bool ball_set=false;
bool cube_set=false; bool cube_set=false;
ifstream file; ifstream file;
file.open( filename->c_str( ), ios::in ); file.open( filename->c_str( ), ios::in );
if ( file == NULL ) { if ( file == NULL ) {
throw InvalidLevelFile( filename ); throw InvalidLevelFile( filename );
}
this->level_h = NGSTAR_LEVEL_HEIGHT;
this->level_w = NGSTAR_LEVEL_WIDTH;
this->board = new cell[ this->level_w * this->level_h ];
for ( i=NGSTAR_LEVEL_HEIGHT ; i-- ; ) {
for ( j=NGSTAR_LEVEL_WIDTH ; j-- ; ) {
file.read( &current_cell, 1 );
if ( file.eof( ) )
throw InvalidLevelFile( filename );
this->set_cell( j, i, current_cell );
if ( current_cell == BALL )
ball_set = true;
else
if ( current_cell == CUBE )
cube_set = true;
} }
}
file.close( ); this->level_h = NGSTAR_LEVEL_HEIGHT;
this->level_w = NGSTAR_LEVEL_WIDTH;
this->board = new cell[ this->level_w * this->level_h ];
if ( ball_set == false || cube_set == false ) {
throw InvalidLevelFile( filename );
}
}
//
// Load a level from file filename in G-star format
// (from g-star source, translated into C++ and adapted
// but basically the structure is the same)
//
inline void NGStar2::read_gsl_level( const string *filename )
{
int i;
int j;
char current_cell;
char width;
char height;
bool ball_set=false;
bool cube_set=false;
ifstream file;
file.open( filename->c_str( ), ios::binary );
if ( file == NULL ) {
throw InvalidLevelFile( filename );
}
/* skeeping header "gsl1" */
file.seekg( 4 );
file.read( &width, 1 );
file.read( &height, 1 );
this->level_h = (int)height; //NGSTAR_LEVEL_HEIGHT;
this->level_w = (int)width; //NGSTAR_LEVEL_WIDTH;
this->board = new cell[ this->level_w * this->level_h ];
if (( width == NGSTAR_LEVEL_WIDTH ) && ( height == NGSTAR_LEVEL_HEIGHT )) {
for ( i=NGSTAR_LEVEL_HEIGHT ; i-- ; ) { for ( i=NGSTAR_LEVEL_HEIGHT ; i-- ; ) {
for ( j=NGSTAR_LEVEL_WIDTH ; j-- ; ) { for ( j=NGSTAR_LEVEL_WIDTH ; j-- ; ) {
file.read( &current_cell, 1 ); file.read( &current_cell, 1 );
if ( file.eof( ) ) if ( file.eof( ) )
throw InvalidLevelFile( filename ); throw InvalidLevelFile( filename );
this->set_cell( j, i, current_cell ); this->set_cell( j, i, current_cell );
if ( current_cell == 0 ) if ( current_cell == BALL )
ball_set = true; ball_set = true;
else else
if ( current_cell == 1 ) if ( current_cell == CUBE )
cube_set = true; cube_set = true;
} }
} }
}
file.close( );
if ( ball_set == false || cube_set == false ) {
throw InvalidLevelFile( filename );
}
}
//
// Determine the format of level in filename and call
// the corresponding method
//
void NGStar2::read_level( const string *filename )
{
ifstream file;
char *foo = new char[ 5 ];
this->nb_gifts = 0;
file.open( filename->c_str( ), ios::binary );
if ( file == NULL ) {
throw InvalidLevelFile( filename );
}
/* verifying file type (gsl or ngs) */
file.read( foo, 4 );
foo[4] = '\0';
if ( (new string( foo ))->compare( "gsl1" ) == 0 ) {
file.close( ); file.close( );
read_gsl_level( filename );
} if ( ball_set == false || cube_set == false ) {
else { throw InvalidLevelFile( filename );
}
}
//
// Load a level from file filename in G-star format
// (from g-star source, translated into C++ and adapted
// but basically the structure is the same)
//
inline void NGStar2::read_gsl_level( const string *filename )
{
int i;
int j;
char current_cell;
char width;
char height;
bool ball_set=false;
bool cube_set=false;
ifstream file;
file.open( filename->c_str( ), ios::binary );
if ( file == NULL ) {
throw InvalidLevelFile( filename );
}
/* skeeping header "gsl1" */
file.seekg( 4 );
file.read( &width, 1 );
file.read( &height, 1 );
this->level_h = (int)height; //NGSTAR_LEVEL_HEIGHT;
this->level_w = (int)width; //NGSTAR_LEVEL_WIDTH;
this->board = new cell[ this->level_w * this->level_h ];
if (( width == NGSTAR_LEVEL_WIDTH ) && ( height == NGSTAR_LEVEL_HEIGHT )) {
for ( i=NGSTAR_LEVEL_HEIGHT ; i-- ; ) {
for ( j=NGSTAR_LEVEL_WIDTH ; j-- ; ) {
file.read( &current_cell, 1 );
if ( file.eof( ) )
throw InvalidLevelFile( filename );
this->set_cell( j, i, current_cell );
if ( current_cell == 0 )
ball_set = true;
else
if ( current_cell == 1 )
cube_set = true;
}
}
}
file.close( ); file.close( );
read_ngs_level( filename );
}
this->nb_moves = 0; if ( ball_set == false || cube_set == false ) {
this->moving = ball; throw InvalidLevelFile( filename );
} }
}
// //
// Move the ball in given direction // Determine the format of level in filename and call
// stopped by walls and the cube // the corresponding method
// //
void NGStar2::move_ball( direction direction ) void NGStar2::read_level( const string *filename )
{ {
int dh=0; ifstream file;
int dv=0; char *foo = new char[ 5 ];
unsigned int tmp_ball_X = this->ball_X;
unsigned int tmp_ball_Y = this->ball_Y;
/* determining the direction it will go */ this->nb_gifts = 0;
switch ( direction ) {
case go_up : dv=1; break;
case go_down : dv=-1; break;
case go_left : dh=1; break;
case go_right : dh=-1; break;
default : break;
}
/* determining the cell it will go */ file.open( filename->c_str( ), ios::binary );
while ( if ( file == NULL ) {
/* checking board limits */ throw InvalidLevelFile( filename );
(( tmp_ball_Y + dv >= 0 ) &&
( tmp_ball_Y + dv < NGSTAR_LEVEL_HEIGHT )) &&
(( tmp_ball_X + dh >= 0 ) &&
( tmp_ball_X + dh < NGSTAR_LEVEL_WIDTH )) &&
/* is next cell a wall ? */
(this->board[(tmp_ball_Y + dv)*NGSTAR_LEVEL_WIDTH+tmp_ball_X + dh] != wall) &&
(this->board[(tmp_ball_Y + dv)*NGSTAR_LEVEL_WIDTH+tmp_ball_X + dh] != cube)
)
{
tmp_ball_Y += dv;
tmp_ball_X += dh;
/* did it collect a gift ? */
if ( this->board[tmp_ball_Y*NGSTAR_LEVEL_WIDTH+tmp_ball_X] == gift )
{
this->board[tmp_ball_Y*NGSTAR_LEVEL_WIDTH+tmp_ball_X] = empty;
this->nb_gifts--;
}
} }
if ( ( tmp_ball_X != this->ball_X ) || ( tmp_ball_Y != this->ball_Y ) ) { /* verifying file type (gsl or ngs) */
// if it's the first move then set t1 as the start of level file.read( foo, 4 );
if ( this->nb_moves == 0 ) foo[4] = '\0';
(void) time( &( this->t1 ) ); if ( (new string( foo ))->compare( "gsl1" ) == 0 ) {
/* empty previous cell */ file.close( );
this->board[this->ball_Y*NGSTAR_LEVEL_WIDTH+this->ball_X] = empty; read_gsl_level( filename );
}
/* changing coordinates */ else {
this->ball_Y = tmp_ball_Y; file.close( );
this->ball_X = tmp_ball_X; read_ngs_level( filename );
/* set destination cell */
this->board[tmp_ball_Y*NGSTAR_LEVEL_WIDTH+tmp_ball_X] = ball;
++(this->nb_moves);
}
}
//
// Move the cube in given direction
// stopped by walls, gifts and the ball
// slightly different from move_ball
//
void NGStar2::move_cube( direction direction )
{
int dh=0;
int dv=0;
unsigned int tmp_cube_X = this->cube_X;
unsigned int tmp_cube_Y = this->cube_Y;
/* determining the direction it will go */
switch ( direction ) {
case go_up : dv=1; break;
case go_down : dv=-1; break;
case go_left : dh=1; break;
case go_right : dh=-1; break;
default : break;
}
/* determining the cell it will go */
while (
/* checking board limits */
(( tmp_cube_Y + dv >= 0 ) &&
( tmp_cube_Y + dv < NGSTAR_LEVEL_HEIGHT )) &&
(( tmp_cube_X + dh >= 0 ) &&
( tmp_cube_X + dh < NGSTAR_LEVEL_WIDTH )) &&
/* is next cell a wall ? */
(this->board[(tmp_cube_Y + dv)*NGSTAR_LEVEL_WIDTH+tmp_cube_X + dh] == empty)
)
{
tmp_cube_Y += dv;
tmp_cube_X += dh;
} }
if ( ( tmp_cube_X != this->cube_X ) || ( tmp_cube_Y != this->cube_Y ) ) { this->nb_moves = 0;
// if it's the first move then set t1 as the start of level this->moving = ball;
if ( this->nb_moves == 0 ) }
(void) time( &( this->t1 ) );
/* empty previous cell */
this->board[this->cube_Y*NGSTAR_LEVEL_WIDTH+this->cube_X] = empty;
/* changing coordinates */ //
this->cube_Y = tmp_cube_Y; // Move the ball in given direction
this->cube_X = tmp_cube_X; // stopped by walls and the cube
//
void NGStar2::move_ball( direction direction )
{
int dh=0;
int dv=0;
unsigned int tmp_ball_X = this->ball_X;
unsigned int tmp_ball_Y = this->ball_Y;
/* set destination cell */ /* determining the direction it will go */
this->board[tmp_cube_Y*NGSTAR_LEVEL_WIDTH+tmp_cube_X] = cube; switch ( direction ) {
case go_up : dv=1; break;
case go_down : dv=-1; break;
case go_left : dh=1; break;
case go_right : dh=-1; break;
default : break;
}
++(this->nb_moves); /* determining the cell it will go */
} while (
} /* checking board limits */
(( tmp_ball_Y + dv >= 0 ) &&
( tmp_ball_Y + dv < NGSTAR_LEVEL_HEIGHT )) &&
(( tmp_ball_X + dh >= 0 ) &&
( tmp_ball_X + dh < NGSTAR_LEVEL_WIDTH )) &&
/* is next cell a wall ? */
(this->board[(tmp_ball_Y + dv)*NGSTAR_LEVEL_WIDTH+tmp_ball_X + dh] != wall) &&
(this->board[(tmp_ball_Y + dv)*NGSTAR_LEVEL_WIDTH+tmp_ball_X + dh] != cube)
)
{
tmp_ball_Y += dv;
tmp_ball_X += dh;
// public: /* did it collect a gift ? */
if ( this->board[tmp_ball_Y*NGSTAR_LEVEL_WIDTH+tmp_ball_X] == gift )
{
this->board[tmp_ball_Y*NGSTAR_LEVEL_WIDTH+tmp_ball_X] = empty;
this->nb_gifts--;
}
}
// if ( ( tmp_ball_X != this->ball_X ) || ( tmp_ball_Y != this->ball_Y ) ) {
// Constructor, initialize things // if it's the first move then set t1 as the start of level
// if ( this->nb_moves == 0 )
NGStar2::NGStar2( Configuration *config ) (void) time( &( this->t1 ) );
{ /* empty previous cell */
this->config = config; this->board[this->ball_Y*NGSTAR_LEVEL_WIDTH+this->ball_X] = empty;
this->load_level( );
}
// /* changing coordinates */
// Load level nb_level from current levelset if level exists this->ball_Y = tmp_ball_Y;
// this->ball_X = tmp_ball_X;
void NGStar2::load_level( )
{
string *level_filename;
if ( ( this->config->levelset != NULL ) && /* set destination cell */
( this->config->current_level <= this->config->nb_levels ) ) { this->board[tmp_ball_Y*NGSTAR_LEVEL_WIDTH+tmp_ball_X] = ball;
level_filename = new string( this->config->levelsets_path->c_str( ) );
level_filename->append( "/" );
level_filename->append( this->config->levelset->c_str( ) );
level_filename->append( "/" );
level_filename->append( itos( this->config->current_level ) );
read_level( level_filename ); ++(this->nb_moves);
} }
} }
// //
// Load level nb_level from current levelset if level exists // Move the cube in given direction
// // stopped by walls, gifts and the ball
void NGStar2::load_level( const char *filename ) // slightly different from move_ball
{ //
read_level( new string( filename ) ); void NGStar2::move_cube( direction direction )
} {
int dh=0;
int dv=0;
unsigned int tmp_cube_X = this->cube_X;
unsigned int tmp_cube_Y = this->cube_Y;
void NGStar2::next_level ( ) /* determining the direction it will go */
{ switch ( direction ) {
if( ( this->config->current_level + 1 ) <= this->config->nb_levels ) case go_up : dv=1; break;
++(this->config->current_level); case go_down : dv=-1; break;
case go_left : dh=1; break;
case go_right : dh=-1; break;
default : break;
}
this->load_level( ); /* determining the cell it will go */
} while (
/* checking board limits */
(( tmp_cube_Y + dv >= 0 ) &&
( tmp_cube_Y + dv < NGSTAR_LEVEL_HEIGHT )) &&
(( tmp_cube_X + dh >= 0 ) &&
( tmp_cube_X + dh < NGSTAR_LEVEL_WIDTH )) &&
/* is next cell a wall ? */
(this->board[(tmp_cube_Y + dv)*NGSTAR_LEVEL_WIDTH+tmp_cube_X + dh] == empty)
)
{
tmp_cube_Y += dv;
tmp_cube_X += dh;
}
void NGStar2::previous_level( ) if ( ( tmp_cube_X != this->cube_X ) || ( tmp_cube_Y != this->cube_Y ) ) {
{ // if it's the first move then set t1 as the start of level
if( this->config->current_level > 1 ) if ( this->nb_moves == 0 )
--(this->config->current_level); (void) time( &( this->t1 ) );
/* empty previous cell */
this->board[this->cube_Y*NGSTAR_LEVEL_WIDTH+this->cube_X] = empty;
this->load_level( ); /* changing coordinates */
} this->cube_Y = tmp_cube_Y;
this->cube_X = tmp_cube_X;
void NGStar2::switch_moving( ) /* set destination cell */
{ this->board[tmp_cube_Y*NGSTAR_LEVEL_WIDTH+tmp_cube_X] = cube;
++( this->nb_moves );
this->moving = ( this->moving == ball ) ? cube : ball;
}
void NGStar2::move( direction where ) ++(this->nb_moves);
{ }
if ( this->moving == ball ) }
this->move_ball( where );
else
this->move_cube( where );
}
// // public:
// Check is there's gifts left and return the result
//
bool NGStar2::is_it_over( void )
{
return( ( this->nb_gifts > 0 ) ? false : true );
}
int NGStar2::get_duration( ) //
{ // Constructor, initialize things
time_t t2; //
(void) time( &( t2 ) ); NGStar2::NGStar2( Configuration *config )
return( (int)t2 - ( this->t1 ) ); {
} this->config = config;
this->load_level( );
}
//
// Load level nb_level from current levelset if level exists
//
void NGStar2::load_level( )
{
string *level_filename;
if ( ( this->config->levelset != NULL ) &&
( this->config->current_level <= this->config->nb_levels ) ) {
level_filename = new string( this->config->levelsets_path->c_str( ) );
level_filename->append( "/" );
level_filename->append( this->config->levelset->c_str( ) );
level_filename->append( "/" );
level_filename->append( itos( this->config->current_level ) );
read_level( level_filename );
}
}
//
// Load level nb_level from current levelset if level exists
//
void NGStar2::load_level( const char *filename )
{
read_level( new string( filename ) );
}
void NGStar2::next_level ( )
{
if( ( this->config->current_level + 1 ) <= this->config->nb_levels )
++(this->config->current_level);
this->load_level( );
}
void NGStar2::previous_level( )
{
if( this->config->current_level > 1 )
--(this->config->current_level);
this->load_level( );
}
void NGStar2::switch_moving( )
{
++( this->nb_moves );
this->moving = ( this->moving == ball ) ? cube : ball;
}
void NGStar2::move( direction where )
{
if ( this->moving == ball )
this->move_ball( where );
else
this->move_cube( where );
}
//
// Check is there's gifts left and return the result
//
bool NGStar2::is_it_over( void )
{
return( ( this->nb_gifts > 0 ) ? false : true );
}
int NGStar2::get_duration( )
{
time_t t2;
(void) time( &( t2 ) );
return( (int)t2 - ( this->t1 ) );
}
} // end namespace ngstar2 } // end namespace ngstar2