Source Code (Use browser search to find items of interest.)

Class Index

ksirtet'GenericTetris (./kdegames/ksirtet/gtetris.h:101)

class GenericTetris
{
 public:
    GenericTetris(uint width, uint height, bool graphic);
	virtual ~GenericTetris();
	virtual void copy(const GenericTetris &);

	virtual void init(int seed);
	void setBlockInfo(BlockInfo *main, BlockInfo *next);
	virtual void start();

	void moveLeft(int steps = 1)  { moveTo(-steps, 0); }
    void moveRight(int steps = 1) { moveTo(steps, 0); }
    void rotateLeft()             { rotate(TRUE); }
    void rotateRight()            { rotate(FALSE); }
	virtual void oneLineDown();
	virtual void dropDown();
	int moveTo(int decX, int decY);

	uint score() const          { return _score; }
    uint level() const          { return _level; }
	uint nbClearLines() const   { return _nbClearLines; }
	uint nbRemoved() const      { return _nbRemoved; }
	bool graphic() const        { return _graphic; }
	uint firstClearLine() const { return _matrix.height() - _nbClearLines; }
	uint currentCol() const     { return _currentCol; }
	int  currentLine() const    { return _currentLine; }
	const Piece *nextPiece() const        { return _nextPiece; }
	const Piece *currentPiece() const     { return _currentPiece; }
	const Matrix<Block *> &matrix() const { return _matrix; }

 protected:
	virtual void pieceDropped(uint dropHeight) = 0;
	virtual void gameOver() = 0;
	virtual void newPiece();
	bool canPosition(uint newCol, uint newLine, const Piece *newPiece) const;
	virtual void gluePiece();
	void computeClearLines();

	virtual void updateRemoved(uint newNbRemoved) { _nbRemoved = newNbRemoved;}
    virtual void updateScore(uint newScore)       { _score = newScore; }
    virtual void updateLevel(uint newLevel)       { _level = newLevel; }

	void setCurrentCol(uint col)   { _currentCol = col; }
	void setCurrentLine(uint line) { _currentLine = line; }
	void addToClearLines(int nb)   { _nbClearLines += nb; }
	void setBlock(uint i, uint j, Block *);
	void removeBlock(uint i, uint j);
	void moveBlock(uint srcI, uint srcJ, uint destI, uint destJ);

	const Block *block(uint i, uint j) const { return _matrix(i, j); }
	Block *block(uint i, uint j)             { return _matrix(i, j); }

	// graphic methods
	int toX(uint i) const;
	int toY(uint j) const;
	virtual void showNextPiece(bool /*show*/)   {}
	virtual void showPieceConfig(bool /*show*/) {}
	void bumpCurrentPiece(int dec);
	void partialMoveBlock(uint i, uint j, int dec);

 private:
	void clear();
	void rotate(bool left);
	GenericTetris(const GenericTetris &); // disabled

	uint             _score, _level, _nbRemoved, _nbClearLines, _currentCol;
	int              _currentLine;
	Piece           *_nextPiece, *_currentPiece;
	bool             _graphic;
	Matrix<Block *>  _matrix;
	KRandomSequence  _random;
};

ksirtet'GenericTetris::GenericTetris() (./kdegames/ksirtet/gtetris.cpp:5)

GenericTetris::GenericTetris(uint width, uint height, bool graphic)
: _nextPiece(new Piece), _currentPiece(new Piece),
  _graphic(graphic), _matrix(width, height)
{
	_matrix.fill(0);
}


ksirtet'GenericTetris::copy() (./kdegames/ksirtet/gtetris.cpp:12)

void GenericTetris::copy(const GenericTetris &g)
{
	// copy to non graphic
	_score         = g._score;
	_level         = g._level;
	_nbRemoved     = g._nbRemoved;
	_nbClearLines  = g._nbClearLines;
	_currentCol    = g._currentCol;
	_currentLine   = g._currentLine;
	_nextPiece->copy(g._nextPiece, TRUE);
	_currentPiece->copy(g._currentPiece, TRUE);
	for (uint i=0; i<_matrix.width(); i++)
		for (uint j=0; j<_matrix.height(); j++) {
			if ( g._matrix(i, j) )
				_matrix(i, j) = new Block(g._matrix(i, j)->value());
			else _matrix(i, j) = 0;
		}
}	


ksirtet'GenericTetris::clear() (./kdegames/ksirtet/gtetris.cpp:31)

void GenericTetris::clear()
{
	_currentCol   = 0;
	_currentLine  = -1;
	_nbClearLines = _matrix.height();
	for (uint i=0; i<_matrix.width(); i++)
		for (uint j=0; j<_matrix.height(); j++) removeBlock(i, j);
}


ksirtet'GenericTetris::~GenericTetris() (./kdegames/ksirtet/gtetris.cpp:40)

GenericTetris::~GenericTetris()
{
	delete _currentPiece;
	delete _nextPiece;
	for (uint i=0; i<_matrix.width(); i++)
		for (uint j=0; j<_matrix.height(); j++)
			delete _matrix(i, j);
}


ksirtet'GenericTetris::setBlockInfo() (./kdegames/ksirtet/gtetris.cpp:49)

void GenericTetris::setBlockInfo(BlockInfo *main, BlockInfo *next)
{
	ASSERT( _graphic );
	_nextPiece->setBlockInfo(next);
	_currentPiece->setBlockInfo(main);
}


ksirtet'GenericTetris::init() (./kdegames/ksirtet/gtetris.cpp:56)

void GenericTetris::init(int seed)
{
	ASSERT( _graphic );
	_random.setSeed(seed);
	_nextPiece->setRandomSequence(&_random);
}


ksirtet'GenericTetris::start() (./kdegames/ksirtet/gtetris.cpp:63)

void GenericTetris::start()
{
	ASSERT( _graphic );
	updateScore(0);
    updateLevel(1);
	updateRemoved(0);
	clear();
	_nextPiece->generateNext();
	newPiece();
}


ksirtet'GenericTetris::dropDown() (./kdegames/ksirtet/gtetris.cpp:74)

void GenericTetris::dropDown()
{
	int dropHeight = moveTo(0, -_currentLine);
	if ( dropHeight>=0 ) pieceDropped(dropHeight);
}


ksirtet'GenericTetris::oneLineDown() (./kdegames/ksirtet/gtetris.cpp:80)

void GenericTetris::oneLineDown()
{
	if ( moveTo(0, -1)==0 ) pieceDropped(0);
}


ksirtet'GenericTetris::newPiece() (./kdegames/ksirtet/gtetris.cpp:85)

void GenericTetris::newPiece()
{
	_currentLine  = _matrix.height() - 1 + _nextPiece->minY();
    _currentCol   =	(_matrix.width() - _nextPiece->width())/2
		- _nextPiece->minX();
	if ( _graphic ) {
		if ( !canPosition(_currentCol, _currentLine, _nextPiece)) {
			_currentLine = -1;
			gameOver();
			return;
		}
		showPieceConfig(FALSE);
	}
	_currentPiece->copy(_nextPiece, FALSE);
	if ( _graphic ) {
		_currentPiece->move(toX(_currentCol), toY(_currentLine));
		showPieceConfig(TRUE);
		showNextPiece(FALSE);
		_nextPiece->generateNext();
		_nextPiece->moveCenter();
		showNextPiece(TRUE);
	}
}


ksirtet'GenericTetris::canPosition() (./kdegames/ksirtet/gtetris.cpp:109)

bool GenericTetris::canPosition(uint col, uint line, const Piece *piece) const
{
    for(uint k=0; k<piece->nbBlocks(); k++) {
        uint i = piece->col(k, col);
        uint j = piece->line(k, line);
        if ( i>=_matrix.width() || j>=_matrix.height() || _matrix(i, j)!=0 )
			return FALSE; // outside or something in the way
    }
    return TRUE;
}


ksirtet'GenericTetris::moveTo() (./kdegames/ksirtet/gtetris.cpp:120)

int GenericTetris::moveTo(int decX, int decY)
{
	if ( _currentLine==-1 ) return -1;
	ASSERT(decX==0 || decY==0);
	int newCol  = _currentCol;
	int newLine = _currentLine;
	int dx = 0, dy = 0;
	uint n, i;

	if (decX) {
		dx = (decX<0 ? -1 : 1);
		n  = (uint)(decX<0 ? -decX : decX);
	} else {
		dy = (decY<0 ? -1 : 1);
		n  = (uint)(decY<0 ? -decY : decY);
	}

	for (i=0; i<n; i++) {
		if ( !canPosition(newCol + dx, newLine + dy, _currentPiece) ) break;
		newCol  += dx;
		newLine += dy;
	}
	if ( i!=0 ) { // piece can be moved
		_currentCol  = newCol;
		_currentLine = newLine;
		if (_graphic) {
			showPieceConfig(FALSE);
			_currentPiece->move(toX(newCol),toY(newLine));
			showPieceConfig(TRUE);
		}
	}
	return i;
}


ksirtet'GenericTetris::rotate() (./kdegames/ksirtet/gtetris.cpp:154)

void GenericTetris::rotate(bool left)
{
	if ( _currentLine==-1 ) return;
	Piece tmp;
	tmp.copy(_currentPiece, TRUE);
    tmp.rotate(left, 0, 0);
    if ( canPosition(_currentCol, _currentLine, &tmp) ) {
		int x = 0, y = 0;
		if (_graphic) {
			showPieceConfig(FALSE);
			x = toX(_currentCol);
			y = toY(_currentLine);
		}
		_currentPiece->rotate(left, x, y);
		if (_graphic) showPieceConfig(TRUE);
	}
}


ksirtet'GenericTetris::computeClearLines() (./kdegames/ksirtet/gtetris.cpp:172)

void GenericTetris::computeClearLines()
{
	_nbClearLines = 0;
	for (uint j=_matrix.height(); j>0; j--) {
		for (uint i=0; i<_matrix.width(); i++)
			if ( _matrix(i, j-1)!=0 ) return;
		_nbClearLines++;
	}
}


ksirtet'GenericTetris::setBlock() (./kdegames/ksirtet/gtetris.cpp:182)

void GenericTetris::setBlock(uint i, uint j, Block *b)
{
	ASSERT( b && _matrix(i, j)==0 );
	_matrix(i, j) = b;
	if (_graphic) b->move(toX(i), toY(j));
}


ksirtet'GenericTetris::removeBlock() (./kdegames/ksirtet/gtetris.cpp:189)

void GenericTetris::removeBlock(uint i, uint j)
{
	delete _matrix(i, j);
	_matrix(i, j) = 0;
}


ksirtet'GenericTetris::moveBlock() (./kdegames/ksirtet/gtetris.cpp:195)

void GenericTetris::moveBlock(uint srcI, uint srcJ, uint destI, uint destJ)
{
	ASSERT( _matrix(destI, destJ)==0 );
	if ( _matrix(srcI, srcJ) ) {
		setBlock(destI, destJ, _matrix(srcI, srcJ));
		_matrix(srcI, srcJ) = 0;
	}
}


ksirtet'GenericTetris::toX() (./kdegames/ksirtet/gtetris.cpp:204)

int GenericTetris::toX(uint i) const
{
	return _currentPiece->toX(i);
}


ksirtet'GenericTetris::toY() (./kdegames/ksirtet/gtetris.cpp:209)

int GenericTetris::toY(uint j) const
{
	return _currentPiece->toY(_matrix.height() - 1 - j);
}


ksirtet'GenericTetris::gluePiece() (./kdegames/ksirtet/gtetris.cpp:214)

void GenericTetris::gluePiece()
{
	for(uint k=0; k<_currentPiece->nbBlocks(); k++) {
		int i = _currentPiece->col(k, currentCol());
		int j = _currentPiece->line(k, currentLine());
		setBlock(i, j, _currentPiece->takeBlock(k));
	}
	computeClearLines();
}


ksirtet'GenericTetris::bumpCurrentPiece() (./kdegames/ksirtet/gtetris.cpp:224)

void GenericTetris::bumpCurrentPiece(int dec)
{
	ASSERT( graphic() );
	_currentPiece->move(toX(_currentCol), toY(_currentLine) + dec);
}


ksirtet'GenericTetris::partialMoveBlock() (./kdegames/ksirtet/gtetris.cpp:230)

void GenericTetris::partialMoveBlock(uint i, uint j, int dec)
{
	ASSERT( graphic() );
	if ( _matrix(i, j)==0 ) return;
	_matrix(i, j)->move(toX(i), toY(j) + dec);
}