#ifndef INTERFACES_H_
#define INTERFACES_H_
struct Field_S
{
Field_S()
: m_Fruit( false ) ,
m_SnakeHead( false ) ,
m_SnakeBody( 0 ) ,
m_Wall( false )
{} ;
~Field_S() {} ;
bool m_Fruit ;
bool m_SnakeHead ;
unsigned int m_SnakeBody ;
bool m_Wall ;
};
// Enum for the movement of the snakes head.
enum Directions_E
{
Up_e, Down_e, Left_e, Right_e
};
class IBoard
{
public:
IBoard() {} ;
virtual ~IBoard() {} ;
// Initializes the board with the given ID, places the
// snake in the center and a fruit randomly on the board.
virtual void InitBoard( unsigned int ID ) = 0 ;
// Returns the field at the given position.
virtual Field_S GetField( unsigned int PosX , unsigned int PosY ) const = 0 ;
// Moves the head of the snake.
virtual void Move( Directions_E Direction ) = 0 ;
// Returns true, if the snake is alive and the game can
// continue and false otherwise.
virtual bool IsSnakeAlive() const = 0 ;
// Returns the current amount of points.
virtual int GetPoints() const = 0 ;
};
class IPlayer
{
public:
IPlayer() {} ;
virtual ~IPlayer() {} ;
// Plays the given board by moving the snake.
virtual void Play( IBoard* pBoard ) = 0 ;
};
#endif /*INTERFACES_H_*/
#include "interfaces.h"
class CBoard : public IBoard
{
public:
CBoard();
~CBoard();
// Initializes the board with the given ID, places the
// snake in the center and a fruit randomly on the board.
void InitBoard( unsigned int ID );
// Returns the field at the given position.
Field_S GetField( unsigned int PosX , unsigned int PosY );
// Moves the head of the snake.
void Move( Directions_E Direction );
// Returns true, if the snake is alive and the game can
// continue and false otherwise.
bool IsSnakeAlive();
// Returns the current amount of points.
int GetPoints();
private:
Field_S *m_pField;
int m_nScore;
bool m_bSnakeAlive;
};
class CPlayer : public IPlayer
{
public:
CPlayer();
~CPlayer();
// Plays the given board by moving the snake.
void Play( IBoard* pBoard );
private:
Directions_E m_nDirection;
CBoard *m_pGameBoard;
};
#include "CInterfaces.h"
CBoard::CBoard
{
m_pField = NULL;
}
CBoard::~CBoard()
{
delete m_pField;
}
// Initializes the board with the given ID, places the
// snake in the center and a fruit randomly on the board.
void CBoard::InitBoard( unsigned int ID )
{
if(m_pField)
delete m_pField;
m_pField = new Field_S[FIELD_WIDTH][FIELD_HEIGHT];
for(int x = 0; x < FIELD_WIDTH; x++)
{
for(int y = 0; y < FIELD_HEIGHT; y++)
{
//initialize walls etc
}
}
}
// Returns the field at the given position.
Field_S CBoard::GetField( unsigned int PosX , unsigned int PosY )
{
Field_S fieldReturned;
if(m_pField)
{
fieldReturned = *m_pField[PosX][PosY];
}
return fieldReturned;
}
// Moves the head of the snake.
void CBoard::Move( Directions_E Direction )
{
//move the board field that has m_SnakeHead set in the requested direction
}
// Returns true, if the snake is alive and the game can
// continue and false otherwise.
bool CBoard::IsSnakeAlive()
{
return m_bSnakeAlive;
}
// Returns the current amount of points.
int CBoard::GetPoints()
{
return m_nScore;
}
/* CPlayer */
CPlayer::CPlayer()
{
}
CPlayer::~CPlayer()
{
}
// Plays the given board by moving the snake.
void CBoard::Play( IBoard* pBoard )
{
pBoard->Move( m_nDirection );
}