This is probably just me being stupid but here's some code..
The issue is on the method draw_board on Board.cpp for some reason y_pos is never being updated. Any thoughts?
Main.cpp-
#include "Board.cpp";
#include "DarkGDK.h"
enum DIFFICULTY{EASY,MEDIUM,HARD};
enum GAME_STATE{WAITING,GENERATING,PLAYING};
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
// a call is made to this function so we can stop the GDK from
// responding to the escape key, we can then add in some code in our
// main loop so we can control what happens when the escape key is pressed
dbDisableEscapeKey ( );
// now we will set the random seed value to the timer, this will
// help us to get more random values each time we run the program
dbRandomize ( dbTimer ( ) );
// next we will load in some animated sprites, before doing this
// we need to adjust the image color key, by using this function we
// can make a specific color be transparent, in our case we want this
// to be bright pink
dbSetImageColorKey ( 255, 0, 255 );
DIFFICULTY difficulty=EASY;
GAME_STATE game_state=WAITING;
int keyPress=0;
game_board current_board;
while ( LoopGDK ( ) )
{
dbCLS();
if(dbScanCode()!=0 && game_state==WAITING)
{
keyPress=dbScanCode();
game_state=GENERATING;
}
if(game_state==GENERATING)
{
switch(keyPress)
{
case 2:
difficulty=EASY;
break;
case 3:
difficulty=MEDIUM;
break;
case 4:
difficulty=HARD;
break;
default:
difficulty=EASY;
break;
}
current_board.generate_game_board(difficulty);
game_state=PLAYING;
}
if(game_state==PLAYING)
{
current_board.draw_board();
}
if ( dbEscapeKey ( ) )
break;
dbSync ( );
}
// and now everything is ready to return back to Windows
return;
}
Board.cpp-
#include "DarkGDK.h"
#include "Matrix.cpp"
class game_board : public Matrix<int>
{
private:
int width,height,num_mines;
Matrix *mine_array,*tile_array;
public:
game_board()
{
}
void generate_game_board(int difficulty)
{
switch(difficulty)
{
case 0:
width=5;
height=5;
num_mines=5;
break;
case 1:
width=10;
height=10;
num_mines=25;
break;
case 2:
width=20;
height=20;
num_mines=100;
break;
}
mine_array=new Matrix(width,height);
tile_array=new Matrix(width,height);
int placed_mines=0;
int total_tiles=width*height;
for(int x=0;x<width;x++)
{
for(int y=0;y<height;y++)
{
if(placed_mines>=num_mines)
{
break;
}
int mine=dbRND(100);
int number=x*width+y;
int tolerance=50;
int percent=number/total_tiles*100;
if(percent>99)
{
tolerance=0;
}
else if(percent>95)
{
tolerance=5;
}
else if(percent>90)
{
tolerance=10;
}
else if(percent>85)
{
tolerance=15;
}
else if(percent>80)
{
tolerance=20;
}
if(mine>tolerance)
{
mine_array->Set(y,x,1);
}
else
{
mine_array->Set(y,x,0);
}
}
}
for(int x=0;x<width;x++)
{
for(int y=0;y<height;y++)
{
tile_array->Set(y,x,0);
}
}
}
void draw_board()
{
for(int a=0;a<this->width;a++)
{
for(int b=0;b<this->height;b++)
{
int x_pos=(a+15);
int y_pos=(a*5);
char temp[1];
sprintf(temp,"%d",this->mine_array->Get(b,a));
dbText(x_pos,y_pos,temp);
}
}
}
~game_board()
{
}
};
Matrix.cpp-
/**
* 2D Matrix with non-contiguous 2D storage internally.
*/
template <typename TValue>
class Matrix
{
public:
/**
* the value type.
*/
typedef TValue ValueType;
/**
* the value type.
*/
typedef Matrix<ValueType> Self;
/**
* the pointer type.
*/
typedef ValueType* PointerType;
/**
* the storage type.
*/
typedef ValueType** StorageType;
/**
* the size type.
*/
typedef int SizeType;
/**
* contructor.
*/
Matrix( SizeType nrow, SizeType ncol )
: m_nrow( nrow )
, m_ncol( ncol )
, m_data( Create( nrow, ncol ) )
{
}
Matrix()
{
}
/**
* destructor.
*/
virtual ~Matrix()
{
for ( int row = 0; row < Rows(); ++row )
{
delete[] m_data[ row ];
}
delete[] m_data;
}
/**
* the i-th row;
* this allows you to write matrix[y][x] to access an element.
*/
PointerType operator[]( int i )
{
return m_data[ i ];
}
/**
* alias for Get(y,x) (const);
* this allows you to write int x = matrix(y,x) to access an element.
*/
ValueType operator()( SizeType y, SizeType x ) const
{
return Get( y, x );
}
/**
* alias for Get(y,x) (non-const);
* this allows you to write matrix(y,x) = x; to access an element.
*/
ValueType& operator()( SizeType y, SizeType x )
{
return Get( y, x );
}
/**
* the number of rows.
*/
SizeType Rows() const
{
return m_nrow;
}
/**
* the number of columns.
*/
SizeType Columns() const
{
return m_ncol;
}
/**
* the value of the given location (const).
*/
ValueType Get( SizeType y, SizeType x ) const
{
return m_data[ y ][ x ];
}
/**
* the value of the given location (non-const).
*/
ValueType& Get( SizeType y, SizeType x )
{
return m_data[ y ][ x ];
}
/**
* set the value of the given location.
*/
void Set( SizeType y, SizeType x, const ValueType& value )
{
m_data[ y ][ x ] = value;
}
/**
* the internal representation.
*/
friend StorageType GetImpl( Self& matrix )
{
return matrix.m_data;
}
protected:
/**
* convenience function to create the internal representation inside the
* member initialization list (MIL).
*/
StorageType Create( SizeType nrow, SizeType ncol )
{
StorageType m = new PointerType[ nrow ];
for ( int row = 0; row < nrow; ++row )
{
m[ row ] = new ValueType[ ncol ];
}
return m;
}
private:
/**
* the number of columns.
*/
SizeType m_ncol;
/**
* the number of rows.
*/
SizeType m_nrow;
/**
* the internal representation.
*/
StorageType m_data;
private:
/**
* prevent asignement.
*/
Self& operator= ( const Self& );
/**
* prevent copy-asignement.
*/
Matrix( const Self& );
};
Charles Thompson