My Opera is closing 1st of March

QUANG HOANG'S BLOG

Welcome to everybody !

Subscribe to RSS feed

Tetris - Class Board

,

#include "square.h"
#include "boardinf.h"
#include "brick.h"

#ifndef _board_h_
#define _board_h_
class Board
{
    public:
        BoardInfo info;
        Brick *currentBrick, *nextBrick;

        //use for gameover method
        int tickCount, numTicks;
        int row, col;
        int isplaying;
        int isgameover;

    public:
        Board();
        ~Board();
        void     initilize(int x, int y, int rows, int cols, int squareWidth);
        void     initNewGame();
        void     initGameOver();
        void     initNextBrick();
        int        isGameOver();
        Brick*     getCurrentBrick();
        void    countSquareInRowsCols();
        void     draw();
        void     process();
        void    processGameOver();
        int        isPlaying();
};
#endif


#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <stdlib.h>
#include <graphics.h>
#include <process.h>

#include "board.h"
#include "brk_type.h"
#include "settings.h"


Board::Board()
{
}

Board::~Board()
{
    delete [] info.countSquareRows;
    delete [] info.countSquareCols;

    for(int i=0; i < info.rows; i++)
        delete [] info.square[i];
    delete [] info.square;
}

void Board::initilize(int x, int y, int rows, int cols, int squareWidth)
{
    isplaying        = 1;
    isgameover       = 0;

    info.x           = x;
    info.y           = y;
    info.rows        = rows;
    info.cols       = cols;
    info.squareWidth = squareWidth;

    //init Square array
    info.square = new Square**[rows];
    int cx, cy;
    cy = y;
    for (int i=0; i < info.rows; i++, cy += info.squareWidth+1)
    {
        info.square[i] = new Square*[cols];
        cx = x;
        for (int j=0; j < cols; j++, cx+= info.squareWidth+1)
            info.square[i][j] = new Square(cx, cy, squareWidth);
    }

    //init countSquareRows, countSquareCols
    info.countSquareRows = new int[rows];
    info.countSquareCols = new int[cols];
    countSquareInRowsCols();

}

int Board::isPlaying()
{
    return isplaying;
}

void Board::draw()
{
    int x = info.x, y = info.y;
    int rows = info.rows, cols = info.cols;
    int width = info.squareWidth +1;

    setcolor(15);
    for(int i=1; i < 3; i++)
        rectangle(x-i, y-i, x+(cols*width)+i,y+(rows*width)+i);

    for (i=0; i < rows; i++)
        for (int j=0; j < cols; j++)
            info.square[i][j]->draw();
}

void Board::initNewGame()
{
    initNextBrick();
    initNextBrick();
}

int    Board::isGameOver()
{
    for (int i=0; i < info.cols; i++)
        if (info.countSquareCols[i] >= info.rows)
            return 1;
    return 0;
}

void Board::initGameOver()
{
    //setcolor(14);
    //outtextxy(info.x, info.y-10, "GAME OVER");

    row = info.rows-1;
    col = 0;
    tickCount = 0;
    numTicks = 2;
    isgameover = 1;

}

void Board::initNextBrick()
{
    if (currentBrick)
        delete currentBrick;

    currentBrick = nextBrick;
    int r = random(MAX_BRICK_TYPES);
    switch(r)
    {
        case BRICK_Z:
        {
            nextBrick = new BrickZ(&info);
            break;
        }
        case BRICK_S:
        {
            nextBrick = new BrickS(&info);
            break;
        }
        case BRICK_T:
        {
            nextBrick = new BrickT(&info);
            break;
        }
        case BRICK_O:
        {
            nextBrick = new BrickO(&info);
            break;
        }
        case BRICK_J:
        {
            nextBrick = new BrickJ(&info);
            break;
        }
        case BRICK_L:
        {
            nextBrick = new BrickL(&info);
            break;
        }
        case BRICK_I:
        {
            nextBrick = new BrickI(&info);
            break;
        }
    }
}

Brick* Board::getCurrentBrick()
{
    return currentBrick;
}

void Board::countSquareInRowsCols()
{
    memset(info.countSquareRows, 0, info.rows * sizeof(int));
    memset(info.countSquareCols, 0, info.cols * sizeof(int));

    for (int j=0; j < info.cols; j++)
    {
        for (int i=0; i < info.rows; i++)
            if (info.square[i][j]->getColor() > 0)
            {
                info.countSquareRows[i]++;
                info.countSquareCols[j] = MAX(info.rows - i, info.countSquareCols[j]);
            }
    }
}

void Board::process()
{
    if (!isplaying)
        return;

    if (isgameover)
    {
        processGameOver();
        return;
    }

    if (isGameOver())
    {
        initGameOver();
        return;
    }

    if (currentBrick != NULL)
    {
        currentBrick->process();
        if (!currentBrick->canMoveDown())
        {
            currentBrick->place();
            countSquareInRowsCols();
            initNextBrick();
        }
    }
}

void Board::processGameOver()
{
    tickCount++;
    if (tickCount >= numTicks)
    {
        tickCount=0;
        info.square[row][col]->draw(LIGHTGRAY);
        col++;
        if (col >= info.cols)
        {
            col = 0;
            row--;
        }

        isplaying= row >= 0;
    }
}
February 2014
S M T W T F S
January 2014March 2014
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28