Coding Snake Getting Started

Posts 1–3 of 3 · Page 1 of 1
Coding Snake Getting Started
I'm take a class about basics in C++ and now i try to make the exercise.

The exercise is to code a console snake game. Given is the following interface:

Code:
#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_*/
It isn't allowed to change this code.

Now my problem is, that i don't now how to start and how i can use this classes.
Using the information provided about classes, you use the functions in your cpp file. This is a header file, it initializes everything and gets it all ready for you to run. Correct me if I am wrong.
CInterfaces.h:

Code:
#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;
};
CInterfaces.cpp:
Code:
#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 );
}

It will obviously require a lot more work then that, did this in my browser so it'll probably contain some errors
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us